Skip to content

Commit

Permalink
added support for EmbeddedLanguageCompletionProvider (#1871)
Browse files Browse the repository at this point in the history
* added support for EmbeddedLanguageCompletionProvider

* added datetime completion tests

* improved test

* added regex test

* changelog update

* fixed name

* removed flaky test
  • Loading branch information
filipw authored Jul 31, 2020
1 parent f7c6f88 commit 02dba81
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All changes to the project will be documented in this file.
* Introduced a new `/quickinfo` endpoint to provide a richer set of information compared to `/typeinfo`. Consumers are encouraged to use it as their hover provider ([#1808](https://github.com/OmniSharp/omnisharp-roslyn/issues/1808), PR: [#1860](https://github.com/OmniSharp/omnisharp-roslyn/pull/1860))
* Fixed return type in LSP completion handler ([#1864](https://github.com/OmniSharp/omnisharp-roslyn/issues/1864), PR: [#1869](https://github.com/OmniSharp/omnisharp-roslyn/pull/1869))
* Upgraded to the latest version of the csharp-language-server-protocol [#1815](https://github.com/OmniSharp/omnisharp-roslyn/pull/1815)
* Added support for Roslyn `EmbeddedLanguageCompletionProvider` which enables completions for string literals for `DateTime` and `Regex` ([#1871](https://github.com/OmniSharp/omnisharp-roslyn/pull/1871))

## [1.35.4] - 2020-07-22
* Update to Roslyn `3.8.0-1.20357.3` (PR: [#1849](https://github.com/OmniSharp/omnisharp-roslyn/pull/1849))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
using OmniSharp.Models.AutoComplete;
using OmniSharp.Utilities;

namespace OmniSharp.Roslyn.CSharp.Services.Intellisense
Expand Down Expand Up @@ -82,5 +83,32 @@ public static bool TryGetInsertionText(this CompletionItem completionItem, out s
{
return completionItem.Properties.TryGetValue(InsertionText, out insertionText);
}

public static AutoCompleteResponse ToAutoCompleteResponse(this CompletionItem item, bool wantKind, bool isSuggestionMode, bool preselect)
{
// for simple use cases we'll just assume that the completion text is the same as the display text
var response = new AutoCompleteResponse()
{
CompletionText = item.DisplayText,
DisplayText = item.DisplayText,
Snippet = item.DisplayText,
Kind = wantKind ? item.Tags.FirstOrDefault() : null,
IsSuggestionMode = isSuggestionMode,
Preselect = preselect
};

// if provider name is "Microsoft.CodeAnalysis.CSharp.Completion.Providers.EmbeddedLanguageCompletionProvider"
// we have access to more elaborate description
if (GetProviderName(item) == "Microsoft.CodeAnalysis.CSharp.Completion.Providers.EmbeddedLanguageCompletionProvider")
{
response.DisplayText = item.InlineDescription;
if (item.Properties.TryGetValue("DescriptionKey", out var description))
{
response.Description = description;
}
}

return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,8 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest
continue;
}

// for other completions, i.e. keywords, create a simple AutoCompleteResponse
// we'll just assume that the completion text is the same
// as the display text.
var response = new AutoCompleteResponse()
{
CompletionText = item.DisplayText,
DisplayText = item.DisplayText,
Snippet = item.DisplayText,
Kind = request.WantKind ? item.Tags.First() : null,
IsSuggestionMode = isSuggestionMode,
Preselect = preselect
};

// for other completions, i.e. keywords or em, create a simple AutoCompleteResponse
var response = item.ToAutoCompleteResponse(request.WantKind, isSuggestionMode, preselect);
completions.Add(response);
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,32 @@ void M()
Assert.True(completions.All(c => !c.IsSuggestionMode));
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Embedded_language_completion_provider_for_datetime_format(string filename)
{
const string source = @"
using System;
class C
{
void M()
{
var d = DateTime.Now.ToString(""$$""
}
}
";

var completions = await FindCompletionsAsync(filename, source);

Assert.NotEmpty(completions);

var gStandardCompletion = completions.FirstOrDefault(x => x.CompletionText == "G");
Assert.NotNull(gStandardCompletion);
Assert.Equal("general long date/time", gStandardCompletion.DisplayText);
Assert.Contains(@"The ""G"" standard format specifier", gStandardCompletion.Description);
}

[Fact]
public async Task Scripting_by_default_returns_completions_for_CSharp7_1()
{
Expand Down

0 comments on commit 02dba81

Please sign in to comment.