Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for element completion commit characters #9379

Merged
merged 11 commits into from
Oct 9, 2023
Prev Previous commit
Next Next commit
Wire up new option to completion options
  • Loading branch information
davidwengier committed Oct 6, 2023
commit d7444d30140f0fe44b6cb79cd7293e805a4e1f6f
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class LegacyRazorCompletionEndpoint : IVSCompletionEndpoint
{
private readonly IRazorCompletionFactsService _completionFactsService;
private readonly CompletionListCache _completionListCache;
private readonly RazorLSPOptionsMonitor _razorLSPOptionsMonitor;
private static readonly Command s_retriggerCompletionCommand = new()
{
CommandIdentifier = "editor.action.triggerSuggest",
Expand All @@ -35,10 +36,11 @@ internal class LegacyRazorCompletionEndpoint : IVSCompletionEndpoint

public bool MutatesSolutionState => false;

public LegacyRazorCompletionEndpoint(IRazorCompletionFactsService completionFactsService, CompletionListCache completionListCache)
public LegacyRazorCompletionEndpoint(IRazorCompletionFactsService completionFactsService, CompletionListCache completionListCache, RazorLSPOptionsMonitor razorLSPOptionsMonitor)
{
_completionFactsService = completionFactsService ?? throw new ArgumentNullException(nameof(completionFactsService));
_completionListCache = completionListCache ?? throw new ArgumentNullException(nameof(completionListCache));
_razorLSPOptionsMonitor = razorLSPOptionsMonitor ?? throw new ArgumentNullException(nameof(razorLSPOptionsMonitor));
}

public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, VSInternalClientCapabilities clientCapabilities)
Expand Down Expand Up @@ -89,7 +91,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(CompletionParams request
CompletionTriggerKind.TriggerCharacter => CompletionReason.Typing,
_ => CompletionReason.Typing,
};
var completionOptions = new RazorCompletionOptions(SnippetsSupported: true);
var completionOptions = new RazorCompletionOptions(SnippetsSupported: true, _razorLSPOptionsMonitor.CurrentValue.CommitElementsWithSpace);
var queryableChange = new SourceChange(hostDocumentIndex, length: 0, newText: string.Empty);
var owner = syntaxTree.Root.LocateOwner(queryableChange);
var completionContext = new RazorCompletionContext(hostDocumentIndex, owner, syntaxTree, tagHelperDocumentContext, reason, completionOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class RazorCompletionListProvider
{
private readonly IRazorCompletionFactsService _completionFactsService;
private readonly CompletionListCache _completionListCache;
private readonly RazorLSPOptionsMonitor _razorLSPOptionsMonitor;
private readonly ILogger<RazorCompletionListProvider> _logger;
private static readonly Command s_retriggerCompletionCommand = new()
{
Expand All @@ -33,10 +34,12 @@ internal class RazorCompletionListProvider
public RazorCompletionListProvider(
IRazorCompletionFactsService completionFactsService,
CompletionListCache completionListCache,
RazorLSPOptionsMonitor razorLSPOptionsMonitor,
ILoggerFactory loggerFactory)
{
_completionFactsService = completionFactsService;
_completionListCache = completionListCache;
_razorLSPOptionsMonitor = razorLSPOptionsMonitor;
_logger = loggerFactory.CreateLogger<RazorCompletionListProvider>();
}

Expand Down Expand Up @@ -65,7 +68,7 @@ public RazorCompletionListProvider(
_ => CompletionReason.Typing,
};

var completionOptions = new RazorCompletionOptions(SnippetsSupported: true);
var completionOptions = new RazorCompletionOptions(SnippetsSupported: true, _razorLSPOptionsMonitor.CurrentValue.CommitElementsWithSpace);
var syntaxTree = await documentContext.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var tagHelperContext = await documentContext.GetTagHelperContextAsync(cancellationToken).ConfigureAwait(false);
var queryableChange = new SourceChange(absoluteIndex, length: 0, newText: string.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

namespace Microsoft.CodeAnalysis.Razor.Completion;

internal record struct RazorCompletionOptions(bool SnippetsSupported);
internal record struct RazorCompletionOptions(bool SnippetsSupported, bool CommitElementsWithSpace);
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public TestRazorCompletionListProvider(
VSInternalCompletionList completionList,
IEnumerable<string> triggerCharacters,
ILoggerFactory loggerFactory)
: base(null, null, loggerFactory)
: base(null, null, TestRazorLSPOptionsMonitor.Create(), loggerFactory)
{
_completionList = completionList;
TriggerCharacters = triggerCharacters.ToImmutableHashSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public async Task Handle_Unsupported_NoCompletionItems()
var codeDocument = CreateCodeDocument("@");
codeDocument.SetUnsupported();
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create());
completionEndpoint.ApplyCapabilities(new(), _clientCapabilities);
var request = new CompletionParams()
{
Expand Down Expand Up @@ -377,7 +377,7 @@ public async Task Handle_ProvidesDirectiveCompletionItems()
var documentPath = new Uri("C:/path/to/document.cshtml");
var codeDocument = CreateCodeDocument("@");
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create());
completionEndpoint.ApplyCapabilities(new(), _clientCapabilities);
var request = new CompletionParams()
{
Expand Down Expand Up @@ -420,7 +420,7 @@ public async Task Handle_ProvidesInjectOnIncomplete_KeywordIn()
var codeDocument = CreateCodeDocument("@in");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create());
completionEndpoint.ApplyCapabilities(new(), _clientCapabilities);
var request = new CompletionParams()
{
Expand Down Expand Up @@ -463,7 +463,7 @@ public async Task Handle_DoesNotProvideInjectOnInvoked()
var codeDocument = CreateCodeDocument("@inje");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create());
completionEndpoint.ApplyCapabilities(new(), _clientCapabilities);
var request = new CompletionParams()
{
Expand Down Expand Up @@ -500,7 +500,7 @@ public async Task Handle_ProvidesInjectOnIncomplete()
var codeDocument = CreateCodeDocument("@inje");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create());
completionEndpoint.ApplyCapabilities(new(), _clientCapabilities);
var request = new CompletionParams()
{
Expand Down Expand Up @@ -544,7 +544,7 @@ public async Task Handle_ProvidesTagHelperElementCompletionItems()
var codeDocument = CreateCodeDocument("<");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create());
completionEndpoint.ApplyCapabilities(new(), _clientCapabilities);
var request = new CompletionParams()
{
Expand Down Expand Up @@ -584,7 +584,7 @@ public async Task Handle_ProvidesTagHelperAttributeItems()
var codeDocument = CreateCodeDocument("<test ");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = CreateDocumentContext(documentPath, codeDocument);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache);
var completionEndpoint = new LegacyRazorCompletionEndpoint(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create());
completionEndpoint.ApplyCapabilities(new(), _clientCapabilities);
var request = new CompletionParams()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public async Task GetCompletionListAsync_ProvidesDirectiveCompletionItems()
var documentPath = "C:/path/to/document.cshtml";
var codeDocument = CreateCodeDocument("@");
var documentContext = TestDocumentContext.From(documentPath, codeDocument, hostDocumentVersion: 0);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);

// Act
var completionList = await provider.GetCompletionListAsync(
Expand Down Expand Up @@ -390,7 +390,7 @@ public async Task GetCompletionListAsync_ProvidesDirectiveCompletions_Incomplete
TriggerKind = CompletionTriggerKind.TriggerForIncompleteCompletions,
InvokeKind = VSInternalCompletionInvokeKind.Deletion,
};
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);

// Act
var completionList = await provider.GetCompletionListAsync(
Expand Down Expand Up @@ -418,7 +418,7 @@ public async Task GetCompletionListAsync_ProvidesInjectOnIncomplete_KeywordIn()
var codeDocument = CreateCodeDocument("@in");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = TestDocumentContext.From(documentPath, codeDocument, hostDocumentVersion: 0);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);
var completionContext = new VSInternalCompletionContext()
{
TriggerKind = CompletionTriggerKind.TriggerForIncompleteCompletions,
Expand Down Expand Up @@ -452,7 +452,7 @@ public async Task GetCompletionListAsync_DoesNotProvideInjectOnInvoked()
var codeDocument = CreateCodeDocument("@inje");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = TestDocumentContext.From(documentPath, codeDocument, hostDocumentVersion: 0);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);
var completionContext = new VSInternalCompletionContext()
{
TriggerKind = CompletionTriggerKind.TriggerCharacter,
Expand Down Expand Up @@ -480,7 +480,7 @@ public async Task GetCompletionListAsync_ProvidesInjectOnIncomplete()
var codeDocument = CreateCodeDocument("@inje");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = TestDocumentContext.From(documentPath, codeDocument, hostDocumentVersion: 0);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);
var completionContext = new VSInternalCompletionContext()
{
TriggerKind = CompletionTriggerKind.TriggerForIncompleteCompletions,
Expand Down Expand Up @@ -515,7 +515,7 @@ public async Task GetCompletionListAsync_ProvidesTagHelperElementCompletionItems
var codeDocument = CreateCodeDocument("<");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = TestDocumentContext.From(documentPath, codeDocument, hostDocumentVersion: 0);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);

// Act
var completionList = await provider.GetCompletionListAsync(
Expand Down Expand Up @@ -545,7 +545,7 @@ public async Task GetCompletionListAsync_ProvidesTagHelperAttributeItems()
var codeDocument = CreateCodeDocument("<test ");
codeDocument.SetTagHelperContext(tagHelperContext);
var documentContext = TestDocumentContext.From(documentPath, codeDocument, hostDocumentVersion: 0);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(_completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);

// Act
var completionList = await provider.GetCompletionListAsync(
Expand Down Expand Up @@ -580,7 +580,7 @@ public async Task GetCompletionListAsync_ProvidesTagHelperAttributeItems_Attribu
await optionsMonitor.UpdateAsync(optionsMonitor.CurrentValue with { AutoInsertAttributeQuotes = false }, DisposalToken);

var completionFactsService = new RazorCompletionFactsService(GetCompletionProviders(optionsMonitor));
var provider = new RazorCompletionListProvider(completionFactsService, _completionListCache, LoggerFactory);
var provider = new RazorCompletionListProvider(completionFactsService, _completionListCache, TestRazorLSPOptionsMonitor.Create(), LoggerFactory);

// Act
var completionList = await provider.GetCompletionListAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ public void GetCompletionAt_SelfClosingTag_NotAtEndOfName_DoesNotReturnCompletio
Assert.Empty(completions);
}


[Fact]
public void GetCompletionAt_SelfClosingTag_ReturnsCompletions()
{
Expand Down Expand Up @@ -305,7 +304,7 @@ public void GetCompletionAt_AtAttributeEdge_IntAttribute_Snippets_ReturnsComplet
{
// Arrange
var service = CreateTagHelperCompletionProvider();
var options = new RazorCompletionOptions(SnippetsSupported: true);
var options = new RazorCompletionOptions(SnippetsSupported: true, CommitElementsWithSpace: true);
var context = CreateRazorCompletionContext(
"""
@addTagHelper *, TestAssembly
Expand Down Expand Up @@ -763,7 +762,7 @@ public void GetCompletionsAt_MiddleOfFullAttribute_ReturnsCompletions_NoSnippetB
<test2 int-$$val=''>
""",
isRazorFile: false,
options: new(SnippetsSupported: true),
options: new(SnippetsSupported: true, CommitElementsWithSpace: true),
tagHelpers: DefaultTagHelpers);

// Act
Expand Down