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); }
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); } }
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); } }
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 ); } }
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 ); } }