forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BicepTextDocumentSyncHandler.cs
118 lines (96 loc) · 5.22 KB
/
BicepTextDocumentSyncHandler.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core;
using Bicep.Core.FileSystem;
using Bicep.LanguageServer.CompilationManager;
using Bicep.LanguageServer.Configuration;
using Bicep.LanguageServer.Utils;
using MediatR;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
namespace Bicep.LanguageServer.Handlers
{
public class BicepTextDocumentSyncHandler : TextDocumentSyncHandlerBase
{
private readonly ICompilationManager compilationManager;
private readonly IBicepConfigChangeHandler bicepConfigChangeHandler;
private readonly DocumentSelectorFactory documentSelectorFactory;
public BicepTextDocumentSyncHandler(ICompilationManager compilationManager, IBicepConfigChangeHandler bicepConfigChangeHandler, DocumentSelectorFactory documentSelectorFactory)
{
this.bicepConfigChangeHandler = bicepConfigChangeHandler;
this.compilationManager = compilationManager;
this.documentSelectorFactory = documentSelectorFactory;
}
public override TextDocumentAttributes GetTextDocumentAttributes(DocumentUri uri)
{
if (ConfigurationHelper.IsBicepConfigFile(uri))
{
return new TextDocumentAttributes(uri, LanguageConstants.JsoncLanguageId);
}
if (PathHelper.HasBicepparamsExtension(uri.ToUriEncoded()))
{
return new TextDocumentAttributes(uri, LanguageConstants.ParamsLanguageId);
}
return new TextDocumentAttributes(uri, LanguageConstants.LanguageId);
}
public override Task<Unit> Handle(DidChangeTextDocumentParams request, CancellationToken token)
{
// we have full sync enabled, so apparently first change is the whole document
var contents = request.ContentChanges.First().Text;
var documentUri = request.TextDocument.Uri;
this.compilationManager.UpdateCompilation(documentUri, request.TextDocument.Version, contents);
// Handle scenario where the bicepconfig.json file was opened prior to
// language service activation. If the config file was opened before the language server
// activation, there won't be an entry for it in the cache. We'll capture the state of the
// config file on disk when it's changed and cache it.
if (ConfigurationHelper.IsBicepConfigFile(documentUri))
{
bicepConfigChangeHandler.HandleBicepConfigChangeEvent(documentUri);
}
return Unit.Task;
}
public override Task<Unit> Handle(DidOpenTextDocumentParams request, CancellationToken cancellationToken)
{
var documentUri = request.TextDocument.Uri;
// If the documentUri corresponds to bicepconfig.json, we'll add an entry to activeBicepConfigCache.
if (ConfigurationHelper.IsBicepConfigFile(documentUri)) //potentially copy this for bicep params
{
bicepConfigChangeHandler.HandleBicepConfigOpenEvent(documentUri);
}
this.compilationManager.OpenCompilation(documentUri, request.TextDocument.Version, request.TextDocument.Text, request.TextDocument.LanguageId);
return Unit.Task;
}
public override Task<Unit> Handle(DidSaveTextDocumentParams request, CancellationToken cancellationToken)
{
var documentUri = request.TextDocument.Uri;
// If the documentUri corresponds to bicepconfig.json and there's an entry in activeBicepConfigCache,
// we'll use the last known configuration and the one from currently saved config file to figure out
// if we need to send out telemetry information regarding the config change.
// We'll also update the entry in activeBicepConfigCache.
if (ConfigurationHelper.IsBicepConfigFile(documentUri))
{
bicepConfigChangeHandler.HandleBicepConfigSaveEvent(documentUri);
}
return Unit.Task;
}
public override Task<Unit> Handle(DidCloseTextDocumentParams request, CancellationToken cancellationToken)
{
var documentUri = request.TextDocument.Uri;
// If the documentUri corresponds to bicepconfig.json, we'll remove the entry from activeBicepConfigCache.
if (ConfigurationHelper.IsBicepConfigFile(documentUri))
{
bicepConfigChangeHandler.HandleBicepConfigCloseEvent(documentUri);
}
this.compilationManager.CloseCompilation(request.TextDocument.Uri);
return Unit.Task;
}
protected override TextDocumentSyncRegistrationOptions CreateRegistrationOptions(TextSynchronizationCapability capability, ClientCapabilities clientCapabilities) => new()
{
Change = TextDocumentSyncKind.Full,
DocumentSelector = documentSelectorFactory.CreateForAllSupportedLangIds()
};
}
}