-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 analyzer/fixer to suggest changing code fluent collection building to a collection expression #69580
Merged
CyrusNajmabadi
merged 48 commits into
dotnet:main
from
CyrusNajmabadi:useCollectionExpressionForFluent
Aug 22, 2023
Merged
Add analyzer/fixer to suggest changing code fluent collection building to a collection expression #69580
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
d2f5718
in progress
CyrusNajmabadi ca406b0
progress
CyrusNajmabadi fd53717
Add helpers
CyrusNajmabadi e88364c
Move helper
CyrusNajmabadi 2925c68
Move helper
CyrusNajmabadi 299fa8a
Move helper
CyrusNajmabadi 412237f
Move helper
CyrusNajmabadi 2db9289
Flesh out
CyrusNajmabadi c239c5c
Fix check
CyrusNajmabadi 650ec70
initial fixer
CyrusNajmabadi ef593d5
Share more
CyrusNajmabadi 787cf94
in progress
CyrusNajmabadi 8de08c4
Merge
CyrusNajmabadi 10817e0
Merge branch 'main' into useCollectionExpressionForFluent
CyrusNajmabadi 9c7efe3
In progress
CyrusNajmabadi 1caeb43
Use helpers
CyrusNajmabadi 4663a33
Store more info
CyrusNajmabadi 1f326c3
in progress
CyrusNajmabadi f8fad50
Move to arguments
CyrusNajmabadi f8ccc00
Add fixer
CyrusNajmabadi dd93254
Move + tests
CyrusNajmabadi d81e539
Add comma
CyrusNajmabadi 995ffbf
Add tests
CyrusNajmabadi fab9144
Add tests
CyrusNajmabadi 31461d2
Add tests
CyrusNajmabadi a16ebf9
Tests
CyrusNajmabadi 1b30473
Almost
CyrusNajmabadi 2ce1641
Improve placement
CyrusNajmabadi 6a5676e
Better formatting
CyrusNajmabadi b26a3b6
Add tests
CyrusNajmabadi 1b9c9af
More tests
CyrusNajmabadi 5f6cc10
More tests
CyrusNajmabadi 2c2feed
Merge branch 'useCollectionExpressionExtracts' into useCollectionExpr…
CyrusNajmabadi 44a3b9f
Add tests
CyrusNajmabadi f6af9f9
Fix formatting
CyrusNajmabadi cb211c2
Add Append support
CyrusNajmabadi 1ed0304
Fix
CyrusNajmabadi a52bbb3
Merge remote-tracking branch 'upstream/main' into useCollectionExpres…
CyrusNajmabadi f6669fe
Apply suggestions from code review
CyrusNajmabadi d1078a3
Add docs
CyrusNajmabadi 089e09f
Add docs
CyrusNajmabadi 83fdc23
Merge branch 'useCollectionExpressionForFluent' of https://github.com…
CyrusNajmabadi 6d72b64
Add tests
CyrusNajmabadi 5fdd2ac
tests
CyrusNajmabadi 555bbcc
Merge remote-tracking branch 'upstream/main' into useCollectionExpres…
CyrusNajmabadi c985341
Update src/Features/RulesMissingDocumentation.md
CyrusNajmabadi 4c3198a
Filter out builders
CyrusNajmabadi 3463acb
Limit cases
CyrusNajmabadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
394 changes: 394 additions & 0 deletions
394
...yzers/UseCollectionExpression/CSharpUseCollectionExpressionForFluentDiagnosticAnalyzer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CSharp.Extensions; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.ForEachCast; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Microsoft.CodeAnalysis.Indentation; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
|
@@ -433,22 +432,7 @@ ExpressionSyntax IndentExpression( | |
if (preferredIndentation is null) | ||
return expression.WithoutLeadingTrivia(); | ||
|
||
// we're starting with something either like: | ||
// | ||
// collection.Add(some_expr + | ||
// cont); | ||
// | ||
// or | ||
// | ||
// collection.Add( | ||
// some_expr + | ||
// cont); | ||
// | ||
// In the first, we want to consider the `some_expr + cont` to actually start where `collection` starts so | ||
// that we can accurately determine where the preferred indentation should move all of it. | ||
|
||
var startLine = document.Text.Lines.GetLineFromPosition(expression.SpanStart); | ||
|
||
var startLine = document.Text.Lines.GetLineFromPosition(GetAnchorNode(expression).SpanStart); | ||
var firstTokenOnLineIndentationString = GetIndentationStringForToken(document.Root.FindToken(startLine.Start)); | ||
|
||
var expressionFirstToken = expression.GetFirstToken(); | ||
|
@@ -465,6 +449,38 @@ ExpressionSyntax IndentExpression( | |
|
||
// Now, once we've indented the expression, attempt to move comments on its containing statement to it. | ||
return TransferParentStatementComments(parentStatement, updatedExpression, preferredIndentation); | ||
|
||
SyntaxNode GetAnchorNode(SyntaxNode node) | ||
{ | ||
// we're starting with something either like: | ||
// | ||
// collection.Add(some_expr + | ||
// cont); | ||
// | ||
// or | ||
// | ||
// collection.Add( | ||
// some_expr + | ||
// cont); | ||
// | ||
// In the first, we want to consider the `some_expr + cont` to actually start where `collection` starts so | ||
// that we can accurately determine where the preferred indentation should move all of it. | ||
|
||
// If the expression is parented by a statement or member-decl (like a field/prop), use that container | ||
// to determine the indentation point. Otherwise, default to the indentation of the line the expression | ||
// is on. | ||
var firstToken = node.GetFirstToken(); | ||
if (document.Text.AreOnSameLine(firstToken.GetPreviousToken(), firstToken)) | ||
{ | ||
for (var current = node; current != null; current = current.Parent) | ||
{ | ||
if (current is StatementSyntax or MemberDeclarationSyntax) | ||
return current; | ||
} | ||
} | ||
|
||
return node; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this improved indentation tracking in a few more situations. |
||
} | ||
|
||
SyntaxToken IndentToken( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cston this appears to be an error. Once this merged, i'll open a bug on this.