Skip to content

Commit

Permalink
Merge pull request #67132 from dibarbet/lsp_only_suggestion_mode_item
Browse files Browse the repository at this point in the history
Fix exception when the list only contains a suggestion mode item
  • Loading branch information
dibarbet authored Mar 2, 2023
2 parents 6a34724 + 51469c4 commit 50e8196
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Completion;
Expand All @@ -19,7 +18,6 @@
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text.Adornments;
using Roslyn.Utilities;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down Expand Up @@ -90,12 +88,15 @@ public CompletionHandler(
return null;

var (list, isIncomplete, resultId) = completionListResult.Value;
if (list.IsEmpty)

if (list.ItemsList.Count == 0)
{
return new LSP.VSInternalCompletionList
{
Items = Array.Empty<LSP.CompletionItem>(),
SuggestionMode = list.SuggestionModeItem != null,
// If we have a suggestion mode item, we just need to keep the list in suggestion mode.
// We don't need to return the fake suggestion mode item.
SuggestionMode = list.SuggestionModeItem is not null,
IsIncomplete = isIncomplete,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,33 @@ void M()
Assert.Empty(results.Items);
}

[Fact, WorkItem(1755138, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1755138")]
public async Task TestOnlyHasSuggestionModeItemAsync()
{
var markup =
@"using System.Threading.Tasks;
class A
{
void M()
{
Task.Run(abcdefg{|caret:|}
}
}";
await using var testLspServer = await CreateTestLspServerAsync(markup, s_vsCompletionCapabilities);
var completionParams = CreateCompletionParams(
testLspServer.GetLocations("caret").Single(),
invokeKind: LSP.VSInternalCompletionInvokeKind.Typing,
triggerCharacter: "g",
triggerKind: LSP.CompletionTriggerKind.TriggerForIncompleteCompletions);

var document = testLspServer.GetCurrentSolution().Projects.First().Documents.First();

var results = await RunGetCompletionsAsync(testLspServer, completionParams).ConfigureAwait(false);
var list = (LSP.VSInternalCompletionList)results;
Assert.Empty(list.Items);
Assert.True(list.SuggestionMode);
}

internal static Task<LSP.CompletionList> RunGetCompletionsAsync(TestLspServer testLspServer, LSP.CompletionParams completionParams)
{
return testLspServer.ExecuteRequestAsync<LSP.CompletionParams, LSP.CompletionList>(LSP.Methods.TextDocumentCompletionName,
Expand Down

0 comments on commit 50e8196

Please sign in to comment.