eclipse IDEA:“将语句分配给新的局部变量”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4816069/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:42:23  来源:igfitidea点击:

IDEA: "Assign statement to new local variable"?

javaeclipseintellij-ideacode-completion

提问by Chris Lercher

As a long time Eclipse user, I'm playing around a little bit with IntelliJ IDEA 10. I can't seem to find out how to perform an "Assign statement to new local variable" code completion.

作为 Eclipse 的长期用户,我正在尝试使用 IntelliJ IDEA 10。我似乎无法找到如何执行“将语句分配给新的局部变量”代码完成。

Feature explanation:

功能说明:

I type something like

我输入类似

new BufferedOutputStream(out)

and then hit Cmd (or Ctrl)+1and enter, and Eclipse changes the line into:

然后点击Cmd (or Ctrl)+1并回车,Eclipse 将这一行更改为:

BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);

At the same time, I can type over "bufferedOutputStream" immediately to rename it (or select from the options "bufferedOutputStream", "outputStream" and "stream" from a dropdown).

同时,我可以立即键入“bufferedOutputStream”以重命名它(或从下拉列表中选择“bufferedOutputStream”、“outputStream”和“stream”选项)。

I use this feature for absolutely every assignment, and it's an enormous time saver - this must be available in IDEA, too - but where is it hidden?

我绝对在每个作业中都使用此功能,它可以节省大量时间 - IDEA 中也必须提供此功能 - 但它隐藏在哪里?

回答by CrazyCoder

Refactor | Introduce Variable(Ctrl+Alt+Von Windows). Note that you don't need to select the text if it's the only text in the current line. Then you can change the variable name in-line just like you've described and press Enterto complete editing.

重构 | 引入变量(Windows 上的Ctrl+ Alt+ V)。请注意,如果该文本是当前行中的唯一文本,则无需选择该文本。然后您可以像您描述的那样在线更改变量名称,然后按Enter完成编辑。

Another way is to use the Postfix Completion:

另一种方法是使用Postfix Completion

Type .var(or just .vto select it from the list) and confirm it with Enter.

键入.var(或只是.v从列表中选择它)并用 确认Enter

回答by G?k?er G?kdal

As CrazyCoder mentions you can use Ctrl+Alt+V. Also instead of selecting the expression, clicking into somewhere in your expression and using Ctrl+Wto expand scope is very useful while using introduce refactorings. Extract refactorings are:

正如 CrazyCoder 提到的,您可以使用Ctrl+ Alt+ V。此外,在使用引入重构时,不是选择表达式,而是单击表达式中的某个位置并使用Ctrl+W扩展范围非常有用。提取重构是:

  • Extract variable: Ctrl+Alt+V
  • Extract field: Ctrl+Alt+F
  • Extract method: Ctrl+Alt+M
  • Extract parameter: Ctrl+Alt+P
  • Extract constant: Ctrl+Alt+C
  • 提取变量:Ctrl+ Alt+V
  • 提取场:Ctrl+ Alt+F
  • 提取方法:Ctrl+ Alt+M
  • 提取参数:Ctrl+ Alt+P
  • 提取常数:Ctrl+ Alt+C

Also, Idea is a polygot editor so you can use these extract refactorings for other file types like js or html also (not all refactorings work in all file types but Ctrl+Wworks mostly).

此外,Idea 是一个 polygot 编辑器,因此您也可以将这些提取重构用于其他文件类型,例如 js 或 html(并非所有重构都适用于所有文件类型,但Ctrl+W主要适用)。

There are more extract refactorings which do not have shortcuts which you can access from Refactor|Extract menu (both menu bar and context menu). To quick access all refactorings you can use Ctrl+Alt+Shift+Tfor a popup menu.

还有更多没有快捷方式的提取重构,您可以从 Refactor|Extract 菜单(菜单栏和上下文菜单)访问它们。要快速访问所有重构,您可以使用Ctrl+ Alt+ Shift+T作为弹出菜单。

As a last word, I highly encourage you using "Tip of the Day" (Help|Tip of the Day). It is a fast way to learn many helpful features of Idea.

最后,我强烈建议您使用“今日提示”(Help|Tip of the Day)。这是学习 Idea 的许多有用功能的快速方法。

回答by dogbane

It's not as nice as Eclipse, but you can try the following:

它不如 Eclipse,但您可以尝试以下操作:

new BufferedOutputStream(out)
  • Select the expression above, either with your mouse (or by using Ctrl+W).
  • Then hit Ctrl+Alt+Vto Introduce a Variableor (Ctrl+Alt+Fto Introduce a Field)
  • 使用鼠标(或使用Ctrl+ W)选择上面的表达式。
  • 然后打Ctrl+ Alt+V介绍一个变量或(Ctrl+ Alt+F介绍一个字段