Skip to content

Commit

Permalink
Do not include commit characters if the typed span is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed Oct 4, 2023
1 parent 22424c7 commit bb6c58f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#nullable enable

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Completion;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Models;
using OmniSharp.Models.v1.Completion;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading.Tasks;
using CompletionItem = OmniSharp.Models.v1.Completion.CompletionItem;
using CSharpCompletionList = Microsoft.CodeAnalysis.Completion.CompletionList;
using CSharpCompletionService = Microsoft.CodeAnalysis.Completion.CompletionService;
Expand Down Expand Up @@ -95,7 +96,15 @@ await completionService.GetChangeAsync(document, completion),
out insertText, out filterText, out sortText, out insertTextFormat, out changeSpan, out additionalTextEdits);
}

var commitCharacters = BuildCommitCharacters(completion.Rules.CommitCharacterRules, isSuggestionMode, commitCharacterRuleCache, commitCharacterRuleBuilder);
var treatAsASuggestion = isSuggestionMode ||
(
// The user hasn't actually typed anything and completion provider does
// not request the item be hard-selected.
completion.Rules.MatchPriority != MatchPriority.Preselect &&
typedSpan.Length == 0 &&
completion.Rules.SelectionBehavior != CompletionItemSelectionBehavior.HardSelection
);
var commitCharacters = BuildCommitCharacters(completion.Rules.CommitCharacterRules, treatAsASuggestion, commitCharacterRuleCache, commitCharacterRuleBuilder);

completionsBuilder.Add(new CompletionItem
{
Expand All @@ -108,7 +117,7 @@ await completionService.GetChangeAsync(document, completion),
Kind = GetCompletionItemKind(completion.Tags),
Detail = completion.InlineDescription,
Data = (cacheId, i),
Preselect = completion.Rules.SelectionBehavior == CompletionItemSelectionBehavior.HardSelection,
Preselect = completion.Rules.MatchPriority == MatchPriority.Preselect,
CommitCharacters = commitCharacters,
HasAfterInsertStep = hasAfterInsertStep,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#nullable enable

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Completion;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Completion;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Extensions;
using OmniSharp.Models;
using OmniSharp.Models.v1.Completion;
using OmniSharp.Roslyn.CSharp.Helpers;
using OmniSharp.Roslyn.Utilities;
using OmniSharp.Utilities;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using CompletionItem = OmniSharp.Models.v1.Completion.CompletionItem;
using CSharpCompletionItem = Microsoft.CodeAnalysis.Completion.CompletionItem;
using CSharpCompletionList = Microsoft.CodeAnalysis.Completion.CompletionList;
Expand Down Expand Up @@ -110,7 +110,15 @@ internal static partial class CompletionListBuilder
}
}

var commitCharacters = BuildCommitCharacters(completion.Rules.CommitCharacterRules, isSuggestionMode, commitCharacterRuleCache, commitCharacterRuleBuilder);
var treatAsASuggestion = isSuggestionMode ||
(
// The user hasn't actually typed anything and completion provider does
// not request the item be hard-selected.
completion.Rules.MatchPriority != MatchPriority.Preselect &&
typedSpan.Length == 0 &&
completion.Rules.SelectionBehavior != CompletionItemSelectionBehavior.HardSelection
);
var commitCharacters = BuildCommitCharacters(completion.Rules.CommitCharacterRules, treatAsASuggestion, commitCharacterRuleCache, commitCharacterRuleBuilder);

completionsBuilder.Add(new CompletionItem
{
Expand All @@ -123,7 +131,7 @@ internal static partial class CompletionListBuilder
Kind = GetCompletionItemKind(completion.Tags),
Detail = completion.InlineDescription,
Data = (cacheId, i),
Preselect = completion.Rules.SelectionBehavior == CompletionItemSelectionBehavior.HardSelection,
Preselect = completion.Rules.MatchPriority == MatchPriority.Preselect,
CommitCharacters = commitCharacters,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Completion;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Completion;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions;
Expand Down
28 changes: 28 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/CompletionFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,34 @@ public async Task ReplacesUpUntilCursorInMiddleOfWord(string filename)
});
}

[Theory]
[InlineData("dummy.cs", true)]
[InlineData("dummy.cs", false)]
[InlineData("dummy.csx", true)]
[InlineData("dummy.csx", false)]
public async Task SoftSelectionWhenFilterTextIsEmpty(string filename, bool useAsyncCompletion)
{
const string input = @"
using System;
using System.Text;
public class A
{
public void M(string someText)
{
var x = new StringBuilder();
x.Append($$
}
}";

using var host = useAsyncCompletion ? GetAsyncCompletionAndImportCompletionHost() : GetImportCompletionHost();

var completions = await FindCompletionsAsync(filename, input, host, '(');
var someTextItem = completions.Items.First(item => item.Label == "someText");

Assert.Null(someTextItem.CommitCharacters);
Assert.False(someTextItem.Preselect);
}

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

Expand Down

0 comments on commit bb6c58f

Please sign in to comment.