Intellij Idea 简明教程
Intellij Idea - Deep Dive into Editor
编辑器是开发人员花费其大部分时间的地方。精通编辑器是提高任何资源生产力的第一步。本章讨论了编辑器可视元素、最常见的编辑器操作及 SonarLint 插件,它提供了 lint 检查。
Editor is that area where a developer spends most of his/her time. Mastering the editor is the first step at improving the productivity of any resource. This chapter discusses visual elements of editor, the most common editor actions and SonarLint plug-in, which provides lint checking.
Visual Elements of Editor
IntelliJ 具备许多功能,其中包括旨在帮助开发人员浏览并了解其编码真实状态的可视元素。
IntelliJ has many provisions that include visual elements designed to assist developers in navigating through and understanding the real status of their coding.
现在让我们了解不同的功能 −
Let us now go through the different provision −
Gutter Area
编辑器小窗口区域位于 IDE 的左侧,如下图所示 −
Editor’s gutter area is located on the left side of IDE as shown in the following image −
Labels
我们现在了解标签如何工作的。上面屏幕截图以数字标记。我们现在看看这些数字必须显示什么 −
We will now understand how the labels work. The above screenshot is marked in numbers. We will now see what the numbers have to show −
-
Here we can see line numbers along with other useful options. If you observe carefully just before class name and main method there is a small green triangle. It provides option to run, debug and run application with coverage. Just click on the triangle icon and it will provide options.
-
You can see the small plus symbol at line number 6. This is the code folding functionality. If you click on that icon, then it will unfold it. Code folding can be done at class, method, loop and other block levels.
-
At line numbers 11 and 14, there are 2 arrows which point towards each other. The marker represents the method’s start and end position. If you click on any arrow, then it will perform the fold action on that code block. The Gutter area will show various symbols when certain actions are taken; for instance, it will show symbols for breakpoints, Java annotations.
Status Bar
在编辑器窗口的底部,有一个状态栏,它会显示有关当前文件和项目状态的信息。
At the bottom of the Editor window, there is a status bar, which shows information about the current file and the project status.
-
In the first image, 16:1 represents the current line number and the column number respectively.
-
Next is the line separator, for UNIX and mac OS it’s \n and for windows it’s \r.
-
UTF-8 represents the file encoding.
-
Next is the lock icon. We can toggle file’s read-only attribute by clicking on it.
-
At the end, there is a symbol with a man’s face. It allows managing the code inspection level. When we type code in Editor, IntelliJ inspects code on the fly and shows hints/suggestion. We can decide the inspection level from here. For instance, we can completely disable it or allow inspection only for syntax and so on. We can also enable the Power Save mode from here, which will disable background code analysis and other background jobs.
-
The Status bar also shows information about project actions. For instance, second image show information about project compilation.
Scratches
IntelliJ 提供了一个临时编辑器。您可以在不修改当前项目结构的情况下创建文本或代码片段。它提供了两种类型的临时文件 -
IntelliJ provides a temporary Editor. You can create text or piece of code without modifying the current project structure. It provides two types of temporary files −
Scratch files
它们是可操作的、可运行的和可调试的。创建草稿文件 −
They are functional, run-able and debug-able. To create a scratch file −
-
Go to File → New → Scratch file.
-
Select the language of your choice.
-
It will create a file in the IntelliJ-Installation-Directory\ config \scratches folder.
Scratch buffers
它仅用于创建任何文本。 若要创建草稿缓冲区:
This is used only for creating any text. To create a scratch buffer −
-
Press Ctrl + Shift + A or click on Search Everywhere icon
-
It will pop up dialog box.
-
Enter new scratch buffer text in that box and press enter.
-
It’ll open scratch buffer in editor window.
与草稿文件类似, scratch buffers 也存储在 IntelliJ-Installation-Directory\config\scratches 文件夹中。
Like the scratch files, scratch buffers are also stored in the IntelliJ-Installation-Directory\config\scratches folder.
Scrollbar
滚动条突出显示代码中的警告/错误消息/待办事项标记。例如,sayHello() 和 sayGoodBye() 方法从未使用过;因此,滚动条显示黄色标记。
Scrollbar highlights warnings/error messages/TODO markers from the code. For instance, the sayHello() and sayGoodBye() method is never used; hence, the scrollbar shows yellow marker for them.
在第 8 行,有待办事项注释,滚动条使用蓝色标记突出显示它。错误使用红色标记突出显示。
At line number 8, there is the TODO comment, scrollbar highlights it using the blue marker. Errors are highlighted using red marker.
Editor Actions
在本节中,我们将讨论 IntelliJ 中的编辑器操作。
In this section, we will discuss the Editor actions in IntelliJ.
Search and Replace
要搜索当前文件中的文本:
To search text in the current file −
-
Navigate to the file in the Editor window and press Ctrl+F.
-
It will show text box, type text to be searched there.
-
You can provide various options here - case sensitive match, exact match, regular expression and so on.
你可以在非常细化的级别执行搜索操作。例如,可以在项目、模块或目录级别进行搜索:
You can perform the search operation at a very granular level. For instance, search can be done at project, module or directory level −
-
Press the Ctrl+Shift+F key combination.
-
A new window will appear; from here, you can select the appropriate level.
要对当前文件执行替换操作:
To perform a replace action in the current file −
-
Press the Ctrl+R key combination.
-
A dialog box will appear, enter the text to be replaced here.
-
This operation allows you to replace single match, all matches or skip current match.
要执行粒度级别的替换操作 -
To perform the replace action at a granular level −
-
Press the Shift+Ctrl+R key combination.
-
It will allow you replace text at project, module, directory and other scopes.
Column Selection Mode
要启用列模式选择,在选择文本时按住 Alt 键。要永久启用列选择模式,选择 Edit → Column selection mode 。
To enable the column mode selection, hold the Alt key while selecting the text. To enable the column selection mode on a permanent basis, select Edit → Column selection mode.
Clipboard History
当我们复制任何文本时,它将进入剪贴板历史记录。IntelliJ 维护剪贴板历史记录。要查看此历史记录,请按 Ctrl+Shift+V 键组合。你可以从该窗口中选择要粘贴的内容。
When we copy any text, it goes to the clipboard history. IntelliJ maintains the clipboard history. To view this history, pressthe Ctrl+Shift+V key combination. You can select the content to paste from this window.
Code Completion
IntelliJ 会检查 code on the fly 并提供准确的代码完成。例如,当我键入单词 say 时 - 它会建议方法 sayHello() 和 sayGoodBye() 。
IntelliJ inspects the code on the fly and provides accurate code completion. For instance, when I type the word say - it suggests sayHello() and sayGoodBye() methods.
代码补全在处理类对象时建议类属性和类方法。
Code completion suggests class attributes and class methods while working with the class objects.
Generating Code
IntelliJ 提供生成有用的代码(例如构造函数、getter、setter、toString() 方法、override 方法等)的一种方法。按照以下步骤操作以在编辑器窗口中右键单击生成。
IntelliJ provides a way to generate useful code like constructor, getter, setter, toString() method, override methods and so on. Follow these steps to generate right-click in the Editor window.
-
Select the Generate option.
-
It will list the methods for which code can be can generated.
Code Inspection
我们已经看到 IntelliJ 识别语法错误、警告消息和 TODO 标记。除此之外,它还会建议代码改进、识别死代码和代码优化。例如,在下面给出的代码中,flag 的值始终为 true。因而 else 部分将永远不会被执行。
We have already seen that IntelliJ identifies syntax errors, warning messages and TODO markers. In addition to this, it suggests code improvement, identifies dead code, code optimization. For instance, in the code given below, the value of flag is always true. Hence, the else part will never be executed.
boolean flag = true;
if (flag == true) {
System.out.println("Value of flag is true");
} else {
System.out.println("Value of flag is false");
}
IntelliJ 识别到这一点并建议移除此代码块。
IntelliJ identifies this and suggests removing this code block.
Comparing Files and Folders
按照以下步骤比较文件和文件夹 -
Follow these steps to compare files and folders −
-
Hold the Ctrl key and select files to be compared from the project perspective.
-
Right-click on it and select the Compare Files option.
-
It will open the Compare window where the changes are highlighted.
你也可以将从一个文件应用到另一个文件中的更改。如果你观察,有两个箭头标记以红色突出显示。单击它们以应用来自其他文件的更改。按 Ctrl+Z 撤消更改。
You can also apply the changes from one file to another. If you observe, there are two arrow markers highlighted in red color. Click on them to apply changes from other file. Press Ctrl+Z to undo the changes.
类似地,要比较文件夹,请从项目视角(而不是文件)选择文件夹。
Similarly, to compare the folders, select folders from project perspective instead of files.
Getting Help
获取代码详细信息非常有用,例如方法说明、其返回类型、参数类型和参数数量——IntelliJ 可在编辑器窗口本身中提供这些详细信息。
It is useful if you get details about code easily. Like the method description, its return type, arguments type and number of arguments - IntelliJ can provide these details in the Editor window itself.
Inline Documentation
顾名思义,IntelliJ 可以通过从代码提取文档来访问文档。如果您为代码提供 Java 文档,那么 IntelliJ 就能够显示有关该方法的帮助。要访问内联文档,请将光标悬停在方法名称上并按 Ctrl+Q 键组合。
As the name suggests, IntelliJ can access documentation by extracting it from the code. If you provide Java Docs for your code, then IntelliJ can show help about it. To access inline documentation, hover click on method name and press the Ctrl+Q key combination.
Viewing Definition
要查看定义,请将鼠标悬停在方法名称上并按 Ctrl+Shift+I 键组合。
To view definition, hover the mouse over method name and press the Ctrl+Shift+I key combination.
Linting Code
Linting 是一个过程,其中 lint 工具分析源代码并报告与编码标准相关的潜在问题。它有助于提高代码质量。IntelliJ 支持可对代码进行 lint 的 SonarLint 插件。
Linting is a process in which the lint tool analyzes the source codes and reports potential issues related to the coding standard. It helps in improving the code quality. IntelliJ supports SonarLint plug-in which lints the code.
About SonarLint
安装并启用 SonarLint 插件后,当您在编辑器窗口中打开一个文件时,它将开始自动分析代码。它将在 SonarLint 面板中报告问题。我们将在本节中详细讨论它。
Once you install and enable the SonarLint plug-in, it will start analyzing the code automatically when you open a file in the Editor window. It will report issues in the SonarLint panel. We will discuss more about it in this section.
SonarLint 支持流行的编程语言,如 Java、JavaScript、PHP 和 Python。
SonarLint supports popular programming languages like Java, JavaScript, PHP and Python.
Installation
按照以下步骤安装 SonarLint −
Follow these steps to install SonarLint −
-
Download SonarLint from here.
-
Go to File → Settings → Select plugins.
-
Click on the Install plugin from disk button.
-
Follow on-screen installation to complete the installation procedure.
Code Analysis
安装并启用插件后,它将开始分析 code on the fly 。它将在 SonarLint 面板中报告问题。按照以下步骤查看当前文件的错误 −
Once the plug-in is installed and enabled, it will start analyzing code on the fly. It will report issues in the SonarLint panel. Follow these steps to view issues of the current file −
-
Click on the Current file tab.
-
It will report issues like unused local variables, unused class attributes, message logging and so on.
-
To know more about issue, click on issue from SolarLint panel.
-
In the right side of the window, it will show its severity as Major, Minor and so on.
Do One Time Code Analysis
如果您不熟悉即时代码分析,那么您可以在准备好代码后禁用它并执行一次代码分析。按照以下步骤禁用即时代码分析 −
If you are not comfortable with on-the-fly code analysis, then you can disable it and perform one time code analysis once you are ready with code. Follow these steps to disable on-the-fly code analysis −
-
Go to File → Settings → Other Setting → SonarLint General Settings
-
Uncheck Automatically Trigger Analysis checkbox from the settings tab.
-
Click on Apply followed by Ok button.