Java 如何在带有选定文本的 Eclipse 中使用“sysout”片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1020027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use the "sysout" snippet in Eclipse with selected text?
提问by Vincent Robert
I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.
我知道 Eclipse 代码段“sysout”,它被 System.out.println(); 巧妙地替换了;当标签完成时。
This is very useful but sometimes, I need to wrap some existing code in a System.out.println();
这非常有用,但有时,我需要将一些现有代码包装在 System.out.println(); 中。
In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.
在 Eclipse 内部,模板被定义为能够包装“选定的单词”。但是如何使用带有选定文本的代码段,因为键入“sysout”显然会删除选定的文本。
Any idea?
任何的想法?
采纳答案by jimr
The sysout template acts upon entire Java statements.
sysout 模板作用于整个 Java 语句。
- Highlight a statement in the editor.
- Hit CTRL-SPACE (or whatever you have set up for content assist.)
- Type sysout then hit enter. Note that when you're typing sysout it willtemporarily overwrite your statement (but it will come back wrapped by
System.out.println
when you hit enter.)
- 在编辑器中突出显示一个语句。
- 按 CTRL-SPACE(或您为内容辅助设置的任何内容。)
- 输入 sysout 然后回车。请注意,当您输入 sysout 时,它会暂时覆盖您的语句(
System.out.println
但当您按下 Enter 键时,它会返回。)
回答by quosoo
I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v it's just one key combination more.
我不认为你可以一次性完成,但是剪切选定的文本然后:tab+space,ctrl+v 这只是一个组合键。
回答by akf
if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.
如果您使用内容辅助(Windows 上的 ctrl-space),则列表末尾将是 sysout 选项。您可能希望在单词选择周围使用引号来扩充模板,这样您就不需要输入它们。
回答by akf
Eclipse has "Surround Width" Option which can do this for you.
Eclipse 具有“环绕宽度”选项,可以为您执行此操作。
SHIFT + ALT + Z should get you that to see how that templates meta-data layout.
SHIFT + ALT + Z 应该让你看到模板元数据布局。
回答by vino777
Preference>General>Keys. then search "content assist" or "content" in "type filter text". don't press Enter. choice "content assist". type Binding and set your own shortcuts. such as "control+space".
首选项>常规>键。然后在“类型过滤文本”中搜索“内容辅助”或“内容”。不要按 Enter。选择“内容辅助”。键入 Binding 并设置您自己的快捷方式。例如“控制+空间”。
回答by Alejandro Hdz
The main part is into:
主要部分是:
Java->Editor->Templates
Java->编辑器->模板
This can accessed by:
这可以通过以下方式访问:
- Preferences window: into Java->Editor->Templates.
Surrounding a text and press:
Windows: SHIFT + ALT + Z (as sadhasivam said) Mac: CMD + OPT + Z
to display the Preferences Window
- Preferences 窗口:进入 Java->Editor->Templates。
环绕文本并按:
Windows: SHIFT + ALT + Z (as sadhasivam said) Mac: CMD + OPT + Z
显示首选项窗口