Skip to content

Commit

Permalink
Merge pull request #674 from DustinCampbell/fix-autocomplete-exception
Browse files Browse the repository at this point in the history
Don't throw null-ref when Roslyn CompletionService doesn't return anything
  • Loading branch information
DustinCampbell authored Nov 22, 2016
2 parents 5a3223d + 4ae6711 commit 58767a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,27 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest
// Add keywords from the completion list. We'll use the recommender service to get symbols
// to create snippets from.

foreach (var item in completionList.Items)
if (completionList != null)
{
if (item.Tags.Contains(CompletionTags.Keyword))
foreach (var item in completionList.Items)
{
// Note: For keywords, we'll just assume that the completion text is the same
// as the display text.
var keyword = item.DisplayText;
if (keyword.IsValidCompletionFor(wordToComplete))
if (item.Tags.Contains(CompletionTags.Keyword))
{
var response = new AutoCompleteResponse()
// Note: For keywords, we'll just assume that the completion text is the same
// as the display text.
var keyword = item.DisplayText;
if (keyword.IsValidCompletionFor(wordToComplete))
{
CompletionText = item.DisplayText,
DisplayText = item.DisplayText,
Snippet = item.DisplayText,
Kind = request.WantKind ? "Keyword" : null
};

completions.Add(response);
var response = new AutoCompleteResponse()
{
CompletionText = item.DisplayText,
DisplayText = item.DisplayText,
Snippet = item.DisplayText,
Kind = request.WantKind ? "Keyword" : null
};

completions.Add(response);
}
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ public MyClass1()
ContainsCompletions(completions.Select(c => c.CompletionText), "MyClass1", "myvar");
}

[Fact]
public async Task Returns_empty_sequence_in_invalid_context()
{
var source =
@"public class MyClass1 {
public MyClass1()
{
var x$
}
}";

var completions = await FindCompletionsAsync(source);
ContainsCompletions(completions.Select(c => c.CompletionText), Array.Empty<string>());
}

private void ContainsCompletions(IEnumerable<string> completions, params string[] expected)
{
if (!completions.SequenceEqual(expected))
Expand Down Expand Up @@ -217,10 +233,7 @@ private async Task<IEnumerable<AutoCompleteResponse>> FindCompletionsAsync(strin
request = CreateRequest(source);
}

var response = await controller.Handle(request);
var completions = response as IEnumerable<AutoCompleteResponse>;

return completions;
return await controller.Handle(request);
}

private AutoCompleteRequest CreateRequest(string source, string fileName = "dummy.cs", bool wantSnippet = false)
Expand Down

0 comments on commit 58767a2

Please sign in to comment.