Skip to content

Repo sync for protected branch #10368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions docs/ide/cpp-editorconfig-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "C++ EditorConfig formatting conventions"
titleSuffix: ""
description: "Learn about how to use EditorConfig to format C++ code in Visual Studio."
ms.date: 9/14/2020
ms.date: 08/22/2024
author: jureid
ms.author: ghogen
manager: mijacobs
Expand All @@ -11,17 +11,23 @@ dev_langs:
- CPP

ms.topic: reference
monikerRange: vs-2019
---
# C++ EditorConfig formatting conventions

The Visual Studio C++ formatter has a rich set of configurable settings that can be applied globally. To set C++ formatting settings for a specific workspace, use [clangformat](https://clang.llvm.org/docs/ClangFormat.html) or [EditorConfig](https://editorconfig.org/). Both Visual Studio and Visual Studio Code have built-in EditorConfig support for each of the global Visual Studio C++ formatting settings, with the EditorConfig settings taking precedence. This means you can add EditorConfig files to your workspace to configure C++ formatting on a more granular level and enforce consistent code style for everyone contributing to the project.
You can add EditorConfig files to your project to configure C++ formatting to enforce a consistent code style for everyone contributing to the project. Both Visual Studio and Visual Studio Code have built-in [EditorConfig](https://editorconfig.org/) support for each of the global Visual Studio C++ formatting settings. The EditorConfig settings are stored in an `.editorconfig` file. When that file is present in your project, it takes precedence over the Visual Studio formatting settings.

This document lists all the EditorConfig C++ formatting settings supported by Visual Studio and Visual Studio Code.

For more information about:

- Using an EditorConfig file in Visual Studio, see [Define consistent coding styles with EditorConfig](create-portable-custom-editor-options.md).
- Manually create an EditorConfig file based on clang formatting settings, see [clangformat](https://clang.llvm.org/docs/ClangFormat.html). Rename that file `.editorconfig` and save it in your project.

## C++ formatting conventions

C++ formatting EditorConfig settings are prefixed with `cpp_`. Here's an example of what your EditorConfig file might look like:
C++ formatting EditorConfig settings are prefixed with `cpp_`. Here's an example of what an `.editorconfig` file might look like:

```ini
```
[*.{c++,cc,cpp,cxx,h,h++,hh,hpp,hxx,inl,ipp,tlh,tli}]

cpp_indent_case_contents_when_block = true
Expand Down
30 changes: 17 additions & 13 deletions docs/ide/create-portable-custom-editor-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ ms.date: 04/01/2024
---
# Define consistent coding styles with EditorConfig

To enforce consistent coding styles for everyone that works in a codebase, you can add an EditorConfig file to your solution or project. EditConfig file settings adhere to a file format specification maintained by [EditorConfig.org](https://editorconfig.org). Many code editors and applications support EditorConfig files, including Visual Studio. It's a portable component that travels with your code, and can enforce coding styles even outside of Visual Studio.
To enforce consistent coding styles for everyone that works in a codebase, you can add an `.editorConfig` file to your solution or project. EditorConfig file settings adhere to a file format specification maintained by [EditorConfig.org](https://editorconfig.org). Many code editors and applications support EditorConfig files, including Visual Studio. Because the settings are in a file, they accompany your code, and can be used even outside of Visual Studio.

In Visual Studio, EditorConfig file settings take precedence over the global text editor settings (accessed by selecting **Text Editor** in the **Tools** > **Options** dialog box). This means that you can tailor each codebase to use text editor settings that are specific to that project. When you use EditorConfig files, you can still continue to set your own personal editor preferences in the Visual Studio text editor. These text editor settings apply whenever you're working in a codebase without an EditorConfig file, or when the EditorConfig file doesn't override a particular setting. An example of such a preference is whether to use tabs or spaces for the code indent style.
In Visual Studio, EditorConfig file settings take precedence over the various global text editor settings available under **Tools** > **Options** > **Text Editor** > **C/C++** > **Code Style**. You can use an EditorConfig file to tailor each codebase to use text editor settings that are specific to that project.

You can manually populate an EditorConfig file, or automatically generate it based on the code style settings you've chosen in Visual Studio. To create a new `.editorconfig` file based on your current Visual Studio settings, from the Visual Studio main menu choose **Tools** > **Options** > **Text Editor** > **C/C++** > **General**. Then choose **Generate .editorconfig file from settings**. Choose the folder where your project is and then choose **Select Folder** to save the `.editorconfig` file.

When you use an EditorConfig file, you can still continue to set your own personal editor preferences in the Visual Studio text editor. These text editor settings apply whenever you're working in a codebase without an EditorConfig file, or when the EditorConfig file doesn't override a particular setting. An example of such a preference is whether to use tabs or spaces for the code indent style.

When you add an EditorConfig file to your project in Visual Studio, only new lines of code are formatted based on the EditorConfig settings. The formatting of existing code isn't changed unless you run one of the following commands:

Expand All @@ -38,7 +42,7 @@ EditorConfig file settings let you maintain consistent coding styles and convent

EditorConfig files are useful when coding conventions you use on your personal projects differ from those conventions used on your team's projects. For example, you might prefer that an indent adds a tab character in your code. However, your team might prefer that an indent adds four space instead. EditorConfig files resolve this problem by letting you have a configuration for each scenario.

Because an *.editorconfig* file in the codebase contains the EditorConfig settings, they travel along with that codebase. As long as you open the code file in an EditorConfig-compliant editor, the text editor settings are activated.
Because an `.editorconfig` file in the codebase contains the EditorConfig settings, they travel along with that codebase. As long as you open the code file in an EditorConfig-compliant editor, the text editor settings are activated.

::: moniker range="<=vs-2019"
> [!NOTE]
Expand Down Expand Up @@ -72,7 +76,7 @@ If you remove an EditorConfig file from your project or codebase, you must close

To add an EditorConfig file to your project or solution, follow these steps:

1. Open a project or solution in Visual Studio. Select either the solution or project node, depending on whether your EditorConfig settings should apply to all projects in the solution or just one. You can also select a folder in your project or solution to add the *.editorconfig* file to.
1. Open a project or solution in Visual Studio. Select either the solution or project node, depending on whether your EditorConfig settings should apply to all projects in the solution or just one. You can also select a folder in your project or solution to add the `.editorconfig` file to.

1. From the menu, choose **Project** > **Add New Item**, or press **Ctrl**+**Shift**+**A**.

Expand All @@ -86,7 +90,7 @@ To add an EditorConfig file to your project or solution, follow these steps:

1. Select the **editorconfig File (empty)** template to add an EditorConfig file prefilled with default EditorConfig options for whitespace, code style, and naming conventions. Or, select the **editorconfig File (.NET)** template to add an EditorConfig file prefilled with default [.NET whitespace, code style, and naming conventions](/dotnet/fundamentals/code-analysis/code-style-rule-options).

A new *.editorconfig* file appears in Solution Explorer, and opens in the editor as a new tab.
A new `.editorconfig` file appears in Solution Explorer, and opens in the editor as a new tab.

::: moniker range=">= vs-2022"
:::image type="content" source="media/vs-2022/editorconfig-dotnet-new.png" alt-text="Screenshot that shows the .editorconfig file in Solution Explorer and editor.":::
Expand All @@ -98,23 +102,23 @@ To add an EditorConfig file to your project or solution, follow these steps:

There are a couple of other ways you can add an EditorConfig file to your project:

- Use [IntelliCode for Visual Studio](/visualstudio/ide/intellicode-visual-studio) to generate an *.editorconfig* file in your solution. Right-click in Solution Explorer and choosing **Add** > **New EditorConfig (IntelliCode)** from the context menu. IntelliCode for Visual Studio infers your code styles from existing code, and then creates an EditorConfig file with your code-style preferences predefined.
- Use [IntelliCode for Visual Studio](/visualstudio/ide/intellicode-visual-studio) to generate an `.editorconfig` file in your solution. Right-click in Solution Explorer and choosing **Add** > **New EditorConfig (IntelliCode)** from the context menu. IntelliCode for Visual Studio infers your code styles from existing code, and then creates an EditorConfig file with your code-style preferences predefined.

- [Automatically generate an EditorConfig file based on your code-style settings](code-styles-and-code-cleanup.md#code-styles-in-editorconfig-files).

## File hierarchy and precedence

When you add an *.editorconfig* file to a folder in your file hierarchy, its settings apply to all applicable files at that level and lower. You can also override EditorConfig settings for a particular project, codebase, or part of a codebase, such that it uses different conventions than other parts of the codebase. Doing so can be useful when you incorporate code from somewhere else, and don’t want to change its conventions.
When you add an `.editorconfig` file to a folder in your file hierarchy, its settings apply to all applicable files at that level and lower. You can also override EditorConfig settings for a particular project, codebase, or part of a codebase, such that it uses different conventions than other parts of the codebase. Doing so can be useful when you incorporate code from somewhere else, and don’t want to change its conventions.

Follow these guidelines:

- To override some or all of the EditorConfig settings, add an *.editorconfig* file at the level of the file hierarchy you want those overridden settings to apply. The new EditorConfig file settings apply to files at the same level and files in any subdirectories.
- To override some or all of the EditorConfig settings, add an `.editorconfig` file at the level of the file hierarchy you want those overridden settings to apply. The new EditorConfig file settings apply to files at the same level and files in any subdirectories.

:::image type="content" source="../ide/media/vside_editorconfig_hierarchy.png" alt-text="Screenshot that shows the EditorConfig hierarchy.":::

- If you want to override some, but not all of the settings, specify just those settings in the *.editorconfig* file. Only those properties that you explicitly list in the lower-level *.editorconfig* file are overridden. Other settings from any higher-level *.editorconfig* files continue to apply.
- If you want to override some, but not all of the settings, specify just those settings in the `.editorconfig` file. Only those properties that you explicitly list in the lower-level `.editorconfig` file are overridden. Other settings from any higher-level `.editorconfig` files continue to apply.

- If you want to ensure that no settings from any higher-level *.editorconfig* files are applied to this part of the codebase, add the `root=true` property to the lower-level *.editorconfig* file.
- If you want to ensure that no settings from any higher-level `.editorconfig` files are applied to this part of the codebase, add the `root=true` property to the lower-level `.editorconfig` file.

```ini
# top-most EditorConfig file for this level
Expand Down Expand Up @@ -167,7 +171,7 @@ The following example shows the indent state of a C# code snippet before and aft
::: moniker-end

::: moniker range="vs-2019"
Add a new file named *.editorconfig* to the project, with the following contents. The `[*.cs]` indicator means that this change applies only to C# code files in the project.
Add a new file named `.editorconfig` to the project, with the following contents. The `[*.cs]` indicator means that this change applies only to C# code files in the project.

```ini
# Top-most EditorConfig file
Expand Down Expand Up @@ -209,9 +213,9 @@ To troubleshoot EditorConfig issues, follow these steps:
dir .editorconfig /s
```

1. To control the scope of your EditorConfig conventions, set the `root=true` property in the *.editorconfig* file at the root of your repo or in the directory that your project resides.
1. To control the scope of your EditorConfig conventions, set the `root=true` property in the `.editorconfig` file at the root of your repo or in the directory that your project resides.

Visual Studio looks for a file named *.editorconfig* in the directory of the opened file and in every parent directory. The search ends when it reaches the root filepath, or if an *.editorconfig* file with `root=true` is found.
Visual Studio looks for a file named `.editorconfig` in the directory of the opened file and in every parent directory. The search ends when it reaches the root filepath, or if an `.editorconfig` file with `root=true` is found.

## Related content

Expand Down