Skip to content

Commit

Permalink
Merge pull request #975 from rchande/triggerOnSpace
Browse files Browse the repository at this point in the history
Trigger completion on space if object creation completion is available
  • Loading branch information
DustinCampbell authored Oct 5, 2017
2 parents 9a8afe7 + 574aabb commit 6b90582
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
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);
}
}
}

0 comments on commit 6b90582

Please sign in to comment.