本文整理汇总了C#中Microsoft.CodeAnalysis.CodeFixes.CodeFixContext.RegisterFix方法的典型用法代码示例。如果您正苦于以下问题:C# CodeFixContext.RegisterFix方法的具体用法?C# CodeFixContext.RegisterFix怎么用?C# CodeFixContext.RegisterFix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CodeFixes.CodeFixContext
的用法示例。
在下文中一共展示了CodeFixContext.RegisterFix方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ComputeFixesAsync
public sealed override async Task ComputeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var token = root.FindToken(diagnosticSpan.Start);
if (token.IsKind(SyntaxKind.CatchKeyword))
{
var catchBlock = (CatchClauseSyntax)token.Parent;
var tryStmt = (TryStatementSyntax)catchBlock.Parent;
var throwStatement = SyntaxFactory.ThrowStatement();
var newStatements = new SyntaxList<StatementSyntax>().Add(throwStatement);
var newBlock = SyntaxFactory.Block().WithStatements(newStatements);
var newCatchBlock = SyntaxFactory.CatchClause().WithBlock(newBlock).WithAdditionalAnnotations(Formatter.Annotation);
var newRoot = root.ReplaceNode(catchBlock, newCatchBlock);
context.RegisterFix(
CodeAction.Create("throw", context.Document.WithSyntaxRoot(newRoot)),
diagnostic);
}
}
示例2: ComputeFixesAsync
public override async Task ComputeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var declaration = root.FindToken(diagnosticSpan.Start).Parent
.FirstAncestorOrSelf<AwaitExpressionSyntax>();
context.RegisterFix(
CodeAction.Create("Add ConfigureAwait(false)", cancellationToken =>
AddConfigureAwait(context.Document, declaration, false, cancellationToken)),
diagnostic);
context.RegisterFix(
CodeAction.Create("Add ConfigureAwait(true)", cancellationToken =>
AddConfigureAwait(context.Document, declaration, true, cancellationToken)),
diagnostic);
}
示例3: ComputeFixesAsync
public sealed override async Task ComputeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<LocalDeclarationStatementSyntax>().First();
context.RegisterFix(CodeAction.Create("Make constant", c => MakeConstAsync(context.Document, declaration, c)), diagnostic);
}
示例4: ComputeFixesAsync
public sealed override async Task ComputeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var declaration = root.FindToken(diagnosticSpan.Start).Parent;
// Register a code action that will invoke the fix.
context.RegisterFix(
CodeAction.Create("Make lowercase", c => MakeLowercaseAsync(context.Document, declaration, c)),
diagnostic);
}
示例5: ComputeFixesAsync
public sealed override async Task ComputeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
// TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().First();
// Register a code action that will invoke the fix.
context.RegisterFix(
CodeAction.Create("Make uppercase", c => MakeUppercaseAsync(context.Document, declaration, c)),
diagnostic);
}
示例6: ComputeFixesAsync
public sealed override async Task ComputeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ConstructorDeclarationSyntax>().First();
var previousWhiteSpacesToken = SyntaxFactory.Token(declaration.GetLeadingTrivia(), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty);
var newList = SyntaxTokenList.Create(previousWhiteSpacesToken);
var newDeclaration = declaration.WithModifiers(newList).
AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword)).
AddModifiers(SyntaxFactory.Token(SyntaxTriviaList.Create(SyntaxFactory.Space), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty));
// Register a code action that will invoke the fix.
context.RegisterFix(
CodeAction.Create("Make constructor protected", c => ReplaceConstructorInDocumentAsync(context.Document, declaration, newDeclaration, c)),
diagnostic);
}