未验证 提交 aba67f39 编写于 作者: J Jason Malinowski 提交者: GitHub

Merge pull request #37594 from...

Merge pull request #37594 from jasonmalinowski/add-nullable-annotations-to-speculative-model-creation

Add nullable annotations to speculative model creation and update code for bug found
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable enable
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
......
......@@ -40,12 +40,14 @@ private class SemanticModelService : ISemanticModelService
private readonly ReaderWriterLockSlim _gate = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
#nullable enable
public async Task<SemanticModel> GetSemanticModelForNodeAsync(Document document, SyntaxNode node, CancellationToken cancellationToken = default)
{
var syntaxFactsService = document.GetLanguageService<ISyntaxFactsService>();
var semanticFactsService = document.GetLanguageService<ISemanticFactsService>();
if (syntaxFactsService == null || semanticFactsService == null || node == null)
if (syntaxFactsService == null || semanticFactsService == null)
{
// it only works if we can track member
return await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
......@@ -160,6 +162,8 @@ private bool IsPrimaryBranch(Document document)
return document.Project.Solution.BranchId == document.Project.Solution.Workspace.PrimaryBranchId;
}
#nullable restore
private Task AddVersionCacheAsync(Project project, VersionStamp version, CancellationToken cancellationToken)
{
return UpdateVersionCacheAsync(project, version, primarySet: null, cancellationToken: cancellationToken);
......
......@@ -33,6 +33,8 @@ public static bool IsOpen(this Document document)
return workspace != null && workspace.IsDocumentOpen(document.Id);
}
#nullable enable
/// <summary>
/// this will return either regular semantic model or speculative semantic based on context.
/// any feature that is involved in typing or run on UI thread should use this to take advantage of speculative semantic model
......@@ -60,7 +62,7 @@ public static async Task<SemanticModel> GetSemanticModelForSpanAsync(this Docume
return await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
}
var node = token.Parent.AncestorsAndSelf().FirstOrDefault(a => a.FullSpan.Contains(span));
var node = token.Parent.AncestorsAndSelf().First(a => a.FullSpan.Contains(span));
return await GetSemanticModelForNodeAsync(semanticModelService, syntaxFactService, document, node, span, cancellationToken).ConfigureAwait(false);
}
......@@ -75,7 +77,7 @@ public static async Task<SemanticModel> GetSemanticModelForSpanAsync(this Docume
/// also, symbols from the semantic model returned by this API might have out of date location information.
/// if exact location (not relative location) is needed from symbol, regular GetSemanticModel should be used.
/// </summary>
public static Task<SemanticModel> GetSemanticModelForNodeAsync(this Document document, SyntaxNode node, CancellationToken cancellationToken)
public static Task<SemanticModel> GetSemanticModelForNodeAsync(this Document document, SyntaxNode? node, CancellationToken cancellationToken)
{
var syntaxFactService = document.GetLanguageService<ISyntaxFactsService>();
var semanticModelService = document.Project.Solution.Workspace.Services.GetService<ISemanticModelService>();
......@@ -101,6 +103,8 @@ public static Task<SemanticModel> GetSemanticModelForNodeAsync(this Document doc
return semanticModelService.GetSemanticModelForNodeAsync(document, node, cancellationToken);
}
#nullable restore
#if DEBUG
public static async Task<bool> HasAnyErrorsAsync(this Document document, CancellationToken cancellationToken, List<string> ignoreErrorCode = null)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册