IntelliJ IDEA

Eclipse 和 IntelliJ IDEA 都提供了写代码的辅助功能,包括代码补全、代码生成、快速修饰动态模板等功能。

 

1. 快速修复(Quick-fixes)

快捷键:Alt+Enter

clip_image001[6]

 

所有的快速修复都是基于 Settings | Editor | Inspections 中的选项进行提示。

clip_image002[4]

 

批量修复

如果需要一次多多个地方进行修复,如整个目录(folder)、一个模块(module)或整个项目(project),可以使用下面两个功能:

  • Analyze | Run Inspection By Name
  • Analyze | Inspect Code

 

Eclipse

 

IntelliJ IDEA

 

Action

Shortcut

Action

Shortcut

Quick fix

Ctrl+1

Show intention action

Alt+Enter

 

2. 生成代码(Generating code)

生成代码操作:Code | Generate (Alt+Insert)

clip_image003[4]

 

该操作不仅可以在编辑器中使用,在项目窗口和导航栏也可以使用:

clip_image004[4]

此时用于新建文件或包。

 

3. 代码补全(Code completion)

IntelliJ IDEA提供了多种类型的代码补全功能,包括:

  • 基本补全(Basic completion)
  • 次级基本补全(Second basic completion)
  • 智能补全(Smart completion)
  • 次级智能补全(Second smart completion)
  • 语句补全(Statement completion)

对于这些补全功能的详细描述,可以参考文章:

https://dzone.com/articles/top-20-code-completions-in-intellij-idea

 

IDEA默认不显示选择项的文档,不过可以通过如下设置启用:

clip_image005[4]

 

不过不建议启用,在需要时,可以用Ctrl+Q快捷键调用,如此可以看到对应方法的文档,如下:

clip_image006[4]

 

当光标在构造函数或方法的括号中,通过 Ctrl+P 可以看到参数信息:

clip_image007[4]

如图,StringReader只有一个构造函数,接受字符串参数。

 

Eclipse

 

IntelliJ IDEA

 

Action

Shortcut

Action

Shortcut

Code completion

Ctrl+Space

Basic completion

Ctrl+Space

-

-

Smart completion

Ctrl+Shift+Space

-

-

Statement completion

Ctrl+Shift+Enter

 

  1. 模板

使用方法:输入触发字符串,按Tab键,如下:

 

clip_image008[4]

 

IDEA的模板触发方式和Eclipse稍有不同,如下:

Template

Eclipse

IntelliJ IDEA

Define a main method

main

psvm

Iterate over an array

for

itar

Iterate over a collection

for

itco

Iterate over a list

for

itli

Iterate over an iterable using foreach syntax

foreach

iter

Print to System.out

sysout

sout

Print to System.err

syserr

serr

Define a static field

static_final

psf

可用模板列表: Settings | Editor | Live Templates

 

除了这种常规模板,还有一些其他形式的模板。

 

后缀模板(postfix templates)

clip_image009[4]

 

可用后缀模板列表:Settings | Editor | General | Postfix Completion

 

包含式动态模板(Surround with live template)

快捷键:Ctrl+Alt+J

和上面不同,此时按 Enter 键选择

 surround

原文地址:https://www.cnblogs.com/jiawei-whu/p/5544813.html