提交 494fcc5f 编写于 作者: S Sam Harwell

Add annotations for extension methods classes

上级 a1d4335a
...@@ -11,5 +11,11 @@ internal static class RoslynString ...@@ -11,5 +11,11 @@ internal static class RoslynString
/// <inheritdoc cref="string.IsNullOrEmpty(string)"/> /// <inheritdoc cref="string.IsNullOrEmpty(string)"/>
public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] string? value) public static bool IsNullOrEmpty([NotNullWhen(returnValue: false)] string? value)
=> string.IsNullOrEmpty(value); => string.IsNullOrEmpty(value);
#if !NET20
/// <inheritdoc cref="string.IsNullOrWhiteSpace(string)"/>
public static bool IsNullOrWhiteSpace([NotNullWhen(returnValue: false)] string? value)
=> string.IsNullOrWhiteSpace(value);
#endif
} }
} }
...@@ -36,7 +36,7 @@ public override bool ContainingScopeHasAsyncKeyword() ...@@ -36,7 +36,7 @@ public override bool ContainingScopeHasAsyncKeyword()
return false; return false;
} }
public override SyntaxNode GetContainingScope() public override SyntaxNode? GetContainingScope()
{ {
Contract.ThrowIfNull(this.SemanticDocument); Contract.ThrowIfNull(this.SemanticDocument);
Contract.ThrowIfFalse(this.SelectionInExpression); Contract.ThrowIfFalse(this.SelectionInExpression);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod; using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Shared.Extensions;
...@@ -16,7 +18,7 @@ namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod ...@@ -16,7 +18,7 @@ namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod
{ {
internal static class Extensions internal static class Extensions
{ {
public static ExpressionSyntax GetUnparenthesizedExpression(this SyntaxNode node) public static ExpressionSyntax? GetUnparenthesizedExpression(this SyntaxNode? node)
{ {
if (!(node is ParenthesizedExpressionSyntax parenthesizedExpression)) if (!(node is ParenthesizedExpressionSyntax parenthesizedExpression))
{ {
...@@ -26,19 +28,17 @@ public static ExpressionSyntax GetUnparenthesizedExpression(this SyntaxNode node ...@@ -26,19 +28,17 @@ public static ExpressionSyntax GetUnparenthesizedExpression(this SyntaxNode node
return GetUnparenthesizedExpression(parenthesizedExpression.Expression); return GetUnparenthesizedExpression(parenthesizedExpression.Expression);
} }
public static StatementSyntax GetStatementUnderContainer(this SyntaxNode node) public static StatementSyntax? GetStatementUnderContainer(this SyntaxNode node)
{ {
Contract.ThrowIfNull(node); Contract.ThrowIfNull(node);
while (node != null) for (SyntaxNode? current = node; current is object; current = current.Parent)
{ {
if (node.Parent != null && if (current.Parent != null &&
node.Parent.IsStatementContainerNode()) current.Parent.IsStatementContainerNode())
{ {
return node as StatementSyntax; return current as StatementSyntax;
} }
node = node.Parent;
} }
return null; return null;
...@@ -49,12 +49,12 @@ public static StatementSyntax GetParentLabeledStatementIfPossible(this SyntaxNod ...@@ -49,12 +49,12 @@ public static StatementSyntax GetParentLabeledStatementIfPossible(this SyntaxNod
return (StatementSyntax)((node.Parent is LabeledStatementSyntax) ? node.Parent : node); return (StatementSyntax)((node.Parent is LabeledStatementSyntax) ? node.Parent : node);
} }
public static bool IsStatementContainerNode(this SyntaxNode node) public static bool IsStatementContainerNode([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{ {
return node is BlockSyntax || node is SwitchSectionSyntax; return node is BlockSyntax || node is SwitchSectionSyntax;
} }
public static BlockSyntax GetBlockBody(this SyntaxNode node) public static BlockSyntax? GetBlockBody(this SyntaxNode? node)
{ {
switch (node) switch (node)
{ {
...@@ -246,22 +246,22 @@ public static bool HasHybridTriviaBetween(this SyntaxToken token1, SyntaxToken t ...@@ -246,22 +246,22 @@ public static bool HasHybridTriviaBetween(this SyntaxToken token1, SyntaxToken t
return false; return false;
} }
public static bool IsArrayInitializer(this SyntaxNode node) public static bool IsArrayInitializer([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{ {
return node is InitializerExpressionSyntax && node.Parent is EqualsValueClauseSyntax; return node is InitializerExpressionSyntax && node.Parent is EqualsValueClauseSyntax;
} }
public static bool IsExpressionInCast(this SyntaxNode node) public static bool IsExpressionInCast([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{ {
return node is ExpressionSyntax && node.Parent is CastExpressionSyntax; return node is ExpressionSyntax && node.Parent is CastExpressionSyntax;
} }
public static bool IsExpression(this SyntaxNode node) public static bool IsExpression([NotNullWhen(returnValue: true)] this SyntaxNode? node)
{ {
return node is ExpressionSyntax; return node is ExpressionSyntax;
} }
public static bool IsObjectType(this ITypeSymbol type) public static bool IsObjectType(this ITypeSymbol? type)
{ {
return type == null || type.SpecialType == SpecialType.System_Object; return type == null || type.SpecialType == SpecialType.System_Object;
} }
......
...@@ -48,7 +48,7 @@ protected override ITypeSymbol DetermineReturnTypeWorker(CancellationToken cance ...@@ -48,7 +48,7 @@ protected override ITypeSymbol DetermineReturnTypeWorker(CancellationToken cance
var typeInference = this.Document.Document.GetLanguageService<ITypeInferenceService>(); var typeInference = this.Document.Document.GetLanguageService<ITypeInferenceService>();
var inferredType = typeInference.InferType( var inferredType = typeInference.InferType(
this.Document.SemanticModel, _invocationExpression, objectAsDefault: true, this.Document.SemanticModel, _invocationExpression, objectAsDefault: true,
nameOpt: this.State.IdentifierToken.ValueText, cancellationToken: cancellationToken); name: this.State.IdentifierToken.ValueText, cancellationToken: cancellationToken);
return inferredType; return inferredType;
} }
......
...@@ -330,6 +330,10 @@ internal ChangeSignatureOptionsResult GetChangeSignatureOptions(ChangeSignatureA ...@@ -330,6 +330,10 @@ internal ChangeSignatureOptionsResult GetChangeSignatureOptions(ChangeSignatureA
var doc = originalSolution.GetDocument(docId)!; var doc = originalSolution.GetDocument(docId)!;
var updater = doc.Project.LanguageServices.GetRequiredService<AbstractChangeSignatureService>(); var updater = doc.Project.LanguageServices.GetRequiredService<AbstractChangeSignatureService>();
var root = doc.GetSyntaxRootSynchronously(CancellationToken.None); var root = doc.GetSyntaxRootSynchronously(CancellationToken.None);
if (root is null)
{
throw new NotSupportedException(WorkspacesResources.Document_does_not_support_syntax_trees);
}
var nodes = nodesToUpdate[docId]; var nodes = nodesToUpdate[docId];
......
...@@ -378,7 +378,7 @@ private int GetStatementIndex(ChildSyntaxList children, SyntaxNode statement) ...@@ -378,7 +378,7 @@ private int GetStatementIndex(ChildSyntaxList children, SyntaxNode statement)
var typeInference = semanticDocument.Document.GetLanguageService<ITypeInferenceService>(); var typeInference = semanticDocument.Document.GetLanguageService<ITypeInferenceService>();
var inferredType = typeInference.InferType( var inferredType = typeInference.InferType(
semanticDocument.SemanticModel, SimpleNameOrMemberAccessExpressionOpt, objectAsDefault: true, semanticDocument.SemanticModel, SimpleNameOrMemberAccessExpressionOpt, objectAsDefault: true,
nameOpt: IdentifierToken.ValueText, cancellationToken: cancellationToken); name: IdentifierToken.ValueText, cancellationToken: cancellationToken);
var compilation = semanticDocument.SemanticModel.Compilation; var compilation = semanticDocument.SemanticModel.Compilation;
inferredType = inferredType.SpecialType == SpecialType.System_Void inferredType = inferredType.SpecialType == SpecialType.System_Void
......
...@@ -55,7 +55,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateMember.GenerateMethod ...@@ -55,7 +55,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateMember.GenerateMethod
Dim typeInference = Document.Project.LanguageServices.GetService(Of ITypeInferenceService)() Dim typeInference = Document.Project.LanguageServices.GetService(Of ITypeInferenceService)()
Dim inferredType = typeInference.InferType( Dim inferredType = typeInference.InferType(
Document.SemanticModel, Me.InvocationExpression, objectAsDefault:=True, Document.SemanticModel, Me.InvocationExpression, objectAsDefault:=True,
nameOpt:=Me.State.IdentifierToken.ValueText, cancellationToken:=cancellationToken) name:=Me.State.IdentifierToken.ValueText, cancellationToken:=cancellationToken)
Return inferredType Return inferredType
End Function End Function
......
...@@ -165,7 +165,7 @@ private void GetContainers(SyntaxNode root, SyntaxNode contextLocation, out Synt ...@@ -165,7 +165,7 @@ private void GetContainers(SyntaxNode root, SyntaxNode contextLocation, out Synt
aliasContainer = contextSpine.FirstOrDefault(HasAliases) ?? fallbackNode; aliasContainer = contextSpine.FirstOrDefault(HasAliases) ?? fallbackNode;
} }
private static SyntaxNode GetFirstApplicableContainer(SyntaxNode contextNode) private static SyntaxNode? GetFirstApplicableContainer(SyntaxNode contextNode)
{ {
var usingDirective = contextNode.GetAncestor<TUsingOrAliasSyntax>(); var usingDirective = contextNode.GetAncestor<TUsingOrAliasSyntax>();
if (usingDirective != null) if (usingDirective != null)
...@@ -174,7 +174,7 @@ private static SyntaxNode GetFirstApplicableContainer(SyntaxNode contextNode) ...@@ -174,7 +174,7 @@ private static SyntaxNode GetFirstApplicableContainer(SyntaxNode contextNode)
} }
return contextNode.GetAncestor<TNamespaceDeclarationSyntax>() ?? return contextNode.GetAncestor<TNamespaceDeclarationSyntax>() ??
(SyntaxNode)contextNode.GetAncestorOrThis<TCompilationUnitSyntax>(); (SyntaxNode?)contextNode.GetAncestorOrThis<TCompilationUnitSyntax>();
} }
} }
} }
...@@ -212,8 +212,7 @@ CancellationToken cancellationToken ...@@ -212,8 +212,7 @@ CancellationToken cancellationToken
SyntaxGenerator generator, SyntaxGenerator generator,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
SyntaxNode? first = null; (SyntaxNode first, SyntaxNode last)? nodes = null;
SyntaxNode? last = null;
var importsToAdd = ArrayBuilder<SyntaxNode>.GetInstance(); var importsToAdd = ArrayBuilder<SyntaxNode>.GetInstance();
var annotatedNodes = syntaxNodes.Where(x => x.HasAnnotations(SymbolAnnotation.Kind)); var annotatedNodes = syntaxNodes.Where(x => x.HasAnnotations(SymbolAnnotation.Kind));
...@@ -246,8 +245,7 @@ CancellationToken cancellationToken ...@@ -246,8 +245,7 @@ CancellationToken cancellationToken
continue; continue;
} }
first ??= annotatedNode; nodes = (first: nodes?.first ?? annotatedNode, last: annotatedNode);
last = annotatedNode;
if (addedSymbols.Contains(namespaceSymbol)) if (addedSymbols.Contains(namespaceSymbol))
{ {
...@@ -276,7 +274,7 @@ CancellationToken cancellationToken ...@@ -276,7 +274,7 @@ CancellationToken cancellationToken
// since whatever added the symbol annotation probably also added simplifier annotations, // since whatever added the symbol annotation probably also added simplifier annotations,
// and if not they probably didn't for a reason // and if not they probably didn't for a reason
return (importsToAdd.ToImmutableAndFree(), addedSymbols, first?.GetCommonRoot(last)); return (importsToAdd.ToImmutableAndFree(), addedSymbols, nodes is var (first, last) ? first.GetCommonRoot(last) : null);
} }
/// <summary> /// <summary>
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
...@@ -61,7 +63,7 @@ public static void PerformAction(this IExtensionManager extensionManager, object ...@@ -61,7 +63,7 @@ public static void PerformAction(this IExtensionManager extensionManager, object
public static async Task PerformActionAsync( public static async Task PerformActionAsync(
this IExtensionManager extensionManager, this IExtensionManager extensionManager,
object extension, object extension,
Func<Task> function) Func<Task?> function)
{ {
try try
{ {
...@@ -84,7 +86,7 @@ public static void PerformAction(this IExtensionManager extensionManager, object ...@@ -84,7 +86,7 @@ public static void PerformAction(this IExtensionManager extensionManager, object
public static async Task<T> PerformFunctionAsync<T>( public static async Task<T> PerformFunctionAsync<T>(
this IExtensionManager extensionManager, this IExtensionManager extensionManager,
object extension, object extension,
Func<Task<T>> function, Func<Task<T>?> function,
T defaultValue) T defaultValue)
{ {
try try
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Shared.Extensions namespace Microsoft.CodeAnalysis.Shared.Extensions
{ {
internal static class FileLinePositionSpanExtensions internal static class FileLinePositionSpanExtensions
...@@ -7,7 +9,7 @@ internal static class FileLinePositionSpanExtensions ...@@ -7,7 +9,7 @@ internal static class FileLinePositionSpanExtensions
/// <summary> /// <summary>
/// Get mapped file path if exist, otherwise return null. /// Get mapped file path if exist, otherwise return null.
/// </summary> /// </summary>
public static string GetMappedFilePathIfExist(this FileLinePositionSpan fileLinePositionSpan) public static string? GetMappedFilePathIfExist(this FileLinePositionSpan fileLinePositionSpan)
{ {
return fileLinePositionSpan.HasMappedPath ? fileLinePositionSpan.Path : null; return fileLinePositionSpan.HasMappedPath ? fileLinePositionSpan.Path : null;
} }
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
...@@ -87,15 +89,15 @@ internal static partial class IFindReferencesResultExtensions ...@@ -87,15 +89,15 @@ internal static partial class IFindReferencesResultExtensions
public static IEnumerable<ReferencedSymbol> FilterToAliasMatches( public static IEnumerable<ReferencedSymbol> FilterToAliasMatches(
this IEnumerable<ReferencedSymbol> result, this IEnumerable<ReferencedSymbol> result,
IAliasSymbol aliasSymbolOpt) IAliasSymbol? aliasSymbol)
{ {
if (aliasSymbolOpt == null) if (aliasSymbol == null)
{ {
return result; return result;
} }
return from r in result return from r in result
let aliasLocations = r.Locations.Where(loc => SymbolEquivalenceComparer.Instance.Equals(loc.Alias, aliasSymbolOpt)) let aliasLocations = r.Locations.Where(loc => SymbolEquivalenceComparer.Instance.Equals(loc.Alias, aliasSymbol))
where aliasLocations.Any() where aliasLocations.Any()
select new ReferencedSymbol(r.DefinitionAndProjectId, aliasLocations); select new ReferencedSymbol(r.DefinitionAndProjectId, aliasLocations);
} }
...@@ -117,7 +119,7 @@ where aliasLocations.Any() ...@@ -117,7 +119,7 @@ where aliasLocations.Any()
{ {
foreach (var reference in result) foreach (var reference in result)
{ {
var isCaseSensitive = solution.Workspace.Services.GetLanguageServices(reference.Definition.Language).GetService<ISyntaxFactsService>().IsCaseSensitive; var isCaseSensitive = solution.Workspace.Services.GetLanguageServices(reference.Definition.Language).GetRequiredService<ISyntaxFactsService>().IsCaseSensitive;
var comparer = isCaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase; var comparer = isCaseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;
if (reference.Definition.IsOrdinaryMethod() && if (reference.Definition.IsOrdinaryMethod() &&
!comparer.Equals(reference.Definition.Name, symbol.Name)) !comparer.Equals(reference.Definition.Name, symbol.Name))
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Host.Mef;
using Roslyn.Utilities; using Roslyn.Utilities;
...@@ -10,6 +13,7 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions ...@@ -10,6 +13,7 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{ {
internal static class ILanguageMetadataExtensions internal static class ILanguageMetadataExtensions
{ {
[return: MaybeNull]
public static TInterface ToSpecificLanguage<TInterface, TMetadata>(this IEnumerable<Lazy<TInterface, TMetadata>> services, string languageName) public static TInterface ToSpecificLanguage<TInterface, TMetadata>(this IEnumerable<Lazy<TInterface, TMetadata>> services, string languageName)
where TMetadata : ILanguageMetadata where TMetadata : ILanguageMetadata
{ {
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -13,7 +15,7 @@ internal static class ILanguageServiceProviderExtensions ...@@ -13,7 +15,7 @@ internal static class ILanguageServiceProviderExtensions
{ {
public static IEnumerable<Lazy<T, TMetadata>> SelectMatchingExtensions<T, TMetadata>( public static IEnumerable<Lazy<T, TMetadata>> SelectMatchingExtensions<T, TMetadata>(
this HostLanguageServices serviceProvider, this HostLanguageServices serviceProvider,
IEnumerable<Lazy<T, TMetadata>> items) IEnumerable<Lazy<T, TMetadata>>? items)
where TMetadata : ILanguageMetadata where TMetadata : ILanguageMetadata
{ {
if (items == null) if (items == null)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Threading; using System.Threading;
...@@ -22,18 +25,20 @@ internal static partial class ISolutionExtensions ...@@ -22,18 +25,20 @@ internal static partial class ISolutionExtensions
foreach (var projectId in solution.ProjectIds) foreach (var projectId in solution.ProjectIds)
{ {
var project = solution.GetProject(projectId); var project = solution.GetProject(projectId)!;
if (project.SupportsCompilation) if (project.SupportsCompilation)
{ {
var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false); var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
#nullable disable // Can 'compilation' be null here?
results.Add(compilation.Assembly.GlobalNamespace); results.Add(compilation.Assembly.GlobalNamespace);
#nullable enable
} }
} }
return results.ToImmutableAndFree(); return results.ToImmutableAndFree();
} }
public static IEnumerable<DocumentId> GetChangedDocuments(this Solution newSolution, Solution oldSolution) public static IEnumerable<DocumentId> GetChangedDocuments(this Solution? newSolution, Solution oldSolution)
{ {
if (newSolution != null) if (newSolution != null)
{ {
...@@ -49,14 +54,14 @@ public static IEnumerable<DocumentId> GetChangedDocuments(this Solution newSolut ...@@ -49,14 +54,14 @@ public static IEnumerable<DocumentId> GetChangedDocuments(this Solution newSolut
} }
} }
public static TextDocument GetTextDocument(this Solution solution, DocumentId documentId) public static TextDocument? GetTextDocument(this Solution solution, DocumentId? documentId)
{ {
return solution.GetDocument(documentId) ?? solution.GetAdditionalDocument(documentId) ?? solution.GetAnalyzerConfigDocument(documentId); return solution.GetDocument(documentId) ?? solution.GetAdditionalDocument(documentId) ?? solution.GetAnalyzerConfigDocument(documentId);
} }
public static TextDocumentKind GetDocumentKind(this Solution solution, DocumentId documentId) public static TextDocumentKind? GetDocumentKind(this Solution solution, DocumentId documentId)
{ {
return solution.GetTextDocument(documentId).Kind; return solution.GetTextDocument(documentId)?.Kind;
} }
public static Solution WithTextDocumentText(this Solution solution, DocumentId documentId, SourceText text, PreservationMode mode = PreservationMode.PreserveIdentity) public static Solution WithTextDocumentText(this Solution solution, DocumentId documentId, SourceText text, PreservationMode mode = PreservationMode.PreserveIdentity)
...@@ -73,6 +78,9 @@ public static Solution WithTextDocumentText(this Solution solution, DocumentId d ...@@ -73,6 +78,9 @@ public static Solution WithTextDocumentText(this Solution solution, DocumentId d
case TextDocumentKind.AdditionalDocument: case TextDocumentKind.AdditionalDocument:
return solution.WithAdditionalDocumentText(documentId, text, mode); return solution.WithAdditionalDocumentText(documentId, text, mode);
case null:
throw new InvalidOperationException(WorkspacesResources.The_solution_does_not_contain_the_specified_document);
default: default:
throw ExceptionUtilities.UnexpectedValue(documentKind); throw ExceptionUtilities.UnexpectedValue(documentKind);
} }
......
...@@ -511,7 +511,7 @@ public static bool IsUnsafe([NotNullWhen(returnValue: true)] this ISymbol? membe ...@@ -511,7 +511,7 @@ public static bool IsUnsafe([NotNullWhen(returnValue: true)] this ISymbol? membe
} }
public static ITypeSymbol ConvertToType( public static ITypeSymbol ConvertToType(
this ISymbol symbol, this ISymbol? symbol,
Compilation compilation, Compilation compilation,
bool extensionUsedAsInstance = false) bool extensionUsedAsInstance = false)
{ {
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Immutable; using System.Collections.Immutable;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
...@@ -22,7 +24,7 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI ...@@ -22,7 +24,7 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI
public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeInferenceService service, SemanticModel semanticModel, SyntaxNode expression, CancellationToken cancellationToken) public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeInferenceService service, SemanticModel semanticModel, SyntaxNode expression, CancellationToken cancellationToken)
=> service.GetTypeInferenceInfo(semanticModel, expression, nameOpt: null, cancellationToken: cancellationToken); => service.GetTypeInferenceInfo(semanticModel, expression, nameOpt: null, cancellationToken: cancellationToken);
public static INamedTypeSymbol InferDelegateType( public static INamedTypeSymbol? InferDelegateType(
this ITypeInferenceService typeInferenceService, this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel, SemanticModel semanticModel,
SyntaxNode expression, SyntaxNode expression,
...@@ -32,7 +34,7 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI ...@@ -32,7 +34,7 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI
return GetFirstDelegateType(semanticModel, types); return GetFirstDelegateType(semanticModel, types);
} }
public static INamedTypeSymbol InferDelegateType( public static INamedTypeSymbol? InferDelegateType(
this ITypeInferenceService typeInferenceService, this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel, SemanticModel semanticModel,
int position, int position,
...@@ -42,13 +44,13 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI ...@@ -42,13 +44,13 @@ public static ImmutableArray<TypeInferenceInfo> GetTypeInferenceInfo(this ITypeI
return GetFirstDelegateType(semanticModel, types); return GetFirstDelegateType(semanticModel, types);
} }
private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel, ImmutableArray<ITypeSymbol> types) private static INamedTypeSymbol? GetFirstDelegateType(SemanticModel semanticModel, ImmutableArray<ITypeSymbol> types)
{ {
var delegateTypes = types.Select(t => t.GetDelegateType(semanticModel.Compilation)); var delegateTypes = types.Select(t => t.GetDelegateType(semanticModel.Compilation));
return delegateTypes.WhereNotNull().FirstOrDefault(); return delegateTypes.WhereNotNull().FirstOrDefault();
} }
public static ITypeSymbol InferType( public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService, this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel, SemanticModel semanticModel,
SyntaxNode expression, SyntaxNode expression,
...@@ -57,28 +59,28 @@ private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel ...@@ -57,28 +59,28 @@ private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel
{ {
return InferType( return InferType(
typeInferenceService, semanticModel, expression, objectAsDefault, typeInferenceService, semanticModel, expression, objectAsDefault,
nameOpt: null, cancellationToken: cancellationToken); name: null, cancellationToken: cancellationToken);
} }
public static ITypeSymbol InferType( public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService, this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel, SemanticModel semanticModel,
SyntaxNode expression, SyntaxNode expression,
bool objectAsDefault, bool objectAsDefault,
string nameOpt, string? name,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var types = typeInferenceService.InferTypes(semanticModel, expression, nameOpt, cancellationToken); var types = typeInferenceService.InferTypes(semanticModel, expression, name, cancellationToken);
if (types.Length == 0) if (types.Length == 0)
{ {
return objectAsDefault ? semanticModel.Compilation.ObjectType : null; return objectAsDefault ? semanticModel.Compilation.ObjectType : null;
} }
return types.FirstOrDefault(); return types.First();
} }
public static ITypeSymbol InferType( public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService, this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel, SemanticModel semanticModel,
int position, int position,
...@@ -87,18 +89,18 @@ private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel ...@@ -87,18 +89,18 @@ private static INamedTypeSymbol GetFirstDelegateType(SemanticModel semanticModel
{ {
return InferType( return InferType(
typeInferenceService, semanticModel, position, objectAsDefault, typeInferenceService, semanticModel, position, objectAsDefault,
nameOpt: null, cancellationToken: cancellationToken); name: null, cancellationToken: cancellationToken);
} }
public static ITypeSymbol InferType( public static ITypeSymbol? InferType(
this ITypeInferenceService typeInferenceService, this ITypeInferenceService typeInferenceService,
SemanticModel semanticModel, SemanticModel semanticModel,
int position, int position,
bool objectAsDefault, bool objectAsDefault,
string nameOpt, string? name,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var types = typeInferenceService.InferTypes(semanticModel, position, nameOpt, cancellationToken); var types = typeInferenceService.InferTypes(semanticModel, position, name, cancellationToken);
if (types.Length == 0) if (types.Length == 0)
{ {
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.CodeAnalysis.Shared.Extensions namespace Microsoft.CodeAnalysis.Shared.Extensions
{ {
internal static partial class ObjectExtensions internal static partial class ObjectExtensions
{ {
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -29,11 +33,12 @@ internal static partial class ObjectExtensions ...@@ -29,11 +33,12 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -61,11 +66,12 @@ internal static partial class ObjectExtensions ...@@ -61,11 +66,12 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -98,11 +104,12 @@ internal static partial class ObjectExtensions ...@@ -98,11 +104,12 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -140,11 +147,12 @@ internal static partial class ObjectExtensions ...@@ -140,11 +147,12 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -232,11 +240,12 @@ internal static partial class ObjectExtensions ...@@ -232,11 +240,12 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -329,11 +338,12 @@ internal static partial class ObjectExtensions ...@@ -329,11 +338,12 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -451,11 +461,12 @@ internal static partial class ObjectExtensions ...@@ -451,11 +461,12 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TDerivedType23, TDerivedType24, TDerivedType25, TDerivedType26, TDerivedType27, TDerivedType28, TDerivedType29, TDerivedType30, TDerivedType31, TDerivedType32, TDerivedType33, TDerivedType34, TDerivedType35, TDerivedType36, TDerivedType37, TDerivedType38, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TDerivedType23, TResult> matchFunc23, Func<TDerivedType24, TResult> matchFunc24, Func<TDerivedType25, TResult> matchFunc25, Func<TDerivedType26, TResult> matchFunc26, Func<TDerivedType27, TResult> matchFunc27, Func<TDerivedType28, TResult> matchFunc28, Func<TDerivedType29, TResult> matchFunc29, Func<TDerivedType30, TResult> matchFunc30, Func<TDerivedType31, TResult> matchFunc31, Func<TDerivedType32, TResult> matchFunc32, Func<TDerivedType33, TResult> matchFunc33, Func<TDerivedType34, TResult> matchFunc34, Func<TDerivedType35, TResult> matchFunc35, Func<TDerivedType36, TResult> matchFunc36, Func<TDerivedType37, TResult> matchFunc37, Func<TDerivedType38, TResult> matchFunc38, Func<TBaseType, TResult> defaultFunc = null) [return: MaybeNull]
public static TResult TypeSwitch<TBaseType, TDerivedType1, TDerivedType2, TDerivedType3, TDerivedType4, TDerivedType5, TDerivedType6, TDerivedType7, TDerivedType8, TDerivedType9, TDerivedType10, TDerivedType11, TDerivedType12, TDerivedType13, TDerivedType14, TDerivedType15, TDerivedType16, TDerivedType17, TDerivedType18, TDerivedType19, TDerivedType20, TDerivedType21, TDerivedType22, TDerivedType23, TDerivedType24, TDerivedType25, TDerivedType26, TDerivedType27, TDerivedType28, TDerivedType29, TDerivedType30, TDerivedType31, TDerivedType32, TDerivedType33, TDerivedType34, TDerivedType35, TDerivedType36, TDerivedType37, TDerivedType38, TResult>(this TBaseType obj, Func<TDerivedType1, TResult> matchFunc1, Func<TDerivedType2, TResult> matchFunc2, Func<TDerivedType3, TResult> matchFunc3, Func<TDerivedType4, TResult> matchFunc4, Func<TDerivedType5, TResult> matchFunc5, Func<TDerivedType6, TResult> matchFunc6, Func<TDerivedType7, TResult> matchFunc7, Func<TDerivedType8, TResult> matchFunc8, Func<TDerivedType9, TResult> matchFunc9, Func<TDerivedType10, TResult> matchFunc10, Func<TDerivedType11, TResult> matchFunc11, Func<TDerivedType12, TResult> matchFunc12, Func<TDerivedType13, TResult> matchFunc13, Func<TDerivedType14, TResult> matchFunc14, Func<TDerivedType15, TResult> matchFunc15, Func<TDerivedType16, TResult> matchFunc16, Func<TDerivedType17, TResult> matchFunc17, Func<TDerivedType18, TResult> matchFunc18, Func<TDerivedType19, TResult> matchFunc19, Func<TDerivedType20, TResult> matchFunc20, Func<TDerivedType21, TResult> matchFunc21, Func<TDerivedType22, TResult> matchFunc22, Func<TDerivedType23, TResult> matchFunc23, Func<TDerivedType24, TResult> matchFunc24, Func<TDerivedType25, TResult> matchFunc25, Func<TDerivedType26, TResult> matchFunc26, Func<TDerivedType27, TResult> matchFunc27, Func<TDerivedType28, TResult> matchFunc28, Func<TDerivedType29, TResult> matchFunc29, Func<TDerivedType30, TResult> matchFunc30, Func<TDerivedType31, TResult> matchFunc31, Func<TDerivedType32, TResult> matchFunc32, Func<TDerivedType33, TResult> matchFunc33, Func<TDerivedType34, TResult> matchFunc34, Func<TDerivedType35, TResult> matchFunc35, Func<TDerivedType36, TResult> matchFunc36, Func<TDerivedType37, TResult> matchFunc37, Func<TDerivedType38, TResult> matchFunc38, Func<TBaseType, TResult>? defaultFunc = null)
where TDerivedType1 : TBaseType where TDerivedType1 : TBaseType
where TDerivedType2 : TBaseType where TDerivedType2 : TBaseType
where TDerivedType3 : TBaseType where TDerivedType3 : TBaseType
...@@ -653,7 +664,7 @@ internal static partial class ObjectExtensions ...@@ -653,7 +664,7 @@ internal static partial class ObjectExtensions
} }
else else
{ {
return default; return default!;
} }
} }
} }
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Shared.Extensions namespace Microsoft.CodeAnalysis.Shared.Extensions
{ {
internal static partial class ObjectExtensions internal static partial class ObjectExtensions
{ {
public static string GetTypeDisplayName(this object obj) public static string GetTypeDisplayName(this object? obj)
{ {
return obj == null ? "null" : obj.GetType().Name; return obj == null ? "null" : obj.GetType().Name;
} }
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Diagnostics.CodeAnalysis;
namespace Microsoft.CodeAnalysis.Extensions namespace Microsoft.CodeAnalysis.Extensions
{ {
internal static class CommonParenthesizedExpressionSyntaxExtensions internal static class CommonParenthesizedExpressionSyntaxExtensions
...@@ -81,14 +85,14 @@ private static bool AnySymbolIsUserDefinedOperator(SymbolInfo symbolInfo) ...@@ -81,14 +85,14 @@ private static bool AnySymbolIsUserDefinedOperator(SymbolInfo symbolInfo)
return false; return false;
} }
private static bool IsUserDefinedOperator(ISymbol symbol) private static bool IsUserDefinedOperator([NotNullWhen(returnValue: true)] ISymbol? symbol)
=> symbol is IMethodSymbol methodSymbol && => symbol is IMethodSymbol methodSymbol &&
methodSymbol.MethodKind == MethodKind.UserDefinedOperator; methodSymbol.MethodKind == MethodKind.UserDefinedOperator;
private static bool IsFloatingPoint(TypeInfo typeInfo) private static bool IsFloatingPoint(TypeInfo typeInfo)
=> IsFloatingPoint(typeInfo.Type) || IsFloatingPoint(typeInfo.ConvertedType); => IsFloatingPoint(typeInfo.Type) || IsFloatingPoint(typeInfo.ConvertedType);
private static bool IsFloatingPoint(ITypeSymbol type) private static bool IsFloatingPoint([NotNullWhen(returnValue: true)] ITypeSymbol? type)
=> type?.SpecialType == SpecialType.System_Single || type?.SpecialType == SpecialType.System_Double; => type?.SpecialType == SpecialType.System_Single || type?.SpecialType == SpecialType.System_Double;
} }
} }
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using Microsoft.CodeAnalysis.LanguageServices; using Microsoft.CodeAnalysis.LanguageServices;
...@@ -32,8 +35,8 @@ public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, SyntaxT ...@@ -32,8 +35,8 @@ public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, SyntaxT
return semanticModel.GetSymbolInfo(token.Parent, cancellationToken); return semanticModel.GetSymbolInfo(token.Parent, cancellationToken);
} }
public static TSymbol GetEnclosingSymbol<TSymbol>(this SemanticModel semanticModel, int position, CancellationToken cancellationToken) public static TSymbol? GetEnclosingSymbol<TSymbol>(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
where TSymbol : ISymbol where TSymbol : class, ISymbol
{ {
for (var symbol = semanticModel.GetEnclosingSymbol(position, cancellationToken); for (var symbol = semanticModel.GetEnclosingSymbol(position, cancellationToken);
symbol != null; symbol != null;
...@@ -54,12 +57,12 @@ public static ISymbol GetEnclosingNamedTypeOrAssembly(this SemanticModel semanti ...@@ -54,12 +57,12 @@ public static ISymbol GetEnclosingNamedTypeOrAssembly(this SemanticModel semanti
(ISymbol)semanticModel.Compilation.Assembly; (ISymbol)semanticModel.Compilation.Assembly;
} }
public static INamedTypeSymbol GetEnclosingNamedType(this SemanticModel semanticModel, int position, CancellationToken cancellationToken) public static INamedTypeSymbol? GetEnclosingNamedType(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
{ {
return semanticModel.GetEnclosingSymbol<INamedTypeSymbol>(position, cancellationToken); return semanticModel.GetEnclosingSymbol<INamedTypeSymbol>(position, cancellationToken);
} }
public static INamespaceSymbol GetEnclosingNamespace(this SemanticModel semanticModel, int position, CancellationToken cancellationToken) public static INamespaceSymbol? GetEnclosingNamespace(this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
{ {
return semanticModel.GetEnclosingSymbol<INamespaceSymbol>(position, cancellationToken); return semanticModel.GetEnclosingSymbol<INamespaceSymbol>(position, cancellationToken);
} }
...@@ -84,7 +87,7 @@ public static INamespaceSymbol GetEnclosingNamespace(this SemanticModel semantic ...@@ -84,7 +87,7 @@ public static INamespaceSymbol GetEnclosingNamespace(this SemanticModel semantic
return symbolInfo.GetAnySymbol().ConvertToType(semanticModel.Compilation); return symbolInfo.GetAnySymbol().ConvertToType(semanticModel.Compilation);
} }
private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type) private static ISymbol? MapSymbol(ISymbol symbol, ITypeSymbol? type)
{ {
if (symbol.IsConstructor() && symbol.ContainingType.IsAnonymousType) if (symbol.IsConstructor() && symbol.ContainingType.IsAnonymousType)
{ {
...@@ -139,19 +142,19 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type) ...@@ -139,19 +142,19 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var languageServices = workspace.Services.GetLanguageServices(token.Language); var languageServices = workspace.Services.GetLanguageServices(token.Language);
var syntaxFacts = languageServices.GetService<ISyntaxFactsService>(); var syntaxFacts = languageServices.GetRequiredService<ISyntaxFactsService>();
if (!syntaxFacts.IsBindableToken(token)) if (!syntaxFacts.IsBindableToken(token))
{ {
return TokenSemanticInfo.Empty; return TokenSemanticInfo.Empty;
} }
var semanticFacts = languageServices.GetService<ISemanticFactsService>(); var semanticFacts = languageServices.GetRequiredService<ISemanticFactsService>();
IAliasSymbol aliasSymbol; IAliasSymbol? aliasSymbol;
ITypeSymbol type; ITypeSymbol? type;
ITypeSymbol convertedType; ITypeSymbol? convertedType;
ISymbol declaredSymbol; ISymbol? declaredSymbol;
ImmutableArray<ISymbol> allSymbols; ImmutableArray<ISymbol?> allSymbols;
var overriddingIdentifier = syntaxFacts.GetDeclarationIdentifierIfOverride(token); var overriddingIdentifier = syntaxFacts.GetDeclarationIdentifierIfOverride(token);
if (overriddingIdentifier.HasValue) if (overriddingIdentifier.HasValue)
...@@ -165,7 +168,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type) ...@@ -165,7 +168,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
type = null; type = null;
convertedType = null; convertedType = null;
declaredSymbol = null; declaredSymbol = null;
allSymbols = overriddenSymbol is null ? ImmutableArray<ISymbol>.Empty : ImmutableArray.Create(overriddenSymbol); allSymbols = overriddenSymbol is null ? ImmutableArray<ISymbol?>.Empty : ImmutableArray.Create<ISymbol?>(overriddenSymbol);
} }
else else
{ {
...@@ -178,7 +181,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type) ...@@ -178,7 +181,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
var skipSymbolInfoLookup = declaredSymbol.IsKind(SymbolKind.RangeVariable); var skipSymbolInfoLookup = declaredSymbol.IsKind(SymbolKind.RangeVariable);
allSymbols = skipSymbolInfoLookup allSymbols = skipSymbolInfoLookup
? ImmutableArray<ISymbol>.Empty ? ImmutableArray<ISymbol?>.Empty
: semanticFacts : semanticFacts
.GetBestOrAllSymbols(semanticModel, bindableParent, token, cancellationToken) .GetBestOrAllSymbols(semanticModel, bindableParent, token, cancellationToken)
.WhereAsArray(s => !s.Equals(declaredSymbol)) .WhereAsArray(s => !s.Equals(declaredSymbol))
...@@ -203,7 +206,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type) ...@@ -203,7 +206,7 @@ private static ISymbol MapSymbol(ISymbol symbol, ITypeSymbol type)
if (namedType.TypeKind == TypeKind.Delegate || if (namedType.TypeKind == TypeKind.Delegate ||
namedType.AssociatedSymbol != null) namedType.AssociatedSymbol != null)
{ {
allSymbols = ImmutableArray.Create<ISymbol>(type); allSymbols = ImmutableArray.Create<ISymbol?>(type);
type = null; type = null;
} }
} }
...@@ -232,7 +235,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic ...@@ -232,7 +235,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
} }
public static HashSet<ISymbol> GetAllDeclaredSymbols( public static HashSet<ISymbol> GetAllDeclaredSymbols(
this SemanticModel semanticModel, SyntaxNode container, CancellationToken cancellationToken, Func<SyntaxNode, bool> filter = null) this SemanticModel semanticModel, SyntaxNode? container, CancellationToken cancellationToken, Func<SyntaxNode, bool>? filter = null)
{ {
var symbols = new HashSet<ISymbol>(); var symbols = new HashSet<ISymbol>();
if (container != null) if (container != null)
...@@ -244,7 +247,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic ...@@ -244,7 +247,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
} }
public static IEnumerable<ISymbol> GetExistingSymbols( public static IEnumerable<ISymbol> GetExistingSymbols(
this SemanticModel semanticModel, SyntaxNode container, CancellationToken cancellationToken, Func<SyntaxNode, bool> descendInto = null) this SemanticModel semanticModel, SyntaxNode? container, CancellationToken cancellationToken, Func<SyntaxNode, bool>? descendInto = null)
{ {
// Ignore an anonymous type property or tuple field. It's ok if they have a name that // Ignore an anonymous type property or tuple field. It's ok if they have a name that
// matches the name of the local we're introducing. // matches the name of the local we're introducing.
...@@ -254,7 +257,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic ...@@ -254,7 +257,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
private static void GetAllDeclaredSymbols( private static void GetAllDeclaredSymbols(
SemanticModel semanticModel, SyntaxNode node, SemanticModel semanticModel, SyntaxNode node,
HashSet<ISymbol> symbols, CancellationToken cancellationToken, Func<SyntaxNode, bool> descendInto = null) HashSet<ISymbol> symbols, CancellationToken cancellationToken, Func<SyntaxNode, bool>? descendInto = null)
{ {
var symbol = semanticModel.GetDeclaredSymbol(node, cancellationToken); var symbol = semanticModel.GetDeclaredSymbol(node, cancellationToken);
...@@ -275,7 +278,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic ...@@ -275,7 +278,7 @@ public static SemanticModel GetOriginalSemanticModel(this SemanticModel semantic
} }
} }
static bool ShouldDescendInto(SyntaxNode node, Func<SyntaxNode, bool> filter) static bool ShouldDescendInto(SyntaxNode node, Func<SyntaxNode, bool>? filter)
=> filter != null ? filter(node) : true; => filter != null ? filter(node) : true;
} }
} }
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
...@@ -69,7 +71,7 @@ public static int ConvertTabToSpace(this string textSnippet, int tabSize, int in ...@@ -69,7 +71,7 @@ public static int ConvertTabToSpace(this string textSnippet, int tabSize, int in
return column - initialColumn; return column - initialColumn;
} }
public static int IndexOf(this string text, Func<char, bool> predicate) public static int IndexOf(this string? text, Func<char, bool> predicate)
{ {
if (text == null) if (text == null)
{ {
...@@ -215,9 +217,9 @@ public static int GetLineOffsetFromColumn(this string line, int column, int tabS ...@@ -215,9 +217,9 @@ public static int GetLineOffsetFromColumn(this string line, int column, int tabS
return line.Length; return line.Length;
} }
public static void AppendToAliasNameSet(this string alias, ImmutableHashSet<string>.Builder builder) public static void AppendToAliasNameSet(this string? alias, ImmutableHashSet<string>.Builder builder)
{ {
if (string.IsNullOrWhiteSpace(alias)) if (RoslynString.IsNullOrWhiteSpace(alias))
{ {
return; return;
} }
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -19,7 +22,7 @@ public static IEnumerable<SyntaxNodeOrToken> DepthFirstTraversal(this SyntaxNode ...@@ -19,7 +22,7 @@ public static IEnumerable<SyntaxNodeOrToken> DepthFirstTraversal(this SyntaxNode
public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxNode node) public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxNode node)
{ {
var current = node.Parent; SyntaxNode? current = node.Parent;
while (current != null) while (current != null)
{ {
...@@ -32,7 +35,7 @@ public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxNode node) ...@@ -32,7 +35,7 @@ public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxNode node)
public static IEnumerable<TNode> GetAncestors<TNode>(this SyntaxNode node) public static IEnumerable<TNode> GetAncestors<TNode>(this SyntaxNode node)
where TNode : SyntaxNode where TNode : SyntaxNode
{ {
var current = node.Parent; SyntaxNode? current = node.Parent;
while (current != null) while (current != null)
{ {
if (current is TNode tNode) if (current is TNode tNode)
...@@ -44,10 +47,10 @@ public static IEnumerable<TNode> GetAncestors<TNode>(this SyntaxNode node) ...@@ -44,10 +47,10 @@ public static IEnumerable<TNode> GetAncestors<TNode>(this SyntaxNode node)
} }
} }
public static TNode GetAncestor<TNode>(this SyntaxNode node) public static TNode? GetAncestor<TNode>(this SyntaxNode node)
where TNode : SyntaxNode where TNode : SyntaxNode
{ {
var current = node.Parent; SyntaxNode? current = node.Parent;
while (current != null) while (current != null)
{ {
if (current is TNode tNode) if (current is TNode tNode)
...@@ -61,13 +64,13 @@ public static TNode GetAncestor<TNode>(this SyntaxNode node) ...@@ -61,13 +64,13 @@ public static TNode GetAncestor<TNode>(this SyntaxNode node)
return null; return null;
} }
public static TNode GetAncestorOrThis<TNode>(this SyntaxNode node) public static TNode? GetAncestorOrThis<TNode>(this SyntaxNode? node)
where TNode : SyntaxNode where TNode : SyntaxNode
{ {
return node?.GetAncestorsOrThis<TNode>().FirstOrDefault(); return node?.GetAncestorsOrThis<TNode>().FirstOrDefault();
} }
public static IEnumerable<TNode> GetAncestorsOrThis<TNode>(this SyntaxNode node) public static IEnumerable<TNode> GetAncestorsOrThis<TNode>(this SyntaxNode? node)
where TNode : SyntaxNode where TNode : SyntaxNode
{ {
var current = node; var current = node;
...@@ -114,7 +117,7 @@ public static bool HasAncestor<TNode>(this SyntaxNode node) ...@@ -114,7 +117,7 @@ public static bool HasAncestor<TNode>(this SyntaxNode node)
} }
} }
public static bool CheckParent<T>(this SyntaxNode node, Func<T, bool> valueChecker) where T : SyntaxNode public static bool CheckParent<T>([NotNullWhen(returnValue: true)] this SyntaxNode? node, Func<T, bool> valueChecker) where T : SyntaxNode
{ {
if (!(node?.Parent is T parentNode)) if (!(node?.Parent is T parentNode))
{ {
...@@ -184,11 +187,11 @@ public static int FullWidth(this SyntaxNode node) ...@@ -184,11 +187,11 @@ public static int FullWidth(this SyntaxNode node)
return node.FullSpan.Length; return node.FullSpan.Length;
} }
public static SyntaxNode FindInnermostCommonNode( public static SyntaxNode? FindInnermostCommonNode(
this IEnumerable<SyntaxNode> nodes, this IEnumerable<SyntaxNode> nodes,
Func<SyntaxNode, bool> predicate) Func<SyntaxNode, bool> predicate)
{ {
IEnumerable<SyntaxNode> blocks = null; IEnumerable<SyntaxNode>? blocks = null;
foreach (var node in nodes) foreach (var node in nodes)
{ {
blocks = blocks == null blocks = blocks == null
...@@ -199,10 +202,10 @@ public static int FullWidth(this SyntaxNode node) ...@@ -199,10 +202,10 @@ public static int FullWidth(this SyntaxNode node)
return blocks?.First(); return blocks?.First();
} }
public static TSyntaxNode FindInnermostCommonNode<TSyntaxNode>(this IEnumerable<SyntaxNode> nodes) public static TSyntaxNode? FindInnermostCommonNode<TSyntaxNode>(this IEnumerable<SyntaxNode> nodes)
where TSyntaxNode : SyntaxNode where TSyntaxNode : SyntaxNode
{ {
return (TSyntaxNode)nodes.FindInnermostCommonNode(n => n is TSyntaxNode); return (TSyntaxNode?)nodes.FindInnermostCommonNode(n => n is TSyntaxNode);
} }
/// <summary> /// <summary>
...@@ -250,9 +253,9 @@ public static TextSpan GetContainedSpan(this IEnumerable<SyntaxNode> nodes) ...@@ -250,9 +253,9 @@ public static TextSpan GetContainedSpan(this IEnumerable<SyntaxNode> nodes)
} }
public static IEnumerable<TextSpan> GetContiguousSpans( public static IEnumerable<TextSpan> GetContiguousSpans(
this IEnumerable<SyntaxNode> nodes, Func<SyntaxNode, SyntaxToken> getLastToken = null) this IEnumerable<SyntaxNode> nodes, Func<SyntaxNode, SyntaxToken>? getLastToken = null)
{ {
SyntaxNode lastNode = null; SyntaxNode? lastNode = null;
TextSpan? textSpan = null; TextSpan? textSpan = null;
// Sort the nodes in source location order. // Sort the nodes in source location order.
...@@ -268,12 +271,12 @@ public static TextSpan GetContainedSpan(this IEnumerable<SyntaxNode> nodes) ...@@ -268,12 +271,12 @@ public static TextSpan GetContainedSpan(this IEnumerable<SyntaxNode> nodes)
if (lastToken.GetNextToken(includeDirectives: true) == node.GetFirstToken()) if (lastToken.GetNextToken(includeDirectives: true) == node.GetFirstToken())
{ {
// Expand the span // Expand the span
textSpan = TextSpan.FromBounds(textSpan.Value.Start, node.Span.End); textSpan = TextSpan.FromBounds(textSpan!.Value.Start, node.Span.End);
} }
else else
{ {
// Return the last span, and start a new one // Return the last span, and start a new one
yield return textSpan.Value; yield return textSpan!.Value;
textSpan = node.Span; textSpan = node.Span;
} }
} }
...@@ -370,12 +373,12 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod ...@@ -370,12 +373,12 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
public static async Task<TRoot> ReplaceSyntaxAsync<TRoot>( public static async Task<TRoot> ReplaceSyntaxAsync<TRoot>(
this TRoot root, this TRoot root,
IEnumerable<SyntaxNode> nodes, IEnumerable<SyntaxNode>? nodes,
Func<SyntaxNode, SyntaxNode, CancellationToken, Task<SyntaxNode>> computeReplacementNodeAsync, Func<SyntaxNode, SyntaxNode, CancellationToken, Task<SyntaxNode>>? computeReplacementNodeAsync,
IEnumerable<SyntaxToken> tokens, IEnumerable<SyntaxToken>? tokens,
Func<SyntaxToken, SyntaxToken, CancellationToken, Task<SyntaxToken>> computeReplacementTokenAsync, Func<SyntaxToken, SyntaxToken, CancellationToken, Task<SyntaxToken>>? computeReplacementTokenAsync,
IEnumerable<SyntaxTrivia> trivia, IEnumerable<SyntaxTrivia>? trivia,
Func<SyntaxTrivia, SyntaxTrivia, CancellationToken, Task<SyntaxTrivia>> computeReplacementTriviaAsync, Func<SyntaxTrivia, SyntaxTrivia, CancellationToken, Task<SyntaxTrivia>>? computeReplacementTriviaAsync,
CancellationToken cancellationToken) CancellationToken cancellationToken)
where TRoot : SyntaxNode where TRoot : SyntaxNode
{ {
...@@ -423,7 +426,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod ...@@ -423,7 +426,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
if (nodesToReplace.TryGetValue(span, out var currentNode)) if (nodesToReplace.TryGetValue(span, out var currentNode))
{ {
var original = (SyntaxNode)retryAnnotations.GetAnnotations(currentNode).SingleOrDefault() ?? currentNode; var original = (SyntaxNode)retryAnnotations.GetAnnotations(currentNode).SingleOrDefault() ?? currentNode;
var newNode = await computeReplacementNodeAsync(original, currentNode, cancellationToken).ConfigureAwait(false); var newNode = await computeReplacementNodeAsync!(original, currentNode, cancellationToken).ConfigureAwait(false);
nodeReplacements[currentNode] = newNode; nodeReplacements[currentNode] = newNode;
} }
else if (tokensToReplace.TryGetValue(span, out var currentToken)) else if (tokensToReplace.TryGetValue(span, out var currentToken))
...@@ -434,7 +437,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod ...@@ -434,7 +437,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
original = currentToken; original = currentToken;
} }
var newToken = await computeReplacementTokenAsync(original, currentToken, cancellationToken).ConfigureAwait(false); var newToken = await computeReplacementTokenAsync!(original, currentToken, cancellationToken).ConfigureAwait(false);
tokenReplacements[currentToken] = newToken; tokenReplacements[currentToken] = newToken;
} }
else if (triviaToReplace.TryGetValue(span, out var currentTrivia)) else if (triviaToReplace.TryGetValue(span, out var currentTrivia))
...@@ -445,7 +448,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod ...@@ -445,7 +448,7 @@ public static bool OverlapsHiddenPosition(this SyntaxNode declaration, SyntaxNod
original = currentTrivia; original = currentTrivia;
} }
var newTrivia = await computeReplacementTriviaAsync(original, currentTrivia, cancellationToken).ConfigureAwait(false); var newTrivia = await computeReplacementTriviaAsync!(original, currentTrivia, cancellationToken).ConfigureAwait(false);
triviaReplacements[currentTrivia] = newTrivia; triviaReplacements[currentTrivia] = newTrivia;
} }
} }
...@@ -756,7 +759,7 @@ private static SyntaxToken FindSkippedTokenBackward(SyntaxTriviaList triviaList, ...@@ -756,7 +759,7 @@ private static SyntaxToken FindSkippedTokenBackward(SyntaxTriviaList triviaList,
} }
// Copy of the same function in SyntaxNode.cs // Copy of the same function in SyntaxNode.cs
public static SyntaxNode GetParent(this SyntaxNode node, bool ascendOutOfTrivia) public static SyntaxNode? GetParent(this SyntaxNode node, bool ascendOutOfTrivia)
{ {
var parent = node.Parent; var parent = node.Parent;
if (parent == null && ascendOutOfTrivia) if (parent == null && ascendOutOfTrivia)
...@@ -770,7 +773,7 @@ public static SyntaxNode GetParent(this SyntaxNode node, bool ascendOutOfTrivia) ...@@ -770,7 +773,7 @@ public static SyntaxNode GetParent(this SyntaxNode node, bool ascendOutOfTrivia)
return parent; return parent;
} }
public static TNode FirstAncestorOrSelfUntil<TNode>(this SyntaxNode node, Func<SyntaxNode, bool> predicate) public static TNode? FirstAncestorOrSelfUntil<TNode>(this SyntaxNode? node, Func<SyntaxNode, bool> predicate)
where TNode : SyntaxNode where TNode : SyntaxNode
{ {
for (var current = node; current != null; current = current.GetParent(ascendOutOfTrivia: true)) for (var current = node; current != null; current = current.GetParent(ascendOutOfTrivia: true))
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -10,12 +12,12 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions ...@@ -10,12 +12,12 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{ {
internal static class SyntaxTokenExtensions internal static class SyntaxTokenExtensions
{ {
public static SyntaxNode GetAncestor(this SyntaxToken token, Func<SyntaxNode, bool> predicate) public static SyntaxNode? GetAncestor(this SyntaxToken token, Func<SyntaxNode, bool>? predicate)
{ {
return token.GetAncestor<SyntaxNode>(predicate); return token.GetAncestor<SyntaxNode>(predicate);
} }
public static T GetAncestor<T>(this SyntaxToken token, Func<T, bool> predicate = null) public static T? GetAncestor<T>(this SyntaxToken token, Func<T, bool>? predicate = null)
where T : SyntaxNode where T : SyntaxNode
{ {
return token.Parent != null return token.Parent != null
...@@ -38,7 +40,7 @@ public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxToken token, Func< ...@@ -38,7 +40,7 @@ public static IEnumerable<SyntaxNode> GetAncestors(this SyntaxToken token, Func<
: SpecializedCollections.EmptyEnumerable<SyntaxNode>(); : SpecializedCollections.EmptyEnumerable<SyntaxNode>();
} }
public static SyntaxNode GetCommonRoot(this SyntaxToken token1, SyntaxToken token2) public static SyntaxNode? GetCommonRoot(this SyntaxToken token1, SyntaxToken token2)
{ {
Contract.ThrowIfTrue(token1.RawKind == 0 || token2.RawKind == 0); Contract.ThrowIfTrue(token1.RawKind == 0 || token2.RawKind == 0);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Text; using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities; using Roslyn.Utilities;
...@@ -34,7 +36,7 @@ public static TextDocument WithText(this TextDocument textDocument, SourceText t ...@@ -34,7 +36,7 @@ public static TextDocument WithText(this TextDocument textDocument, SourceText t
public static TextDocument WithAdditionalDocumentText(this TextDocument textDocument, SourceText text) public static TextDocument WithAdditionalDocumentText(this TextDocument textDocument, SourceText text)
{ {
Contract.ThrowIfFalse(textDocument is AdditionalDocument); Contract.ThrowIfFalse(textDocument is AdditionalDocument);
return textDocument.Project.Solution.WithAdditionalDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id); return textDocument.Project.Solution.WithAdditionalDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id)!;
} }
/// <summary> /// <summary>
...@@ -43,7 +45,7 @@ public static TextDocument WithAdditionalDocumentText(this TextDocument textDocu ...@@ -43,7 +45,7 @@ public static TextDocument WithAdditionalDocumentText(this TextDocument textDocu
public static TextDocument WithAnalyzerConfigDocumentText(this TextDocument textDocument, SourceText text) public static TextDocument WithAnalyzerConfigDocumentText(this TextDocument textDocument, SourceText text)
{ {
Contract.ThrowIfFalse(textDocument is AnalyzerConfigDocument); Contract.ThrowIfFalse(textDocument is AnalyzerConfigDocument);
return textDocument.Project.Solution.WithAnalyzerConfigDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id); return textDocument.Project.Solution.WithAnalyzerConfigDocumentText(textDocument.Id, text, PreservationMode.PreserveIdentity).GetTextDocument(textDocument.Id)!;
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册