Skip to content

Commit

Permalink
Merge pull request #2253 from JoeRobich/fix-suggestion-mode
Browse files Browse the repository at this point in the history
Remove all completion commit characters in suggestion mode.
  • Loading branch information
JoeRobich authored Oct 20, 2021
2 parents e705ec3 + eee8655 commit 1521fca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ internal static partial class CompletionListBuilder
internal const string ExtensionMethodImportCompletionProvider = "Microsoft.CodeAnalysis.CSharp.Completion.Providers.ExtensionMethodImportCompletionProvider";
internal const string EmeddedLanguageCompletionProvider = "Microsoft.CodeAnalysis.CSharp.Completion.Providers.EmbeddedLanguageCompletionProvider";

// VS has a more complex concept of a commit mode vs suggestion mode for intellisense.
// LSP doesn't have this, so mock it as best we can by removing space ` ` from the list
// of commit characters if we're in suggestion mode.
private static readonly IReadOnlyList<char> DefaultRulesWithoutSpace = CompletionRules.Default.DefaultCommitCharacters.Where(c => c != ' ').ToList();

internal static async Task<(IReadOnlyList<CompletionItem>, bool)> BuildCompletionItems(
Document document,
SourceText sourceText,
Expand Down Expand Up @@ -100,10 +95,17 @@ internal static LinePositionSpanTextChange GetChangeForTextAndSpan(string? inser

private static IReadOnlyList<char>? BuildCommitCharacters(ImmutableArray<CharacterSetModificationRule> characterRules, bool isSuggestionMode, Dictionary<ImmutableArray<CharacterSetModificationRule>, IReadOnlyList<char>> commitCharacterRulesCache, HashSet<char> commitCharactersBuilder)
{
if (isSuggestionMode)
{
// To emulate soft selection we should remove all trigger characters forcing the Editor to
// fallback to only <tab> and <enter> for accepting the completions.
return null;
}

if (characterRules.IsEmpty)
{
// Use defaults
return isSuggestionMode ? DefaultRulesWithoutSpace : CompletionRules.Default.DefaultCommitCharacters;
return CompletionRules.Default.DefaultCommitCharacters;
}

if (commitCharacterRulesCache.TryGetValue(characterRules, out var cachedRules))
Expand Down
23 changes: 23 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/CompletionFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,29 @@ public void M()
Assert.Equal(@"\A", aCompletion.TextEdit.NewText);
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task NoTriggerCharactersWhenInSuggestionMode(string filename)
{
const string input = @"
using System;
class Program
{
public static void Main()
{
var fn = (DateTime $$
}
}";

var completions = await FindCompletionsAsync(filename, input, SharedOmniSharpTestHost);
foreach (var item in completions.Items)
{
Assert.Null(item.CommitCharacters);
}
}

private CompletionService GetCompletionService(OmniSharpTestHost host)
=> host.GetRequestHandler<CompletionService>(EndpointName);

Expand Down

0 comments on commit 1521fca

Please sign in to comment.