目录

IDEA

  • Command
  • Shift
  • Option
  • Control
  • Tab ⇥

Source code navigation

  • ⌘[ To navigate backwards
  • ⌘] To navigate forward

Source file structure

The Structure tool window:

⌘7 View | Tool Windows | Structure

The Structure popup:

⌘F12 Navigate | File Structure

Code completion 代码补全

Postfix code completion

后缀代码完成可帮助您在编写代码时减少向后的插入符号跳转。您可以根据在点后键入的后缀、表达式的类型及其上下文,将已键入的表达式转换为不同的表达式。

所有元素都拥有的一些后向声明选择,仅选择我认为常用的:

Operation Postfix Completion Keyword Expands To Comments
变量声明 element.var Type $name = element; 必备技能,声明式使用的典范
转换为类字段 element.field private $Type element; 刚发现的
将当前变量打印到输出流 element.sout System.out.println(element);
返回当前值 element.return return element;
类型转换 element.cast (($Type) element)

然后,对于特定的元素类型,有的后向声明还有非常实用的选项,比如字符串的判空、布尔值的if-else、列表的循环等等:

ElementType Operation Postfix Completion Keyword Expands To Comments
字符串 判空 string.null if (string == null) {}
判非空 string.notnull if (string != null) {}
整数 分支 int.switch switch (int) {}
布尔值 如果为真 boolean.if if (boolean) {}
如果为假 boolean.else if (!boolean) {}
反义 boolean.not !boolean
列表 循环元素 lists.for for (Type element : lists) {} 非常常用
带索引循环 lists.fori for (int i = 0; i < lists.size(); i++) {}

生成代码

⌘N Code | Generate...

⌥⌘T Code | Surround With...

Find and replace

⇧⌘F Edit | Find | Find in Files

Live templates

Expand a live template: ⇥

Configure: ⌘ , Settings/Preferences | Editor | Live Templates

Keep pressing ⇥ to jump from one variable in the template to the next one. Press ⇧ ⇥ to move to the previous variable.

Abbreviation(缩写) Expands to
psfs public static final String
main or psvm public static void main(String[] args){}
sout System.out.println();
Operation Keyword 缩写 Expands to Comments
判空 ifn if ($1 == null) {} 后向声明也有同样功能,读者可拣顺手的用
判非空 inn if ($1 != null) {}
创建索引循环 fori for (int $1 = 0; $1 < $2; $1++) {}
添加TODO todo // TODO: $1 非常声明式的用法
添加Fixme fixme // FIXME: 7/19/16 $1 同上
类型关系判定 inst if ($1 instanceof $2) {}
System.out sout System.out.println($1); 必须入手的黑技能,调试打log的时候非常有用
1
2
3
"hello".sout
list.for
psvm

自定义

<logger>
----------
/** logger */
privatestaticfinal Logger LOGGER = LoggerFactory.getLogger($CLASS$.class);

Inject language

alt + Enter

/images/tool/idea/image-20200904174328559.png

选择红框 输入 json 回车

/images/tool/idea/image-20200904174423092.png

alt + Enter

/images/tool/idea/image-20200904174550803.png

/images/tool/idea/image-20200904174701540.png

IDE viewing modes

You can enter and exit viewing modes using actions under View | Appearance in the main menu.

IDEA 配置优化

⌘ + ,

IDEA 2020.1 以上

通过将配置导出为Jar包的形式,实现备份,但是,不推荐,并不能备份全部配置。

File > Export Settings…

File > Import Settings…

1
2
# 备份
~/Library/Application\ Support/IntelliJIdea2019.2/

恢复:安装前直接将此文件放到默认目录。或则安装完成后提醒是否导入配置,可以手动选择

设置

主要路径File | Settings | Editor | File and Code Templates 1、首先点击右上角的file 2、点击进入settings 3、选择 Editor—> File and Code Templates—>includes—>FileHeader

/**
 * @author  Ynthm
 * @date  ${DATE} ${TIME}
 * @version 1.0
 */

显示行号

