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

Add IsSuggestionMode to AutoCompleteResponse for determining when the completion list should commit aggressively #882

Merged
merged 1 commit into from
Jun 2, 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 @@ -17,6 +17,7 @@ public class AutoCompleteResponse
public string ReturnType { get; set; }
public string Snippet { get; set; }
public string Kind { get; set; }
public bool IsSuggestionMode { get; set; }

public override bool Equals(object other)
{
Expand All @@ -37,4 +38,4 @@ public override int GetHashCode()
return hashCode;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest
var semanticModel = await document.GetSemanticModelAsync();
var recommendedSymbols = await Recommender.GetRecommendedSymbolsAtPositionAsync(semanticModel, position, _workspace);

var isSuggestionMode = completionList.SuggestionModeItem != null;

foreach (var item in completionList.Items)
{
var completionText = item.DisplayText;
Expand Down Expand Up @@ -74,14 +76,14 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest
{
if (request.WantSnippet)
{
foreach (var completion in MakeSnippetedResponses(request, symbol, completionText))
foreach (var completion in MakeSnippetedResponses(request, symbol, completionText, isSuggestionMode))
{
completions.Add(completion);
}
}
else
{
completions.Add(MakeAutoCompleteResponse(request, symbol, completionText));
completions.Add(MakeAutoCompleteResponse(request, symbol, completionText, isSuggestionMode));
}
}
}
Expand All @@ -99,7 +101,8 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest
CompletionText = item.DisplayText,
DisplayText = item.DisplayText,
Snippet = item.DisplayText,
Kind = request.WantKind ? item.Tags.First() : null
Kind = request.WantKind ? item.Tags.First() : null,
IsSuggestionMode = isSuggestionMode
};

completions.Add(response);
Expand All @@ -117,53 +120,53 @@ public async Task<IEnumerable<AutoCompleteResponse>> Handle(AutoCompleteRequest
.ThenBy(c => c.CompletionText, StringComparer.OrdinalIgnoreCase);
}

private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, ISymbol symbol, string completionText)
private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, ISymbol symbol, string completionText, bool isSuggestionMode)
{
switch (symbol)
{
case IMethodSymbol methodSymbol:
return MakeSnippetedResponses(request, methodSymbol, completionText);
return MakeSnippetedResponses(request, methodSymbol, completionText, isSuggestionMode);
case INamedTypeSymbol typeSymbol:
return MakeSnippetedResponses(request, typeSymbol, completionText);
return MakeSnippetedResponses(request, typeSymbol, completionText, isSuggestionMode);

default:
return new[] { MakeAutoCompleteResponse(request, symbol, completionText) };
return new[] { MakeAutoCompleteResponse(request, symbol, completionText, isSuggestionMode) };
}
}

private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, IMethodSymbol methodSymbol, string completionText)
private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, IMethodSymbol methodSymbol, string completionText, bool isSuggestionMode)
{
var completions = new List<AutoCompleteResponse>();

if (methodSymbol.Parameters.Any(p => p.IsOptional))
{
completions.Add(MakeAutoCompleteResponse(request, methodSymbol, completionText, includeOptionalParams: false));
completions.Add(MakeAutoCompleteResponse(request, methodSymbol, completionText, isSuggestionMode, includeOptionalParams: false));
}

completions.Add(MakeAutoCompleteResponse(request, methodSymbol, completionText));
completions.Add(MakeAutoCompleteResponse(request, methodSymbol, completionText, isSuggestionMode));

return completions;
}

private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, INamedTypeSymbol typeSymbol, string completionText)
private IEnumerable<AutoCompleteResponse> MakeSnippetedResponses(AutoCompleteRequest request, INamedTypeSymbol typeSymbol, string completionText, bool isSuggestionMode)
{
var completions = new List<AutoCompleteResponse>
{
MakeAutoCompleteResponse(request, typeSymbol, completionText)
MakeAutoCompleteResponse(request, typeSymbol, completionText, isSuggestionMode)
};

if (typeSymbol.TypeKind != TypeKind.Enum)
{
foreach (var ctor in typeSymbol.InstanceConstructors)
{
completions.Add(MakeAutoCompleteResponse(request, ctor, completionText));
completions.Add(MakeAutoCompleteResponse(request, ctor, completionText, isSuggestionMode));
}
}

return completions;
}

private AutoCompleteResponse MakeAutoCompleteResponse(AutoCompleteRequest request, ISymbol symbol, string completionText, bool includeOptionalParams = true)
private AutoCompleteResponse MakeAutoCompleteResponse(AutoCompleteRequest request, ISymbol symbol, string completionText, bool isSuggestionMode, bool includeOptionalParams = true)
{
var displayNameGenerator = new SnippetGenerator();
displayNameGenerator.IncludeMarkers = false;
Expand All @@ -175,6 +178,8 @@ private AutoCompleteResponse MakeAutoCompleteResponse(AutoCompleteRequest reques
// TODO: Do something more intelligent here
response.DisplayText = displayNameGenerator.Generate(symbol);

response.IsSuggestionMode = isSuggestionMode;

if (request.WantDocumentationForEveryCompletionResult)
{
response.Description = DocumentationConverter.ConvertDocumentation(symbol.GetDocumentationCommentXml(), _formattingOptions.NewLine);
Expand Down
96 changes: 96 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/IntellisenseFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,102 @@ public async Task Returns_host_object_members_in_csx()
ContainsCompletions(completions.Select(c => c.CompletionText), new[] { "Print", "PrintOptions" });
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Is_suggestion_mode_true_for_lambda_expression_position1(string filename)
{
const string source = @"
using System;
class C
{
int CallMe(int i) => 42;

void M(Func<int, int> a) { }

void M()
{
M(c$$
}
}
";

var completions = await FindCompletionsAsync(filename, source);

Assert.True(completions.All(c => c.IsSuggestionMode));
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Is_suggestion_mode_true_for_lambda_expression_position2(string filename)
{
const string source = @"
using System;
class C
{
int CallMe(int i) => 42;

void M()
{
Func<int, int> a = c$$
}
}
";

var completions = await FindCompletionsAsync(filename, source);

Assert.True(completions.All(c => c.IsSuggestionMode));
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Is_suggestion_mode_false_for_normal_position1(string filename)
{
const string source = @"
using System;
class C
{
int CallMe(int i) => 42;

void M(int a) { }

void M()
{
M(c$$
}
}
";

var completions = await FindCompletionsAsync(filename, source);

Assert.True(completions.All(c => !c.IsSuggestionMode));
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Is_suggestion_mode_false_for_normal_position2(string filename)
{
const string source = @"
using System;
class C
{
int CallMe(int i) => 42;

void M()
{
int a = c$$
}
}
";

var completions = await FindCompletionsAsync(filename, source);

Assert.True(completions.All(c => !c.IsSuggestionMode));
}

private void ContainsCompletions(IEnumerable<string> completions, params string[] expected)
{
if (!completions.SequenceEqual(expected))
Expand Down