本文整理汇总了C#中Microsoft.CodeAnalysis.CodeFixes.CodeFixContext.RegisterFixes方法的典型用法代码示例。如果您正苦于以下问题:C# CodeFixContext.RegisterFixes方法的具体用法?C# CodeFixContext.RegisterFixes怎么用?C# CodeFixContext.RegisterFixes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CodeFixes.CodeFixContext
的用法示例。
在下文中一共展示了CodeFixContext.RegisterFixes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterCodeFixesAsync
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var cancellationToken = context.CancellationToken;
var uniqueIdentities = await GetUniqueIdentitiesAsync(context).ConfigureAwait(false);
var assemblyNames = uniqueIdentities.Select(i => i.Name).ToSet();
var addPackageCodeActions = await GetAddPackagesCodeActionsAsync(context, assemblyNames).ConfigureAwait(false);
var addReferenceCodeActions = await GetAddReferencesCodeActionsAsync(context, uniqueIdentities).ConfigureAwait(false);
context.RegisterFixes(addPackageCodeActions, context.Diagnostics);
context.RegisterFixes(addReferenceCodeActions, context.Diagnostics);
}
示例2: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var document = context.Document;
var span = context.Span;
var cancellationToken = context.CancellationToken;
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = root.FindToken(span.Start);
if (!token.Span.IntersectsWith(span))
{
return;
}
var service = document.GetLanguageService<IImplementInterfaceService>();
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var actions = token.Parent.GetAncestorsOrThis<TypeSyntax>()
.Where(_interfaceName)
.Select(n => service.GetCodeActions(document, model, n, cancellationToken))
.FirstOrDefault(_codeActionAvailable);
if (_codeActionAvailable(actions))
{
context.RegisterFixes(actions, context.Diagnostics);
}
}
示例3: RegisterCodeFixesAsync
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var cancellationToken = context.CancellationToken;
var assemblyName = GetAssemblyName(context.Diagnostics[0].Id);
if (assemblyName != null)
{
var assemblyNames = new HashSet<string> { assemblyName };
var addPackageCodeActions = await GetAddPackagesCodeActionsAsync(context, assemblyNames).ConfigureAwait(false);
context.RegisterFixes(addPackageCodeActions, context.Diagnostics);
}
}
示例4: 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 model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var diagnostic = diagnostics.First();
var node = root.FindToken(context.Span.Start).Parent as LiteralExpressionSyntax;
if (node == null)
return;
var argumentList = node.Parent.Parent as ArgumentListSyntax;
var objectCreateExpression = argumentList.Parent as ObjectCreationExpressionSyntax;
var validNames = NotResolvedInTextAnalyzer.GetValidParameterNames(objectCreateExpression);
var guessName = NotResolvedInTextAnalyzer.GuessParameterName(model, objectCreateExpression, validNames);
ExpressionSyntax paramNode;
ExpressionSyntax altParamNode;
bool canAddParameterName;
NotResolvedInTextAnalyzer.CheckExceptionType(model, objectCreateExpression, out paramNode, out altParamNode, out canAddParameterName);
if (diagnostic.Id == CSharpDiagnosticIDs.NotResolvedInTextAnalyzer_SwapID)
{
context.RegisterCodeFix(
CodeActionFactory.Create(
node.Span,
diagnostic.Severity,
GettextCatalog.GetString("Swap parameter"),
(token) =>
{
var list = new List<ArgumentSyntax>();
foreach (var arg in argumentList.Arguments)
{
if (arg.Expression == paramNode)
{
list.Add((Microsoft.CodeAnalysis.CSharp.Syntax.ArgumentSyntax)altParamNode.Parent);
continue;
}
if (arg.Expression == altParamNode)
{
list.Add((Microsoft.CodeAnalysis.CSharp.Syntax.ArgumentSyntax)paramNode.Parent);
continue;
}
list.Add(arg);
}
var newRoot = root.ReplaceNode(argumentList, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(list)).WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
),
diagnostic
);
}
else if (diagnostic.Id == CSharpDiagnosticIDs.NotResolvedInTextAnalyzerID)
{
var fixes = new List<CodeAction>();
if (canAddParameterName)
{
fixes.Add(
CodeActionFactory.Create(
node.Span,
diagnostic.Severity,
string.Format(GettextCatalog.GetString("Add '\"{0}\"' parameter."), guessName),
(token) =>
{
var newArgument = SyntaxFactory.ParseExpression('"' + guessName + '"');
var newRoot = root.ReplaceNode(argumentList, argumentList.WithArguments(argumentList.Arguments.Insert(0, SyntaxFactory.Argument(newArgument))).WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
)
);
}
fixes.Add(CodeActionFactory.Create(
node.Span,
diagnostic.Severity,
string.Format(GettextCatalog.GetString("Replace with '\"{0}\"'."), guessName),
(token) =>
{
var newArgument = SyntaxFactory.ParseExpression('"' + guessName + '"');
var newRoot = root.ReplaceNode(node, newArgument.WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()));
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
));
context.RegisterFixes(
fixes,
diagnostic
);
}
}
示例5: RegisterCodeFixesAsync
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var document = context.Document;
if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
return;
var span = context.Span;
var cancellationToken = context.CancellationToken;
if (cancellationToken.IsCancellationRequested)
return;
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (model.IsFromGeneratedCode(cancellationToken))
return;
var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var diagnostic = context.Diagnostics.First();
var syntaxNode = root.FindNode(span);
var node = syntaxNode as MethodDeclarationSyntax;
if (node != null && node.Identifier.Span.Contains(span))
{
context.RegisterFixes(
new[] {
CodeActionFactory.Create (
node.Span,
DiagnosticSeverity.Error,
GettextCatalog.GetString ("This is a constructor"),
t => Task.FromResult (
document.WithSyntaxRoot (
root.ReplaceNode (
(SyntaxNode)node,
node.WithIdentifier (node.AncestorsAndSelf ().OfType<BaseTypeDeclarationSyntax> ().First ().Identifier.WithoutTrivia ()).WithAdditionalAnnotations (Formatter.Annotation)
)
)
)
),
CodeActionFactory.Create (
node.Span,
DiagnosticSeverity.Error,
GettextCatalog.GetString ("This is a void method"),
t => Task.FromResult (
document.WithSyntaxRoot (
root.ReplaceNode (
(SyntaxNode)node,
node.WithReturnType (SyntaxFactory.ParseTypeName ("void")).WithAdditionalAnnotations (Formatter.Annotation)
)
)
)
)
},
diagnostic
);
}
var constructor = syntaxNode as MethodDeclarationSyntax;
if (constructor != null)
{
context.RegisterFixes(
new[] {
CodeActionFactory.Create (
node.Span,
DiagnosticSeverity.Error,
GettextCatalog.GetString ("Fix constructor"),
t => Task.FromResult (
document.WithSyntaxRoot (
root.ReplaceNode (
(SyntaxNode)node,
node.WithIdentifier (node.AncestorsAndSelf ().OfType<BaseTypeDeclarationSyntax> ().First ().Identifier.WithoutTrivia ()).WithAdditionalAnnotations (Formatter.Annotation)
)
)
)
)
},
diagnostic
);
}
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:75,代码来源:CS1520MethodMustHaveAReturnTypeCodeFixProvider.cs