Application scenario introduction
When we use some open source projects, we basically see a copyright notice at the head of each source code file. The number of source code files in a project or solution can range from tens to thousands or even more, so how can we easily add or modify consistent file headers for so many files in batches? In the Visual Studio 2019 v16.7.1 version updated on August 11, 2020, the ability to use EditorConfig to add headers to existing files, projects, and solutions was added . Mention EditorConfig , then:
What is EditorConfig?
EditorConfig
It is a text file that helps multiple developers working on the same project across multiple editors and IDEs to maintain a consistent coding style. The EditorConfig file can be set such as indentation style, tab width, end-of-line characters and encoding, etc., regardless of the editor or IDE used. Adding an EditorConfig file to the project can force the implementation of a consistent coding style for all people who use the project. EditorConfig settings take precedence over the global Visual Studio text editor settings.
Since these settings are contained in the basic code file, they can be moved together with the basic code. As long as you open the code file in an EditorConfig compatible editor, you can implement the text editor settings. For more information about EditorConfig files, please refer to the EditorConfig.org website.
Use Visual Studio 2019 to add file headers
First, open in Visual Studio project or solution, click on "Help"> "About" to see your version of Visual Studio 2019 is whether 16.7.1
or later, if not please upgrade.
-
Add EditorConfig to the project or solution.
According to the application
.editorconfig
object settings (all items in the solution is one of the items), select the project or the solution node. You can also select a folder to add the project or solution.editorconfig
file.From the menu bar, select "Project"> "Add New Item", or press Ctrl+Shift+A:
Or right-click the project, solution, or folder in the "Solution Explorer" and select "Add"> "New EditorConfig":
After the addition is complete you can see the
.editorconfig
contents of the file as follows:
-
In
.editorconfig
add filefile_header_template
entriesfile_header_template = 添加文件头(add file header)示例程序\n Copyright (c) https://ittranslator.cn/
.editorconfig
In line breaks, you need to use the Unix line break (\n
) to insert a new line.As shown in the figure:
-
Place the cursor on the first line of any C # or Visual Basic file, press the
Ctrl+.
trigger "Access and reconstruction" menu, select "Add header" as:
At "Repair all instances in the following objects:" you can select "Document", "Project" or "Solution". The following figure shows the preview effect after selecting "Project":
-
Click the "Apply" button to apply the changes.
At this point you can see all the code files in the project have added a
.editorconfig
set offile_header_template
string entry as a file header:
Modify the file header
The steps for modifying the file header and adding the file header are the same.
-
In the
.editorconfig
modified file infile_header_template
value of the itemTry adding a few words of "Technical Translation Station":
file_header_template = 添加文件头(add file header)示例程序\n Copyright (c) 技术译站 https://ittranslator.cn/
-
Repeat step 3 of "Add File Header" above, and the preview is as follows:
-
Repeat step 4 of "Add File Header" above and click the "Apply" button, the result is as follows:
references
- https://docs.microsoft.com/zh-cn/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019
- https://docs.microsoft.com/zh-cn/visualstudio/ide/reference/add-file-header?view=vs-2019
Author: Technical Zemin
Publisher: Technical Translation Station