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 = to completion #4599

Merged
merged 4 commits into from
Sep 27, 2021
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
58 changes: 55 additions & 3 deletions src/Bicep.LangServer.IntegrationTests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,6 @@ await RunCompletionScenarioTest(this.TestContext, fileWithCursors, completions =
public async Task Completions_after_resource_type_should_only_include_existing_keyword()
{
var fileWithCursors = @"
resource testRes 'Test.Rp/readWriteTests@2020-01-01' |

resource testRes2 'Test.Rp/readWriteTests@2020-01-01' | = {
}

Expand Down Expand Up @@ -631,10 +629,64 @@ await RunCompletionScenarioTest(this.TestContext, fileWithCursors, completions =
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(d => AssertExistingKeywordCompletion(d)),
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(d => AssertExistingKeywordCompletion(d)),
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(d => AssertExistingKeywordCompletion(d)),
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(d => AssertExistingKeywordCompletion(d)),
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(d => AssertExistingKeywordCompletion(d))));
}

[TestMethod]
public async Task ResourceTypeFollowerWithoCompletionsOffersEqualsAndExisting()
{

var fileWithCursors = @"
resource base64 'Microsoft.Foo/foos@2020-09-01' |

resource base64 'Microsoft.Foo/foos@2020-09-01' | {}

resource base64 'Microsoft.Foo/foos@2020-09-01' existing | {}

";

static void AssertEqualsOperatorCompletion(CompletionItem item)
{
item.Label.Should().Be("=");
item.Documentation.Should().BeNull();
item.Kind.Should().Be(CompletionItemKind.Operator);
item.Preselect.Should().BeTrue();
item.TextEdit!.TextEdit!.NewText.Should().Be("=");

// do not add = to the list of commit chars
// it makes it difficult to type = without the "existing" keyword :)
item.CommitCharacters.Should().BeNull();
}

static void AssertExistingKeywordCompletion(CompletionItem item)
{
item.Label.Should().Be("existing");
item.Detail.Should().Be("existing");
item.Documentation.Should().BeNull();
item.Kind.Should().Be(CompletionItemKind.Keyword);
item.Preselect.Should().BeFalse();
item.TextEdit!.TextEdit!.NewText.Should().Be("existing");

// do not add = to the list of commit chars
// it makes it difficult to type = without the "existing" keyword :)
item.CommitCharacters.Should().BeNull();
}

await RunCompletionScenarioTest(this.TestContext, fileWithCursors, completions =>
completions.Should().SatisfyRespectively(
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(
d => AssertEqualsOperatorCompletion(d),
d => AssertExistingKeywordCompletion(d)
),
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(
d => AssertEqualsOperatorCompletion(d),
d => AssertExistingKeywordCompletion(d)
),
x => x!.OrderBy(d => d.SortText).Should().SatisfyRespectively(
d => AssertEqualsOperatorCompletion(d)
)));
}

[TestMethod]
public async Task PropertyNameCompletionsShouldIncludeTrailingColonIfColonIsMissing()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ public void CommentShouldNotGiveAnyCompletions(string codeFragment)

completions.Should().BeEmpty();
}

private static void AssertExpectedDeclarationTypeCompletions(List<CompletionItem> completions)
{
completions.Should().SatisfyRespectively(
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.LangServer/Completions/BicepCompletionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ private static bool IsResourceTypeFollowerContext(List<SyntaxBase> matchingNodes
// resource foo '...' |
// OR
// resource foo '...' | = {
SyntaxMatcher.IsTailMatch<ResourceDeclarationSyntax>(matchingNodes, resource => !resource.IsExistingResource() && offset > resource.Type.GetEndPosition() && offset <= resource.Assignment.Span.Position) ||
SyntaxMatcher.IsTailMatch<ResourceDeclarationSyntax>(matchingNodes, resource => offset > resource.Type.GetEndPosition() && offset <= resource.Assignment.Span.Position) ||
// resource foo '...' e|
// OR
// resource foo '...' e| = {
SyntaxMatcher.IsTailMatch<ResourceDeclarationSyntax, SkippedTriviaSyntax, Token>(matchingNodes, (resource, skipped, token) => !resource.IsExistingResource() && resource.Assignment == skipped && token.Type == TokenType.Identifier) ||
SyntaxMatcher.IsTailMatch<ResourceDeclarationSyntax, SkippedTriviaSyntax, Token>(matchingNodes, (resource, skipped, token) => resource.Assignment == skipped && token.Type == TokenType.Identifier) ||
// resource foo '...' |=
SyntaxMatcher.IsTailMatch<ResourceDeclarationSyntax, Token>(matchingNodes, (resource, token) => !resource.IsExistingResource() && resource.Assignment == token && token.Type == TokenType.Assignment && offset == token.Span.Position);
SyntaxMatcher.IsTailMatch<ResourceDeclarationSyntax, Token>(matchingNodes, (resource, token) => resource.Assignment == token && token.Type == TokenType.Assignment && offset == token.Span.Position);

private static bool IsTargetScopeContext(List<SyntaxBase> matchingNodes, int offset) =>
SyntaxMatcher.IsTailMatch<TargetScopeSyntax>(matchingNodes, targetScope =>
Expand Down
21 changes: 19 additions & 2 deletions src/Bicep.LangServer/Completions/BicepCompletionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,18 @@ private IEnumerable<CompletionItem> GetResourceTypeFollowerCompletions(BicepComp
{
if (context.Kind.HasFlag(BicepCompletionContextKind.ResourceTypeFollower))
{
const string existing = "existing";
yield return CreateKeywordCompletion(existing, existing, context.ReplacementRange);
// Only when there is no existing assignment sign
if (context.EnclosingDeclaration is ResourceDeclarationSyntax { Assignment: SkippedTriviaSyntax { Elements: { IsDefaultOrEmpty: true }} })
{
const string equals = "=";
yield return CreateOperatorCompletion(equals, context.ReplacementRange, preselect: true);
}

if (context.EnclosingDeclaration is ResourceDeclarationSyntax { ExistingKeyword: null })
{
const string existing = "existing";
yield return CreateKeywordCompletion(existing, existing, context.ReplacementRange);
}
}
}

Expand Down Expand Up @@ -971,6 +981,13 @@ private static CompletionItem CreateTypeCompletion(TypeSymbol type, Range replac
.WithSortText(GetSortText(type.Name, priority))
.Build();

private static CompletionItem CreateOperatorCompletion(string op, Range replacementRange, bool preselect = false, CompletionPriority priority = CompletionPriority.Medium) =>
CompletionItemBuilder.Create(CompletionItemKind.Operator, op)
.WithPlainTextEdit(replacementRange, op)
.Preselect(preselect)
.WithSortText(GetSortText(op, priority))
.Build();

private static CompletionItem CreateResourceTypeCompletion(ResourceTypeReference resourceType, int index, Range replacementRange, bool showApiVersion)
{
// Splitting ResourceType Completion in to two pieces, one for the 'Namespace/type', the second for '@<api-version>'
Expand Down