forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBicepDocumentSymbolHandler.cs
112 lines (99 loc) · 4.93 KB
/
BicepDocumentSymbolHandler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Bicep.LanguageServer.CompilationManager;
using Bicep.LanguageServer.Extensions;
using Bicep.LanguageServer.Utils;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using SymbolKind = OmniSharp.Extensions.LanguageServer.Protocol.Models.SymbolKind;
namespace Bicep.LanguageServer.Handlers
{
public class BicepDocumentSymbolHandler : DocumentSymbolHandlerBase
{
private readonly ILogger<BicepDocumentSymbolHandler> logger;
private readonly ICompilationManager compilationManager;
public BicepDocumentSymbolHandler(ILogger<BicepDocumentSymbolHandler> logger, ICompilationManager compilationManager)
{
this.logger = logger;
this.compilationManager = compilationManager;
}
public override Task<SymbolInformationOrDocumentSymbolContainer> Handle(DocumentSymbolParams request, CancellationToken cancellationToken)
{
var context = this.compilationManager.GetCompilation(request.TextDocument.Uri);
if (context is null)
{
// we have not yet compiled this document, which shouldn't really happen
this.logger.LogError("Document symbol request arrived before file {Uri} could be compiled.", request.TextDocument.Uri);
return Task.FromResult(new SymbolInformationOrDocumentSymbolContainer());
}
return Task.FromResult(new SymbolInformationOrDocumentSymbolContainer(GetSymbols(context)));
}
private IEnumerable<SymbolInformationOrDocumentSymbol> GetSymbols(CompilationContext context)
{
var model = context.Compilation.GetEntrypointSemanticModel();
return model.Root.Declarations
.OrderBy(symbol => symbol.DeclaringSyntax.Span.Position)
.Select(symbol => new SymbolInformationOrDocumentSymbol(CreateDocumentSymbol(model, symbol, context.LineStarts)));
}
private DocumentSymbol CreateDocumentSymbol(SemanticModel model, DeclaredSymbol symbol, ImmutableArray<int> lineStarts)
{
var children = Enumerable.Empty<DocumentSymbol>();
if (symbol is ResourceSymbol resourceSymbol &&
resourceSymbol.DeclaringResource.TryGetBody() is ObjectSyntax body)
{
children = body.Resources
.Select(r => model.GetSymbolInfo(r) as ResourceSymbol)
.Where(s => s is ResourceSymbol)
.Select(s => CreateDocumentSymbol(model, s!, lineStarts));
}
return new DocumentSymbol
{
Name = symbol.Name,
Kind = SelectSymbolKind(symbol),
Detail = FormatDetail(symbol),
Children = new Container<DocumentSymbol>(children),
Range = symbol.DeclaringSyntax.ToRange(lineStarts),
// use the name node span with fallback to entire declaration span
SelectionRange = symbol.NameSource.ToRange(lineStarts)
};
}
private static SymbolKind SelectSymbolKind(DeclaredSymbol symbol) => symbol switch
{
ImportedNamespaceSymbol => SymbolKind.Namespace,
ParameterSymbol => SymbolKind.Field,
TypeAliasSymbol => SymbolKind.Field,
VariableSymbol => SymbolKind.Variable,
DeclaredFunctionSymbol => SymbolKind.Function,
ResourceSymbol => SymbolKind.Object,
ModuleSymbol => SymbolKind.Module,
OutputSymbol => SymbolKind.Interface,
ParameterAssignmentSymbol => SymbolKind.Constant,
_ => SymbolKind.Key,
};
private static string FormatDetail(DeclaredSymbol symbol) => symbol switch
{
ParameterSymbol parameter => parameter.Type.Name,
TypeAliasSymbol declaredType => declaredType.Type.Name,
VariableSymbol variable => variable.Type.Name,
DeclaredFunctionSymbol func => func.Type.Name,
ResourceSymbol resource => resource.Type.Name,
ModuleSymbol module => module.Type.Name,
OutputSymbol output => output.Type.Name,
ParameterAssignmentSymbol paramAssignment => paramAssignment.Type.Name,
_ => string.Empty,
};
protected override DocumentSymbolRegistrationOptions CreateRegistrationOptions(DocumentSymbolCapability capability, ClientCapabilities clientCapabilities) => new()
{
DocumentSelector = DocumentSelectorFactory.CreateForBicepAndParams()
};
}
}