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

fix nullable warnings

上级 c5c9ce04
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.Options; using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editing namespace Microsoft.CodeAnalysis.Editing
{ {
...@@ -52,6 +53,7 @@ public static Task<Document> AddImportsAsync(Document document, IEnumerable<Text ...@@ -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) 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); var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSyntaxesAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false); return await AddImportsFromSyntaxesAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false);
} }
...@@ -69,6 +71,7 @@ internal static Task<Document> AddImportsFromSyntaxesAsync(Document document, Te ...@@ -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) 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); 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); 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 ...@@ -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) 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); var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(root);
return await AddImportsFromSymbolAnnotationAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false); return await AddImportsFromSymbolAnnotationAsync(document, root.FullSpan, safe, options, cancellationToken).ConfigureAwait(false);
} }
...@@ -111,6 +115,7 @@ internal static Task<Document> AddImportsFromSymbolAnnotationAsync(Document docu ...@@ -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) 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); 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); return await AddImportsFromSymbolAnnotationAsync(document, root.GetAnnotatedNodesAndTokens(annotation).Select(t => t.FullSpan), safe, options, cancellationToken).ConfigureAwait(false);
} }
......
...@@ -40,6 +40,7 @@ public enum Strategy ...@@ -40,6 +40,7 @@ public enum Strategy
options ??= await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false); options ??= await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(model);
var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false); var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var addImportsService = document.GetLanguageService<IAddImportsService>(); var addImportsService = document.GetLanguageService<IAddImportsService>();
var generator = SyntaxGenerator.GetGenerator(document); var generator = SyntaxGenerator.GetGenerator(document);
...@@ -68,14 +69,18 @@ public enum Strategy ...@@ -68,14 +69,18 @@ public enum Strategy
// This will allow us to find it after we have called MakeSafeToAddNamespaces. // This will allow us to find it after we have called MakeSafeToAddNamespaces.
var annotation = new SyntaxAnnotation(); var annotation = new SyntaxAnnotation();
document = document.WithSyntaxRoot(root.ReplaceNode(context, context.WithAdditionalAnnotations(annotation))); 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); model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
Contract.ThrowIfNull(model);
// Make Safe to add namespaces // Make Safe to add namespaces
document = document.WithSyntaxRoot( document = document.WithSyntaxRoot(
MakeSafeToAddNamespaces(root, namespaceSymbols, model, document.Project.Solution.Workspace, cancellationToken)); 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); 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. // 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; 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.
先完成此消息的编辑!
想要评论请 注册