Skip to content
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

Add formatting rules #1576

Merged
merged 6 commits into from
Nov 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
55 changes: 53 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,63 @@
# top-most EditorConfig file
root = true

[*]
indent_style = space

# Code files
[*.{cs,csx}]

indent_style = space
indent_size = 4

# https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options

# New-line options
# https://docs.microsoft.com/visualstudio/ide/editorconfig-formatting-conventions#new-line-options
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# Indentation options
# https://docs.microsoft.com/visualstudio/ide/editorconfig-formatting-conventions#indentation-options
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents_when_block = false

# Spacing options
# https://docs.microsoft.com/visualstudio/ide/editorconfig-formatting-conventions#spacing-options
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_after_comma = true
csharp_space_before_comma = false
csharp_space_after_dot = false
csharp_space_before_dot = false
csharp_space_after_semicolon_in_for_statement = true
csharp_space_before_semicolon_in_for_statement = false
csharp_space_around_declaration_statements = false
csharp_space_before_open_square_brackets = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_square_brackets = false

# Wrap options
# https://docs.microsoft.com/visualstudio/ide/editorconfig-formatting-conventions#wrap-options
csharp_preserve_single_line_statements = false
csharp_preserve_single_line_blocks = true

dotnet_sort_system_directives_first = true

csharp_style_namespace_declarations = file_scoped:suggestion
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,6 @@ ModelManifest.xml
.paket/paket.exe

# FAKE - F# Make
.fake/
.fake/

