forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BicepSymbolResolver.cs
61 lines (51 loc) · 2.27 KB
/
BicepSymbolResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.Navigation;
using Bicep.Core.Parsing;
using Bicep.Core.Syntax;
using Bicep.LanguageServer.CompilationManager;
using Bicep.LanguageServer.Utils;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace Bicep.LanguageServer.Providers
{
public class BicepSymbolResolver : ISymbolResolver
{
private readonly ILogger<BicepSymbolResolver> logger;
private readonly ICompilationManager compilationManager;
public BicepSymbolResolver(ILogger<BicepSymbolResolver> logger, ICompilationManager compilationManager)
{
this.logger = logger;
this.compilationManager = compilationManager;
}
public SymbolResolutionResult? ResolveSymbol(DocumentUri uri, Position position)
{
var context = this.compilationManager.GetCompilation(uri);
if (context == null)
{
// we have not yet compiled this document, which shouldn't really happen
this.logger.LogError("The symbol resolution request arrived before the file {Uri} could be compiled.", uri);
return null;
}
// convert text coordinates
int offset = PositionHelper.GetOffset(context.LineStarts, position);
var semanticModel = context.Compilation.GetEntrypointSemanticModel();
// locate the most specific node that can be bound as a symbol
var node = context.ProgramSyntax.TryFindMostSpecificNodeInclusive(
offset,
n => n is not IdentifierSyntax && n is not Token && n is not AliasAsClauseSyntax);
if (node is null)
{
// the program node must enclose all locations in the file, so this should not happen
this.logger.LogError("The symbol resolution request position exceeded the bounds of the file '{Uri}'.", uri);
return null;
}
if (semanticModel.GetSymbolInfo(node) is { } symbol)
{
return new SymbolResolutionResult(node, symbol, context);
}
return null;
}
}
}