本文整理汇总了C#中Microsoft.CodeAnalysis.CodeFixes.CodeFixContext.RegisterCodeFix方法的典型用法代码示例。如果您正苦于以下问题:C# CodeFixContext.RegisterCodeFix方法的具体用法?C# CodeFixContext.RegisterCodeFix怎么用?C# CodeFixContext.RegisterCodeFix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CodeFixes.CodeFixContext
的用法示例。
在下文中一共展示了CodeFixContext.RegisterCodeFix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterCodeFixesAsync
public async override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var document = context.Document;
var cancellationToken = context.CancellationToken;
var span = context.Span;
var diagnostics = context.Diagnostics;
var root = await document.GetSyntaxRootAsync(cancellationToken);
var diagnostic = diagnostics.First();
var node = root.FindNode(context.Span);
if (node is BinaryExpressionSyntax)
{
var newRoot = root.ReplaceNode(node, SyntaxFactory.LiteralExpression(node.IsKind(SyntaxKind.EqualsExpression) ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression));
context.RegisterCodeFix(CodeActionFactory.Create(node.Span, diagnostic.Severity, diagnostic.GetMessage(), document.WithSyntaxRoot(newRoot)), diagnostic);
}
else if (node is InvocationExpressionSyntax)
{
var newRoot = root.ReplaceNode(node, SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression));
context.RegisterCodeFix(CodeActionFactory.Create(node.Span, diagnostic.Severity, diagnostic.GetMessage(), document.WithSyntaxRoot(newRoot)), diagnostic);
}
else if (node is PrefixUnaryExpressionSyntax)
{
var newRoot = root.ReplaceNode(node, SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression));
context.RegisterCodeFix(CodeActionFactory.Create(node.Span, diagnostic.Severity, diagnostic.GetMessage(), document.WithSyntaxRoot(newRoot)), diagnostic);
}
}
示例2: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(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 baseTypeDeclaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<SimpleBaseTypeSyntax>().FirstOrDefault();
if (baseTypeDeclaration != null)
{
context.RegisterCodeFix(
CodeAction.Create(
title: title,
createChangedDocument: c => GenerateInterfaceImplementationAsync(context.Document, baseTypeDeclaration, c),
equivalenceKey: title),
diagnostic);
return;
}
var methodDeclaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().FirstOrDefault();
if (methodDeclaration != null)
{
context.RegisterCodeFix(
CodeAction.Create(
title: title,
createChangedDocument: c => GenerateMethodImplementationAsync(context.Document, methodDeclaration, c),
equivalenceKey: title),
diagnostic);
return;
}
}
示例3: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var refactorType = diagnostic.Properties[ForeachToLinqAnalyzer.RefactorType];
var foreachStatement = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ForEachStatementSyntax>().First();
if (refactorType == ForeachToLinqAnalyzer.ContainingIfToWhere)
{
var ifStatement = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>().First();
context.RegisterCodeFix(
CodeAction.Create("Convert if statement to LINQ method chain",
c => ConvertIfToLINQ(context.Document, ifStatement, foreachStatement, c)),
diagnostic);
}
else if (refactorType == ForeachToLinqAnalyzer.IfWithContinueToWhere)
{
var ifStatement = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>().First();
context.RegisterCodeFix(
CodeAction.Create("Convert if statement to LINQ method chain",
c => ConvertIfContinueToLINQ(context.Document, ifStatement, foreachStatement, c)),
diagnostic);
}
else if (refactorType == ForeachToLinqAnalyzer.VariableToSelect)
{
var variableDeclarator = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<VariableDeclaratorSyntax>().First();
context.RegisterCodeFix(
CodeAction.Create("Convert variable assignment to LINQ method chain",
c => ConvertVariableAssignmentToLINQ(context.Document, variableDeclarator, foreachStatement, c)),
diagnostic);
}
}
示例4: RegisterCodeFixesAsync
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span);
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var symbol = model.GetDeclaredSymbol(node, context.CancellationToken);
if (symbol == null)
{
return;
}
var diagnostic = context.Diagnostics.Single();
// There was no constructor and so the diagnostic was on the type. Generate a serialization ctor.
if (symbol.Kind == SymbolKind.NamedType)
{
context.RegisterCodeFix(new MyCodeAction(AnalyzerPowerPackFixersResources.ImplementSerializationConstructor,
async ct => await GenerateConstructor(context.Document, node, symbol, ct).ConfigureAwait(false)),
diagnostic);
}
// There is a serialization constructor but with incorrect accessibility. Set that right.
else if (symbol.Kind == SymbolKind.Method)
{
context.RegisterCodeFix(new MyCodeAction(AnalyzerPowerPackFixersResources.ImplementSerializationConstructor,
async ct => await SetAccessibility(context.Document, node, symbol, ct).ConfigureAwait(false)),
diagnostic);
}
return;
}
示例5: RegisterCodeFixesAsync
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
context.RegisterCodeFix(CodeAction.Create("Rethrow as inner exception", c => MakeThrowAsInnerAsync(context.Document, diagnostic, c)), diagnostic);
context.RegisterCodeFix(CodeAction.Create("Throw original exception", c => MakeThrowAsync(context.Document, diagnostic, c)), diagnostic);
return Task.FromResult(0);
}
示例6: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(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<ClassDeclarationSyntax>().First();
context.RegisterCodeFix(
CodeAction.Create(
title: publicTitle,
createChangedDocument: c => AddModifier(context.Document, declaration, SyntaxKind.PublicKeyword, c),
equivalenceKey: publicTitle),
diagnostic);
context.RegisterCodeFix(
CodeAction.Create(
title: internalTitle,
createChangedDocument: c => AddModifier(context.Document, declaration, SyntaxKind.InternalKeyword, c),
equivalenceKey: internalTitle),
diagnostic);
context.RegisterCodeFix(
CodeAction.Create(
title: privateTitle,
createChangedDocument: c => AddModifier(context.Document, declaration, SyntaxKind.PrivateKeyword, c),
equivalenceKey: privateTitle),
diagnostic);
}
示例7: RegisterCodeFixesAsync
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span);
var fieldNode = GetFieldDeclarationNode(node);
if(fieldNode == null)
{
return;
}
var diagnostic = context.Diagnostics.Single();
// Fix 1: Add a NonSerialized attribute to the field
context.RegisterCodeFix(new MyCodeAction(AnalyzerPowerPackFixersResources.AddNonSerializedAttribute,
async ct => await AddNonSerializedAttribute(context.Document, fieldNode, ct).ConfigureAwait(false)),
diagnostic);
// Fix 2: If the type of the field is defined in source, then add the serializable attribute to the type.
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var fieldSymbol = model.GetDeclaredSymbol(node, context.CancellationToken) as IFieldSymbol;
var type = fieldSymbol?.Type;
if (type != null && type.Locations.Any(l => l.IsInSource))
{
context.RegisterCodeFix(new MyCodeAction(AnalyzerPowerPackFixersResources.AddSerializableAttribute,
async ct => await AddSerializableAttributeToType(context.Document, type, ct).ConfigureAwait(false)),
diagnostic);
}
}
示例8: RegisterCodeFixesAsync
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var node = root.FindNode(context.Span);
if (node == null)
{
return;
}
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var charSetType = model.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.CharSet");
var dllImportType = model.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.DllImportAttribute");
var marshalAsType = model.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.MarshalAsAttribute");
var unmanagedType = model.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.UnmanagedType");
if (charSetType == null || dllImportType == null || marshalAsType == null || unmanagedType == null)
{
return;
}
// We cannot have multiple overlapping diagnostics of this id.
var diagnostic = context.Diagnostics.Single();
if (IsAttribute(node))
{
context.RegisterCodeFix(new MyCodeAction(SystemRuntimeInteropServicesAnalyzersResources.SpecifyMarshalingForPInvokeStringArgumentsTitle,
async ct => await FixAttributeArguments(context.Document, node, charSetType, dllImportType, marshalAsType, unmanagedType, ct).ConfigureAwait(false)),
diagnostic);
}
else if (IsDeclareStatement(node))
{
context.RegisterCodeFix(new MyCodeAction(SystemRuntimeInteropServicesAnalyzersResources.SpecifyMarshalingForPInvokeStringArgumentsTitle,
async ct => await FixDeclareStatement(context.Document, node, ct).ConfigureAwait(false)),
diagnostic);
}
}
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:35,代码来源:SpecifyMarshalingForPInvokeStringArguments.Fixer.cs
示例9: RegisterCodeFixesAsync
/// <inheritdoc/>
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
foreach (var diagnostic in context.Diagnostics)
{
var queryExpression = (QueryExpressionSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan).Parent;
if (queryExpression.DescendantTrivia().All(AcceptableSingleLineTrivia))
{
context.RegisterCodeFix(
CodeAction.Create(
ReadabilityResources.SA1103CodeFixSingleLine,
cancellationToken => GetTransformedDocumentFromSingleLineAsync(context.Document, diagnostic, cancellationToken),
nameof(SA1103CodeFixProvider) + "Single"),
diagnostic);
}
context.RegisterCodeFix(
CodeAction.Create(
ReadabilityResources.SA1103CodeFixMultipleLines,
cancellationToken => GetTransformedDocumentForMultipleLinesAsync(context.Document, diagnostic, cancellationToken),
nameof(SA1103CodeFixProvider) + "Multiple"),
diagnostic);
}
}
示例10: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(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<MethodDeclarationSyntax>().First();
var title = "Return void";
context.RegisterCodeFix(
CodeAction.Create(
title
, c => ReturnVoidAsync(context.Document, declaration, c)
, title)
, diagnostic);
var title2 = "Return self";
context.RegisterCodeFix(
CodeAction.Create(
title2
, c => ReturnSelfAsync(context.Document, declaration, c)
, title2)
, diagnostic);
}
开发者ID:marcoparenzan,项目名称:CSharpDay2015,代码行数:26,代码来源:AggregateMethodMustReturnVoidOrSelfCodeFixProvider.cs
示例11: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) as CompilationUnitSyntax;
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var classDeclaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().First();
var missingInterfaces = classDeclaration.GetMissingInterfaces();
if (missingInterfaces.Any())
{
var codeAction = CodeAction.Create(
"Add Missing Interfaces",
_ => AddInterface(context.Document, root, classDeclaration, missingInterfaces),
$"{ExportAnalyzer.DiagnosticId} Add Missing Interfaces");
context.RegisterCodeFix(codeAction, diagnostic);
}
var missingExports = classDeclaration.GetMissingExports();
if (missingExports.Any())
{
var codeAction = CodeAction.Create(
"Add Missing Exports",
_ => AddExport(context.Document, root, classDeclaration, missingExports),
$"{ExportAnalyzer.DiagnosticId} Add Missing Exports");
context.RegisterCodeFix(codeAction, diagnostic);
}
}
示例12: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var methodSyntax = root.FindNode(diagnosticSpan) as MethodDeclarationSyntax;
var methodSymbol = model.GetDeclaredSymbol(methodSyntax);
var xml = methodSymbol.GetDocumentationCommentXml();
//<!-- Badly formed XML comment ignored for member "M:XX(YY)" -->
if (xml.StartsWith("<!--"))
{
context.RegisterCodeFix(
CodeAction.Create(Resources.RepairXmlCommentTitle, c => RepairXmlCommentAsync(context.Document, methodSyntax, c), equivalenceKey: Resources.RepairXmlCommentTitle),
diagnostic);
}
else
{
context.RegisterCodeFix(
CodeAction.Create(Resources.CodeFixTitle, c => UpdateDocumentCommentAsync(context.Document, methodSyntax, c), equivalenceKey: Resources.CodeFixTitle),
diagnostic);
}
}
开发者ID:miya2000,项目名称:DocumentationCommentAnalyzer,代码行数:27,代码来源:DocumentationCommentAnalyzerCodeFixProvider.cs
示例13: RegisterCodeFixesAsync
public async override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var document = context.Document;
var cancellationToken = context.CancellationToken;
var span = context.Span;
var diagnostics = context.Diagnostics;
var root = await document.GetSyntaxRootAsync(cancellationToken);
var diagnostic = diagnostics.First();
var constructor = root.FindToken(span.Start).Parent.AncestorsAndSelf().OfType<ConstructorDeclarationSyntax>().First();
context.RegisterCodeFix(CodeActionFactory.Create(span, diagnostic.Severity, "Make constructor protected", delegate
{
var publicToken = constructor.Modifiers.First(m => m.IsKind(SyntaxKind.PublicKeyword));
var newConstructor = constructor.WithModifiers(constructor.Modifiers.Replace(publicToken, SyntaxFactory.Token(publicToken.LeadingTrivia, SyntaxKind.ProtectedKeyword,
publicToken.TrailingTrivia)));
var newRoot = root.ReplaceNode(constructor, newConstructor);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}), diagnostic);
context.RegisterCodeFix(CodeActionFactory.Create(span, diagnostic.Severity, "Make constructor private", delegate
{
var publicToken = constructor.Modifiers.First(m => m.IsKind(SyntaxKind.PublicKeyword));
var newConstructor = constructor.WithModifiers(constructor.Modifiers.Remove(publicToken));
var newRoot = root.ReplaceNode(constructor, newConstructor);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}), diagnostic);
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:25,代码来源:PublicConstructorInAbstractClassCodeFixProvider.cs
示例14: RegisterCodeFixesAsync
public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
context.RegisterCodeFix(CodeAction.Create("Change property to 'private set'", c => ChangePropertySetAsync(context.Document, diagnostic, c, FixType.PrivateFix), nameof(PropertyPrivateSetCodeFixProvider) + nameof(FixType.PrivateFix)), diagnostic);
context.RegisterCodeFix(CodeAction.Create("Change property to 'protected set'", c => ChangePropertySetAsync(context.Document, diagnostic, c, FixType.ProtectedFix), nameof(PropertyPrivateSetCodeFixProvider) + nameof(FixType.ProtectedFix)), diagnostic);
return Task.FromResult(0);
}
示例15: RegisterCodeFixesAsync
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
SyntaxNode node = root.FindNode(context.Span);
SemanticModel model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
ISymbol symbol = model.GetDeclaredSymbol(node, context.CancellationToken);
if (symbol == null)
{
return;
}
INamedTypeSymbol notImplementedExceptionType = WellKnownTypes.NotImplementedException(model.Compilation);
if (notImplementedExceptionType == null)
{
return;
}
Diagnostic diagnostic = context.Diagnostics.Single();
// There was no constructor and so the diagnostic was on the type. Generate a serialization ctor.
if (symbol.Kind == SymbolKind.NamedType)
{
context.RegisterCodeFix(new MyCodeAction(DesktopAnalyzersResources.ImplementSerializationConstructorsCodeActionTitle,
async ct => await GenerateConstructor(context.Document, node, symbol, notImplementedExceptionType, ct).ConfigureAwait(false)),
diagnostic);
}
// There is a serialization constructor but with incorrect accessibility. Set that right.
else if (symbol.Kind == SymbolKind.Method)
{
context.RegisterCodeFix(new MyCodeAction(DesktopAnalyzersResources.ImplementSerializationConstructorsCodeActionTitle,
async ct => await SetAccessibility(context.Document, symbol, ct).ConfigureAwait(false)),
diagnostic);
}
}