提交 a1d4335a 编写于 作者: S Sam Harwell

Add annotations for DocumentExtensions

上级 7b262611
......@@ -29,13 +29,13 @@ internal abstract class AbstractRefactoringHelpersService<TExpressionSyntax, TAr
// block's Node. That is because in addition to LocalFunctionStatement the selection would also contain trailing trivia
// (whitespace) of following statement.
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
if (root == null)
{
return ImmutableArray<TSyntaxNode>.Empty;
}
var syntaxFacts = document.Project.LanguageServices.GetRequiredService<ISyntaxFactsService>();
var selectionTrimmed = await CodeRefactoringHelpers.GetTrimmedTextSpan(document, selectionRaw, cancellationToken).ConfigureAwait(false);
// If user selected only whitespace we don't want to return anything. We could do following:
......@@ -454,6 +454,11 @@ protected virtual IEnumerable<SyntaxNode> ExtractNodesInHeader(SyntaxNode root,
// If we're deep inside we don't have to deal with being on edges (that gets dealt by TryGetSelectedNodeAsync)
// -> can simply FindToken -> proceed testing its ancestors
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
if (root is null)
{
throw new NotSupportedException(WorkspacesResources.Document_does_not_support_syntax_trees);
}
var token = root.FindTokenOnRightOfPosition(position, true);
// traverse upwards and add all parents if of correct type
......
......@@ -54,7 +54,7 @@ internal static Task<ImmutableArray<TSyntaxNode>> GetRelevantNodesAsync<TSyntaxN
TextSpan span,
CancellationToken cancellationToken) where TSyntaxNode : SyntaxNode
{
var helpers = document.GetLanguageService<IRefactoringHelpersService>();
var helpers = document.Project.LanguageServices.GetRequiredService<IRefactoringHelpersService>();
var potentialNodes = await helpers.GetRelevantNodesAsync<TSyntaxNode>(document, span, cancellationToken).ConfigureAwait(false);
return potentialNodes;
}
......
......@@ -570,7 +570,7 @@ private void AddVariableToMap(IDictionary<ISymbol, VariableInfo> variableInfoMap
// we probably need to move the API to syntaxFact service not semanticFact.
//
// if one wants to get result that also considers semantic, he should use data control flow analysis API.
var semanticFacts = _semanticDocument.Document.GetLanguageService<ISemanticFactsService>();
var semanticFacts = _semanticDocument.Document.Project.LanguageServices.GetRequiredService<ISemanticFactsService>();
return tokens.Any(t => semanticFacts.IsWrittenTo(model, t.Parent, CancellationToken.None));
}
......@@ -913,7 +913,7 @@ private OperationStatus CheckReadOnlyFields(SemanticModel semanticModel, Diction
}
List<string>? names = null;
var semanticFacts = _semanticDocument.Document.GetLanguageService<ISemanticFactsService>();
var semanticFacts = _semanticDocument.Document.Project.LanguageServices.GetRequiredService<ISemanticFactsService>();
foreach (var pair in symbolMap.Where(p => p.Key.Kind == SymbolKind.Field))
{
var field = (IFieldSymbol)pair.Key;
......
......@@ -41,7 +41,7 @@ internal partial class ImplementInterfaceCodeAction
compilation, property, accessibility, generateAbstractly, useExplicitInterfaceSymbol,
propertyGenerationBehavior, attributesToRemove, cancellationToken);
var syntaxFacts = Document.GetLanguageService<ISyntaxFactsService>();
var syntaxFacts = Document.Project.LanguageServices.GetRequiredService<ISyntaxFactsService>();
var parameterNames = NameGenerator.EnsureUniqueness(
property.Parameters.Select(p => p.Name).ToList(), isCaseSensitive: syntaxFacts.IsCaseSensitive);
......@@ -145,7 +145,7 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
return default;
}
var factory = Document.GetLanguageService<SyntaxGenerator>();
var factory = Document.Project.LanguageServices.GetRequiredService<SyntaxGenerator>();
if (ThroughMember != null)
{
var throughExpression = CreateThroughExpression(factory);
......@@ -188,7 +188,7 @@ private INamedTypeSymbol[] AttributesToRemove(Compilation compilation)
return default;
}
var factory = Document.GetLanguageService<SyntaxGenerator>();
var factory = Document.Project.LanguageServices.GetRequiredService<SyntaxGenerator>();
if (ThroughMember != null)
{
var throughExpression = CreateThroughExpression(factory);
......
......@@ -79,7 +79,7 @@ private void UpdateBuffer(TextDocument document, SpanChange spanSource, out Sour
private void ApplyDocumentToBuffer(TextDocument document, SpanChange spanSource, out SourceTextContainer container, out TextDocument documentBackedByTextBuffer)
{
var contentTypeService = document.GetLanguageService<IContentTypeLanguageService>();
var contentTypeService = document.Project.LanguageServices.GetRequiredService<IContentTypeLanguageService>();
var contentType = contentTypeService.GetDefaultContentType();
TextView.TextBuffer.ChangeContentType(contentType, null);
......
......@@ -42,7 +42,7 @@ public enum Strategy
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 addImportsService = document.Project.LanguageServices.GetRequiredService<IAddImportsService>();
var generator = SyntaxGenerator.GetGenerator(document);
// Create a simple interval tree for simplification spans.
......
// 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;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.GeneratedCodeRecognition;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.SemanticModelWorkspaceService;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.GeneratedCodeRecognition;
namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static partial class DocumentExtensions
{
public static TLanguageService GetLanguageService<TLanguageService>(this TextDocument document) where TLanguageService : class, ILanguageService
=> document?.Project?.LanguageServices?.GetService<TLanguageService>();
// ⚠ Verify IVTs do not use this method before removing it.
public static TLanguageService GetLanguageService<TLanguageService>(this Document document) where TLanguageService : class, ILanguageService
public static TLanguageService? GetLanguageService<TLanguageService>(this Document? document) where TLanguageService : class, ILanguageService
=> document?.Project?.LanguageServices?.GetService<TLanguageService>();
public static bool IsOpen(this Document document)
......@@ -33,8 +33,6 @@ 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
......@@ -106,16 +104,14 @@ 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)
public static async Task<bool> HasAnyErrorsAsync(this Document document, CancellationToken cancellationToken, List<string>? ignoreErrorCode = null)
{
var errors = await GetErrorsAsync(document, cancellationToken, ignoreErrorCode).ConfigureAwait(false);
return errors.Length > 0;
}
public static async Task<ImmutableArray<Diagnostic>> GetErrorsAsync(this Document document, CancellationToken cancellationToken, IList<string> ignoreErrorCode = null)
public static async Task<ImmutableArray<Diagnostic>> GetErrorsAsync(this Document document, CancellationToken cancellationToken, IList<string>? ignoreErrorCode = null)
{
if (!document.SupportsSemanticModel)
{
......@@ -124,14 +120,14 @@ public static async Task<ImmutableArray<Diagnostic>> GetErrorsAsync(this Documen
ignoreErrorCode ??= SpecializedCollections.EmptyList<string>();
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return semanticModel.GetDiagnostics(cancellationToken: cancellationToken).WhereAsArray(
return semanticModel!.GetDiagnostics(cancellationToken: cancellationToken).WhereAsArray(
diag => diag.Severity == DiagnosticSeverity.Error && !ignoreErrorCode.Contains(diag.Id));
}
/// <summary>
/// Debug only extension method to verify no errors were introduced by formatting, pretty listing and other related document altering service in error-free code.
/// </summary>
public static async Task VerifyNoErrorsAsync(this Document newDocument, string message, CancellationToken cancellationToken, List<string> ignoreErrorCodes = null)
public static async Task VerifyNoErrorsAsync(this Document newDocument, string message, CancellationToken cancellationToken, List<string>? ignoreErrorCodes = null)
{
var errors = await newDocument.GetErrorsAsync(cancellationToken, ignoreErrorCodes).ConfigureAwait(false);
if (errors.Length > 0)
......@@ -180,7 +176,7 @@ public static Task<SyntaxTreeIndex> GetSyntaxTreeIndexAsync(this Document docume
/// Returns the semantic model for this document that may be produced from partial semantics. The semantic model
/// is only guaranteed to contain the syntax tree for <paramref name="document"/> and nothing else.
/// </summary>
public static async Task<SemanticModel> GetPartialSemanticModelAsync(this Document document, CancellationToken cancellationToken)
public static async Task<SemanticModel?> GetPartialSemanticModelAsync(this Document document, CancellationToken cancellationToken)
{
if (document.Project.TryGetCompilation(out var compilation))
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册