forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BicepDocumentHighlightHandler.cs
55 lines (48 loc) · 2.18 KB
/
BicepDocumentHighlightHandler.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bicep.Core.Navigation;
using Bicep.Core.Syntax;
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 BicepDocumentHighlightHandler : DocumentHighlightHandlerBase
{
private readonly ISymbolResolver symbolResolver;
public BicepDocumentHighlightHandler(ISymbolResolver symbolResolver) : base()
{
this.symbolResolver = symbolResolver;
}
public override Task<DocumentHighlightContainer?> Handle(DocumentHighlightParams request, CancellationToken cancellationToken)
{
var result = this.symbolResolver.ResolveSymbol(request.TextDocument.Uri, request.Position);
if (result == null)
{
return Task.FromResult<DocumentHighlightContainer?>(null);
}
var highlights = result.Context.Compilation.GetEntrypointSemanticModel()
.FindReferences(result.Symbol)
.Select(referenceSyntax => new DocumentHighlight
{
Range = PositionHelper.GetNameRange(result.Context.LineStarts, referenceSyntax),
Kind = referenceSyntax switch
{
INamedDeclarationSyntax _ => DocumentHighlightKind.Write,
ObjectPropertySyntax _ => DocumentHighlightKind.Write,
_ => DocumentHighlightKind.Read,
},
});
return Task.FromResult<DocumentHighlightContainer?>(new DocumentHighlightContainer(highlights));
}
protected override DocumentHighlightRegistrationOptions CreateRegistrationOptions(DocumentHighlightCapability capability, ClientCapabilities clientCapabilities) => new()
{
DocumentSelector = DocumentSelectorFactory.Create()
};
}
}