private async Task <Solution> GetUpdatedSolutionAsync( CancellationToken cancellationToken ) { var newDocument = await GetUpdatedDocumentAsync(cancellationToken) .ConfigureAwait(false); var newRoot = await newDocument .GetSyntaxRootAsync(cancellationToken) .ConfigureAwait(false); // Suppress diagnostics on the import we create. Because we only get here when we are // adding a nuget package, it is certainly the case that in the preview this will not // bind properly. It will look silly to show such an error, so we just suppress things. var updatedRoot = newRoot.WithAdditionalAnnotations( SuppressDiagnosticsAnnotation.Create() ); var updatedDocument = newDocument.WithSyntaxRoot(updatedRoot); return(updatedDocument.Project.Solution); }
protected override Task <Document> AddImportAsync( SyntaxNode contextNode, IReadOnlyList <string> namespaceParts, Document document, bool placeSystemNamespaceFirst, CancellationToken cancellationToken) { var root = GetCompilationUnitSyntaxNode(contextNode, cancellationToken); // Suppress diagnostics on the import we create. Because we only get here when we are // adding a nuget package, it is certainly the case that in the preview this will not // bind properly. It will look silly to show such an error, so we just suppress things. var simpleUsingDirective = SyntaxFactory.UsingDirective( CreateNameSyntax(namespaceParts, namespaceParts.Count - 1)).WithAdditionalAnnotations( SuppressDiagnosticsAnnotation.Create()); // If we have an existing using with this name then don't bother adding this new using. if (root.Usings.Any(u => u.IsEquivalentTo(simpleUsingDirective, topLevel: false))) { return(Task.FromResult(document)); } var newRoot = root.AddUsingDirective( simpleUsingDirective, contextNode, placeSystemNamespaceFirst, Formatter.Annotation); return(Task.FromResult(document.WithSyntaxRoot(newRoot))); }