.DS_Store
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix analyzer [RCS1213](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1213) ([PR](https://github.com/dotnet/roslynator/pull/1586))

### Changed

- Update whitespace formatting rules ([PR](https://github.com/dotnet/roslynator/pull/1576))

## [4.12.9] - 2024-10-25

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
switch (diagnostic.Id)
{
case DiagnosticIdentifiers.AddExceptionToDocumentationComment:
{
switch (node.Kind())
{
switch (node.Kind())
case SyntaxKind.ThrowStatement:
{
case SyntaxKind.ThrowStatement:
{
CodeAction codeAction = CodeAction.Create(
"Add exception to documentation comment",
ct => AddExceptionElementToDocumentationCommentRefactoring.RefactorAsync(context.Document, (ThrowStatementSyntax)node, ct),
GetEquivalenceKey(diagnostic));
CodeAction codeAction = CodeAction.Create(
"Add exception to documentation comment",
ct => AddExceptionElementToDocumentationCommentRefactoring.RefactorAsync(context.Document, (ThrowStatementSyntax)node, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
case SyntaxKind.ThrowExpression:
{
CodeAction codeAction = CodeAction.Create(
"Add exception to documentation comment",
ct => AddExceptionElementToDocumentationCommentRefactoring.RefactorAsync(context.Document, (ThrowExpressionSyntax)node, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
context.RegisterCodeFix(codeAction, diagnostic);
break;
}
case SyntaxKind.ThrowExpression:
{
CodeAction codeAction = CodeAction.Create(
"Add exception to documentation comment",
ct => AddExceptionElementToDocumentationCommentRefactoring.RefactorAsync(context.Document, (ThrowExpressionSyntax)node, ct),
GetEquivalenceKey(diagnostic));

break;
context.RegisterCodeFix(codeAction, diagnostic);
break;
}
}

break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,33 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
switch (diagnostic.Id)
{
case DiagnosticIdentifiers.UseUnaryOperatorInsteadOfAssignment:
{
string operatorText = UseUnaryOperatorInsteadOfAssignmentAnalyzer.GetOperatorText(assignment);
{
string operatorText = UseUnaryOperatorInsteadOfAssignmentAnalyzer.GetOperatorText(assignment);

CodeAction codeAction = CodeAction.Create(
$"Use {operatorText} operator",
ct => UseUnaryOperatorInsteadOfAssignmentAsync(document, assignment, ct),
GetEquivalenceKey(diagnostic));
CodeAction codeAction = CodeAction.Create(
$"Use {operatorText} operator",
ct => UseUnaryOperatorInsteadOfAssignmentAsync(document, assignment, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
context.RegisterCodeFix(codeAction, diagnostic);
break;
}
case DiagnosticIdentifiers.RemoveRedundantDelegateCreation:
{
CodeAction codeAction = CodeAction.Create(
"Remove redundant delegate creation",
ct =>
{
return RemoveRedundantDelegateCreationRefactoring.RefactorAsync(
document,
(ObjectCreationExpressionSyntax)assignment.Right,
ct);
},
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
{
CodeAction codeAction = CodeAction.Create(
"Remove redundant delegate creation",
ct =>
{
return RemoveRedundantDelegateCreationRefactoring.RefactorAsync(
document,
(ObjectCreationExpressionSyntax)assignment.Right,
ct);
},
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
}
}
}
Expand Down Expand Up @@ -116,32 +116,32 @@ List<SyntaxTrivia> GetTrailingTrivia()
{
case SyntaxKind.AddAssignmentExpression:
case SyntaxKind.SubtractAssignmentExpression:
{
trivia.AddRange(assignment.OperatorToken.GetAllTrivia());
{
trivia.AddRange(assignment.OperatorToken.GetAllTrivia());

if (right?.IsMissing == false)
trivia.AddRange(right.GetLeadingAndTrailingTrivia());
if (right?.IsMissing == false)
trivia.AddRange(right.GetLeadingAndTrailingTrivia());

return trivia;
}
return trivia;
}
}

switch (right?.Kind())
{
case SyntaxKind.AddExpression:
case SyntaxKind.SubtractExpression:
{
trivia.AddRange(assignment.OperatorToken.GetAllTrivia());

if (right?.IsMissing == false)
{
var binaryExpression = (BinaryExpressionSyntax)right;
{
trivia.AddRange(assignment.OperatorToken.GetAllTrivia());

trivia.AddRange(binaryExpression.DescendantTrivia());
}
if (right?.IsMissing == false)
{
var binaryExpression = (BinaryExpressionSyntax)right;

return trivia;
trivia.AddRange(binaryExpression.DescendantTrivia());
}

return trivia;
}
}

throw new InvalidOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
switch (diagnostic.Id)
{
case DiagnosticIdentifiers.AvoidBoxingOfValueType:
{
CodeAction codeAction = CodeAction.Create(
(expression.IsKind(SyntaxKind.CharacterLiteralExpression))
? "Use string literal instead of character literal"
: "Call 'ToString'",
ct => RefactorAsync(context.Document, expression, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
{
CodeAction codeAction = CodeAction.Create(
(expression.IsKind(SyntaxKind.CharacterLiteralExpression))
? "Use string literal instead of character literal"
: "Call 'ToString'",
ct => RefactorAsync(context.Document, expression, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ private static Task<Document> AddCallToConfigureAwaitRefactorAsync(
switch (expression.Kind())
{
case SyntaxKind.SimpleMemberAccessExpression:
{
var memberAccess = (MemberAccessExpressionSyntax)expression;
{
var memberAccess = (MemberAccessExpressionSyntax)expression;

leading = memberAccess.OperatorToken.LeadingTrivia;
break;
}
leading = memberAccess.OperatorToken.LeadingTrivia;
break;
}
case SyntaxKind.InvocationExpression:
{
var invocation = (InvocationExpressionSyntax)expression;
{
var invocation = (InvocationExpressionSyntax)expression;

SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(invocation);
SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(invocation);

leading = invocationInfo.OperatorToken.LeadingTrivia;
break;
}
leading = invocationInfo.OperatorToken.LeadingTrivia;
break;
}
}

SyntaxTrivia last = leading.LastOrDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
switch (diagnostic.Id)
{
case DiagnosticIdentifiers.OrderNamedArguments:
{
CodeAction codeAction = CodeAction.Create(
"Order arguments",
ct => OrderNamedArgumentsAsync(document, baseArgumentList, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
{
CodeAction codeAction = CodeAction.Create(
"Order arguments",
ct => OrderNamedArgumentsAsync(document, baseArgumentList, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
}
}

Expand Down
Loading