编辑Java代码
作为Java编辑器,它还支持 Code Lens(引用)和鼠标悬停显示 Javadoc。
还能够自动检测代码中的问题,并为您提供快速修复建议。

查看定义
此功能在窥视窗口内的定义附近显示几行代码,因此您无需离开当前位置即可查看。
要查看某符号的定义,请将光标放在符号在源代码中使用的任何位置,然后按Alt + F12
。你也可以通过右键👉查看定义。
转到定义
要转到符号的定义,请将光标放在符号在源代码中使用的任何位置,然后按F12
键。或者,您可以从上下文菜单中选择“ 转到定义 ”(右键单击,然后选择“ 转到定义”)
Go to Super Implementation
跟踪类的实现和重写方法:将鼠标悬停在方法或类名上,点击悬停窗口中的 Go to Super Implementation
即可。

- 此文最先发布于:VS Code中进行Java开发(2):代码编辑 – 就是这个范儿 自带目录 和《VS Code中进行Java开发》完整系列文章。
- VS Code的使用可参考我的: VS Code用户指南与使用技巧 – 就是这个范儿
Call Hierarchy
在一个函数上选择右键👉快速查看👉速览调用层次结构

浏览Spring Boot代码
Spring Boot Tools扩展为Spring Boot项目提供了增强的导航和代码补全:
-
@/
显示所有定义的请求映射(映射的路径,请求方法,源位置) -
@+
显示所有已定义的bean(bean名称,bean类型,源位置) -
@>
显示所有功能(原型实现) -
@
显示代码中的所有Spring注释

智能感知
Visual Studio Java代码中的代码补全是由插件 Language Support for Java(TM) by Red Hat 提供,该插件背后使用了Eclipse中的 Java development tools (JDT) 技术,因此可以获得相同级别的支持。
此外,还有AI辅助的IntelliSense,称为IntelliCode。通过将最可能使用的内容放在完成列表的顶部,可以节省您的时间。IntelliCode的建议基于GitHub上的数千个开源项目,每个项目都超过100星,因此接受了有关高质量项目最常见用法的培训。补全列表将根据代码的上下文进行量身定制。

代码重构
重构功能包含:重命名,移动,提取方法和变量等。
重命名
重命名:通过重命名,您可以重命名变量,类,方法,包,文件夹以及几乎所有的Java标识符。重命名标识符时,对该标识符的所有引用也会被重命名。调用“重命名”重构的快捷方式是F2
;当您按Enter
时,对该标识符的所有引用也会更改。
你也可以通过右键👉重命名符号,进行重命名。

移动
通过“移动重构”,您可以将包和类在项目的源根之间移动,将类成员移动到其他类,将内部类移动到更高层次。
示例一:如果在另一个类中使用的静态方法多于非静态方法,则可以对静态方法执行移动重构:

示例二:您也可以将一个类移至另一个包。

示例三:将内部类移至新文件。

提取方法和变量
可以提取到常量,方法和局部变量


提取后,您还可以在同一事务中执行重命名。

将局部变量转换为字段
选择表达式时,提取到字段。

选择变量声明时,将变量转换为字段。

Inline
内联重构使您可以反向进行局部变量,方法和常量的重构。

转换为增强型for循环

生成getter和setter
您可以为所有新成员变量批量生成getter和setter。如果该类具有多个字段,则source操作将提示您执行快速选择,以供您选择用于生成访问器方法的目标字段。

解决模棱两可的 import

Override/implement 方法

生成hashCode()
和equals()
hashCode()
并equals()
可以使用默认实现生成。列出了所有非静态成员变量,您可以使用检查表来自定义生成的代码。
您可以通过两个选项来自定义生成的代码:
- 如果您使用Java 7+,您可以设置
java.codeGeneration.hashCodeEquals.useJava7Objects
以true
产生更短的代码,调用Objects.hash
和Objects.equals
。 - 您还可以设置
java.codeGeneration.hashCodeEquals.useInstanceof
为true
使用instanceOf
运算符检查对象类型,而不是调用Object.getClass()
。

生成 toString()

转换为静态导入
将静态函数调用转换为静态导入。

生成委托方法

从父类生成构造函数

反转局部变量
通过这种重构,您可以将布尔变量的含义更改为相反的含义。

将参数分配给新字段
此源操作将参数分配给构造函数中未使用的参数的新字段

修复无法访问的引用
此快速修复可帮助您修复不可访问的引用。

创建不存在的包
当软件包名称与文件夹名称不匹配时,您可以选择更改源代码中的软件包名称,或在文件系统中移动文件夹(即使目标文件夹尚不存在)。

支持的其他代码操作
VS Code支持的代码操作列表在不断增长,仅在上面列出了最受欢迎的代码操作。其他值得注意的受支持的操作包括(但不限于):
- 将匿名类转换为Lambda表达式
- 将Lambda转换为匿名类
- 将匿名类转换为嵌套类
- 创建未解析的类型
- 将语句分配给新变量/字段
-
final
尽可能添加修饰符 - 删除
final
修饰符 - 删除不必要的演员表
- 删除冗余接口
- 在switch语句中添加缺少的案例标签
- 跳至中断/继续的定义
- 正确访问静态元素
代码片段 Snippets
Visual Studio代码支持各种流行的Java代码片段,以使您的工作效率更高,例如类/接口,syserr
,sysout
,if / else
,try / catch
,静态main方法。利用来自Java语言服务器的信息,它还可以在选择期间预览代码段。

格式化
Language Support for Java(TM) by Red Hat 插件提供了格式设置功能
此外,还有一个名为 Checkstyle for Java 扩展,您可以将其与现有checkstyle
配置(Google或Sun的Check)一起使用
设置Checkstyle配置文件

- 要设置配置文件,请右键单击该
.xml
文件,然后选择“ 设置Checkstyle配置文件”。 - 您还可以触发命令
Set the Checkstyle Configuration File
以在文件资源管理器中选择配置文件。它会在你的工作区中查找checkstyle.xml
文件,以使 Checkstyle 的配置更加容易。您还将看到两个内置配置:- Google's Check
- Sun's Check
- 命令
Checkstyle: Set the Checkstyle Configuration
检测潜在的Checkstyle配置文件并列出它们。您也可以通过在输入框中直接写入URL来提供配置文件:

您还可以使用命令 Checkstyle: Set the Checkstyle Version
来设置Checkstyle版本。
该命令将:
- 列出主仓库中的最新Checkstyle版本。
- 列出所有下载的版本。
- 列出所有受支持的版本。
- 用复选标记标记当前使用的版本。
此外,您还可以通过配置Checkstyle的路径为Checkstyle带来任何第三方模块,略。
参考
Writing Java with Visual Studio Code ,Code为Java等各种语言提供了详细的文档
VS Code的使用可参考:VS Code用户指南与使用技巧
网友评论