如何在 Jetbrains Rider 套用 editorconfig

在同一個團隊裡面每個人的開環境和習慣都不太一樣,IDE 的選擇也玲瑯滿目,也很難硬性規範團隊使用特定的環境跟 IDE。我們會面臨到 IDE 程式碼編排風格的問題,這時候 Editor Config 就是統一不同 IDE 程式碼格式的套件,只要在你的專案資料夾加入 .editorconfig 並配置好程式碼的編排風格,比如像是縮排、tab 的寬度以及 EOL 字元,只要確定編輯器所安裝的套件認得 .editorconfig 這支檔案就會自動套用了。

開發環境

  • Windows 10 Pro
  • Rider 2021.2.1

Editor Config 簡介

.editorconfig 是一個 INI 格式的檔案,是由 section 跟 properties 組成

section

  • 套用的檔案路徑跟副檔名,例如.py .jc .java 格式檔 [*.{js,py}],匹配方式請參考官方網站
     

properties 

  • 排版規則,要注意的是並非所有的套件都有支援所有的屬性,這裡有一個 完整的列表
  • 屬性和值不分大小寫

 

配置方式

有關 Code Style 的設定都會被存放在 Jetbrains Rider 的組態設定裡

x 
x 
Editor Code Style C# 
Auto-Detect Formatting Rules... 
> Appearance & Behavior 
Keymap 
v Editor 
> General 
Code Editing 
Font 
> Color Scheme 
v Code Style 
> SQL 
AsciiDoc 
ASP.NET (Razor) 
CoffeeScript 
> Style Sheets 
CSV/TSV/PSV 
Dart 
EditorConfig 
GherkinDotnet 
HTML 
JavaScript 
JSON 
Markdown 
Manage Layers 
Based on the existing code of the entire solution 
Tabs, Indents, Alignment 
'var' Usage in Declarations 
For built-in types 
For simple types 
Elsewhere 
Naming 
Syntax Style 
Braces Layout 
Blank Lines 
Use 'var' 
Use 'var' 
Use 'var' 
Line Breaks and Wrapping 
Prefer Roslyn (Visual Studio) logic for type evidence 
Prefer separate declarations for deconstructed variables 
Use 'var' keyword for discards 
Instance Members Qualification 
Use "this." qualifier for 
Qualify members declared in 
Static Members Qualification 
Field, Property, Event, Method 
same class, base class 
Spaces 
Hint 
Hint 
Hint 
Suggestion 
Hint 
Hint 
Hint 
Null Chec 
Cancel 
rby, 
Click an option to see preview 
fi ter 
majestic! 
Save 
Gitlab pipeline viewer 
arnings in 93 files (D

 

組態設定有分個人、方案資料夾、電腦 三種等級,按下紅框處,可以取得組態設定的路徑,我的例路徑如下:

  • 個人:D:\src\gitlab\yao.git.lab\Lab.Project.sln.DotSettings.user
  • 方案資料夾:D:\src\gitlab\yao.git.lab\Lab.Project.sln.DotSettings
  • 電腦:C:\Users\Yao Chang Yu\AppData\Roaming\JetBrains\Rider2021.2\resharper-host\GlobalSettingsStorage.DotSettings
Settings Layers 
Solution "Lab.Project" personal 
Solution "Lab.Project" team-shared 
This computer 
x 
sln.DotSettings.User file in solution folder — should not be added to Sou... 
sln.DotSettings file in solution folder — shared with team if added to Sou... 
Rider User Application Data folder. 
Close

 

除此之外,也可以套用團隊內寫的 .editorconfig,Jetbrains Rider 預設支援 EditorConfig

Settings 
editorconfig 
v Appearance & Behavior 
Notifications 
Keymap 
v Editor 
v Color Scheme 
EditorConfig 
v Code Style 
EditorConfig 
v Inspection Settings 
Inspection Severity 
Plugins 
Manage Layers 
x 
Plugins 
Q' editorconfig 
Search Results (1) 
EditorConfig 
bundled 
Marketplace 
Installed 
EditorConFig 
bundled 212.5080.71 
Enabled 
Adds support for EditorConfig files. 
• Define code style settings using EditorConfig files 
• Use coding assistance in EditorConfig files 
To create an EditorConfig file in a directory, right-click this directory and 
select New I EditorConfig File. 
Originally developed by Kirill Glazyrin and Kevin Bell. 
Save 
Cancel

 

啟用/停用 editorconfig

我先把 .editorconfig 放到方案路徑下 D:\project\src\.editorconfig

官方文件說,Editor Config 套件會自動套用 .editorconfig,如果沒有套用可以依照以下步驟啟用

在檔案總管模式開啟 .editorconfig → 啟動

File 
Edit 
View 
Navigate 
•torconfig 
Code Refactor Build Run 
Debug I Any CPU v 
Tests Tools 
WebApi 
.editorconfig X 
Git 
Window 
Help 
Lab.Project C. 
..\yao.git.lab] 
x 
Lab.Project 
File System 
yao.git.lab • 
EditorConfig support is disabled for the project 
http: //editorconfig . org 
root 
= true 
•ndent_styte = space 
•ndent size = 2 
nd of tine = If 
harset 
= utf-8 
rim_traiting_whitespace 
= true 
•nsert final newline 
= true 
.cs] 
•ndent size = 4 
Use 4 spaces for the Python files 
Enable Open settings 
03 A 6 A v 
.git • no index 
.idea 
Lab.AspNetCore5 
WebApi 
.editorcontig 10:06, 742 B Mome 
/9/3 _L{F 12:42, 306 B Today _ 
C# file.cs 2021/9/3 _E{F 12:46, 82 B 32 minutes ago 
IT Lab.Project.sln 2021/8/31 02:23, 816B Yesi 
Scratches and Consoles 
13 
12 
11 
9 
8 
6 
3 
2 
1 
14 
1 
2 
3 
4 
6 
7 
01 
01 
01 
01 
•ndent size = 4 
The JSON files 
= 80 
contain newlines inconsistently 
p Git 
TODO 
Dynamic Program Analysis 
6) Unit Tests dotTrace Profiler 
5 hrs 8 mins V 1 A/up-to-date Misc Files 
14:21 CRLF 
E Terminal NuGet Endpoints O Services 
p master 16 warnings in 3 files (D 
4 spaces

 

啟動之後就可以在 Jetbrains Rider 狀態列看到 EditorConfig 可以從這裡打開 .editorconfig 

•e NuGet Endpoints 
/11 12:16 Misc Files 
O Service 
CRLF 
EditorConfig 
Open Current File 
Disable for Project 
Show Files Related to Project... 
4 spaces P Query 
Gitlab pipeline viewer 
ent Log 
311 warnings in 93 files

 

在設定畫面裡面,可以看到黃色驚嘆號描述 .editorconfig 已經套用並且複寫 Setting;反之,沒有啟用

Settings 
> Appearance & Behavior 
Keymap 
v Editor 
> General 
Code Editing 
Font 
> Color Scheme 
> Code Style 
> Inspection Settings 
Members Generation 
File Encodings 
File Types 
Code Vision 
> Inlay Hints 
Code Cleanup 
Context Actions 
> Emmet 
File Header Template 
> File Templates 
Language Injections 
> Live Templates 
Log Highlighting (Ideolog) 
> Spelling 
Manage Layers 
x 
Editor 
Scheme: 
Code Style 
Default 
IDE 
A Settings may be overridden by EditorConfig Disable 
General 
Formatter 
Line separator: 
1 
Hard wrap at: 
Visual guides: 
System-Dependent 
Applied to new files 
120 
Optional 
Specify one guide (80) or several (80, 120) 
columns 
columns 
Wrap on typing 
Detect and use existing file indents for editing 
StyleCop 
Enable StyleCop support 
StyleCop configuration may override the IDE code style settings 
•V Enable EditorConfig support 
EditorConfig may override the IDE code style settings 
Export 
Save 
Cancel

 

接著,可以看到有哪一些樣式已經被套用,棕色底色代表已經套用 .editorconfig;除此之外,按下 Edit 按鈕就可以直接編輯格式化定義

Settings 
> Appearance & Behavior 
Keymap 
v Editor 
> General 
Code Editing 
Font 
> Color Scheme 
v Code Style 
> SQL 
AsciiDoc 
ASP.NET (Razor) 
CoffeeScript 
> Style Sheets 
CSV/TSV/PSV 
Dart 
EditorConfig 
GherkinDotnet 
HTML 
JavaScript 
JSON 
Markdown 
Manage Layers 
Editor Code Style C# 
Auto-Detect Formatting Rules... 
Based on the existing code of the entire solution 
Line Breaks and Wrapping 
120 
38 
Spaces 
x 
Edi 
Tabs, Indents, Alignment 
Naming 
Syntax Style 
Blank Lines 
Braces Layout 
EditorConfig fo ..\.editorconfig override(s) some Code Style settings on this page 
General 
Keep existing line breaks 
Wrap long lines 
Hard wrap at 
Prefer wrap before ", 
Prefer wrap before " 
Special "else if" treatment 
Line feed at end of file 
Arrangement of Attributes 
Place attribute section list on separate line if it is longer than 
Keep existing arrangement of attributes 
Click an option to see preview 
Save 
Null Chec 
Cancel

 

修改 .editorconfig 檔案

透過編輯畫面,我們就可以編輯 .editorconfig,完成後按下 Save

Layer Settings 
Layer: .editorconfig 
.editorconfig files for ..\.editorconfig 
Editor Code Style C# 
x 
v Editor 
v Code Style 
ASP.NET (Razor) 
GherkinDotnet 
ShaderLab 
VB.NET 
XAML 
Auto-Detect Formatting Rules... 
Based on the existing code of the entire solution 
Tabs, Indents, Alignment 
General 
Keep existing line breaks 
Wrap long lines 
Hard wrap at 
Prefer wrap before ", 
Prefer wrap before " 
Syntax Style 
Braces Layout 
Blank Lines 
Line Breaks and Wrapping 
Spaces 
120 
38 
XML documentation 
Special "else if" treatment 
Line feed at end of file 
Arrangement of Attributes 
Place attribute section list on separate line if it is longer than 
Keep existing arrangement of attributes 
Before: 
= condition 
int var 
? SomeFunc1() 
. SomeFunc2(); 
After: 
int var 
= condition 
1 
? SomeFunc1() 
. SomeFunc2(); 
Save 
Cancel

 

存檔時,可以選擇要套用的 Section 和需要匯出的內容

 

匯出.editorconfig

我們也可以把 Rider 的設定直接匯出成 .editorconfig 檔,下圖有兩種匯出方式,經過我試驗產生的內容不太一樣

最後我選用步驟 2

除錯

如果排版沒有生效,可以試著還原 CodeStyle 的設定

參考

Use EditorConfig | JetBrains Rider

EditorConfig properties | JetBrains Rider

EditorConfig

VS IDE 設定參考

.NET 程式碼樣式規則選項 - .NET | Microsoft Docs

EditorConfig 設定 - Visual Studio (Windows) | Microsoft Docs

程式碼樣式語言規則 - .NET | Microsoft Docs

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo