英文:
Automatically replacing with var using IntelliJ
问题
我正在迁移一些Java 10之前的代码,想知道IntelliJ是否提供一种自动重构的方式,以在可能的情况下用var替换使用实际类型的变量声明。
代码中到处都是这样的内容:
String status = "empty";
BigDecimal interest = BigDecimal.ZERO;
List<Future<Boolean>> results = es.invokeAll(tasks);
LocalDate start = LocalDate.of(2020, 1, 1);
我更希望是:
var status = "empty";
var interest = BigDecimal.ZERO;
var results = es.invokeAll(tasks);
var start = LocalDate.of(2020, 1, 1);
我已经查看了IntelliJ的设置(代码样式/检查),但没有找到任何相关选项。
英文:
I'm migrating some pre Java 10 code and I'm wondering if IntelliJ offers a way to automatically refactor the code to replace the variable declarations that uses the actual type with var wherever it's possible.
The code is full of stuff like:
String status = "empty";
BigDecimal interest = BigDecimal.ZERO;
List<Future<Boolean>> results = es.invokeAll(tasks);
LocalDate start = LocalDate.of(2020, 1, 1);
And I would prefer:
var status = "empty";
var interest = BigDecimal.ZERO;
var results = es.invokeAll(tasks);
var start = LocalDate.of(2020, 1, 1);
I already looked in IntelliJ's settings (Code Style/Inspections) and couldn't find anything.
答案1
得分: 15
-
转到
File | Settings
,在那里选择Editor | Inspections
,然后在Java | Java language level migration aids | Java 10
下。 -
右键单击
Local variable type can be omitted
,选择Weak Warning
或类似选项。 -
将光标移到代码中的任何这些警告上(灰色高亮显示),打开快速修复上下文帮助(alt+enter),在
Replace explicit type with 'var'
处向右移动,然后选择Fix all 'Local variable type can be omitted' problems in file
。
感谢 @olga-klisho 提出的想法(在评论中)。
英文:
-
Go to
File | Settings
, there selectEditor | Inspections
, then underJava | Java language level migration aids | Java 10
. -
Right click on
Local variable type can be omitted
and selectWeak Warning
or similar. -
Move Your cursor onto any of those warnings in Your code (highlighted grey), open quick fix context help (alt+enter), at
Replace explicit type with 'var'
move to the right and selectFix all 'Local variable type can be omitted' problems in file
Thanks for @olga-klisho for the idea (in comments)
答案2
得分: 4
我正在使用 IntelliJ IDEA 2021.3.2,但不认为这个设置是新的。
我自己也曾为此苦恼过。
似乎第一次在本地安装 IntelliJ 时,默认情况下它会回退到使用传统的变量定义方式(即 String s = new String();
)。
我成功将其改为使用 var
的方法是,在我声明了一些东西之后,例如 new String()
,要么我按下<kbd>⌥ Alt/Option</kbd>+<kbd>Enter</kbd>来为该声明声明一个变量,要么通过使用<kbd>⌘ Command</kbd>+<kbd>⇧ Shift</kbd>+<kbd>V</kbd> 快捷键(我使用的是 Mac 和经典的 Intellij 快捷键映射,所以可能会有所不同),这会显示如下:
正如你所看到的,它建议按下这个快捷键组合,或者点击设置按钮,会弹出一个类似这样的弹窗:
确保你选择了 Declare var type
,然后就可以继续了。
英文:
I'm using IntelliJ IDEA 2021.3.2, but don't think the setting is new.
I've been struggling with this one myself.
It seems that the first time to install IntelliJ locally, by default, it will fall back to using traditional defining of variables (i.e. String s = new String();
)
How I managed to change it into using var
is after I declared something, for example new String()
, either I pressed <kbd>⌥ Alt/Option</kbd>+<kbd>Enter</kbd> to declare a variable for that declaration or by using <kbd>⌘ Command</kbd>+<kbd>⇧ Shift</kbd>+<kbd>V</kbd> shortcut (I'm using Mac and classic Intellij key mapping, so YMMV) which activates declaration of a variable, this would show as follows:
As you see, it suggest to hit that key combination shortcut or clicking on the settings button that would open a pop up like this one:
Make sure you have Declare var type
selected and you should be good to go.
答案3
得分: -6
使用IntelliJ的Edit -> Find -> Replace...
选项。
或者使用Ctrl + R
。
英文:
Use the IntelliJ Edit -> Find -> Replace...
option.
Or Ctrl + R
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论