Skip to content

Commit

Permalink
Changes to get type from GetDeclaredType(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsubra committed Apr 14, 2021
1 parent 751dccf commit e97046a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void CompletionPriorityOfNonResourceSnippets_ShouldBeMedium()
public void GetResourceBodyCompletionSnippet_WithValidTypeAndNoDependencies_ShouldReturnSnippet()
{
SnippetsProvider snippetsProvider = new SnippetsProvider();
Snippet? snippet = snippetsProvider.GetResourceBodyCompletionSnippet("'Microsoft.Network/dnsZones@2018-05-01'");
Snippet? snippet = snippetsProvider.GetResourceBodyCompletionSnippet("Microsoft.Network/dnsZones@2018-05-01");

Assert.IsNotNull(snippet);
Assert.AreEqual("res-dns-zone", snippet.Prefix);
Expand Down Expand Up @@ -177,7 +177,7 @@ public void GetResourceBodyCompletionSnippet_WithInvalidType_ShouldReturnNull(st
public void GetResourceBodyCompletionSnippet_WithValidTypeAndDependencies_ShouldReturnSnippet()
{
SnippetsProvider snippetsProvider = new SnippetsProvider();
Snippet? snippet = snippetsProvider.GetResourceBodyCompletionSnippet("'Microsoft.Automation/automationAccounts/modules@2015-10-31'");
Snippet? snippet = snippetsProvider.GetResourceBodyCompletionSnippet("Microsoft.Automation/automationAccounts/modules@2015-10-31");

Assert.IsNotNull(snippet);
Assert.AreEqual("res-automation-module", snippet.Prefix);
Expand Down
18 changes: 9 additions & 9 deletions src/Bicep.LangServer/Completions/BicepCompletionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem;
using Bicep.Core.TypeSystem.Az;
using Bicep.LanguageServer.Extensions;
using Bicep.LanguageServer.Snippets;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
Expand Down Expand Up @@ -58,7 +59,7 @@ public IEnumerable<CompletionItem> GetFilteredCompletions(Compilation compilatio
.Concat(GetResourceTypeCompletions(model, context))
.Concat(GetModulePathCompletions(model, context))
.Concat(GetModuleBodyCompletions(context))
.Concat(GetResourceBodyCompletions(context))
.Concat(GetResourceBodyCompletions(compilation, context))
.Concat(GetParameterDefaultValueCompletions(model, context))
.Concat(GetVariableValueCompletions(context))
.Concat(GetOutputValueCompletions(model, context))
Expand Down Expand Up @@ -338,11 +339,11 @@ private IEnumerable<CompletionItem> GetOutputValueCompletions(SemanticModel mode
return GetValueCompletionsForType(declaredType, context.ReplacementRange, model, context, loopsAllowed: true);
}

private IEnumerable<CompletionItem> GetResourceBodyCompletions(BicepCompletionContext context)
private IEnumerable<CompletionItem> GetResourceBodyCompletions(Compilation compilation, BicepCompletionContext context)
{
if (context.Kind.HasFlag(BicepCompletionContextKind.ResourceBody))
{
yield return CreateResourceBodyCompletion(context);
yield return CreateResourceBodyCompletion(compilation, context);

yield return CreateResourceOrModuleConditionCompletion(context.ReplacementRange);

Expand All @@ -354,15 +355,14 @@ private IEnumerable<CompletionItem> GetResourceBodyCompletions(BicepCompletionCo
}
}

private CompletionItem CreateResourceBodyCompletion(BicepCompletionContext context)
private CompletionItem CreateResourceBodyCompletion(Compilation compilation, BicepCompletionContext context)
{
StringSyntax? stringSyntax = (context.EnclosingDeclaration as ResourceDeclarationSyntax)?.TypeString;

if (stringSyntax is not null)
if (context.EnclosingDeclaration is ResourceDeclarationSyntax resourceDeclarationSyntax)
{
string type = stringSyntax.StringTokens[0].Text;
IBinder binder = compilation.GetEntrypointSemanticModel().Binder;
TypeSymbol typeSymbol = resourceDeclarationSyntax.GetDeclaredType(binder, AzResourceTypeProvider.CreateWithAzTypes());

Snippet? snippet = SnippetsProvider.GetResourceBodyCompletionSnippet(type);
Snippet? snippet = SnippetsProvider.GetResourceBodyCompletionSnippet(typeSymbol.Name);

if (snippet is not null)
{
Expand Down
21 changes: 6 additions & 15 deletions src/Bicep.LangServer/Snippets/SnippetsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ private void CacheResourceDeclarationAndDependencies(string template, string man

foreach (KeyValuePair<DeclaredSymbol, ImmutableHashSet<ResourceDependency>> kvp in dependencies)
{
(ResourceDeclarationSyntax? resourceDeclarationSyntax, string? resourceType) = GetResourceDeclarationSyntaxWithType(kvp.Key);
DeclaredSymbol declaredSymbol = kvp.Key;

if (resourceDeclarationSyntax is not null && resourceType is not null)
if (declaredSymbol.DeclaringSyntax is ResourceDeclarationSyntax resourceDeclarationSyntax)
{
CacheResourceDeclaration(resourceDeclarationSyntax, resourceType, template);
CacheResourceDependencies(kvp.Value, template, resourceType);
string type = declaredSymbol.Type.Name;

CacheResourceDeclaration(resourceDeclarationSyntax, type, template);
CacheResourceDependencies(kvp.Value, template, type);
}
}
}
Expand Down Expand Up @@ -132,17 +134,6 @@ private void CacheResourceDependencies(ImmutableHashSet<ResourceDependency> reso
}
}

private (ResourceDeclarationSyntax?, string?) GetResourceDeclarationSyntaxWithType(DeclaredSymbol declaredSymbol)
{
if (declaredSymbol.DeclaringSyntax is ResourceDeclarationSyntax resourceDeclarationSyntax &&
resourceDeclarationSyntax.TypeString is StringSyntax stringSyntax)
{
return (resourceDeclarationSyntax, stringSyntax.StringTokens.First().Text);
}

return (null, null);
}

private ImmutableDictionary<DeclaredSymbol, ImmutableHashSet<ResourceDependency>> GetResourceDependencies(string template, string manifestResourceName)
{
string path = Path.GetFullPath(manifestResourceName);
Expand Down

0 comments on commit e97046a

Please sign in to comment.