Skip to content
Open
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/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,52 @@ public async Task TypePropertyDecoratorsShouldAlignWithPropertyType()
completions.Should().NotContain(x => x.Label == "discriminator");
}

[TestMethod]
public async Task InlineTypePropertyDecoratorsShouldAlignWithPropertyType()
{
var fileWithCursors = """
param clusterSettings {
@|
name: string
}
""";
var (text, cursor) = ParserHelper.GetFileWithSingleCursor(fileWithCursors, '|');
var file = await new ServerRequestHelper(TestContext, DefaultServer).OpenFile(text);
var completions = await file.RequestAndResolveCompletions(cursor);

completions.Should().Contain(x => x.Label == "description");
completions.Should().Contain(x => x.Label == "metadata");
completions.Should().Contain(x => x.Label == "minLength");
completions.Should().Contain(x => x.Label == "maxLength");

completions.Should().NotContain(x => x.Label == "allowed");
completions.Should().NotContain(x => x.Label == "sealed");
completions.Should().NotContain(x => x.Label == "discriminator");
}

[TestMethod]
public async Task QualifiedInlineTypePropertyDecoratorsShouldAlignWithPropertyType()
{
var fileWithCursors = """
param clusterSettings {
@sys.|
name: string
}
""";
var (text, cursor) = ParserHelper.GetFileWithSingleCursor(fileWithCursors, '|');
var file = await new ServerRequestHelper(TestContext, DefaultServer).OpenFile(text);
var completions = await file.RequestAndResolveCompletions(cursor);

completions.Should().Contain(x => x.Label == "description");
completions.Should().Contain(x => x.Label == "metadata");
completions.Should().Contain(x => x.Label == "minLength");
completions.Should().Contain(x => x.Label == "maxLength");

completions.Should().NotContain(x => x.Label == "allowed");
completions.Should().NotContain(x => x.Label == "sealed");
completions.Should().NotContain(x => x.Label == "discriminator");
}

