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

Add annotations for classification types

上级 e445f1e6
// 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 Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.CSharp.Extensions;
......@@ -24,7 +26,7 @@ internal static class ClassificationHelpers
/// </summary>
/// <param name="token">The token.</param>
/// <returns>The correct syntactic classification for the token.</returns>
public static string GetClassification(SyntaxToken token)
public static string? GetClassification(SyntaxToken token)
{
if (IsControlKeyword(token))
{
......@@ -183,7 +185,7 @@ private static bool IsVerbatimStringToken(SyntaxToken token)
return false;
}
private static string GetClassificationForIdentifier(SyntaxToken token)
private static string? GetClassificationForIdentifier(SyntaxToken token)
{
if (token.Parent is BaseTypeDeclarationSyntax typeDeclaration && typeDeclaration.Identifier == token)
{
......@@ -232,7 +234,7 @@ private static string GetClassificationForIdentifier(SyntaxToken token)
else if (token.Parent is VariableDeclaratorSyntax variableDeclarator && variableDeclarator.Identifier == token)
{
var varDecl = variableDeclarator.Parent as VariableDeclarationSyntax;
return varDecl.Parent switch
return varDecl?.Parent switch
{
FieldDeclarationSyntax fieldDeclaration => fieldDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword) ? ClassificationTypeNames.ConstantName : ClassificationTypeNames.FieldName,
LocalDeclarationStatementSyntax localDeclarationStatement => localDeclarationStatement.IsConst ? ClassificationTypeNames.ConstantName : ClassificationTypeNames.LocalName,
......@@ -343,7 +345,7 @@ private static bool IsExtensionMethod(MethodDeclarationSyntax methodDeclaration)
return methodDeclaration.ParameterList.Parameters.FirstOrDefault()?.Modifiers.Any(SyntaxKind.ThisKeyword) == true;
}
private static string GetClassificationForTypeDeclarationIdentifier(SyntaxToken identifier)
private static string? GetClassificationForTypeDeclarationIdentifier(SyntaxToken identifier)
=> identifier.Parent.Kind() switch
{
SyntaxKind.ClassDeclaration => ClassificationTypeNames.ClassName,
......
// 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.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Classification;
......@@ -128,7 +131,7 @@ protected override bool IsParentAnAttribute(SyntaxNode node)
private bool TryClassifySymbol(
NameSyntax name,
ISymbol symbol,
[NotNullWhen(returnValue: true)] ISymbol? symbol,
SemanticModel semanticModel,
CancellationToken cancellationToken,
out ClassifiedSpan classifiedSpan)
......@@ -315,7 +318,9 @@ private bool IsInVarContext(NameSyntax name)
var identifierName = name as IdentifierNameSyntax;
if (symbolInfo.Symbol.IsImplicitValueParameter())
{
#nullable disable // Can 'identifierName' be null here?
result.Add(new ClassifiedSpan(identifierName.Identifier.Span, ClassificationTypeNames.Keyword));
#nullable enable
return true;
}
......@@ -337,7 +342,7 @@ private bool IsInVarContext(NameSyntax name)
return false;
}
private bool IsSymbolWithName(ISymbol symbol, string name)
private bool IsSymbolWithName([NotNullWhen(returnValue: true)] ISymbol? symbol, string name)
{
if (symbol is null || symbol.Name != name)
{
......
......@@ -44,7 +44,9 @@ internal class SyntaxTokenClassifier : AbstractSyntaxClassifier
var types = semanticModel.LookupTypeRegardlessOfArity(identifier, cancellationToken);
if (types.Any(s_shouldInclude))
{
#nullable disable // Can 'GetClassificationForType(types.First()' be null here?
result.Add(new ClassifiedSpan(identifier.Span, GetClassificationForType(types.First())));
#nullable enable
}
}
}
......
// 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 Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
......@@ -60,7 +62,7 @@ private void ClassifyXmlNode(XmlNodeSyntax node)
}
}
private void ClassifyXmlTrivia(SyntaxTriviaList triviaList, string whitespaceClassificationType = null)
private void ClassifyXmlTrivia(SyntaxTriviaList triviaList, string? whitespaceClassificationType = null)
{
foreach (var t in triviaList)
{
......
// 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 Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.CSharp.Syntax;
......@@ -104,7 +106,7 @@ private void ClassifyPreprocessorTrivia(SyntaxTrivia trivia, bool allowComments)
}
}
private void ClassifyPreprocessorExpression(ExpressionSyntax node)
private void ClassifyPreprocessorExpression(ExpressionSyntax? node)
{
if (node == null)
{
......
......@@ -42,7 +42,7 @@ public async Task AddSemanticClassificationsAsync(Document document, TextSpan te
return;
}
var extensionManager = document.Project.Solution.Workspace.Services.GetService<IExtensionManager>();
var extensionManager = document.Project.Solution.Workspace.Services.GetRequiredService<IExtensionManager>();
var classifiers = classificationService.GetDefaultSyntaxClassifiers();
var getNodeClassifiers = extensionManager.CreateNodeExtensionGetter(classifiers, c => c.SyntaxNodeTypes);
......
// 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
namespace Microsoft.CodeAnalysis.Classification
{
internal static class ClassificationExtensions
{
public static string GetClassification(this ITypeSymbol type)
public static string? GetClassification(this ITypeSymbol type)
{
switch (type.TypeKind)
{
......
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
......@@ -30,11 +32,11 @@ public static class Classifier
Workspace workspace,
CancellationToken cancellationToken = default)
{
var service = workspace.Services.GetLanguageServices(semanticModel.Language).GetService<ISyntaxClassificationService>();
var service = workspace.Services.GetLanguageServices(semanticModel.Language).GetRequiredService<ISyntaxClassificationService>();
var syntaxClassifiers = service.GetDefaultSyntaxClassifiers();
var extensionManager = workspace.Services.GetService<IExtensionManager>();
var extensionManager = workspace.Services.GetRequiredService<IExtensionManager>();
var getNodeClassifiers = extensionManager.CreateNodeExtensionGetter(syntaxClassifiers, c => c.SyntaxNodeTypes);
var getTokenClassifiers = extensionManager.CreateTokenExtensionGetter(syntaxClassifiers, c => c.SyntaxTokenKinds);
......@@ -61,19 +63,6 @@ public static class Classifier
}
}
internal static async Task<ImmutableArray<SymbolDisplayPart>> GetClassifiedSymbolDisplayPartsAsync(
Document document,
TextSpan textSpan,
CancellationToken cancellationToken = default)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
return await GetClassifiedSymbolDisplayPartsAsync(
semanticModel, textSpan,
document.Project.Solution.Workspace,
cancellationToken).ConfigureAwait(false);
}
internal static async Task<ImmutableArray<SymbolDisplayPart>> GetClassifiedSymbolDisplayPartsAsync(
SemanticModel semanticModel, TextSpan textSpan, Workspace workspace,
CancellationToken cancellationToken = default)
......
using Microsoft.CodeAnalysis.PooledObjects;
// 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 Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
......@@ -9,7 +13,7 @@ internal abstract class AbstractNameSyntaxClassifier : AbstractSyntaxClassifier
protected abstract int? GetRightmostNameArity(SyntaxNode node);
protected abstract bool IsParentAnAttribute(SyntaxNode node);
protected ISymbol TryGetSymbol(SyntaxNode node, SymbolInfo symbolInfo, SemanticModel semanticModel)
protected ISymbol? TryGetSymbol(SyntaxNode node, SymbolInfo symbolInfo, SemanticModel semanticModel)
{
var symbol = symbolInfo.Symbol;
......@@ -76,7 +80,7 @@ protected ISymbol TryGetSymbol(SyntaxNode node, SymbolInfo symbolInfo, SemanticM
}
protected void TryClassifyStaticSymbol(
ISymbol symbol,
ISymbol? symbol,
TextSpan span,
ArrayBuilder<ClassifiedSpan> result)
{
......
// 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.Immutable;
using System.Threading;
......@@ -14,7 +16,7 @@ protected AbstractSyntaxClassifier()
{
}
protected string GetClassificationForType(ITypeSymbol type)
protected string? GetClassificationForType(ITypeSymbol type)
=> type.GetClassification();
public virtual void AddClassifications(Workspace workspace, SyntaxNode syntax, SemanticModel semanticModel, ArrayBuilder<ClassifiedSpan> result, CancellationToken cancellationToken)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册