Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Services\Precedence\AbstractCSharpPrecedenceService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\Precedence\CSharpPatternPrecedenceService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\Precedence\CSharpExpressionPrecedenceService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\RefactoringHelpers\CSharpRefactoringHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\SelectedMembers\CSharpSelectedMembers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\SemanticFacts\CSharpSemanticFacts.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\SyntaxFacts\CSharpAccessibilityFacts.cs" />
Expand Down
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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Options\OptionKey2.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Options\Option2.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Options\PerLanguageOption2.cs" />
<Compile Include="$(MSBuildThisFileDirectory)services\refactoringhelpers\AbstractRefactoringHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)services\refactoringhelpers\IRefactoringHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\SelectedMembers\AbstractSelectedMembers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\SemanticFacts\ForEachSymbols.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\SemanticFacts\ISemanticFactsExtensions.cs" />
Expand Down Expand Up @@ -548,5 +550,6 @@
<Folder Include="$(MSBuildThisFileDirectory)Extensions\Compiler\" />
<Folder Include="$(MSBuildThisFileDirectory)Hashing\" />
<Folder Include="$(MSBuildThisFileDirectory)Services\FileBannerFacts\" />
<Folder Include="$(MSBuildThisFileDirectory)Services\RefactoringHelpers\" />
</ItemGroup>
</Project>
Loading
Loading