Back to Devexpress

Document Formatting (C#)

coderushforroslyn-118891-code-style-assistance-document-formatting-c.md

latest8.5 KB
Original Source

Document Formatting (C#)

  • Jun 28, 2024
  • 5 minutes to read

Document Formatting allows you to enforce code formatting aspects unavailable in the Visual Studio’s Code Formatting feature.

Run Format Document Action

You can run the Format Document action in one of the following ways:

Check Apply in Action and Apply on Save for the “ Format document “ rule on the Editor | C# | Code Cleanup options page and run a Code Cleanup.

Enable “ Format block on } “ and/or “ Format statement on ; “ options on the Editor | C# | Formatting | General options page to maintain formatting as you type.

Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu , select “Format Document” and press Enter.

or

Create a new shortcut and bind it to the “ FormatDocument “ command.

The following screencast shows how the Ctrl+G shortcut initiates a Document Formatting action:

Document Formatting Options

Use the preview box at the bottom of the Editor | C# | Formatting options page to see an example of every item.

C# 10 Support

The formatting engine supports the new namespace declaration style and the “global using directives” feature for C# 10.

EditorConfig Support

CodeRush supports Visual Studio’s format settings defined in the EditorConfig file. EditorConfig settings have a higher priority than formatting preferences set in the CodeRush options dialog.

The tables below show EditorConfig settings and corresponding CodeRush formatting options.

New Line Options

EditorConfig SettingCodeRush Setting
csharp_new_line_before_open_brace = methodsThe Braces page: (Method, Simple Method) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = typesThe Braces page: (Type, Namespace) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = accessorsThe Braces page: (Accessor, Simple Accessor) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = indexersThe Braces page: Property\Indexer\Event -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = control_blocksThe Braces page: (Statement, Simple Statement) -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = lambdasThe Braces page: Lambda Expression \ Anonymous Method -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = anonymous_methodsThe Braces page: Lambda Expression \ Anonymous Method -> Wrap open brace: Wrap Before
csharp_new_line_before_open_brace = allFor all options above
csharp_new_line_before_open_brace = noneFor none of the options above
csharp_new_line_before_else = true/falseThe Other page: Place On New Line -> Place ‘else’ on new line: true/false
csharp_new_line_before_catch = true/falseThe Other page: Place On New Line -> Place ‘catch’ on new line: true/false
csharp_new_line_before_finally = true/falseThe Other page: Place On New Line -> Place ‘finally’ on new line: true/false
csharp_new_line_before_members_in_object_initializers = true/falseThe Wrapping page: Object Initializer -> Wrap separator: Wrap if long or multiline/ the Wrap separator’s current value in your settings
csharp_new_line_between_query_expression_clauses = true/falseThe Wrapping page: LINQ Expression -> Wrap clauses: Wrap/No Wrap

Indentation Options

EditorConfig SettingCodeRush Setting
csharp_indent_block_contents = true/falseThe Braces page: (Method, Simple Method, Statement, Simple Statement) -> Align content by -> Indent: 1/0
csharp_indent_braces = true/falseThe Braces page: (Method, Simple Method, Statement, Simple Statement) -> Wrap open brace-> Align by -> the Indent option: 1/0 and Method -> Wrap close brace -> Align by -> Indent: 1/0

Spacing Options

EditorConfig SettingCodeRush Setting
csharp_space_after_keywords_in_control_flow_statements = true/falseThe Spacing page: Statement -> Insert space before parentheses: true/false. Note: If this EditorConfig setting is set to true, CodeRush enables the “Insert space before parentheses” option.
csharp_space_between_parentheses = control_flow_statementsThe Spacing page: Statement -> Insert space within parentheses: true
csharp_space_before_colon_in_inheritance_clause = true/falseThe Spacing page: Base List -> Insert space before colon: true/false
csharp_space_after_colon_in_inheritance_clause = true/falseThe Spacing page: Base List -> Insert space after colon: true/false
csharp_space_around_binary_operators = before_and_after/none/ignoreThe Spacing page: (Arithmetic Expression, Bitwise Expression, Logical Expression, Relational Expression) -> Insert spaces around an operator: true/false/options are disabled for arithmetic, bitwise, logical, and relational expressions
csharp_space_between_method_declaration_parameter_list_parentheses = true/falseThe Spacing page: Parameters -> Insert spaces within parentheses: true/false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = true/falseThe Spacing page: Parameters -> Insert space within empty parentheses: true/false
csharp_space_between_method_declaration_name_and_open_parenthesis = true/falseThe Spacing page: Parameters -> Insert space before parentheses: true/false
csharp_space_between_method_call_parameter_list_parentheses = true/falseThe Spacing page: Arguments -> Insert spaces within parentheses: true/false
csharp_space_between_method_call_empty_parameter_list_parentheses = true/falseThe Spacing page: Arguments -> Insert space within empty parentheses: true/false
csharp_space_between_method_call_name_and_opening_parenthesis = true/falseThe Spacing page: Arguments -> Insert space before parentheses: true/false
csharp_space_after_comma = true/falseThe Spacing page: (Arguments, Initializer, Parameters, Type Arguments) -> Insert space after a separator (comma): true/false
csharp_space_before_comma = true/falseThe Spacing page: (Arguments, Initializer, Parameters, Type Arguments) -> Insert space before a separator (comma): true/false
csharp_space_after_semicolon_in_for_statement = true/falseThe Spacing page: Statement -> Insert space after semicolons in ‘for’ loops: true/false
csharp_space_before_semicolon_in_for_statement = true/falseThe Spacing page: Statement -> Insert space before semicolons in ‘for’ loops: true/false

Example

The “csharp_new_line_before_open_brace = types, methods” setting overrides the “Wrap open brace” option set to “After” for namespaces, types, methods, and simple methods on the Editor | C# | Formatting | Braces options page.

The screencast below shows the “csharp_new_line_before_open_brace = types, methods” setting in action: