Skip to content

Commit ab8460e

Browse files
authored
Merge pull request #4514 from gewarren/code-cleanup
Rework Code Cleanup sections
2 parents 89043c6 + 872d78e commit ab8460e

19 files changed

+165
-91
lines changed

.openpublishing.redirection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,11 @@
12801280
"redirect_url": "/visualstudio/ide/class-designer/designing-and-viewing-classes-and-types",
12811281
"redirect_document_id": false
12821282
},
1283+
{
1284+
"source_path": "docs/ide/code-styles-and-quick-actions.md",
1285+
"redirect_url": "/visualstudio/ide/code-styles-and-code-cleanup",
1286+
"redirect_document_id": false
1287+
},
12831288
{
12841289
"source_path": "docs/ide/command-line-arguments-for-the-help-content-manager.md",
12851290
"redirect_url": "/visualstudio/help-viewer/command-line-arguments",

docs/code-quality/analyzers-faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This page contains answers to some frequently asked questions about Roslyn analy
1818

1919
**Q**: Should I use Roslyn analyzers or .editorconfig for code style?
2020

21-
**A**: Roslyn analyzers and .editorconfig files work hand-in-hand. When you define code styles [in an .editorconfig file](../ide/editorconfig-code-style-settings-reference.md) or on the [text editor Options](../ide/code-styles-and-quick-actions.md) page, you're actually configuring the Roslyn analyzers that are built into Visual Studio. EditorConfig files can also be used to configure some third-party analyzer packages, such as [FxCop analyzers](configure-fxcop-analyzers.md).
21+
**A**: Roslyn analyzers and .editorconfig files work hand-in-hand. When you define code styles [in an .editorconfig file](../ide/editorconfig-code-style-settings-reference.md) or on the [text editor Options](../ide/code-styles-and-code-cleanup.md) page, you're actually configuring the Roslyn analyzers that are built into Visual Studio. EditorConfig files can also be used to configure some third-party analyzer packages, such as [FxCop analyzers](configure-fxcop-analyzers.md).
2222

2323
## EditorConfig versus rule sets
2424

docs/code-quality/roslyn-analyzers-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ms.workload:
1414
---
1515
# Overview of .NET Compiler Platform analyzers
1616

17-
.NET Compiler Platform ("Roslyn") analyzers analyze your code for style, quality and maintainability, design, and other issues. Visual Studio includes a built-in set of analyzers that analyze your C# or Visual Basic code as you type. You configure preferences for these built-in analyzers on the [text editor Options](../ide/code-styles-and-quick-actions.md) page or in an [.editorconfig file](../ide/editorconfig-code-style-settings-reference.md). You can install additional analyzers as a Visual Studio extension or a NuGet package.
17+
.NET Compiler Platform ("Roslyn") analyzers analyze your code for style, quality and maintainability, design, and other issues. Visual Studio includes a built-in set of analyzers that analyze your C# or Visual Basic code as you type. You configure preferences for these built-in analyzers on the [text editor Options](../ide/code-styles-and-code-cleanup.md) page or in an [.editorconfig file](../ide/editorconfig-code-style-settings-reference.md). You can install additional analyzers as a Visual Studio extension or a NuGet package.
1818

1919
If rule violations are found by an analyzer, they are reported in the code editor (as a *squiggly* under the offending code) and in the **Error List** window.
2020

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Code style options and code cleanup
3+
ms.date: 04/25/2019
4+
ms.topic: conceptual
5+
author: gewarren
6+
ms.author: gewarren
7+
manager: jillfra
8+
f1_keywords:
9+
- "VS.ToolsOptionsPages.Text_Editor.CSharp.Code_Style.General"
10+
- "VS.ToolsOptionsPages.Text_Editor.Basic.Code_Style.General"
11+
ms.workload:
12+
- "multiple"
13+
---
14+
# Code style preferences
15+
16+
You can define code style settings per-project by using an [EditorConfig file](#code-styles-in-editorconfig-files), or for all code you edit in Visual Studio on the text editor [**Options** page](#code-styles-in-the-options-dialog-box). For C# code, you can also configure Visual Studio to apply these code style preferences using the **Code Cleanup** (Visual Studio 2019) and **Format Document** (Visual Studio 2017) commands.
17+
18+
> [!NOTE]
19+
> This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see [Editor behavior in Visual Studio for Mac](/visualstudio/mac/editor-behavior).
20+
21+
## Code styles in EditorConfig files
22+
23+
[Code style settings](../ide/editorconfig-code-style-settings-reference.md) for .NET can be specified by adding an [EditorConfig](create-portable-custom-editor-options.md) file to your project. EditorConfig files are associated with a codebase rather than a Visual Studio personalization account. Settings in an EditorConfig file take precedence over code styles that are specified in the **Options** dialog box. Use an EditorConfig file when you want to enforce coding styles for all contributors to your repo or project.
24+
25+
::: moniker range=">=vs-2019"
26+
27+
You can manually populate your EditorConfig file, or you can automatically generate the file based on code style settings you've set in the Visual Studio **Options** dialog box for the C# or Visual Basic text editor. This options page is available at **Tools** > **Options** > **Text Editor** > [**C#** or **Basic**] > **Code Style** > **General**.
28+
Click **Generate .editorconfig file from settings** to automatically generate a coding style *.editorconfig* file based on the settings on this **Options** page.
29+
30+
![Generate editorconfig file from settings in Visual Studio 2019](media/vs-2019/generate-editorconfig-file-small.png)
31+
32+
::: moniker-end
33+
34+
## Code styles in the Options dialog box
35+
36+
Code style preferences can be set for all of your C# and Visual Basic projects by opening the **Options** dialog box from the **Tools** menu. In the **Options** dialog box, select **Text Editor** > [**C#** or **Basic**] > **Code Style** > **General**.
37+
38+
Each item in the list shows a preview of the preference when selected:
39+
40+
::: moniker range="vs-2017"
41+
42+
![Code style options](media/code-style-quick-actions-dialog.png)
43+
44+
::: moniker-end
45+
46+
::: moniker range=">=vs-2019"
47+
48+
![Code style options](media/vs-2019/code-style-quick-actions-dialog.png)
49+
50+
::: moniker-end
51+
52+
Options set in this window are applicable to your Visual Studio personalization account and aren't associated with a particular project or codebase. In addition, they aren't enforced at build time, including in continuous integration (CI) builds. If you want to associate code style preferences with your project and have the styles enforced during build, specify the preferences in an [.editorconfig file](#code-styles-in-editorconfig-files) that's associated with the project.
53+
54+
### Preference and severity
55+
56+
For each code style setting on this page, you can set the **Preference** and **Severity** values using the drop-downs on each line. Severity can be set to **None**, **Suggestion**, **Warning**, or **Error**. If you want to enable [Quick Actions](../ide/quick-actions.md) for a code style, ensure that the **Severity** setting is set to something other than **None**. The **Quick Actions** light bulb ![light bulb](media/light-bulb-dropdown.png), error light bulb ![error light bulb](media/error-bulb.png), or screwdriver ![screwdriver](media/screwdriver.png) icon appears when a non-preferred style is used, and you can choose an option on the **Quick Actions** list to automatically rewrite code to the preferred style.
57+
58+
## Apply code styles
59+
60+
::: moniker range="vs-2017"
61+
62+
You can configure the **Format Document** command (**Edit** > **Advanced** > **Format Document**) to apply your code style settings (from an EditorConfig file or **Code Style** options) along with the regular formatting that it does (such as indentation). If an *.editorconfig* file exists for the project, those settings take precedence.
63+
64+
> [!NOTE]
65+
> Applying code styles by using the **Format Document** command is only available for C# code files. This is an experimental feature.
66+
67+
Configure which settings you want **Format Document** to apply on the [Formatting options page](reference/options-text-editor-csharp-formatting.md#format-document-settings).
68+
69+
![Code style settings for format document in Visual Studio 2017](media/format-document-settings-experiment.png)
70+
71+
> [!TIP]
72+
> Rules configured with a severity of **None** don't participate in code cleanup but can be individually applied via the **Quick Actions and Refactorings** menu.
73+
74+
The first time you trigger the **Format Document** command, a yellow info bar prompts you to configure your code cleanup settings.
75+
76+
::: moniker-end
77+
78+
::: moniker range=">=vs-2019"
79+
80+
For C# code files, Visual Studio 2019 has a **Code Cleanup** button at the bottom of the editor (keyboard: **Ctrl**+**K**, **Ctrl**+**E**) to apply code styles from an EditorConfig file or from the **Code Style** options page. If an *.editorconfig* file exists for the project, those are the settings that take precedence.
81+
82+
![Execute code cleanup in Visual Studio 2019](media/execute-code-cleanup.png)
83+
84+
> [!TIP]
85+
> Rules configured with a severity of **None** don't participate in code cleanup but can be individually applied via the **Quick Actions and Refactorings** menu.
86+
87+
First, configure which code styles you want to apply (in one of two profiles) in the **Configure Code Cleanup** dialog box. To open this dialog box, click the expander arrow next to the code cleanup broom icon and then choose **Configure Code Cleanup**. Or, press **Ctrl**+**K**, **Ctrl**+**Q**.
88+
89+
![Configure Code Cleanup in Visual Studio 2019](media/configure-code-cleanup.png)
90+
91+
If you want your code style settings to be applied every time you save a file, you may like the [Code Cleanup on Save](https://marketplace.visualstudio.com/items?itemName=MadsKristensen.CodeCleanupOnSave) extension.
92+
93+
::: moniker-end
94+
95+
## See also
96+
97+
- [Quick Actions](../ide/quick-actions.md)
98+
- [.NET coding convention settings for EditorConfig](../ide/editorconfig-code-style-settings-reference.md)
99+
- [Editor behavior (Visual Studio for Mac)](/visualstudio/mac/editor-behavior)

docs/ide/code-styles-and-quick-actions.md

Lines changed: 0 additions & 70 deletions
This file was deleted.

docs/ide/create-portable-custom-editor-options.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ You can add an [EditorConfig](http://editorconfig.org/) file to your project or
1414

1515
EditorConfig settings are supported by numerous code editors and IDEs, including Visual Studio. It's a portable component that travels with your code, and can enforce coding styles even outside of Visual Studio.
1616

17-
When you add an EditorConfig file to your project in Visual Studio, the formatting of existing code is not changed unless you format the document (**Edit** > **Advanced** > **Format Document** or **Ctrl**+**K**, **Ctrl**+**D** in the default profile). However, any new lines of code are formatted according to the EditorConfig settings. You can define which EditorConfig settings you want **Format Document** to apply on the [**Formatting** options page](reference/options-text-editor-csharp-formatting.md#format-document-settings).
17+
When you add an EditorConfig file to your project in Visual Studio, the formatting of existing code is not changed unless you format the document (**Edit** > **Advanced** > **Format Document** or **Ctrl**+**K**, **Ctrl**+**D** in the default profile). However, any new lines of code are formatted according to the EditorConfig settings.
18+
19+
::: moniker range="vs-2017"
20+
21+
You can define which EditorConfig settings you want **Format Document** to apply on the [**Formatting** options page](reference/options-text-editor-csharp-formatting.md#format-document-settings).
22+
23+
::: moniker-end
1824

1925
> [!NOTE]
2026
> This topic applies to Visual Studio on Windows. For Visual Studio for Mac, see [EditorConfig in Visual Studio for Mac](/visualstudio/mac/editorconfig).

0 commit comments

Comments
 (0)