/images/tool/idea/image-20200904172422691.png

方法分割线以及字符缩进

Editor > General > Appearance

/images/tool/idea/image-20200730094605717.png

自动换行

如果Wrap on typing更改为Yes,那么当你输入内容超过限制线时会自动换行,否则只有当你使用格式化快捷键Ctrl+Alt+L时才会手动换行。

/images/tool/idea/image-20200904175918676.png

如果勾选上Ensure right marigin is not exceed,那么当你使用格式化快捷键Ctrl+Alt+L时,超过限制线的代码会被换行。

/images/tool/idea/image-20200904180011442.png

缩进2个空格

/images/tool/idea/image-20200904183847876.png

编码设置

如果系统默认不是 UTF-8 请设置。

/images/tool/idea/image-20200904172848311.png

cmd + E 快速定位 Editor Tabs

插件

/images/tool/idea/image-20200425120048937.png

/images/tool/idea/image-20200425120217644.png

Material Theme UI

/images/tool/idea/image-20200904162813975.png

Atom Material Icons

/images/tool/idea/image-20200904162537616.png

Rainbow Brackets

多彩的括号匹配

区域代码高亮 ⌘N + 鼠标右键

/images/tool/idea/image-20200904164134767.png

/images/tool/idea/image-20200904164423282.png

Grep Console

/images/tool/idea/image-20200904164709142.png

CodeGlance

/images/tool/idea/image-20200904161940185.png

/images/tool/idea/image-20200904162035674.png

google-java-format

/images/tool/idea/image-20200425120519324.png

Alibaba Java Coding Guidelines

项目文件夹或文件右键

/images/tool/idea/image-20200904165120328.png

SonarLint

Lombok

Save Actions

/images/tool/idea/image-20200904170130932.png

/images/tool/idea/image-20200425120624902.png

若非新项目不想改变格式 请选择 Reformat only changed code

GenerateSerialVersionUID

.ignore

项目文件夹右键 > 新建 > .ignore file > .gitignore file(Git)

搜索选择

Java

Gradle or Maven

JetBrains

VisualStudioCode

macOS

RestfulToolkit

快捷键 cmd+\, 可以根据关键字快速找到接口,回车迅速到达代码接口位置

Codota

智能自动补全让编码速度更快

从经过测试或证明过的程序中获得编码建议

ctrl + shift + o

不脱离IDE发现并利用更多开源代码

ctrl + shift + y

Maven Helper

/images/tool/idea/image-20200904171202145.png

JMH plugin

生成执行 JMH benchmarks

MyBatis Log Plugin

  • 还原 mybatis sql 为原始可执行 sql
  • 将 ? 替换为真实的参数

MyBatisX

  • mapper类和xml跳转
  • mybatis.xml,mapper.xml 配置文件提示
  • mapper类智能填充方法名,以JPA的形式填充方法和xml

Key Promoter X

使用过的功能都会提示快捷键

Http Client

通常喜欢在项目的 resources 文件夹( eg. Scratches)下面新建一个 http 的文件夹,选中文件夹,右键选择 New -> New Http Request

测试完成以后,所有的日志存在项目文件夹 .idea -> httpRequests 文件夹下面

.http 文件 IDEA 打开 右上角有 Examples 可以参考

全局变量

文件名固定为 http.client.env.json ,还可以是 http.client.private.env.json

全局变量文件http.client.env.json 和我们的 .http 文件必须在相同的文件夹下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "dev": {
    "host": "localhost",
    "port": 8081,
    "identifier": "admin",
    "password": "admin"
  },
  "prod": {
    "host": "dayarch.top",
    "port": 8080,
    "identifier": "admin",
    "password": "admin"
  }
}

巧用 response handler 脚本

1
2
3
4
5
6
7
POST http://{{host}}:{{port}}/login
Content-Type: application/json
Accept: application/json

> {%
client.global.set("auth_token", response.body.result.token);
 %}
1
2
GET http://{{host}}:{{port}}/users/1
Authorization: Bearer {{auth_token}}

附录