Skip to content

Commit

Permalink
Show tooltips on hover for type property definitions and type additio…
Browse files Browse the repository at this point in the history
…nal properties accesses (#13546)

Resolves #13461 
Resolves #13398

There were a couple of scenarios where the LS was not showing hover
tooltips for properties:
* When hovering over a type property definition
* When hovering over a property name that would match against a value's
additional properties type

There's no way to capture a description for an object's additional
properties type, but the hover can at least show the expected type.
  • Loading branch information
jeskew authored Mar 11, 2024
1 parent e7c4db3 commit 5e20b29
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task ProviderNameAndVersionAreUsedAsCacheKeys()
var (clientFactory, _) = RegistryHelper.CreateMockRegistryClients(repositoryNames.Select(name => (registry, $"{repositoryPath}/{name}")).ToArray());

var services = new ServiceBuilder()
.WithFeatureOverrides(new(ExtensibilityEnabled: true, ProviderRegistry: true, DynamicTypeLoadingEnabled: true))
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true, ProviderRegistry: true, DynamicTypeLoadingEnabled: true))
.WithContainerRegistryClientFactory(clientFactory);

foreach (var repoName in repositoryNames)
Expand All @@ -46,7 +46,7 @@ public async Task ProviderNameAndVersionAreUsedAsCacheKeys()
"main.bicep",
@$"
provider 'br:example.azurecr.io/test/provider/[email protected]' as foo
module mod './mod.bicep' = {{
name: 'mod'
params: {{ }}
Expand Down
59 changes: 54 additions & 5 deletions src/Bicep.Core/Semantics/SymbolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class SymbolHelper
// The decision to pass in getDeclaredTypeFunc as a lambda rather than the ITypeManager interface is deliberate.
// We should be conscious about not introducing cyclic dependencies, as this code is also used within the type manager, but it only needs declared types.

static PropertySymbol? GetPropertySymbol(TypeSymbol? baseType, string property)
static PropertySymbol? GetPropertySymbol(TypeSymbol? baseType, string property, bool useAdditionalPropertiesType)
{
if (baseType is null)
{
Expand All @@ -37,12 +37,30 @@ public static class SymbolHelper

if (typeProperty is null)
{
if (useAdditionalPropertiesType)
{
return GetAdditionalPropertiesSymbol(baseType);
}

return null;
}

return new PropertySymbol(property, typeProperty.Description, typeProperty.TypeReference.Type);
}

static PropertySymbol? GetAdditionalPropertiesSymbol(TypeSymbol? baseType)
{
if (baseType is null ||
TypeAssignmentVisitor.UnwrapType(baseType) is not ObjectType objectType ||
objectType.AdditionalPropertiesType is null ||
objectType.AdditionalPropertiesFlags.HasFlag(TypePropertyFlags.FallbackProperty))
{
return null;
}

return new PropertySymbol("*", string.Empty, objectType.AdditionalPropertiesType.Type);
}

switch (syntax)
{
case InstanceFunctionCallSyntax ifc:
Expand All @@ -69,20 +87,33 @@ public static class SymbolHelper
return null;
}
case InstanceParameterizedTypeInstantiationSyntax iptic:
return GetPropertySymbol(getDeclaredTypeFunc(iptic.BaseExpression), iptic.PropertyName.IdentifierName);
return GetPropertySymbol(getDeclaredTypeFunc(iptic.BaseExpression), iptic.PropertyName.IdentifierName, true);
case PropertyAccessSyntax propertyAccess:
{
var baseType = getDeclaredTypeFunc(propertyAccess.BaseExpression);
var property = propertyAccess.PropertyName.IdentifierName;

return GetPropertySymbol(baseType, property);
return GetPropertySymbol(baseType, property, true);
}
case TypePropertyAccessSyntax typePropertyAccess:
{
var baseType = getDeclaredTypeFunc(typePropertyAccess.BaseExpression);
if (baseType is not null)
{
baseType = TypeAssignmentVisitor.UnwrapType(baseType);
}
var property = typePropertyAccess.PropertyName.IdentifierName;

return GetPropertySymbol(baseType, property);
return GetPropertySymbol(baseType, property, false);
}
case TypeAdditionalPropertiesAccessSyntax addlPropertiesAccess:
{
var baseType = getDeclaredTypeFunc(addlPropertiesAccess.BaseExpression);
if (baseType is not null)
{
baseType = TypeAssignmentVisitor.UnwrapType(baseType);
}
return GetAdditionalPropertiesSymbol(baseType);
}
case ObjectPropertySyntax objectProperty:
{
Expand All @@ -97,7 +128,25 @@ public static class SymbolHelper
return null;
}

return GetPropertySymbol(baseType, property);
return GetPropertySymbol(baseType, property, true);
}
case ObjectTypePropertySyntax objectTypeProperty:
{
if (objectTypeProperty.TryGetKeyText() is not string propertyName || binder.GetParent(objectTypeProperty) is not SyntaxBase parentSyntax)
{
return null;
}

return GetPropertySymbol(getDeclaredTypeFunc(parentSyntax), propertyName, false);
}
case ObjectTypeAdditionalPropertiesSyntax addlProperties:
{
if (binder.GetParent(addlProperties) is not SyntaxBase parentSyntax)
{
return null;
}

return GetAdditionalPropertiesSymbol(getDeclaredTypeFunc(parentSyntax));
}
}

Expand Down
Loading

0 comments on commit 5e20b29

Please sign in to comment.