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

Don't throw null-ref when Roslyn CompletionService doesn't return anything #674

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually git diff is much better than this! lol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally

{
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