public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out SyntaxNode node, predicate: f => f.IsKind(SyntaxKind.StringLiteralExpression, SyntaxKind.InterpolatedStringExpression))) { return; } switch (node.Kind()) { case SyntaxKind.StringLiteralExpression: { CodeAction codeAction = CodeAction.Create( Title, cancellationToken => UseRegularStringLiteralInsteadOfVerbatimStringLiteralRefactoring.RefactorAsync(context.Document, (LiteralExpressionSyntax)node, cancellationToken), GetEquivalenceKey(DiagnosticIdentifiers.UseRegularStringLiteralInsteadOfVerbatimStringLiteral)); context.RegisterCodeFix(codeAction, context.Diagnostics); break; } case SyntaxKind.InterpolatedStringExpression: { CodeAction codeAction = CodeAction.Create( Title, cancellationToken => UseRegularStringLiteralInsteadOfVerbatimStringLiteralRefactoring.RefactorAsync(context.Document, (InterpolatedStringExpressionSyntax)node, cancellationToken), GetEquivalenceKey(DiagnosticIdentifiers.UseRegularStringLiteralInsteadOfVerbatimStringLiteral)); context.RegisterCodeFix(codeAction, context.Diagnostics); break; } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); SyntaxNode node = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf(SyntaxKind.StringLiteralExpression, SyntaxKind.InterpolatedStringExpression); Debug.Assert(node != null, $"{nameof(node)} is null"); if (node == null) { return; } switch (node.Kind()) { case SyntaxKind.StringLiteralExpression: { CodeAction codeAction = CodeAction.Create( Title, cancellationToken => UseRegularStringLiteralInsteadOfVerbatimStringLiteralRefactoring.RefactorAsync(context.Document, (LiteralExpressionSyntax)node, cancellationToken), DiagnosticIdentifiers.UseRegularStringLiteralInsteadOfVerbatimStringLiteral + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); break; } case SyntaxKind.InterpolatedStringExpression: { CodeAction codeAction = CodeAction.Create( Title, cancellationToken => UseRegularStringLiteralInsteadOfVerbatimStringLiteralRefactoring.RefactorAsync(context.Document, (InterpolatedStringExpressionSyntax)node, cancellationToken), DiagnosticIdentifiers.UseRegularStringLiteralInsteadOfVerbatimStringLiteral + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, context.Diagnostics); break; } } }