Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.ComponentModel.Composition;
using Microsoft.CodeAnalysis.Razor.Completion;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

[Export(typeof(CompletionListCache))]
internal class CohostCompletionListCache : CompletionListCache;
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal sealed class CohostDocumentCompletionEndpoint(
SnippetCompletionItemProvider snippetCompletionItemProvider,
LanguageServerFeatureOptions languageServerFeatureOptions,
LSPRequestInvoker requestInvoker,
CompletionListCache completionListCache,
ILoggerFactory loggerFactory)
: AbstractRazorCohostDocumentRequestHandler<RoslynCompletionParams, VSInternalCompletionList?>, IDynamicRegistrationProvider
{
Expand All @@ -52,6 +53,7 @@ internal sealed class CohostDocumentCompletionEndpoint(
private readonly SnippetCompletionItemProvider _snippetCompletionItemProvider = snippetCompletionItemProvider;
private readonly CompletionTriggerAndCommitCharacters _triggerAndCommitCharacters = new(languageServerFeatureOptions);
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
private readonly CompletionListCache _completionListCache = completionListCache;
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<CohostDocumentCompletionEndpoint>();

protected override bool MutatesSolutionState => false;
Expand Down Expand Up @@ -211,9 +213,10 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
return null;
}

var originalTdi = request.TextDocument;
request.TextDocument = RoslynLspExtensions.WithUri(request.TextDocument, htmlDocument.Uri);

_logger.LogDebug($"Resolving auto-insertion edit for {htmlDocument.Uri}");
_logger.LogDebug($"Getting completion list for {htmlDocument.Uri} at {request.Position}");

var result = await _requestInvoker.ReinvokeRequestOnServerAsync<RoslynCompletionParams, VSInternalCompletionList?>(
htmlDocument.Buffer,
Expand All @@ -224,6 +227,14 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie

var rewrittenResponse = DelegatedCompletionHelper.RewriteHtmlResponse(result?.Response, razorCompletionOptions);

var completionCapability = _clientCapabilitiesService.ClientCapabilities.TextDocument?.Completion as VSInternalCompletionSetting;

var textDocument = JsonHelpers.ToVsLSP<TextDocumentIdentifier, RoslynTextDocumentIdentifier>(originalTdi).AssumeNotNull();
var razorDocumentIdentifier = new TextDocumentIdentifierAndVersion(textDocument, Version: 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "Version: 0" going to bite us later? If so, is there an issue tracking work to fix it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, versions aren't used in cohosting at all, since things run off a snapshot, but the context object stored in the cache is shared. This can likely be cleaned up in future to just store the document identifier in the context, and pass the version number to the things that need it in the language server. I actually think storing a possibly-old version number has caused us bugs in the past, but I didn't want to break into that jail in this PR.

var resolutionContext = new DelegatedCompletionResolutionContext(razorDocumentIdentifier, RazorLanguageKind.Html, rewrittenResponse.Data);
var resultId = _completionListCache.Add(rewrittenResponse, resolutionContext);
rewrittenResponse.SetResultId(resultId, completionCapability);

return rewrittenResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,15 @@ private async Task VerifyCompletionListAsync(
var snippetInfos = snippetLabels.Select(label => new SnippetInfo(label, label, label, string.Empty, SnippetLanguage.Html)).ToImmutableArray();
snippetCompletionItemProvider.SnippetCache.Update(SnippetLanguage.Html, snippetInfos);

var completionListCache = new CompletionListCache();
var endpoint = new CohostDocumentCompletionEndpoint(
RemoteServiceInvoker,
clientSettingsManager,
TestHtmlDocumentSynchronizer.Instance,
snippetCompletionItemProvider,
TestLanguageServerFeatureOptions.Instance,
requestInvoker,
completionListCache,
LoggerFactory);

var request = new RoslynCompletionParams()
Expand Down