自动在 IntelliJ 中使用 var 进行替换

huangapple go评论84阅读模式
英文:

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 = &quot;empty&quot;;
BigDecimal interest = BigDecimal.ZERO;
List&lt;Future&lt;Boolean&gt;&gt; results = es.invokeAll(tasks);
LocalDate start = LocalDate.of(2020, 1, 1);

And I would prefer:

var status = &quot;empty&quot;;
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

  1. 转到 File | Settings,在那里选择 Editor | Inspections,然后在 Java | Java language level migration aids | Java 10 下。

  2. 右键单击 Local variable type can be omitted,选择 Weak Warning 或类似选项。

  3. 将光标移到代码中的任何这些警告上(灰色高亮显示),打开快速修复上下文帮助(alt+enter),在 Replace explicit type with &#39;var&#39; 处向右移动,然后选择 Fix all &#39;Local variable type can be omitted&#39; problems in file

感谢 @olga-klisho 提出的想法(在评论中)。

英文:
  1. Go to File | Settings, there select Editor | Inspections, then under Java | Java language level migration aids | Java 10.

  2. Right click on Local variable type can be omitted and select Weak Warning or similar.

  3. 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 &#39;var&#39; move to the right and select Fix all &#39;Local variable type can be omitted&#39; 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 快捷键映射,所以可能会有所不同),这会显示如下:

自动在 IntelliJ 中使用 var 进行替换

正如你所看到的,它建议按下这个快捷键组合,或者点击设置按钮,会弹出一个类似这样的弹窗:

自动在 IntelliJ 中使用 var 进行替换

确保你选择了 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:

自动在 IntelliJ 中使用 var 进行替换

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:

自动在 IntelliJ 中使用 var 进行替换

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 -&gt; Find -&gt; Replace... option.

Or Ctrl + R

huangapple
  • 本文由 发表于 2020年10月22日 20:15:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64482017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定