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

adding code completion for extends keyword in bicepparam files #14986

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
46 changes: 46 additions & 0 deletions src/Bicep.LangServer.IntegrationTests/ParamsCompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,52 @@ public async Task Request_for_using_declaration_path_completions_should_return_c
});
}

[TestMethod]
public async Task Request_for_extends_declaration_path_completions_should_return_correct_paths_for_file_directories()
{
var fileTextsByUri = new Dictionary<Uri, string>
{
[InMemoryFileResolver.GetFileUri("/path/to/main.bicepparam")] = "using none",
[InMemoryFileResolver.GetFileUri("/path/to/base.bicepparam")] = "using none",
[InMemoryFileResolver.GetFileUri("/path/to/main2.txt")] = "param bar int",
[InMemoryFileResolver.GetFileUri("/path/to/nested1/main3.bicep")] = "param foo int",
[InMemoryFileResolver.GetFileUri("/path/to/module1.bicep")] = "param foo string",
[InMemoryFileResolver.GetFileUri("/path/to/nested1/module2.bicep")] = "param bar bool",
[InMemoryFileResolver.GetFileUri("/path/to/nested2/module3.bicep")] = "param bar string"
};

var completions = await RunCompletionScenario(@"
extends |
", fileTextsByUri.ToImmutableDictionary(), '|');

completions.Take(5).Should().SatisfyRespectively(
x =>
{
x.Label.Should().Be("base.bicepparam");
x.Kind.Should().Be(CompletionItemKind.File);
},
x =>
{
x.Label.Should().Be("main.bicepparam");
x.Kind.Should().Be(CompletionItemKind.File);
},
x =>
{
x.Label.Should().Be("../");
x.Kind.Should().Be(CompletionItemKind.Folder);
},
x =>
{
x.Label.Should().Be("nested1/");
x.Kind.Should().Be(CompletionItemKind.Folder);
},
x =>
{
x.Label.Should().Be("nested2/");
x.Kind.Should().Be(CompletionItemKind.Folder);
});
}

[TestMethod]
public async Task Request_for_using_declaration_path_completions_should_return_correct_partial_paths()
{
Expand Down
9 changes: 9 additions & 0 deletions src/Bicep.LangServer/Completions/BicepCompletionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public static BicepCompletionContext Create(IFeatureProvider featureProvider, Co
ConvertFlag(IsDecoratorNameContext(matchingNodes, offset), BicepCompletionContextKind.DecoratorName) |
ConvertFlag(functionArgumentContext is not null, BicepCompletionContextKind.FunctionArgument | BicepCompletionContextKind.Expression) |
ConvertFlag(IsUsingPathContext(matchingNodes, offset), BicepCompletionContextKind.UsingFilePath) |
ConvertFlag(IsExtendsPathContext(matchingNodes, offset), BicepCompletionContextKind.ExtendsFilePath) |
ConvertFlag(IsTestPathContext(matchingNodes, offset), BicepCompletionContextKind.TestPath) |
ConvertFlag(IsModulePathContext(matchingNodes, offset), BicepCompletionContextKind.ModulePath) |
ConvertFlag(IsImportPathContext(matchingNodes, offset), BicepCompletionContextKind.ModulePath) |
Expand Down Expand Up @@ -864,6 +865,14 @@ private static bool IsUsingPathContext(IList<SyntaxBase> matchingNodes, int offs
// using fo|o
SyntaxMatcher.IsTailMatch<UsingDeclarationSyntax, SkippedTriviaSyntax, Token>(matchingNodes, (@using, skipped, _) => @using.Path == skipped);

private static bool IsExtendsPathContext(IList<SyntaxBase> matchingNodes, int offset) =>
// extends |
SyntaxMatcher.IsTailMatch<ExtendsDeclarationSyntax>(matchingNodes, extendsClause => extendsClause.Keyword.IsBefore(offset)) ||
// extends 'f|oo'
SyntaxMatcher.IsTailMatch<ExtendsDeclarationSyntax, StringSyntax, Token>(matchingNodes, (@extends, @string, _) => @extends.Path == @string) ||
// extends fo|o
SyntaxMatcher.IsTailMatch<ExtendsDeclarationSyntax, SkippedTriviaSyntax, Token>(matchingNodes, (@extends, skipped, _) => @extends.Path == skipped);

private static bool IsResourceBodyContext(List<SyntaxBase> matchingNodes, int offset) =>
// resources only allow {} as the body so we don't need to worry about
// providing completions for a partially-typed identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,10 @@ public enum BicepCompletionContextKind : ulong
/// The current location is accessing properties or elements of a type.
/// </summary>
TypeMemberAccess = 1UL << 46,

/// <summary>
/// The current location needs a bicepparam file path completion for extends declaration
/// </summary>
ExtendsFilePath = 1UL << 47,
}
}
15 changes: 13 additions & 2 deletions src/Bicep.LangServer/Completions/BicepCompletionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ private IEnumerable<CompletionItem> GetLocalModulePathCompletions(SemanticModel
{
if (!context.Kind.HasFlag(BicepCompletionContextKind.ModulePath) &&
!context.Kind.HasFlag(BicepCompletionContextKind.TestPath) &&
!context.Kind.HasFlag(BicepCompletionContextKind.UsingFilePath))
!context.Kind.HasFlag(BicepCompletionContextKind.UsingFilePath) &&
!context.Kind.HasFlag(BicepCompletionContextKind.ExtendsFilePath))
{
return [];
}
Expand Down Expand Up @@ -637,10 +638,18 @@ private IEnumerable<CompletionItem> GetLocalModulePathCompletions(SemanticModel
var replacementSyntax = (context.EnclosingDeclaration as IArtifactReferenceSyntax)?.Path;
var replacementRange = replacementSyntax?.ToRange(model.SourceFile.LineStarts) ?? context.ReplacementRange;

var dirItems = CreateDirectoryCompletionItems(replacementRange, fileCompletionInfo);

if (context.Kind.HasFlag(BicepCompletionContextKind.ExtendsFilePath))
{
var bicepParamFileItems = CreateFileCompletionItems(model.SourceFile.FileUri, replacementRange, fileCompletionInfo, IsBicepParamFile, CompletionPriority.High);

return bicepParamFileItems.Concat(dirItems);
}

// Prioritize .bicep files higher than other files.
var bicepFileItems = CreateFileCompletionItems(model.SourceFile.FileUri, replacementRange, fileCompletionInfo, IsBicepFile, CompletionPriority.High);
var armTemplateFileItems = CreateFileCompletionItems(model.SourceFile.FileUri, replacementRange, fileCompletionInfo, IsArmTemplateFileLike, CompletionPriority.Medium);
var dirItems = CreateDirectoryCompletionItems(replacementRange, fileCompletionInfo);

return bicepFileItems.Concat(armTemplateFileItems).Concat(dirItems);
}
Expand All @@ -653,6 +662,8 @@ private IEnumerable<CompletionItem> GetLocalModulePathCompletions(SemanticModel

bool IsBicepFile(Uri fileUri) => PathHelper.HasBicepExtension(fileUri);

bool IsBicepParamFile(Uri fileUri) => PathHelper.HasBicepparamsExtension(fileUri);

bool IsArmTemplateFileLike(Uri fileUri)
{
if (PathHelper.HasExtension(fileUri, LanguageConstants.ArmTemplateFileExtension))
Expand Down
Loading