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

Trigger completion on space if object creation completion is available #975

Merged
merged 1 commit into from
Oct 5, 2017
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 @@ -51,5 +51,7 @@ public string WordToComplete
/// Returns the kind (i.e Method, Property, Field)
/// </summary>
public bool WantKind { get; set; }

public string TriggerCharacter { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static class CompletionItemExtensions
{
private const string GetSymbolsAsync = nameof(GetSymbolsAsync);
private const string InsertionText = nameof(InsertionText);
private const string ObjectCreationCompletionProvider = "Microsoft.CodeAnalysis.CSharp.Completion.Providers.ObjectCreationCompletionProvider";
private const string NamedParameterCompletionProvider = "Microsoft.CodeAnalysis.CSharp.Completion.Providers.NamedParameterCompletionProvider";
private const string OverrideCompletionProvider = "Microsoft.CodeAnalysis.CSharp.Completion.Providers.OverrideCompletionProvider";
private const string ParitalMethodCompletionProvider = "Microsoft.CodeAnalysis.CSharp.Completion.Providers.PartialMethodCompletionProvider";
Expand All @@ -32,6 +33,12 @@ static CompletionItemExtensions()
_getSymbolsAsync = symbolCompletionItemType.GetMethod(GetSymbolsAsync, BindingFlags.Public | BindingFlags.Static);
}

public static bool IsObjectCreationCompletionItem(this CompletionItem item)
{
var properties = item.Properties;
return properties.TryGetValue(Provider, out var provider) && provider == ObjectCreationCompletionProvider;
}

public static async Task<IEnumerable<ISymbol>> GetCompletionSymbolsAsync(this CompletionItem completionItem, IEnumerable<ISymbol> recommendedSymbols, Document document)
{
var properties = completionItem.Properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest

if (completionList != null)
{
// get recommened symbols to match them up later with SymbolCompletionProvider
// Only trigger on space if Roslyn has object creation items
if (request.TriggerCharacter == " " && !completionList.Items.Any(i => i.IsObjectCreationCompletionItem()))
{
return completions;
}

// get recommended symbols to match them up later with SymbolCompletionProvider
var semanticModel = await document.GetSemanticModelAsync();
var recommendedSymbols = await Recommender.GetRecommendedSymbolsAtPositionAsync(semanticModel, position, _workspace);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected AbstractAutoCompleteTestFixture(ITestOutputHelper output)

protected override string EndpointName => OmniSharpEndpoints.AutoComplete;

protected async Task<IEnumerable<AutoCompleteResponse>> FindCompletionsAsync(string filename, string source, bool wantSnippet = false)
protected async Task<IEnumerable<AutoCompleteResponse>> FindCompletionsAsync(string filename, string source, bool wantSnippet = false, string triggerChar = null)
{
var testFile = new TestFile(filename, source);
using (var host = CreateOmniSharpHost(testFile))
Expand All @@ -32,7 +32,8 @@ protected async Task<IEnumerable<AutoCompleteResponse>> FindCompletionsAsync(str
WordToComplete = GetPartialWord(testFile.Content),
WantMethodHeader = true,
WantSnippet = wantSnippet,
WantReturnType = true
WantReturnType = true,
TriggerCharacter = triggerChar
};

var requestHandler = GetRequestHandler(host);
Expand Down
34 changes: 34 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,5 +454,39 @@ private void ContainsCompletions(IEnumerable<string> completions, params string[

Assert.Equal(expected, completions.ToArray());
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task TriggeredOnSpaceForObjectCreation(string filename)
{
const string input =
@"public class Class1 {
public M()
{
Class1 c = new $$
}
}";

var completions = await FindCompletionsAsync(filename, input, wantSnippet: true, triggerChar: " ");
Assert.NotEmpty(completions);
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task NotTriggeredOnSpaceWithoutObjectCreation(string filename)
{
const string input =
@"public class Class1 {
public M()
{
$$
}
}";

var completions = await FindCompletionsAsync(filename, input, wantSnippet: true, triggerChar: " ");
Assert.Empty(completions);
}
}
}