简体   繁体   English

"将突出显示的文本包装到 intellij 上的 System.out.println() 的快捷方式"

[英]Shortcut to wrap highlighted text into a System.out.println() on intellij

Is there a way to highlight a text and put it into a System.out.println() by using a shortcut on intellij ?有没有办法通过使用 intellij 上的快捷方式突出显示文本并将其放入 System.out.println() 中?

Here's what I'm looking for :这就是我要找的东西:
1. I highlight dog.getName()<\/code> using my mouse 1. 我用鼠标突出显示dog.getName()<\/code>
2. I use a shortcut 2.我使用快捷方式
3. Result : System.out.println(dog.getName());<\/code> 3. 结果: System.out.println(dog.getName());<\/code>

"

There is no ready to use way/shortcut in Intellij Idea to do this. Intellij Idea 中没有现成的方法/快捷方式来执行此操作。

I can offer you to ways to reach something similar.我可以为您提供达到类似目标的方法。

Live templates 实时模板

Live templates lets you create small code snippets that are expanded by an abbreviation.实时模板可让您创建由缩写扩展的小代码片段。

Live templates can have variables that are are derived from the context, other variables or just filled by you when inserting the templates.实时模板可以具有从上下文派生的变量、其他变量或仅由您在插入模板时填充的变量。

There are surrounding live templates that only work when code has been selected.周围的实时模板仅在选择代码时才起作用。 In most cases, the selected code is wrapped by the live template.在大多数情况下,所选代码由实时模板包装。

Intellij offers quite a few ready to use live templates, like surround with IF or surround with try-catch aso. Intellij 提供了很多现成的实时模板,例如surround with IFsurround with try-catch aso。

A live template for your case looks like this:您案例的实时模板如下所示:

System.out.println($SELECTION$);$END$

You have to save that live template in the surround category under Editor->Live Templates and assign it a proper abbreviation.您必须将该实时模板保存在Editor->Live Templates下的surround类别中,并为其分配适当的缩写。

Select the code you want to surround, press Ctrl+Alt+t and select the newly created live template or just type the assigned abbreviation.选择要包围的代码,按Ctrl+Alt+t并选择新创建的实时模板或只需键入指定的缩写。 That's it.就是这样。

Macros

The second way is recording a macro and replay it.第二种方法是录制宏并重放它。 Furthermore, a shortcut can be assigned to a macros, but is a replay of your typing and this can be a bit slow if you want to input more than just a few characters.此外,可以为宏指定快捷方式,但它是您输入的重播,如果您想输入的不仅仅是几个字符,这可能会有点慢。

Imports aren't done automatically as well, like they are when using live templates.导入也不会自动完成,就像使用实时模板时一样。 Therefore, if you want to use a macro for inserting code, you have to use the full qualified class name or optimize imports after running the macro.因此,如果要使用宏插入代码,则必须在运行宏后使用完全限定的类名或优化导入。

I suggest you try out both ways and decide afterwards what works best for you.我建议您尝试两种方式,然后再决定哪种方式最适合您。

This is a very needed and useful feature.这是一个非常需要和有用的功能。 Someone should tell IntelliJ and Eclipse about this?有人应该告诉 IntelliJ 和 Eclipse 吗?

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM