Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async override Task<CodeAction> ResolveAsync(
throw new ArgumentNullException(nameof(codeAction));
}

var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(csharpParams.RazorFileUri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileUri);
if (documentContext is null)
{
return codeAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async override Task<CodeAction> ResolveAsync(

cancellationToken.ThrowIfCancellationRequested();

var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(csharpParams.RazorFileUri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreateForOpenDocument(csharpParams.RazorFileUri);
if (documentContext is null)
{
return codeAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async override Task<CodeAction> ResolveAsync(
throw new ArgumentNullException(nameof(codeAction));
}

var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(resolveParams.RazorFileUri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreateForOpenDocument(resolveParams.RazorFileUri);
if (documentContext is null)
{
return codeAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AddUsingsCodeActionResolver(DocumentContextFactory documentContextFactory
return null;
}

var documentContext = await _documentContextFactory.TryCreateAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
if (documentContext is null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CreateComponentCodeActionResolver(DocumentContextFactory documentContextF
return null;
}

var documentContext = await _documentContextFactory.TryCreateAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
if (documentContext is null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ExtractToCodeBehindCodeActionResolver(

var path = FilePathNormalizer.Normalize(actionParams.Uri.GetAbsoluteOrUNCPath());

var documentContext = await _documentContextFactory.TryCreateAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreate(actionParams.Uri);
if (documentContext is null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public GenerateMethodCodeActionResolver(DocumentContextFactory documentContextFa
return null;
}

var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(actionParams.Uri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreateForOpenDocument(actionParams.Uri);
if (documentContext is null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private async Task<VSInternalCompletionItem> PostProcessCompletionItemAsync(
}

var identifier = context.OriginalRequestParams.Identifier.TextDocumentIdentifier;
var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(identifier, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreateForOpenDocument(identifier);
if (documentContext is null)
{
return resolvedCompletionItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
Expand All @@ -15,52 +13,24 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal class DefaultDocumentContextFactory : DocumentContextFactory
{
private readonly ProjectSnapshotManagerDispatcher _projectSnapshotManagerDispatcher;
private readonly ISnapshotResolver _snapshotResolver;
private readonly DocumentVersionCache _documentVersionCache;
private readonly ILogger<DefaultDocumentContextFactory> _logger;

public DefaultDocumentContextFactory(
ProjectSnapshotManagerDispatcher projectSnapshotManagerDispatcher,
ISnapshotResolver snapshotResolver,
DocumentVersionCache documentVersionCache,
ILoggerFactory loggerFactory)
{
_projectSnapshotManagerDispatcher = projectSnapshotManagerDispatcher;
_snapshotResolver = snapshotResolver;
_documentVersionCache = documentVersionCache;
_logger = loggerFactory.CreateLogger<DefaultDocumentContextFactory>();
}

protected override async Task<DocumentContext?> TryCreateCoreAsync(Uri documentUri, VSProjectContext? projectContext, bool versioned, CancellationToken cancellationToken)
protected override DocumentContext? TryCreateCore(Uri documentUri, VSProjectContext? projectContext, bool versioned)
{
var filePath = documentUri.GetAbsoluteOrUNCPath();

var documentAndVersion = await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(() =>
{
// TODO: Supply a ProjectKey from the ProjectContext attached to the Uri somehow
if (_snapshotResolver.TryResolveDocumentInAnyProject(filePath, out var documentSnapshot))
{
if (!versioned)
{
return new DocumentSnapshotAndVersion(documentSnapshot, Version: null);
}

if (_documentVersionCache.TryGetDocumentVersion(documentSnapshot, out var version))
{
return new DocumentSnapshotAndVersion(documentSnapshot, version.Value);
}
}

// This is super rare, if we get here it could mean many things. Some of which:
// 1. Stale request:
// - Got queued after a "document closed" / "document removed" type action
// - Took too long to run and by the time the request needed the document context the
// version cache has evicted the entry
// 2. Client is misbehaving and sending requests for a document that we've never seen before.
_logger.LogWarning("Tried to create context for document {documentUri} which was not found.", documentUri);
return null;
}, cancellationToken).ConfigureAwait(false);
var documentAndVersion = TryGetDocumentAndVersion(filePath, versioned);

if (documentAndVersion is null)
{
Expand All @@ -75,8 +45,6 @@ public DefaultDocumentContextFactory(
return null;
}

cancellationToken.ThrowIfCancellationRequested();

if (versioned)
{
// If we were asked for a versioned document, but have no version info, then we didn't find the document
Expand All @@ -91,5 +59,31 @@ public DefaultDocumentContextFactory(
return new DocumentContext(documentUri, documentSnapshot, projectContext);
}

private DocumentSnapshotAndVersion? TryGetDocumentAndVersion(string filePath, bool versioned)
{
// TODO: Supply a ProjectKey from the ProjectContext attached to the Uri somehow
if (_snapshotResolver.TryResolveDocumentInAnyProject(filePath, out var documentSnapshot))
{
if (!versioned)
{
return new DocumentSnapshotAndVersion(documentSnapshot, Version: null);
}

if (_documentVersionCache.TryGetDocumentVersion(documentSnapshot, out var version))
{
return new DocumentSnapshotAndVersion(documentSnapshot, version.Value);
}
}

// This is super rare, if we get here it could mean many things. Some of which:
// 1. Stale request:
// - Got queued after a "document closed" / "document removed" type action
// - Took too long to run and by the time the request needed the document context the
// version cache has evicted the entry
// 2. Client is misbehaving and sending requests for a document that we've never seen before.
_logger.LogWarning("Tried to create context for document {filePath} which was not found.", filePath);
return null;
}

private record DocumentSnapshotAndVersion(IDocumentSnapshot Snapshot, int? Version);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal class DefaultRazorComponentSearchEngine : RazorComponentSearchEngine
{
private readonly ProjectSnapshotManagerDispatcher _projectSnapshotManagerDispatcher;
private readonly ProjectSnapshotManager _projectSnapshotManager;
private readonly ILogger<DefaultRazorComponentSearchEngine> _logger;

public DefaultRazorComponentSearchEngine(
ProjectSnapshotManagerDispatcher projectSnapshotManagerDispatcher,
ProjectSnapshotManagerAccessor projectSnapshotManagerAccessor,
ILoggerFactory loggerFactory)
{
Expand All @@ -30,7 +28,6 @@ public DefaultRazorComponentSearchEngine(
throw new ArgumentNullException(nameof(loggerFactory));
}

_projectSnapshotManagerDispatcher = projectSnapshotManagerDispatcher ?? throw new ArgumentNullException(nameof(projectSnapshotManagerDispatcher));
_projectSnapshotManager = projectSnapshotManagerAccessor?.Instance ?? throw new ArgumentNullException(nameof(projectSnapshotManagerAccessor));
_logger = loggerFactory.CreateLogger<DefaultRazorComponentSearchEngine>();
}
Expand All @@ -49,9 +46,7 @@ public DefaultRazorComponentSearchEngine(
return null;
}

var projects = await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
() => _projectSnapshotManager.GetProjects().ToArray(),
cancellationToken).ConfigureAwait(false);
var projects = _projectSnapshotManager.GetProjects();

foreach (var project in projects)
{
Expand Down Expand Up @@ -110,9 +105,7 @@ public DefaultRazorComponentSearchEngine(

var lookupSymbolName = RemoveGenericContent(typeName.AsMemory());

var projects = await _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
() => _projectSnapshotManager.GetProjects(),
CancellationToken.None).ConfigureAwait(false);
var projects = _projectSnapshotManager.GetProjects();

foreach (var project in projects)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Extensions;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal abstract class DocumentContextFactory
{
public Task<DocumentContext?> TryCreateAsync(TextDocumentIdentifier documentIdentifier, CancellationToken cancellationToken)
=> TryCreateCoreAsync(documentIdentifier.Uri, documentIdentifier.GetProjectContext(), versioned: false, cancellationToken);
public DocumentContext? TryCreate(TextDocumentIdentifier documentIdentifier)
=> TryCreateCore(documentIdentifier.Uri, documentIdentifier.GetProjectContext(), versioned: false);

public Task<DocumentContext?> TryCreateAsync(Uri documentUri, CancellationToken cancellationToken)
=> TryCreateCoreAsync(documentUri, projectContext: null, versioned: false, cancellationToken);
public DocumentContext? TryCreate(Uri documentUri)
=> TryCreateCore(documentUri, projectContext: null, versioned: false);

public Task<DocumentContext?> TryCreateAsync(Uri documentUri, VSProjectContext? projectContext, CancellationToken cancellationToken)
=> TryCreateCoreAsync(documentUri, projectContext, versioned: false, cancellationToken);
public DocumentContext? TryCreate(Uri documentUri, VSProjectContext? projectContext)
=> TryCreateCore(documentUri, projectContext, versioned: false);

public async Task<VersionedDocumentContext?> TryCreateForOpenDocumentAsync(Uri documentUri, CancellationToken cancellationToken)
=> (VersionedDocumentContext?) await TryCreateCoreAsync(documentUri, projectContext: null, versioned: true, cancellationToken).ConfigureAwait(false);
public VersionedDocumentContext? TryCreateForOpenDocument(Uri documentUri)
=> (VersionedDocumentContext?) TryCreateCore(documentUri, projectContext: null, versioned: true);

public async Task<VersionedDocumentContext?> TryCreateForOpenDocumentAsync(TextDocumentIdentifier documentIdentifier, CancellationToken cancellationToken)
=> (VersionedDocumentContext?)await TryCreateCoreAsync(documentIdentifier.Uri, documentIdentifier.GetProjectContext(), versioned: true, cancellationToken).ConfigureAwait(false);
public VersionedDocumentContext? TryCreateForOpenDocument(TextDocumentIdentifier documentIdentifier)
=> (VersionedDocumentContext?)TryCreateCore(documentIdentifier.Uri, documentIdentifier.GetProjectContext(), versioned: true);

public async Task<VersionedDocumentContext?> TryCreateForOpenDocumentAsync(Uri documentUri, VSProjectContext? projectContext, CancellationToken cancellationToken)
=> (VersionedDocumentContext?)await TryCreateCoreAsync(documentUri, projectContext, versioned: true, cancellationToken).ConfigureAwait(false);
public VersionedDocumentContext? TryCreateForOpenDocument(Uri documentUri, VSProjectContext? projectContext)
=> (VersionedDocumentContext?)TryCreateCore(documentUri, projectContext, versioned: true);

protected abstract Task<DocumentContext?> TryCreateCoreAsync(Uri documentUri, VSProjectContext? projectContext, bool versioned, CancellationToken cancellationToken);
protected abstract DocumentContext? TryCreateCore(Uri documentUri, VSProjectContext? projectContext, bool versioned);
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected override IRazorPresentationParams CreateRazorRequestParameters(UriPres
{
_logger.LogInformation("Trying to find document info for dropped uri {uri}.", uri);

var documentContext = await _documentContextFactory.TryCreateAsync(uri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreate(uri);
if (documentContext is null)
{
_logger.LogInformation("Failed to find document for component {uri}.", uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public async Task<WorkspaceEdit> RemapWorkspaceEditAsync(WorkspaceEdit workspace
return (generatedDocumentUri, generatedDocumentRange);
}

var documentContext = await _documentContextFactory.TryCreateAsync(razorDocumentUri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreate(razorDocumentUri);
if (documentContext is null)
{
return (generatedDocumentUri, generatedDocumentRange);
Expand Down Expand Up @@ -832,7 +832,7 @@ private async Task<TextDocumentEdit[]> RemapVersionedDocumentEditsAsync(TextDocu
}

var razorDocumentUri = _languageServerFeatureOptions.GetRazorDocumentUri(generatedDocumentUri);
var documentContext = await _documentContextFactory.TryCreateForOpenDocumentAsync(razorDocumentUri, entry.TextDocument.GetProjectContext(), cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreateForOpenDocument(razorDocumentUri, entry.TextDocument.GetProjectContext());
if (documentContext is null)
{
continue;
Expand Down Expand Up @@ -876,7 +876,7 @@ private async Task<Dictionary<string, TextEdit[]>> RemapDocumentEditsAsync(Dicti
continue;
}

var documentContext = await _documentContextFactory.TryCreateAsync(uri, cancellationToken).ConfigureAwait(false);
var documentContext = _documentContextFactory.TryCreate(uri);
if (documentContext is null)
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public RazorRequestContextFactory(ILspServices lspServices)
_lspServices = lspServices;
}

public async Task<RazorRequestContext> CreateRequestContextAsync<TRequestParams>(IQueueItem<RazorRequestContext> queueItem, TRequestParams @params, CancellationToken cancellationToken)
public Task<RazorRequestContext> CreateRequestContextAsync<TRequestParams>(IQueueItem<RazorRequestContext> queueItem, TRequestParams @params, CancellationToken cancellationToken)
{
VersionedDocumentContext? documentContext = null;
var textDocumentHandler = queueItem.MethodHandler as ITextDocumentIdentifierHandler;
Expand All @@ -31,12 +31,12 @@ public async Task<RazorRequestContext> CreateRequestContextAsync<TRequestParams>
if (textDocumentHandler is ITextDocumentIdentifierHandler<TRequestParams, TextDocumentIdentifier> tdiHandler)
{
var textDocumentIdentifier = tdiHandler.GetTextDocumentIdentifier(@params);
documentContext = await documentContextFactory.TryCreateForOpenDocumentAsync(textDocumentIdentifier, cancellationToken).ConfigureAwait(false);
documentContext = documentContextFactory.TryCreateForOpenDocument(textDocumentIdentifier);
}
else if (textDocumentHandler is ITextDocumentIdentifierHandler<TRequestParams, Uri> uriHandler)
{
uri = uriHandler.GetTextDocumentIdentifier(@params);
documentContext = await documentContextFactory.TryCreateForOpenDocumentAsync(uri, cancellationToken).ConfigureAwait(false);
documentContext = documentContextFactory.TryCreateForOpenDocument(uri);
}
else
{
Expand All @@ -52,6 +52,6 @@ public async Task<RazorRequestContext> CreateRequestContextAsync<TRequestParams>
#endif
);

return requestContext;
return Task.FromResult(requestContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand All @@ -26,23 +24,23 @@ public TestDocumentContextFactory(string filePath, RazorCodeDocument codeDocumen
_version = version;
}

protected override Task<DocumentContext?> TryCreateCoreAsync(Uri documentUri, VSProjectContext? projectContext, bool versioned, CancellationToken cancellationToken)
protected override DocumentContext? TryCreateCore(Uri documentUri, VSProjectContext? projectContext, bool versioned)
{
if (FilePath is null || CodeDocument is null)
{
return Task.FromResult<DocumentContext?>(null);
return null;
}

if (versioned)
{
if (_version is null)
{
return Task.FromResult<DocumentContext?>(null);
return null;
}

return Task.FromResult<DocumentContext?>(TestDocumentContext.From(FilePath, CodeDocument, _version.Value));
return TestDocumentContext.From(FilePath, CodeDocument, _version.Value);
}

return Task.FromResult<DocumentContext?>(TestDocumentContext.From(FilePath, CodeDocument));
return TestDocumentContext.From(FilePath, CodeDocument);
}
}
Loading