[TestMethod]
public async Task ModuleCompletionsShouldNotBeUrlEscaped()
{
Expand Down
47 changes: 31 additions & 16 deletions src/Bicep.LangServer/Completions/BicepCompletionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,9 @@ private static IEnumerable<CompletionItem> GetAccessibleSymbolCompletions(Semant
var enclosingDeclarationSymbol = context.EnclosingDeclaration == null
? null
: model.GetSymbolInfo(context.EnclosingDeclaration);
var decoratorTargetType = context.EnclosingDecorable is null
? null
: model.GetDeclaredType(context.EnclosingDecorable);

// local function
void AddSymbolCompletions(IDictionary<string, CompletionItem> result, IEnumerable<Symbol> symbols)
Expand All @@ -1046,9 +1049,7 @@ IEnumerable<FunctionSymbol> GetAccessibleDecoratorFunctionsWithCache(NamespaceTy
return result;
}

result = GetAccessibleDecoratorFunctions(namespaceType,
context.EnclosingDecorable is null ? null : model.GetDeclaredType(context.EnclosingDecorable),
enclosingDeclarationSymbol);
result = GetAccessibleDecoratorFunctions(namespaceType, context.EnclosingDecorable, decoratorTargetType, enclosingDeclarationSymbol);
accessibleDecoratorFunctionsCache.Add(namespaceType, result);

return result;
Expand Down Expand Up @@ -1136,7 +1137,7 @@ IEnumerable<FunctionSymbol> GetAccessibleDecoratorFunctionsWithCache(NamespaceTy
return completions.Values;
}

private static IEnumerable<FunctionSymbol> GetAccessibleDecoratorFunctions(NamespaceType namespaceType, TypeSymbol? targetType, Symbol? enclosingDeclarationSymbol)
private static IEnumerable<FunctionSymbol> GetAccessibleDecoratorFunctions(NamespaceType namespaceType, SyntaxBase? enclosingDecorable, TypeSymbol? targetType, Symbol? enclosingDeclarationSymbol)
{
// Local function.
IEnumerable<FunctionSymbol> GetAccessible(IEnumerable<FunctionSymbol> symbols, TypeSymbol targetType, FunctionFlags flags) =>
Expand All @@ -1146,25 +1147,40 @@ IEnumerable<FunctionSymbol> GetAccessible(IEnumerable<FunctionSymbol> symbols, T

var knownDecoratorFunctions = namespaceType.DecoratorResolver.GetKnownDecoratorFunctions().Values;

return enclosingDeclarationSymbol switch
return GetDecoratorCompletionTarget(enclosingDecorable, targetType, enclosingDeclarationSymbol) switch
{
MetadataSymbol metadataSymbol => GetAccessible(knownDecoratorFunctions, metadataSymbol.Type, FunctionFlags.MetadataDecorator),
ParameterSymbol parameterSymbol => GetAccessible(knownDecoratorFunctions, parameterSymbol.Type, FunctionFlags.ParameterDecorator),
TypeAliasSymbol declaredTypeSymbol when targetType is not null => GetAccessible(knownDecoratorFunctions, (targetType as TypeType)?.Unwrapped ?? targetType, FunctionFlags.TypeDecorator),
VariableSymbol variableSymbol => GetAccessible(knownDecoratorFunctions, variableSymbol.Type, FunctionFlags.VariableDecorator),
DeclaredFunctionSymbol functionSymbol => GetAccessible(knownDecoratorFunctions, functionSymbol.Type, FunctionFlags.FunctionDecorator),
ResourceSymbol resourceSymbol => GetAccessible(knownDecoratorFunctions, resourceSymbol.Type, FunctionFlags.ResourceDecorator),
ModuleSymbol moduleSymbol => GetAccessible(knownDecoratorFunctions, moduleSymbol.Type, FunctionFlags.ModuleDecorator),
OutputSymbol outputSymbol => GetAccessible(knownDecoratorFunctions, outputSymbol.Type, FunctionFlags.OutputDecorator),
{ } decoratorTarget => GetAccessible(knownDecoratorFunctions, decoratorTarget.targetType, decoratorTarget.flags),
/*
* The decorator is dangling if enclosingDeclarationSymbol is null. Return all decorator factory functions since
* we don't know which kind of declaration it will attach to.
*/
null => knownDecoratorFunctions,
null when enclosingDeclarationSymbol is null => knownDecoratorFunctions,
_ => []
};
}

private static (TypeSymbol targetType, FunctionFlags flags)? GetDecoratorCompletionTarget(SyntaxBase? enclosingDecorable, TypeSymbol? targetType, Symbol? enclosingDeclarationSymbol)
{
if (enclosingDecorable is TypeDeclarationSyntax or ObjectTypePropertySyntax or ObjectTypeAdditionalPropertiesSyntax or TupleTypeItemSyntax && targetType is not null)
{
return ((targetType as TypeType)?.Unwrapped ?? targetType, FunctionFlags.TypeDecorator);
}

return enclosingDeclarationSymbol switch
{
MetadataSymbol metadataSymbol => (metadataSymbol.Type, FunctionFlags.MetadataDecorator),
ParameterSymbol parameterSymbol => (parameterSymbol.Type, FunctionFlags.ParameterDecorator),
TypeAliasSymbol when targetType is not null => ((targetType as TypeType)?.Unwrapped ?? targetType, FunctionFlags.TypeDecorator),
VariableSymbol variableSymbol => (variableSymbol.Type, FunctionFlags.VariableDecorator),
DeclaredFunctionSymbol functionSymbol => (functionSymbol.Type, FunctionFlags.FunctionDecorator),
ResourceSymbol resourceSymbol => (resourceSymbol.Type, FunctionFlags.ResourceDecorator),
ModuleSymbol moduleSymbol => (moduleSymbol.Type, FunctionFlags.ModuleDecorator),
ExtensionNamespaceSymbol extensionSymbol => (extensionSymbol.DeclaredType, FunctionFlags.ExtensionDecorator),
OutputSymbol outputSymbol => (outputSymbol.Type, FunctionFlags.OutputDecorator),
_ => null,
};
}

private static IEnumerable<CompletionItem> GetMemberAccessCompletions(Compilation compilation, BicepCompletionContext context)
{
if (!context.Kind.HasFlag(BicepCompletionContextKind.MemberAccess) || context.PropertyAccess == null)
Expand All @@ -1180,8 +1196,7 @@ private static IEnumerable<CompletionItem> GetMemberAccessCompletions(Compilatio
var enclosingDeclarationSymbol = context.EnclosingDeclaration is null ? null : model.GetSymbolInfo(context.EnclosingDeclaration);
var decoratorTargetType = context.EnclosingDecorable is null ? null : model.GetDeclaredType(context.EnclosingDecorable);

return GetAccessibleDecoratorFunctions(namespaceType, decoratorTargetType, enclosingDeclarationSymbol)
.Select(symbol => CreateSymbolCompletion(symbol, context.ReplacementRange, model));
return GetAccessibleDecoratorFunctions(namespaceType, context.EnclosingDecorable, decoratorTargetType, enclosingDeclarationSymbol).Select(symbol => CreateSymbolCompletion(symbol, context.ReplacementRange, model));
}

if (declaredType is not null && TypeHelper.TryRemoveNullability(declaredType) is TypeSymbol nonNullable)
Expand Down
Loading