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

Cleanup CompletionTriggerAndCommitCharacters #11600

Merged
Prev Previous commit
Next Next commit
Don't expose HtmlTriggerCharacters set
Rather than directly exposing a set, this change adds instance methods to test for valid HTML triggers.
DustinCampbell committed Mar 10, 2025
commit 1dbebddf01b91bc383ac2bbd78427f7c94b73e9b
Original file line number Diff line number Diff line change
@@ -25,7 +25,8 @@ internal class CompletionTriggerAndCommitCharacters(LanguageServerFeatureOptions
private static readonly FrozenSet<string> s_razorTriggerCharacters = new[] { RazorDelegationTriggerCharacter, "<", ":", " " }.ToFrozenSet();
private static readonly FrozenSet<string> s_razorDelegationTriggerCharacters = new[] { RazorDelegationTriggerCharacter }.ToFrozenSet();
private static readonly FrozenSet<string> s_csharpTriggerCharacters = new[] { " ", "(", "=", "#", ".", "<", "[", "{", "\"", "/", ":", "~" }.ToFrozenSet();
public FrozenSet<string> HtmlTriggerCharacters =>

private FrozenSet<string> HtmlTriggerCharacters =>
_languageServerFeatureOptions.UseVsCodeCompletionTriggerCharacters ? s_vsCodeHtmlTriggerCharacters : s_vsHtmlTriggerCharacters;

private FrozenSet<string> AllDelegationTriggerCharacters => _allDelegationTriggerCharacters
@@ -52,6 +53,9 @@ public bool IsValidCSharpTrigger(CompletionContext completionContext)
public bool IsValidDelegationTrigger(CompletionContext completionContext)
=> IsValidTrigger(AllDelegationTriggerCharacters, completionContext);

public bool IsValidHtmlTrigger(CompletionContext completionContext)
=> IsValidTrigger(HtmlTriggerCharacters, completionContext);

public bool IsValidRazorTrigger(CompletionContext completionContext)
=> IsValidTrigger(s_razorTriggerCharacters, completionContext);

@@ -61,6 +65,9 @@ public bool IsCSharpTriggerCharacter(string ch)
public bool IsDelegationTriggerCharacter(string ch)
=> AllDelegationTriggerCharacters.Contains(ch);

public bool IsHtmlTriggerCharacter(string ch)
=> HtmlTriggerCharacters.Contains(ch);

public bool IsRazorTriggerCharacter(string ch)
=> s_razorTriggerCharacters.Contains(ch);

Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ internal static class DelegatedCompletionHelper
// For HTML we don't want to delegate to HTML language server is completion is due to a trigger characters that is not
// HTML trigger character. Doing so causes bad side effects in VSCode HTML client as we will end up with non-matching
// completion entries
return triggerAndCommitCharacters.HtmlTriggerCharacters.Contains(triggerCharacter) ? context : null;
return triggerAndCommitCharacters.IsHtmlTriggerCharacter(triggerCharacter) ? context : null;
}

// Trigger character not associated with the current language. Transform the context into an invoked context.
Original file line number Diff line number Diff line change
@@ -137,14 +137,15 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
CommitElementsWithSpace: clientSettings.AdvancedSettings.CommitElementsWithSpace);
using var _ = HashSetPool<string>.GetPooledObject(out var existingHtmlCompletions);

if (CompletionTriggerAndCommitCharacters.IsValidTrigger(_triggerAndCommitCharacters.HtmlTriggerCharacters, completionContext))
if (_triggerAndCommitCharacters.IsValidHtmlTrigger(completionContext))
{
// We can just blindly call HTML LSP because if we are in C#, generated HTML seen by HTML LSP may return
// results we don't want to show. So we want to call HTML LSP only if we know we are in HTML content.
if (documentPositionInfo.LanguageKind == RazorLanguageKind.Html)
{
htmlCompletionList = await GetHtmlCompletionListAsync(request, razorDocument, razorCompletionOptions, cancellationToken)
.ConfigureAwait(false);
htmlCompletionList = await GetHtmlCompletionListAsync(
request, razorDocument, razorCompletionOptions, cancellationToken).ConfigureAwait(false);

if (htmlCompletionList is not null)
{
existingHtmlCompletions.UnionWith(htmlCompletionList.Items.Select(i => i.Label));