From 4ae67118dda872e54a18986e8f54c5c1e7787bb6 Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Tue, 22 Nov 2016 10:55:02 -0800 Subject: [PATCH] Don't throw null-ref when Roslyn CompletionService doesn't return anything --- .../Intellisense/IntellisenseService.cs | 31 ++++++++++--------- .../IntellisenseFacts.cs | 21 ++++++++++--- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/OmniSharp.Roslyn.CSharp/Services/Intellisense/IntellisenseService.cs b/src/OmniSharp.Roslyn.CSharp/Services/Intellisense/IntellisenseService.cs index bf9945d4c7..2489bb679b 100644 --- a/src/OmniSharp.Roslyn.CSharp/Services/Intellisense/IntellisenseService.cs +++ b/src/OmniSharp.Roslyn.CSharp/Services/Intellisense/IntellisenseService.cs @@ -44,24 +44,27 @@ public async Task> 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); + } } } } diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs index cf91241a29..f8425d8483 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs @@ -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()); + } + private void ContainsCompletions(IEnumerable completions, params string[] expected) { if (!completions.SequenceEqual(expected)) @@ -217,10 +233,7 @@ private async Task> FindCompletionsAsync(strin request = CreateRequest(source); } - var response = await controller.Handle(request); - var completions = response as IEnumerable; - - return completions; + return await controller.Handle(request); } private AutoCompleteRequest CreateRequest(string source, string fileName = "dummy.cs", bool wantSnippet = false)