forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBicepHoverHandler.cs
335 lines (281 loc) · 15.2 KB
/
BicepHoverHandler.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Bicep.Core;
using Bicep.Core.Extensions;
using Bicep.Core.Registry;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Metadata;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem;
using Bicep.LanguageServer.Providers;
using Bicep.LanguageServer.Utils;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace Bicep.LanguageServer.Handlers
{
public class BicepHoverHandler : HoverHandlerBase
{
private readonly IModuleDispatcher moduleDispatcher;
private readonly IModuleRegistryProvider moduleRegistryProvider;
private readonly ISymbolResolver symbolResolver;
private const int MaxHoverMarkdownCodeBlockLength = 90000;
//actual limit for hover in VS code is 100,000 characters.
public BicepHoverHandler(
IModuleDispatcher moduleDispatcher,
IModuleRegistryProvider moduleRegistryProvider,
ISymbolResolver symbolResolver)
{
this.moduleDispatcher = moduleDispatcher;
this.moduleRegistryProvider = moduleRegistryProvider;
this.symbolResolver = symbolResolver;
}
public override async Task<Hover?> Handle(HoverParams request, CancellationToken cancellationToken)
{
var result = this.symbolResolver.ResolveSymbol(request.TextDocument.Uri, request.Position);
if (result == null)
{
return null;
}
var markdown = await GetMarkdown(request, result, this.moduleDispatcher, this.moduleRegistryProvider);
if (markdown == null)
{
return null;
}
return new Hover
{
Contents = markdown,
Range = PositionHelper.GetNameRange(result.Context.LineStarts, result.Origin)
};
}
private static string? TryGetDescriptionMarkdown(SymbolResolutionResult result, DeclaredSymbol symbol)
{
if (symbol.DeclaringSyntax is DecorableSyntax decorableSyntax &&
DescriptionHelper.TryGetFromDecorator(result.Context.Compilation.GetEntrypointSemanticModel(), decorableSyntax) is { } description)
{
return description;
}
return null;
}
private static async Task<MarkedStringsOrMarkupContent?> GetMarkdown(
HoverParams request,
SymbolResolutionResult result,
IModuleDispatcher moduleDispatcher,
IModuleRegistryProvider moduleRegistryProvider)
{
// all of the generated markdown includes the language id to avoid VS code rendering
// with multiple borders
switch (result.Symbol)
{
case ImportedNamespaceSymbol import:
return AsMarkdown(CodeBlockWithDescription(
$"import {import.Name}", TryGetDescriptionMarkdown(result, import)));
case MetadataSymbol metadata:
return AsMarkdown(CodeBlockWithDescription(
$"metadata {metadata.Name}: {metadata.Type}", TryGetDescriptionMarkdown(result, metadata)));
case ParameterSymbol parameter:
return AsMarkdown(CodeBlockWithDescription(
WithTypeModifiers($"param {parameter.Name}: {parameter.Type}", parameter.Type), TryGetDescriptionMarkdown(result, parameter)));
case TypeAliasSymbol declaredType:
return AsMarkdown(CodeBlockWithDescription(
WithTypeModifiers($"type {declaredType.Name}: {declaredType.Type}", declaredType.Type), TryGetDescriptionMarkdown(result, declaredType)));
case AmbientTypeSymbol ambientType:
return AsMarkdown(CodeBlock(WithTypeModifiers($"type {ambientType.Name}: {ambientType.Type}", ambientType.Type)));
case VariableSymbol variable:
return AsMarkdown(CodeBlockWithDescription($"var {variable.Name}: {variable.Type}", TryGetDescriptionMarkdown(result, variable)));
case ResourceSymbol resource:
var docsSuffix = TryGetTypeDocumentationLink(resource) is { } typeDocsLink ? MarkdownHelper.GetDocumentationLink(typeDocsLink) : "";
var description = TryGetDescriptionMarkdown(result, resource);
return AsMarkdown(CodeBlockWithDescription(
$"resource {resource.Name} {(resource.Type is ResourceType ? $"'{resource.Type}'" : resource.Type)}",
description is { } ? $"{description}\n{docsSuffix}" : docsSuffix));
case ModuleSymbol module:
return await GetModuleMarkdown(request, result, moduleDispatcher, moduleRegistryProvider, module);
case OutputSymbol output:
return AsMarkdown(CodeBlockWithDescription(
WithTypeModifiers($"output {output.Name}: {output.Type}", output.Type), TryGetDescriptionMarkdown(result, output)));
case BuiltInNamespaceSymbol builtInNamespace:
return AsMarkdown(CodeBlock($"{builtInNamespace.Name} namespace"));
case IFunctionSymbol function when result.Origin is FunctionCallSyntaxBase functionCall:
// it's not possible for a non-function call syntax to resolve to a function symbol
// but this simplifies the checks
return GetFunctionMarkdown(function, functionCall, result.Context.Compilation.GetEntrypointSemanticModel());
case DeclaredFunctionSymbol function:
// A declared function can only have a single overload!
return AsMarkdown(GetFunctionOverloadMarkdown(function.Overloads.Single()));
case PropertySymbol property:
return AsMarkdown(CodeBlockWithDescription($"{property.Name}: {property.Type}", property.Description));
case LocalVariableSymbol local:
return AsMarkdown(CodeBlock($"{local.Name}: {local.Type}"));
case ParameterAssignmentSymbol parameterAssignment:
if (GetDeclaredParameterMetadata(parameterAssignment) is not ParameterMetadata declaredParamMetadata)
{
return null;
}
return AsMarkdown(CodeBlockWithDescription(
WithTypeModifiers($"param {parameterAssignment.Name}: {declaredParamMetadata.TypeReference.Type}", declaredParamMetadata.TypeReference.Type), declaredParamMetadata.Description));
default:
return null;
}
}
private static async Task<MarkedStringsOrMarkupContent> GetModuleMarkdown(HoverParams request, SymbolResolutionResult result, IModuleDispatcher moduleDispatcher, IModuleRegistryProvider moduleRegistryProvider, ModuleSymbol module)
{
var filePath = SyntaxHelper.TryGetModulePath(module.DeclaringModule, out _) ?? string.Empty;
var descriptionLines = new List<string?>();
descriptionLines.Add(TryGetDescriptionMarkdown(result, module));
var uri = request.TextDocument.Uri.ToUri();
var registries = moduleRegistryProvider.Registries(uri);
if (registries != null &&
registries.Any() &&
moduleDispatcher.TryGetModuleReference(module.DeclaringModule, uri, out var moduleReference, out _) &&
moduleReference is not null)
{
var registry = registries.FirstOrDefault(r => r.Scheme == moduleReference.Scheme);
if (registry is not null)
{
try
{
descriptionLines.Add(await registry.TryGetDescription(moduleReference));
}
catch
{
// ignore
}
try
{
if (registry.GetDocumentationUri(moduleReference) is string documentationUri && !string.IsNullOrEmpty(documentationUri))
{
descriptionLines.Add(MarkdownHelper.GetDocumentationLink(documentationUri));
}
}
catch
{
// ignore
}
}
}
var descriptions = string.Join("\n\n", descriptionLines.WhereNotNull());
return AsMarkdown(CodeBlockWithDescription($"module {module.Name} '{filePath}'", descriptions));
}
private static ParameterMetadata? GetDeclaredParameterMetadata(ParameterAssignmentSymbol symbol)
{
if (!symbol.Context.Compilation.GetEntrypointSemanticModel().Root.TryGetBicepFileSemanticModelViaUsing(out var bicepSemanticModel, out _))
{
// failed to resolve using
return null;
}
if (bicepSemanticModel.Parameters.TryGetValue(symbol.Name, out var parameterMetadata))
{
return parameterMetadata;
}
return null;
}
private static string WithTypeModifiers(string coreContent, TypeSymbol type)
{
type = UnwrapType(type);
StringBuilder contentBuilder = new();
switch (type)
{
case IntegerType integer:
if (integer.MinValue.HasValue)
{
contentBuilder.Append("@minValue(").Append(integer.MinValue.Value).Append(")\n");
}
if (integer.MaxValue.HasValue)
{
contentBuilder.Append("@maxValue(").Append(integer.MaxValue.Value).Append(")\n");
}
break;
case StringType @string:
if (@string.MinLength.HasValue)
{
contentBuilder.Append("@minLength(").Append(@string.MinLength.Value).Append(")\n");
}
if (@string.MaxLength.HasValue)
{
contentBuilder.Append("@maxLength(").Append(@string.MaxLength.Value).Append(")\n");
}
break;
case ArrayType array when array is not TupleType:
if (array.MinLength.HasValue)
{
contentBuilder.Append("@minLength(").Append(array.MinLength.Value).Append(")\n");
}
if (array.MaxLength.HasValue)
{
contentBuilder.Append("@maxLength(").Append(array.MaxLength.Value).Append(")\n");
}
break;
}
if (type.ValidationFlags.HasFlag(TypeSymbolValidationFlags.IsSecure))
{
contentBuilder.Append("@secure()\n");
}
contentBuilder.Append(coreContent);
return contentBuilder.ToString();
}
private static TypeSymbol UnwrapType(TypeSymbol type) => type switch
{
TypeType tt => UnwrapType(tt.Unwrapped),
_ when TypeHelper.TryRemoveNullability(type) is { } nonNullable => UnwrapType(nonNullable),
_ => type,
};
//we need to check for overflow due to using code blocks.
//if we reach limit in a code block vscode will truncate it automatically, the block will not be terminated so the hover will not be properly formatted
//therefore we need to check for the limit ourselves and truncate text inside code block, making sure it's terminated properly.
private static string CodeBlock(string content) =>
$"```bicep\n{(content.Length > MaxHoverMarkdownCodeBlockLength ? content.Substring(0, MaxHoverMarkdownCodeBlockLength) : content)}\n```\n";
// Markdown needs two leading whitespaces before newline to insert a line break
private static string CodeBlockWithDescription(string content, string? description) =>
CodeBlock(content) + (!string.IsNullOrEmpty(description) ? $"{description.Replace("\n", " \n")}\n" : string.Empty);
private static MarkedStringsOrMarkupContent GetFunctionMarkdown(IFunctionSymbol function, FunctionCallSyntaxBase functionCall, SemanticModel model)
{
if (model.TypeManager.GetMatchedFunctionOverload(functionCall) is { } matchedOverload)
{
return AsMarkdown(GetFunctionOverloadMarkdown(matchedOverload));
}
var potentialMatches =
function.Overloads
.Select(overload => (overload, matchType:
overload.Match(functionCall.Arguments.Select(model.GetTypeInfo).ToList(), out _, out _)))
.Where(t => t.matchType == FunctionMatchResult.Match || t.matchType == FunctionMatchResult.PotentialMatch)
.Select(t => t.overload)
.ToList();
// If there are no potential matches, just show all overloads
IEnumerable<FunctionOverload> toShow = potentialMatches.Count > 0 ? potentialMatches : function.Overloads;
return AsMarkdown(toShow.Select(GetFunctionOverloadMarkdown));
}
private static string GetFunctionOverloadMarkdown(FunctionOverload overload)
=> CodeBlockWithDescription($"function {overload.Name}{overload.TypeSignature}", overload.Description);
private static string? TryGetTypeDocumentationLink(ResourceSymbol resource)
{
if (resource.TryGetResourceType() is { } resourceType &&
resourceType.DeclaringNamespace.ProviderNameEquals(AzNamespaceType.BuiltInName) &&
resourceType.DeclaringNamespace.ResourceTypeProvider.HasDefinedType(resourceType.TypeReference))
{
var provider = resourceType.TypeReference.TypeSegments.First().ToLowerInvariant();
var typePath = resourceType.TypeReference.TypeSegments.Skip(1).Select(x => x.ToLowerInvariant());
return $"https://docs.microsoft.com/azure/templates/{provider}/{string.Join('/', typePath)}?tabs=bicep";
}
return null;
}
private static MarkedStringsOrMarkupContent AsMarkdown(string markdown) => new MarkedStringsOrMarkupContent(new MarkupContent
{
Kind = MarkupKind.Markdown,
Value = markdown,
});
private static MarkedStringsOrMarkupContent AsMarkdown(IEnumerable<string> markdown)
=> new MarkedStringsOrMarkupContent(markdown.Select(md => new MarkedString(md)));
protected override HoverRegistrationOptions CreateRegistrationOptions(HoverCapability capability, ClientCapabilities clientCapabilities) => new()
{
DocumentSelector = DocumentSelectorFactory.CreateForBicepAndParams()
};
}
}