提交 c0363c56 编写于 作者: Y yair halberstadt

fix nullable warnings

上级 c5c9ce04
......@@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editing
{
......@@ -52,6 +53,7 @@ public static Task<Document> AddImportsAsync(Document document, IEnumerable<Text
internal static async Task<Document> AddImportsFromSyntaxesAsync(Document document, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSyntaxesAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false);
}
......@@ -69,6 +71,7 @@ internal static Task<Document> AddImportsFromSyntaxesAsync(Document document, Te
internal static async Task<Document> AddImportsFromSyntaxesAsync(Document document, SyntaxAnnotation annotation, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSyntaxesAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), safe, options, cancellationToken).ConfigureAwait(false);
}
......@@ -94,6 +97,7 @@ internal static Task<Document> AddImportsFromSyntaxesAsync(Document document, IE
internal static async Task<Document> AddImportsFromSymbolAnnotationAsync(Document document, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSymbolAnnotationAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false);
}
......@@ -111,6 +115,7 @@ internal static Task<Document> AddImportsFromSymbolAnnotationAsync(Document docu
internal static async Task<Document> AddImportsFromSymbolAnnotationAsync(Document document, SyntaxAnnotation annotation, bool safe = true, OptionSet? options = null, CancellationToken cancellationToken = default)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSymbolAnnotationAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), safe, options, cancellationToken).ConfigureAwait(false);
}
......
......@@ -40,6 +40,7 @@ public enum Strategy
options ??= await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(model);
var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var addImportsService = document.GetLanguageService<IAddImportsService>();
var generator = SyntaxGenerator.GetGenerator(document);
......@@ -68,14 +69,18 @@ public enum Strategy
// This will allow us to find it after we have called MakeSafeToAddNamespaces.
var annotation = new SyntaxAnnotation();
document = document.WithSyntaxRoot(root.ReplaceNode(context, context.WithAdditionalAnnotations(annotation)));
root = await document.GetSyntaxRootAsync().ConfigureAwait(false);
root = (await document.GetSyntaxRootAsync().ConfigureAwait(false))!;
model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(model);
// Make Safe to add namespaces
document = document.WithSyntaxRoot(
MakeSafeToAddNamespaces(root, namespaceSymbols, model, document.Project.Solution.Workspace, cancellationToken));
root = await document.GetSyntaxRootAsync().ConfigureAwait(false);
root = (await document.GetSyntaxRootAsync().ConfigureAwait(false))!;
model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(model);
// Find the context. It might be null if we have removed the context in the process of complexifying the tree.
context = root.DescendantNodesAndSelf().FirstOrDefault(x => x.HasAnnotation(annotation)) ?? root;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册