-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Breaking out refactoring helpers into core api vs service #79012
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
CyrusNajmabadi
merged 10 commits into
dotnet:main
from
CyrusNajmabadi:refactoringHelpers
Jun 17, 2025
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
efe88dd
Breaking out refactoring helpers into core api vs service
CyrusNajmabadi 3edd6ce
VB side
CyrusNajmabadi 9a26edc
Move type
CyrusNajmabadi bbe7943
Update src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CS…
CyrusNajmabadi 5341b77
workaround
CyrusNajmabadi d6b73e9
Merge branch 'refactoringHelpers' of https://github.com/CyrusNajmabad…
CyrusNajmabadi ada3486
Merge remote-tracking branch 'upstream/main' into refactoringHelpers
CyrusNajmabadi 177a46c
Update src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CS…
CyrusNajmabadi bc1e751
Update src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Comp…
CyrusNajmabadi 91ff4b4
Update src/Workspaces/SharedUtilitiesAndExtensions/Compiler/VisualBas…
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 hidden or 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
113 changes: 113 additions & 0 deletions
113
...tiesAndExtensions/Compiler/CSharp/Services/RefactoringHelpers/CSharpRefactoringHelpers.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis.CodeRefactorings; | ||
| using Microsoft.CodeAnalysis.CSharp.Extensions; | ||
| using Microsoft.CodeAnalysis.CSharp.LanguageService; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.LanguageService; | ||
| using Microsoft.CodeAnalysis.Shared.Extensions; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using Roslyn.Utilities; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.CodeRefactorings; | ||
|
|
||
| internal sealed class CSharpRefactoringHelpers : AbstractRefactoringHelpers<ExpressionSyntax, ArgumentSyntax, ExpressionStatementSyntax> | ||
| { | ||
| public static readonly CSharpRefactoringHelpers Instance = new(); | ||
|
|
||
| private CSharpRefactoringHelpers() | ||
| { | ||
| } | ||
|
|
||
| protected override IHeaderFacts HeaderFacts => CSharpHeaderFacts.Instance; | ||
| protected override ISyntaxFacts SyntaxFacts => CSharpSyntaxFacts.Instance; | ||
|
|
||
| public override bool IsBetweenTypeMembers(SourceText sourceText, SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? typeDeclaration) | ||
|
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. all teh logic below here is a move. |
||
| { | ||
| var token = root.FindToken(position); | ||
| var typeDecl = token.GetAncestor<TypeDeclarationSyntax>(); | ||
| typeDeclaration = typeDecl; | ||
|
|
||
| if (typeDecl == null) | ||
| return false; | ||
|
|
||
| RoslynDebug.AssertNotNull(typeDeclaration); | ||
| if (position < typeDecl.OpenBraceToken.Span.End || | ||
| position > typeDecl.CloseBraceToken.Span.Start) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var line = sourceText.Lines.GetLineFromPosition(position); | ||
| if (!line.IsEmptyOrWhitespace()) | ||
| return false; | ||
|
|
||
| var member = typeDecl.Members.FirstOrDefault(d => d.FullSpan.Contains(position)); | ||
| if (member == null) | ||
| { | ||
| // There are no members, or we're after the last member. | ||
| return true; | ||
| } | ||
| else | ||
| { | ||
| // We're within a member. Make sure we're in the leading whitespace of | ||
| // the member. | ||
| if (position < member.SpanStart) | ||
| { | ||
| foreach (var trivia in member.GetLeadingTrivia()) | ||
| { | ||
| if (!trivia.IsWhitespaceOrEndOfLine()) | ||
| return false; | ||
|
|
||
| if (trivia.FullSpan.Contains(position)) | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| protected override IEnumerable<SyntaxNode> ExtractNodesSimple(SyntaxNode? node, ISyntaxFacts syntaxFacts) | ||
| { | ||
| if (node == null) | ||
| { | ||
| yield break; | ||
| } | ||
|
|
||
| foreach (var extractedNode in base.ExtractNodesSimple(node, syntaxFacts)) | ||
| { | ||
| yield return extractedNode; | ||
| } | ||
|
|
||
| // `var a = b;` | ||
| // -> `var a = b`; | ||
| if (node is LocalDeclarationStatementSyntax localDeclaration) | ||
| { | ||
| yield return localDeclaration.Declaration; | ||
| } | ||
|
|
||
| // var `a = b`; | ||
| if (node is VariableDeclaratorSyntax declarator) | ||
| { | ||
| var declaration = declarator.Parent; | ||
| if (declaration?.Parent is LocalDeclarationStatementSyntax localDeclarationStatement) | ||
| { | ||
| var variables = syntaxFacts.GetVariablesOfLocalDeclarationStatement(localDeclarationStatement); | ||
| if (variables.Count == 1) | ||
| { | ||
| // -> `var a = b`; | ||
| yield return declaration; | ||
|
|
||
| // -> `var a = b;` | ||
| yield return localDeclarationStatement; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.