在 VS Code 中通过修改或添加覆盖文本
我想使用 Intellij 中可用的函数,该函数允许在 java 中编写此内容:
"Hello, World!".sout
并将其转换为:
System.out.println("Hello, World!);
但我找不到如何在 Visual Studio Code 中执行此操作,因此我们将非常感激。
更新:我刚刚取得一些进展...
这是结果,但有一个错误说我缺少一个逗号,我不知道出了什么问题...
"Prints to console the given value": {
"prefix": ".sout",
"body": [
"System.out.println("${TM_CURRENT_LINE/(\\S)[^(\\.)sout]}");" //${\S[^\.sout]}
],
"description": "Prints to console the given value"
}
I wanted to use a function available in Intellij that allows to write this in java:
"Hello, World!".sout
And have it converted to:
System.out.println("Hello, World!);
But I can't find how to do this in Visual Studio Code, so this would be highly appreciated.
UPDATE: I just got to make some progress...
This is the result, but there's an error that says I'm missing a comma, I don't know what's wrong...
"Prints to console the given value": {
"prefix": ".sout",
"body": [
"System.out.println("${TM_CURRENT_LINE/(\\S)[^(\\.)sout]}");" //${\S[^\.sout]}
],
"description": "Prints to console the given value"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
VSCode 允许你自定义代码片段,但无法像使用方法那样集成现有字段。可以直接输入“sout”,然后在括号中输入“Hello word”即可达到这种效果。同时,您还可以点击此了解自定义代码片段,其中会让你的编译更快更舒服
Vscode allows you to customize code snippets, but you can't integrate existing fields like using methods. You can directly enter "sout" and then enter "Hello word" in parentheses to achieve this effect. At the same time, you can also click this to learn about custom code fragments, which will make your compilation faster and more comfortable
我认为您会发现很难(或不可能)创建一个片段来执行您想要的操作。
TM_CURRENT_LINE
将获取当前行的值,但不会替换它,因此它的行为并不像您首先选择了该行。因此,代码片段的插入点是光标所在的位置 - 可能是当前行上的任何位置。"System.out.println("${TM_CURRENT_LINE/(\\S)[^(\\.)sout]}");"
不会写入
System.out.println< /code> 在当前行的任何内容之前(除非您的光标位于最开始),它将将该文本放置在光标所在的位置 - 在您的情况下可能位于该行的最末尾。
出于类似的原因,我在扩展中添加了在查找/替换之前运行一些命令的功能: 查找并转换。您可以使用此键绑定(在
keybindings.json
中)实现所需的结果:这将首先选择从光标到开头的行,然后将所有内容视为正则表达式捕获组 1。
替换使用
$1
将选定的文本插入到您想要的位置。演示:I think you will find it hard (or impossible) to create a snippet to do what you want.
TM_CURRENT_LINE
will get the value of the current line but it does not replace it, so it does not act as if you selected the line first. So the insertion point of your snippet is wherever the cursor is - which may be anywhere on that current line."System.out.println("${TM_CURRENT_LINE/(\\S)[^(\\.)sout]}");"
will not write
System.out.println
before anything on the current line (unless your cursor is at the very beginning), it will put that text wherever the cursor is located - in your case presumably at the very end of the line.For reasons like that, I added the ability to run some commands before a find/replace in my extension: Find and Transform. You can achieve your desired result with this keybinding (in your
keybindings.json
):This will first select the line from your cursor to the beginning and then treat all that as a regex capture group 1.
The replace uses
$1
to insert that selected text where you want it. Demo:为什么不使用快速命令
sysout
,它也可以根据需要生成System.out.println()
。Why don't you use quick command
sysout
, which can also generateSystem.out.println()
as you need.