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

Remove ISyntaxFacts from snippet work #72897

Merged
merged 1 commit into from
Apr 6, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override async Task<PropertyDeclarationSyntax> GenerateSnippetSyntaxAs
accessorList: SyntaxFactory.AccessorList([.. accessors.Where(a => a is not null)!]));
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, PropertyDeclarationSyntax propertyDeclaration, SourceText sourceText)
protected override int GetTargetCaretPosition(PropertyDeclarationSyntax propertyDeclaration, SourceText sourceText)
=> propertyDeclaration.AccessorList!.CloseBraceToken.Span.End;

protected override ImmutableArray<SnippetPlaceholder> GetPlaceHolderLocationsList(PropertyDeclarationSyntax propertyDeclaration, ISyntaxFacts syntaxFacts, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected override ImmutableArray<SnippetPlaceholder> GetPlaceHolderLocationsLis
return result.ToImmutableAndClear();
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, ForStatementSyntax forStatement, SourceText sourceText)
protected override int GetTargetCaretPosition(ForStatementSyntax forStatement, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
forStatement,
static s => (BlockSyntax)s.Statement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected override bool IsValidSnippetLocation(in SnippetContext context, Cancel
return new TextChange(TextSpan.FromBounds(targetPosition, targetPosition), SyntaxFacts.GetText(SyntaxKind.PublicKeyword) + " ");
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, TTypeDeclarationSyntax typeDeclaration, SourceText sourceText)
protected override int GetTargetCaretPosition(TTypeDeclarationSyntax typeDeclaration, SourceText sourceText)
{
var triviaSpan = typeDeclaration.CloseBraceToken.LeadingTrivia.Span;
var line = sourceText.Lines.GetLineFromPosition(triviaSpan.Start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ namespace Microsoft.CodeAnalysis.CSharp.Snippets;
[ExportSnippetProvider(nameof(ISnippetProvider), LanguageNames.CSharp), Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpConsoleSnippetProvider() : AbstractConsoleSnippetProvider<ExpressionStatementSyntax>
internal sealed class CSharpConsoleSnippetProvider() : AbstractConsoleSnippetProvider<
ExpressionStatementSyntax,
ExpressionSyntax,
ArgumentListSyntax>
{
protected override ExpressionSyntax GetExpression(ExpressionStatementSyntax expressionStatement)
=> expressionStatement.Expression;

protected override ArgumentListSyntax GetArgumentList(ExpressionSyntax expression)
=> ((InvocationExpressionSyntax)expression).ArgumentList;

protected override SyntaxToken GetOpenParenToken(ArgumentListSyntax argumentList)
=> argumentList.OpenParenToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected override async Task<TextChange> GenerateSnippetTextChangeAsync(Documen
return new TextChange(TextSpan.FromBounds(position, position), constructorDeclaration.NormalizeWhitespace().ToFullString());
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, ConstructorDeclarationSyntax constructorDeclaration, SourceText sourceText)
protected override int GetTargetCaretPosition(ConstructorDeclarationSyntax constructorDeclaration, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
constructorDeclaration,
static d => d.Body!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override DoStatementSyntax GenerateStatement(SyntaxGenerator generator
protected override ExpressionSyntax GetCondition(DoStatementSyntax node)
=> node.Condition;

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, DoStatementSyntax doStatement, SourceText sourceText)
protected override int GetTargetCaretPosition(DoStatementSyntax doStatement, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
doStatement,
static s => (BlockSyntax)s.Statement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected override Task<TextChange> GenerateSnippetTextChangeAsync(Document docu
return Task.FromResult(new TextChange(TextSpan.FromBounds(position, position), elseClause.ToFullString()));
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, ElseClauseSyntax elseClause, SourceText sourceText)
protected override int GetTargetCaretPosition(ElseClauseSyntax elseClause, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
elseClause,
static c => (BlockSyntax)c.Statement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected override ImmutableArray<SnippetPlaceholder> GetPlaceHolderLocationsLis
return arrayBuilder.ToImmutable();
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, ForEachStatementSyntax forEachStatement, SourceText sourceText)
protected override int GetTargetCaretPosition(ForEachStatementSyntax forEachStatement, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
forEachStatement,
static s => (BlockSyntax)s.Statement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal sealed class CSharpIfSnippetProvider() : AbstractIfSnippetProvider<IfSt
protected override ExpressionSyntax GetCondition(IfStatementSyntax node)
=> node.Condition;

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, IfStatementSyntax ifStatement, SourceText sourceText)
protected override int GetTargetCaretPosition(IfStatementSyntax ifStatement, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
ifStatement,
static s => (BlockSyntax)s.Statement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override IEnumerable<StatementSyntax> GenerateInnerStatements(SyntaxGe
return [returnStatement];
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, MethodDeclarationSyntax methodDeclaration, SourceText sourceText)
protected override int GetTargetCaretPosition(MethodDeclarationSyntax methodDeclaration, SourceText sourceText)
{
var body = methodDeclaration.Body!;
var returnStatement = body.Statements.First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override ImmutableArray<SnippetPlaceholder> GetPlaceHolderLocationsLis
return [new SnippetPlaceholder(expression.ToString(), expression.SpanStart)];
}

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, LockStatementSyntax lockStatement, SourceText sourceText)
protected override int GetTargetCaretPosition(LockStatementSyntax lockStatement, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
lockStatement,
static s => (BlockSyntax)s.Statement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override TypeSyntax GenerateReturnType(SyntaxGenerator generator)
protected override IEnumerable<StatementSyntax> GenerateInnerStatements(SyntaxGenerator generator)
=> [];

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, MethodDeclarationSyntax methodDeclaration, SourceText sourceText)
protected override int GetTargetCaretPosition(MethodDeclarationSyntax methodDeclaration, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
methodDeclaration,
static d => d.Body!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal sealed class CSharpWhileLoopSnippetProvider() : AbstractWhileLoopSnippe
protected override ExpressionSyntax GetCondition(WhileStatementSyntax node)
=> node.Condition;

protected override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, WhileStatementSyntax whileStatement, SourceText sourceText)
protected override int GetTargetCaretPosition(WhileStatementSyntax whileStatement, SourceText sourceText)
=> CSharpSnippetHelpers.GetTargetCaretPositionInBlock(
whileStatement,
static s => (BlockSyntax)s.Statement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@

namespace Microsoft.CodeAnalysis.Snippets;

internal abstract class AbstractConsoleSnippetProvider<TExpressionStatementSyntax> : AbstractStatementSnippetProvider<TExpressionStatementSyntax>
internal abstract class AbstractConsoleSnippetProvider<
TExpressionStatementSyntax,
TExpressionSyntax,
TArgumentListSyntax> : AbstractStatementSnippetProvider<TExpressionStatementSyntax>
where TExpressionStatementSyntax : SyntaxNode
where TExpressionSyntax : SyntaxNode
where TArgumentListSyntax : SyntaxNode
{
public sealed override string Identifier => CommonSnippetIdentifiers.ConsoleWriteLine;

public sealed override string Description => FeaturesResources.console_writeline;

public sealed override ImmutableArray<string> AdditionalFilterTexts { get; } = ["WriteLine"];

protected abstract TExpressionSyntax GetExpression(TExpressionStatementSyntax expressionStatement);
protected abstract TArgumentListSyntax GetArgumentList(TExpressionSyntax expression);
protected abstract SyntaxToken GetOpenParenToken(TArgumentListSyntax argumentList);

protected sealed override bool IsValidSnippetLocation(in SnippetContext context, CancellationToken cancellationToken)
{
var consoleSymbol = GetConsoleSymbolFromMetaDataName(context.SyntaxContext.SemanticModel.Compilation);
Expand All @@ -51,17 +60,17 @@ protected sealed override Task<TextChange> GenerateSnippetTextChangeAsync(Docume
/// Tries to get the location after the open parentheses in the argument list.
/// If it can't, then we default to the end of the snippet's span.
/// </summary>
protected sealed override int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, TExpressionStatementSyntax caretTarget, SourceText sourceText)
protected sealed override int GetTargetCaretPosition(TExpressionStatementSyntax caretTarget, SourceText sourceText)
{
var invocationExpression = caretTarget.DescendantNodes().Where(syntaxFacts.IsInvocationExpression).FirstOrDefault();
var invocationExpression = GetExpression(caretTarget);
if (invocationExpression is null)
return caretTarget.Span.End;

var argumentListNode = syntaxFacts.GetArgumentListOfInvocationExpression(invocationExpression);
var argumentListNode = GetArgumentList(invocationExpression);
if (argumentListNode is null)
return caretTarget.Span.End;

syntaxFacts.GetPartsOfArgumentList(argumentListNode, out var openParenToken, out _, out _);
var openParenToken = GetOpenParenToken(argumentListNode);
return openParenToken.Span.End;
}

Expand All @@ -81,25 +90,6 @@ protected sealed override async Task<SyntaxNode> AnnotateNodesToReformatAsync(
protected sealed override ImmutableArray<SnippetPlaceholder> GetPlaceHolderLocationsList(TExpressionStatementSyntax node, ISyntaxFacts syntaxFacts, CancellationToken cancellationToken)
=> [];

private static SyntaxToken? GetOpenParenToken(SyntaxNode node, ISyntaxFacts syntaxFacts)
{
var invocationExpression = node.DescendantNodes().Where(syntaxFacts.IsInvocationExpression).FirstOrDefault();
if (invocationExpression is null)
{
return null;
}

var argumentListNode = syntaxFacts.GetArgumentListOfInvocationExpression(invocationExpression);
if (argumentListNode is null)
{
return null;
}

syntaxFacts.GetPartsOfArgumentList(argumentListNode, out var openParenToken, out _, out _);

return openParenToken;
}

private static INamedTypeSymbol? GetConsoleSymbolFromMetaDataName(Compilation compilation)
=> compilation.GetBestTypeByMetadataName(typeof(Console).FullName!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal abstract class AbstractSnippetProvider<TSnippetSyntax> : ISnippetProvid
/// <summary>
/// Gets the position that we want the caret to be at after all of the indentation/formatting has been done.
/// </summary>
protected abstract int GetTargetCaretPosition(ISyntaxFactsService syntaxFacts, TSnippetSyntax caretTarget, SourceText sourceText);
protected abstract int GetTargetCaretPosition(TSnippetSyntax caretTarget, SourceText sourceText);

/// <summary>
/// Method to find the locations that must be renamed and where tab stops must be inserted into the snippet.
Expand Down Expand Up @@ -119,7 +119,7 @@ public async Task<SnippetChange> GetSnippetAsync(Document document, int position

return new SnippetChange(
textChanges: changesArray,
cursorPosition: GetTargetCaretPosition(syntaxFacts, mainChangeNode, sourceText),
cursorPosition: GetTargetCaretPosition(mainChangeNode, sourceText),
placeholders: placeholders);
}

Expand Down
Loading