提交 9e64998d 编写于 作者: S Sam Harwell

Use SpecializedTasks where appropriate

上级 bbd26ab8
......@@ -96,7 +96,7 @@ public bool TryGetSpan(ActiveStatementId id, SourceText source, out TextSpan spa
}
public Task<ImmutableArray<ActiveStatementTrackingSpan>> GetLatestSpansAsync(Document document, ITextSnapshot snapshot, CancellationToken cancellationToken)
=> _session?.GetLatestSpansAsync(document, snapshot, cancellationToken) ?? Task.FromResult(ImmutableArray<ActiveStatementTrackingSpan>.Empty);
=> _session?.GetLatestSpansAsync(document, snapshot, cancellationToken) ?? SpecializedTasks.EmptyImmutableArray<ActiveStatementTrackingSpan>();
// internal for testing
internal sealed class TrackingSession
......
......@@ -10,6 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Remote;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.SymbolSearch
{
......@@ -53,13 +54,13 @@ public static ISymbolSearchUpdateEngine CreateEngineInProcess(ISymbolSearchLogSe
private sealed class NoOpUpdateEngine : ISymbolSearchUpdateEngine
{
public Task<ImmutableArray<PackageWithAssemblyResult>> FindPackagesWithAssemblyAsync(string source, string assemblyName, CancellationToken cancellationToken)
=> Task.FromResult(ImmutableArray<PackageWithAssemblyResult>.Empty);
=> SpecializedTasks.EmptyImmutableArray<PackageWithAssemblyResult>();
public Task<ImmutableArray<PackageWithTypeResult>> FindPackagesWithTypeAsync(string source, string name, int arity, CancellationToken cancellationToken)
=> Task.FromResult(ImmutableArray<PackageWithTypeResult>.Empty);
=> SpecializedTasks.EmptyImmutableArray<PackageWithTypeResult>();
public Task<ImmutableArray<ReferenceAssemblyWithTypeResult>> FindReferenceAssembliesWithTypeAsync(string name, int arity, CancellationToken cancellationToken)
=> Task.FromResult(ImmutableArray<ReferenceAssemblyWithTypeResult>.Empty);
=> SpecializedTasks.EmptyImmutableArray<ReferenceAssemblyWithTypeResult>();
public Task UpdateContinuouslyAsync(string sourceName, string localSettingsDirectory)
=> Task.CompletedTask;
......
......@@ -32,7 +32,7 @@ public Task<IEnumerable<RecommendedKeyword>> RecommendKeywordsAsync(int position
return Task.FromResult(SpecializedCollections.SingletonEnumerable(new RecommendedKeyword("dynamic")));
}
return Task.FromResult<IEnumerable<RecommendedKeyword>>(null);
return SpecializedTasks.Null<IEnumerable<RecommendedKeyword>>();
}
protected static bool IsDynamicTypeContext(
......
......@@ -20,7 +20,7 @@ public Task<IEnumerable<RecommendedKeyword>> RecommendKeywordsAsync(int position
return Task.FromResult(SpecializedCollections.SingletonEnumerable(new RecommendedKeyword("notnull")));
}
return Task.FromResult<IEnumerable<RecommendedKeyword>>(null);
return SpecializedTasks.Null<IEnumerable<RecommendedKeyword>>();
}
}
}
......@@ -20,7 +20,7 @@ public Task<IEnumerable<RecommendedKeyword>> RecommendKeywordsAsync(int position
return Task.FromResult(SpecializedCollections.SingletonEnumerable(new RecommendedKeyword("unmanaged")));
}
return Task.FromResult<IEnumerable<RecommendedKeyword>>(null);
return SpecializedTasks.Null<IEnumerable<RecommendedKeyword>>();
}
}
}
......@@ -37,7 +37,7 @@ public Task<IEnumerable<RecommendedKeyword>> RecommendKeywordsAsync(int position
return Task.FromResult(SpecializedCollections.SingletonEnumerable(new RecommendedKeyword("var")));
}
return Task.FromResult<IEnumerable<RecommendedKeyword>>(null);
return SpecializedTasks.Null<IEnumerable<RecommendedKeyword>>();
}
}
}
......@@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Snippets;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Completion
{
......@@ -92,7 +93,7 @@ public override async Task<CompletionChange> GetChangeAsync(Document document, C
=> GetTextChangeAsync(selectedItem, ch, cancellationToken);
protected virtual Task<TextChange?> GetTextChangeAsync(CompletionItem selectedItem, char? ch, CancellationToken cancellationToken)
=> Task.FromResult<TextChange?>(null);
=> SpecializedTasks.Default<TextChange?>();
private static readonly CompletionItemRules s_suggestionItemRules = CompletionItemRules.Create(enterKeyRule: EnterKeyRule.Never);
......
......@@ -33,7 +33,7 @@ public override Task<ImmutableArray<Diagnostic>> AnalyzeSyntaxAsync(Document doc
public override Task<ImmutableArray<Diagnostic>> AnalyzeSemanticsAsync(Document document, CancellationToken cancellationToken)
{
var encService = document.Project.Solution.Workspace.Services.GetService<IEditAndContinueWorkspaceService>();
return (encService != null) ? encService.GetDocumentDiagnosticsAsync(document, cancellationToken) : Task.FromResult(ImmutableArray<Diagnostic>.Empty);
return (encService != null) ? encService.GetDocumentDiagnosticsAsync(document, cancellationToken) : SpecializedTasks.EmptyImmutableArray<Diagnostic>();
}
}
}
......@@ -20,7 +20,7 @@ public Task<ImmutableArray<DiagnosticData>> GetSpecificCachedDiagnosticsAsync(So
{
if (!(id is LiveDiagnosticUpdateArgsId argsId))
{
return Task.FromResult(ImmutableArray<DiagnosticData>.Empty);
return SpecializedTasks.EmptyImmutableArray<DiagnosticData>();
}
var (documentId, projectId) = (argsId.ProjectOrDocumentId is DocumentId docId) ? (docId, docId.ProjectId) : (null, (ProjectId)argsId.ProjectOrDocumentId);
......
......@@ -365,7 +365,7 @@ public Task<bool> HasChangesAsync(Solution solution, string? sourceFilePath, Can
var editSession = _editSession;
if (editSession == null)
{
return Task.FromResult(false);
return SpecializedTasks.False;
}
return editSession.HasChangesAsync(solution, sourceFilePath, cancellationToken);
......
......@@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript
{
......@@ -21,7 +22,7 @@ public override Task<ImmutableArray<Diagnostic>> AnalyzeSyntaxAsync(Document doc
var analyzer = document.Project.LanguageServices.GetRequiredService<VSTypeScriptDiagnosticAnalyzerLanguageService>().Implementation;
if (analyzer == null)
{
return Task.FromResult(ImmutableArray<Diagnostic>.Empty);
return SpecializedTasks.EmptyImmutableArray<Diagnostic>();
}
return analyzer.AnalyzeDocumentSyntaxAsync(document, cancellationToken);
......@@ -32,7 +33,7 @@ public override Task<ImmutableArray<Diagnostic>> AnalyzeSemanticsAsync(Document
var analyzer = document.Project.LanguageServices.GetRequiredService<VSTypeScriptDiagnosticAnalyzerLanguageService>().Implementation;
if (analyzer == null)
{
return Task.FromResult(ImmutableArray<Diagnostic>.Empty);
return SpecializedTasks.EmptyImmutableArray<Diagnostic>();
}
return analyzer.AnalyzeDocumentSemanticsAsync(document, cancellationToken);
......
......@@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript
{
......@@ -21,7 +22,7 @@ public override Task<ImmutableArray<Diagnostic>> AnalyzeProjectAsync(Project pro
var analyzer = project.LanguageServices.GetRequiredService<VSTypeScriptDiagnosticAnalyzerLanguageService>().Implementation;
if (analyzer == null)
{
return Task.FromResult(ImmutableArray<Diagnostic>.Empty);
return SpecializedTasks.EmptyImmutableArray<Diagnostic>();
}
return analyzer.AnalyzeProjectAsync(project, cancellationToken);
......
......@@ -51,7 +51,7 @@ internal abstract partial class AbstractInitializeMemberFromParameterCodeRefacto
Document document, SyntaxNode functionDeclaration, IMethodSymbol method, IBlockOperation? blockStatementOpt,
ImmutableArray<SyntaxNode> listOfParameterNodes, TextSpan parameterSpan, CancellationToken cancellationToken)
{
return Task.FromResult(ImmutableArray<CodeAction>.Empty);
return SpecializedTasks.EmptyImmutableArray<CodeAction>();
}
protected override async Task<ImmutableArray<CodeAction>> GetRefactoringsForSingleParameterAsync(
......
......@@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Utilities;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.SplitOrMergeIfStatements
{
......@@ -52,7 +53,7 @@ protected sealed override CodeAction CreateCodeAction(Func<CancellationToken, Ta
var ifGenerator = document.GetLanguageService<IIfLikeStatementGenerator>();
if (CanBeMergedWithParent(syntaxFacts, ifGenerator, ifOrElseIf, out firstIfOrElseIf))
return Task.FromResult(true);
return SpecializedTasks.True;
return CanBeMergedWithPreviousStatementAsync(document, syntaxFacts, ifGenerator, ifOrElseIf, cancellationToken, out firstIfOrElseIf);
}
......@@ -64,7 +65,7 @@ protected sealed override CodeAction CreateCodeAction(Func<CancellationToken, Ta
var ifGenerator = document.GetLanguageService<IIfLikeStatementGenerator>();
if (CanBeMergedWithElseIf(syntaxFacts, ifGenerator, ifOrElseIf, out secondIfOrElseIf))
return Task.FromResult(true);
return SpecializedTasks.True;
return CanBeMergedWithNextStatementAsync(document, syntaxFacts, ifGenerator, ifOrElseIf, cancellationToken, out secondIfOrElseIf);
}
......@@ -154,7 +155,7 @@ protected sealed override SyntaxNode GetChangedRoot(Document document, SyntaxNod
{
return TryGetSiblingStatement(syntaxFacts, ifOrElseIf, relativeIndex: -1, out previousStatement)
? CanStatementsBeMergedAsync(document, syntaxFacts, ifGenerator, previousStatement, ifOrElseIf, cancellationToken)
: Task.FromResult(false);
: SpecializedTasks.False;
}
private static Task<bool> CanBeMergedWithNextStatementAsync(
......@@ -167,7 +168,7 @@ protected sealed override SyntaxNode GetChangedRoot(Document document, SyntaxNod
{
return TryGetSiblingStatement(syntaxFacts, ifOrElseIf, relativeIndex: 1, out nextStatement)
? CanStatementsBeMergedAsync(document, syntaxFacts, ifGenerator, ifOrElseIf, nextStatement, cancellationToken)
: Task.FromResult(false);
: SpecializedTasks.False;
}
private static async Task<bool> CanStatementsBeMergedAsync(
......
......@@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Utilities;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.SplitOrMergeIfStatements
{
......@@ -40,7 +41,7 @@ protected sealed override CodeAction CreateCodeAction(Func<CancellationToken, Ta
var ifGenerator = document.GetLanguageService<IIfLikeStatementGenerator>();
if (!IsFirstStatementOfIfOrElseIf(syntaxFacts, ifGenerator, ifOrElseIf, out outerIfOrElseIf))
return Task.FromResult(false);
return SpecializedTasks.False;
return CanBeMergedAsync(document, syntaxFacts, ifGenerator, outerIfOrElseIf, ifOrElseIf, cancellationToken);
}
......@@ -52,7 +53,7 @@ protected sealed override CodeAction CreateCodeAction(Func<CancellationToken, Ta
var ifGenerator = document.GetLanguageService<IIfLikeStatementGenerator>();
if (!IsFirstStatementIfStatement(syntaxFacts, ifGenerator, ifOrElseIf, out innerIfStatement))
return Task.FromResult(false);
return SpecializedTasks.False;
return CanBeMergedAsync(document, syntaxFacts, ifGenerator, ifOrElseIf, innerIfStatement, cancellationToken);
}
......
......@@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Roslyn.Utilities;
using static Microsoft.CodeAnalysis.CodeActions.CodeAction;
namespace Microsoft.CodeAnalysis.UpgradeProject
......@@ -100,6 +101,6 @@ public ProjectOptionsChangeAction(string title, Func<CancellationToken, Task<Sol
}
protected override Task<IEnumerable<CodeActionOperation>> ComputePreviewOperationsAsync(CancellationToken cancellationToken)
=> Task.FromResult(Enumerable.Empty<CodeActionOperation>());
=> SpecializedTasks.EmptyEnumerable<CodeActionOperation>();
}
}
......@@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.LanguageServer.Handler
{
......@@ -32,7 +33,7 @@ public GetTextDocumentWithContextHandler(ILspSolutionProvider solutionProvider)
if (!documents.Any())
{
return Task.FromResult<ActiveProjectContexts?>(null);
return SpecializedTasks.Null<ActiveProjectContexts>();
}
var contexts = new List<ProjectContext>();
......
......@@ -56,12 +56,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Async
Select Case diagnostic.Id
Case BC30311
If Not DoesExpressionReturnGenericTaskWhoseArgumentsMatchLeftSide(expression, semanticModel, document.Project, cancellationToken) Then
Return Task.FromResult(Of SyntaxNode)(Nothing)
Return SpecializedTasks.Null(Of SyntaxNode)()
End If
Return Task.FromResult(root.ReplaceNode(oldNode, ConverToAwaitExpression(expression)))
Case BC37055
If Not DoesExpressionReturnTask(expression, semanticModel) Then
Return Task.FromResult(Of SyntaxNode)(Nothing)
Return SpecializedTasks.Null(Of SyntaxNode)()
End If
Return Task.FromResult(root.ReplaceNode(oldNode, ConverToAwaitExpression(expression)))
Case BC42358
......
......@@ -35,7 +35,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Iterator
Protected Overrides Function GetCodeFixAsync(root As SyntaxNode, node As SyntaxNode, document As Document, diagnostics As Diagnostic, cancellationToken As CancellationToken) As Task(Of CodeAction)
If Not node.IsKind(SyntaxKind.ReturnStatement) Then
Return Task.FromResult(Of CodeAction)(Nothing)
Return SpecializedTasks.Null(Of CodeAction)()
End If
Dim returnStatement = TryCast(node, ReturnStatementSyntax)
......
......@@ -58,7 +58,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ChangeNamespace
' TODO: Implement the service for VB
Protected Overrides Function GetValidContainersFromAllLinkedDocumentsAsync(document As Document, container As SyntaxNode, cancellationToken As CancellationToken) As Task(Of ImmutableArray(Of (DocumentId, SyntaxNode)))
Return Task.FromResult(CType(Nothing, ImmutableArray(Of (DocumentId, SyntaxNode))))
Return SpecializedTasks.Default(Of ImmutableArray(Of (DocumentId, SyntaxNode)))()
End Function
' This is only reachable when called from a VB service, which is not implemented yet.
......
......@@ -93,7 +93,7 @@ public Task GetAssetsAsync(int scopeId, Checksum[] checksums, string pipeName, C
/// Remote API.
/// </summary>
public Task<bool> IsExperimentEnabledAsync(string experimentName, CancellationToken cancellationToken)
=> Task.FromResult(_services.GetRequiredService<IExperimentationService>().IsExperimentEnabled(experimentName));
=> _services.GetRequiredService<IExperimentationService>().IsExperimentEnabled(experimentName) ? SpecializedTasks.True : SpecializedTasks.False;
public RemoteHostTestData TestData => _inprocServices.TestData;
......
......@@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Test.Utilities
{
......@@ -25,7 +26,7 @@ public TestDynamicFileInfoProviderThatProducesNoFiles()
event EventHandler<string> IDynamicFileInfoProvider.Updated { add { } remove { } }
public Task<DynamicFileInfo> GetDynamicFileInfoAsync(ProjectId projectId, string projectFilePath, string filePath, CancellationToken cancellationToken)
=> Task.FromResult<DynamicFileInfo>(null);
=> SpecializedTasks.Null<DynamicFileInfo>();
public Task RemoveDynamicFileInfoAsync(ProjectId projectId, string projectFilePath, string filePath, CancellationToken cancellationToken)
=> Task.CompletedTask;
......
......@@ -214,7 +214,9 @@ public Task<bool> IsExperimentEnabledAsync(string experimentName, CancellationTo
{
try
{
return Task.FromResult(_services.GetRequiredService<IExperimentationService>().IsExperimentEnabled(experimentName));
return _services.GetRequiredService<IExperimentationService>().IsExperimentEnabled(experimentName)
? SpecializedTasks.True
: SpecializedTasks.False;
}
catch (Exception ex) when (FatalError.ReportWithoutCrashUnlessCanceledAndPropagate(ex, cancellationToken))
{
......
......@@ -5,6 +5,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeActions
{
......@@ -22,7 +23,7 @@ public SimpleCodeAction(string title, string equivalenceKey)
public sealed override string EquivalenceKey { get; }
protected override Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
=> Task.FromResult<Document>(null);
=> SpecializedTasks.Null<Document>();
}
internal class DocumentChangeAction : SimpleCodeAction
......
......@@ -63,7 +63,7 @@ public override Task<IEnumerable<Diagnostic>> GetDocumentDiagnosticsAsync(Docume
return Task.FromResult<IEnumerable<Diagnostic>>(diagnostics);
}
return Task.FromResult(SpecializedCollections.EmptyEnumerable<Diagnostic>());
return SpecializedTasks.EmptyEnumerable<Diagnostic>();
}
public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project project, CancellationToken cancellationToken)
......@@ -73,7 +73,7 @@ public override Task<IEnumerable<Diagnostic>> GetProjectDiagnosticsAsync(Project
return Task.FromResult<IEnumerable<Diagnostic>>(diagnostics);
}
return Task.FromResult(SpecializedCollections.EmptyEnumerable<Diagnostic>());
return SpecializedTasks.EmptyEnumerable<Diagnostic>();
}
}
}
......
......@@ -29,7 +29,7 @@ Namespace Microsoft.CodeAnalysis.FindSymbols
Return DetermineCascadedSymbolsAsync(
DirectCast(symbol, INamedTypeSymbol), project, cancellationToken)
Else
Return Task.FromResult(ImmutableArray(Of ISymbol).Empty)
Return SpecializedTasks.EmptyImmutableArray(Of ISymbol)()
End If
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册