public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var document = context.Document; var span = context.Span; var diagnostics = context.Diagnostics; var cancellationToken = context.CancellationToken; var project = document.Project; var diagnostic = diagnostics.First(); var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var token = root.FindToken(span.Start, findInsideTrivia: true); var ancestors = token.GetAncestors <SyntaxNode>(); if (!ancestors.Any()) { return; } var node = ancestors.FirstOrDefault(n => n.Span.Contains(span) && n != root); if (node == null) { return; } var placeSystemNamespaceFirst = true; if (!cancellationToken.IsCancellationRequested) { if (CanAddImport(node, cancellationToken)) { var typeName = node.ToString(); var projectTargetFrameworks = _targetFrameworkProvider.GetTargetFrameworks(document.FilePath); // Note: allowHigherVersions=false here since we don't want to provide code fix that adds another // dependency for the same package but different version, user should upgrade it on his own when // see Diagnostic suggestion var packagesWithGivenType = TargetFrameworkHelper.GetSupportedPackages(_packageSearcher.Search(typeName), projectTargetFrameworks, allowHigherVersions: false) .Take(MaxPackageSuggestions); foreach (var typeInfo in packagesWithGivenType) { cancellationToken.ThrowIfCancellationRequested(); var namespaceName = typeInfo.FullName.Contains(".") ? Path.GetFileNameWithoutExtension(typeInfo.FullName) : null; if (!string.IsNullOrEmpty(namespaceName)) { var action = new AddPackageCodeAction(_packageInstaller, typeInfo, ActionTitle, (c) => AddImportAsync(node, namespaceName, document, placeSystemNamespaceFirst, cancellationToken)); context.RegisterCodeFix(action, diagnostic); } } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var document = context.Document; var projects = _projectMetadataProvider.GetProjects(document.FilePath); if (projects == null || !projects.Any()) { // project is unsupported return; } var span = context.Span; var diagnostics = context.Diagnostics; var cancellationToken = context.CancellationToken; var project = document.Project; var diagnostic = diagnostics.First(); var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var token = root.FindToken(span.Start, findInsideTrivia: true); var ancestors = token.GetAncestors <SyntaxNode>(); if (!ancestors.Any()) { return; } var node = ancestors.FirstOrDefault(n => n.Span.Contains(span) && n != root); if (node == null) { return; } var placeSystemNamespaceFirst = true; if (!cancellationToken.IsCancellationRequested) { // get distinct frameworks from all projects current file belongs to var suggestions = Analyzer.GetSuggestions(node, projects); foreach (var packageInfo in suggestions.Take(MaxPackageSuggestions)) { cancellationToken.ThrowIfCancellationRequested(); var namespaceName = packageInfo.GetNamespace(); AddPackageCodeAction action = null; if (string.IsNullOrEmpty(namespaceName)) { // namspaces suggestions don't need to add another namespace action = new AddPackageCodeAction(_packageInstaller, packageInfo, projects.Where(x => TargetFrameworkHelper.SupportsProjectTargetFrameworks(packageInfo, x.TargetFrameworks)).ToList(), ActionTitle, (c) => Task.FromResult(document)); } else if (CanAddImport(node, cancellationToken)) { action = new AddPackageCodeAction(_packageInstaller, packageInfo, projects.Where(x => TargetFrameworkHelper.SupportsProjectTargetFrameworks(packageInfo, x.TargetFrameworks)).ToList(), ActionTitle, (c) => AddImportAsync(node, namespaceName, document, placeSystemNamespaceFirst, cancellationToken)); } context.RegisterCodeFix(action, diagnostic); } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var document = context.Document; var projects = _projectMetadataProvider.GetProjects(document.FilePath); if (projects == null || !projects.Any()) { // project is unsupported return; } var span = context.Span; var diagnostics = context.Diagnostics; var cancellationToken = context.CancellationToken; var project = document.Project; var diagnostic = diagnostics.First(); var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var token = root.FindToken(span.Start, findInsideTrivia: true); var ancestors = token.GetAncestors<SyntaxNode>(); if (!ancestors.Any()) { return; } var node = ancestors.FirstOrDefault(n => n.Span.Contains(span) && n != root); if (node == null) { return; } var placeSystemNamespaceFirst = true; if (!cancellationToken.IsCancellationRequested) { // get distinct frameworks from all projects current file belongs to var suggestions = Analyzer.GetSuggestions(node, projects); foreach (var packageInfo in suggestions.Take(MaxPackageSuggestions)) { cancellationToken.ThrowIfCancellationRequested(); var namespaceName = packageInfo.GetNamespace(); AddPackageCodeAction action = null; if (string.IsNullOrEmpty(namespaceName)) { // namspaces suggestions don't need to add another namespace action = new AddPackageCodeAction(_packageInstaller, packageInfo, projects.Where(x => TargetFrameworkHelper.SupportsProjectTargetFrameworks(packageInfo, x.TargetFrameworks)).ToList(), ActionTitle, (c) => Task.FromResult(document)); } else if (CanAddImport(node, cancellationToken)) { action = new AddPackageCodeAction(_packageInstaller, packageInfo, projects.Where(x => TargetFrameworkHelper.SupportsProjectTargetFrameworks(packageInfo, x.TargetFrameworks)).ToList(), ActionTitle, (c) => AddImportAsync(node, namespaceName, document, placeSystemNamespaceFirst, cancellationToken)); } context.RegisterCodeFix(action, diagnostic); } } }