-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLspServer.cs
More file actions
376 lines (334 loc) · 15.5 KB
/
Copy pathLspServer.cs
File metadata and controls
376 lines (334 loc) · 15.5 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Heddle.LanguageServices;
using StreamJsonRpc;
using LspProtocol = Heddle.LanguageServer.Protocol;
namespace Heddle.LanguageServer
{
/// <summary>
/// The hand-rolled LSP 3.17 layer (phase 6 D4–D8): thin StreamJsonRpc target methods, each a projection of a
/// <see cref="HeddleLanguageService"/> call plus DTO mapping. Full-document sync, 300 ms debounce,
/// request-forced analysis. No compiler logic lives here.
/// </summary>
internal sealed class LspServer
{
internal const string InformationalVersion = "1.0.0";
private const int DebounceMs = 300;
private readonly ConcurrentDictionary<string, (string Text, int Version)> _buffers =
new ConcurrentDictionary<string, (string, int)>(StringComparer.Ordinal);
private readonly ConcurrentDictionary<string, CancellationTokenSource> _debounce =
new ConcurrentDictionary<string, CancellationTokenSource>(StringComparer.Ordinal);
private readonly object _lifecycleGate = new object();
private JsonRpc _rpc;
private HeddleLanguageService _service;
private string _workspaceRoot;
private bool _initialized;
private bool _shutdownReceived;
private readonly TaskCompletionSource<int> _exit = new TaskCompletionSource<int>();
internal Task<int> ExitCode => _exit.Task;
internal bool ShutdownReceived => _shutdownReceived;
internal void Attach(JsonRpc rpc)
{
_rpc = rpc;
}
// ---- Lifecycle -----------------------------------------------------------------------------------------
[JsonRpcMethod("initialize", UseSingleObjectParameterDeserialization = true)]
public LspProtocol.InitializeResult Initialize(LspProtocol.InitializeParams @params)
{
lock (_lifecycleGate)
{
_workspaceRoot = ResolveWorkspaceRoot(@params);
var options = BuildOptions(_workspaceRoot, @params?.InitializationOptions);
_service = new HeddleLanguageService(options) { LogSink = LogSink };
_initialized = true;
}
return new LspProtocol.InitializeResult(
new LspProtocol.ServerCapabilities
{
TextDocumentSync = new LspProtocol.TextDocumentSyncOptions
{
OpenClose = true,
Change = 1, // Full
Save = new LspProtocol.SaveOptions()
},
CompletionProvider = new LspProtocol.CompletionOptions
{
TriggerCharacters = new[] { "@", "(", ".", ":", "," }
},
HoverProvider = true,
DefinitionProvider = true,
SemanticTokensProvider = new LspProtocol.SemanticTokensOptions
{
Legend = new LspProtocol.SemanticTokensLegend
{
TokenTypes = SemanticTokensBuilder.TokenTypes,
TokenModifiers = Array.Empty<string>()
},
Full = true
}
},
new LspProtocol.ServerInfo("heddle-lsp", InformationalVersion));
}
[JsonRpcMethod("initialized")]
public void Initialized()
{
}
[JsonRpcMethod("$/setTrace", UseSingleObjectParameterDeserialization = true)]
public void SetTrace(JsonElement value)
{
}
[JsonRpcMethod("shutdown")]
public object Shutdown()
{
_shutdownReceived = true;
_service?.Dispose();
_service = null;
return null;
}
[JsonRpcMethod("exit")]
public void Exit()
{
_exit.TrySetResult(_shutdownReceived ? 0 : 1);
}
[JsonRpcMethod("workspace/didChangeConfiguration", UseSingleObjectParameterDeserialization = true)]
public void DidChangeConfiguration(LspProtocol.DidChangeConfigurationParams @params)
{
// v1: re-read the workspace file/settings and rebuild. The one-shot export scan never re-runs (D23/D24).
lock (_lifecycleGate)
{
if (!_initialized)
return;
var options = BuildOptions(_workspaceRoot, @params?.Settings);
_service?.Dispose();
_service = new HeddleLanguageService(options) { LogSink = LogSink };
}
foreach (var uri in _buffers.Keys.ToArray())
AnalyzeAndPublish(uri);
}
// ---- Document sync -------------------------------------------------------------------------------------
[JsonRpcMethod("textDocument/didOpen", UseSingleObjectParameterDeserialization = true)]
public void DidOpen(LspProtocol.DidOpenTextDocumentParams @params)
{
var doc = @params.TextDocument;
_buffers[doc.Uri] = (doc.Text ?? string.Empty, doc.Version);
AnalyzeAndPublish(doc.Uri);
}
[JsonRpcMethod("textDocument/didChange", UseSingleObjectParameterDeserialization = true)]
public void DidChange(LspProtocol.DidChangeTextDocumentParams @params)
{
var uri = @params.TextDocument.Uri;
string text = _buffers.TryGetValue(uri, out var current) ? current.Text : string.Empty;
foreach (var change in @params.ContentChanges ?? Array.Empty<LspProtocol.TextDocumentContentChangeEvent>())
{
if (change.Range != null)
{
LogSink($"Ignoring ranged change event under Full sync for {uri} (client contract violation).");
continue;
}
text = change.Text ?? string.Empty;
}
_buffers[uri] = (text, @params.TextDocument.Version);
ScheduleDebounced(uri);
}
[JsonRpcMethod("textDocument/didSave", UseSingleObjectParameterDeserialization = true)]
public void DidSave(LspProtocol.DidSaveTextDocumentParams @params)
{
}
[JsonRpcMethod("textDocument/didClose", UseSingleObjectParameterDeserialization = true)]
public void DidClose(LspProtocol.DidCloseTextDocumentParams @params)
{
var uri = @params.TextDocument.Uri;
_buffers.TryRemove(uri, out _);
_service?.Close(UriToPath(uri));
PublishDiagnostics(uri, null, Array.Empty<LspProtocol.Diagnostic>());
}
// ---- Features ------------------------------------------------------------------------------------------
[JsonRpcMethod("textDocument/completion", UseSingleObjectParameterDeserialization = true)]
public LspProtocol.CompletionItem[] Completion(LspProtocol.CompletionParams @params, CancellationToken ct)
{
var uri = @params.TextDocument.Uri;
var analysis = EnsureAnalyzed(uri);
if (analysis == null)
return Array.Empty<LspProtocol.CompletionItem>();
int offset = analysis.Lines.PositionToOffset(@params.Position.Line, @params.Position.Character);
var result = _service.GetCompletions(UriToPath(uri), offset, ct);
return result.Items.Select(ToLspCompletion).ToArray();
}
[JsonRpcMethod("textDocument/hover", UseSingleObjectParameterDeserialization = true)]
public LspProtocol.Hover Hover(LspProtocol.HoverParams @params)
{
var uri = @params.TextDocument.Uri;
var analysis = EnsureAnalyzed(uri);
if (analysis == null)
return null;
int offset = analysis.Lines.PositionToOffset(@params.Position.Line, @params.Position.Character);
var hover = _service.GetHover(UriToPath(uri), offset);
if (hover == null)
return null;
return new LspProtocol.Hover(new LspProtocol.MarkupContent("markdown", hover.Markdown),
ToRange(analysis, hover.Offset, hover.Length));
}
[JsonRpcMethod("textDocument/definition", UseSingleObjectParameterDeserialization = true)]
public LspProtocol.Location Definition(LspProtocol.DefinitionParams @params)
{
var uri = @params.TextDocument.Uri;
var analysis = EnsureAnalyzed(uri);
if (analysis == null)
return null;
int offset = analysis.Lines.PositionToOffset(@params.Position.Line, @params.Position.Character);
var target = _service.GetDefinition(UriToPath(uri), offset);
if (target == null)
return null;
var targetText = ReadDocument(target.SourcePath);
var targetMap = new Heddle.LanguageServices.LineMap(targetText ?? string.Empty);
var (sl, sc) = targetMap.OffsetToPosition(target.Offset);
var (el, ec) = targetMap.OffsetToPosition(target.Offset + target.Length);
return new LspProtocol.Location(PathToUri(target.SourcePath),
new LspProtocol.Range(new LspProtocol.Position(sl, sc), new LspProtocol.Position(el, ec)));
}
[JsonRpcMethod("textDocument/semanticTokens/full", UseSingleObjectParameterDeserialization = true)]
public LspProtocol.SemanticTokens SemanticTokensFull(LspProtocol.SemanticTokensParams @params)
{
var analysis = EnsureAnalyzed(@params.TextDocument.Uri);
return new LspProtocol.SemanticTokens(analysis == null
? Array.Empty<int>()
: SemanticTokensBuilder.Build(analysis));
}
// ---- Internals -----------------------------------------------------------------------------------------
private void ScheduleDebounced(string uri)
{
var cts = new CancellationTokenSource();
var previous = _debounce.AddOrUpdate(uri, cts, (_, old) =>
{
old.Cancel();
old.Dispose();
return cts;
});
_ = Task.Run(async () =>
{
try
{
await Task.Delay(DebounceMs, cts.Token).ConfigureAwait(false);
AnalyzeAndPublish(uri);
}
catch (OperationCanceledException)
{
}
});
}
private DocumentAnalysis EnsureAnalyzed(string uri)
{
if (_service == null || !_buffers.TryGetValue(uri, out var buffer))
return _service?.GetAnalysis(UriToPath(uri));
var path = UriToPath(uri);
var existing = _service.GetAnalysis(path);
if (existing != null && existing.Version == buffer.Version && ReferenceEquals(existing.Text, buffer.Text))
return existing;
return _service.Analyze(path, buffer.Text, buffer.Version);
}
private void AnalyzeAndPublish(string uri)
{
if (_service == null || !_buffers.TryGetValue(uri, out var buffer))
return;
var analysis = _service.Analyze(UriToPath(uri), buffer.Text, buffer.Version);
var diagnostics = analysis.Diagnostics.Select(d => ToLspDiagnostic(analysis, d)).ToArray();
PublishDiagnostics(uri, buffer.Version, diagnostics);
}
private void PublishDiagnostics(string uri, int? version, LspProtocol.Diagnostic[] diagnostics)
{
_rpc?.NotifyWithParameterObjectAsync("textDocument/publishDiagnostics",
new LspProtocol.PublishDiagnosticsParams { Uri = uri, Version = version, Diagnostics = diagnostics });
}
private LspProtocol.Diagnostic ToLspDiagnostic(DocumentAnalysis analysis, HeddleDiagnostic d)
{
var message = d.Fix != null ? $"{d.Message}\nFix: {d.Fix}" : d.Message;
return new LspProtocol.Diagnostic
{
Range = ToRange(analysis, d.Offset, d.Length),
Severity = d.Severity == HeddleDiagnosticSeverity.Warning ? 2 : 1,
Code = d.Id,
Source = "heddle",
Message = message
};
}
private static LspProtocol.CompletionItem ToLspCompletion(CompletionItem item)
{
int kind = item.Kind switch
{
CompletionItemKind.Property => 10,
CompletionItemKind.Definition => 7,
CompletionItemKind.Extension => 3,
CompletionItemKind.Function => 3,
CompletionItemKind.Prop => 5,
_ => 14
};
return new LspProtocol.CompletionItem
{
Label = item.Label,
Kind = kind,
Detail = item.Detail,
InsertText = item.InsertText
};
}
private static LspProtocol.Range ToRange(DocumentAnalysis analysis, int offset, int length)
{
var (sl, sc) = analysis.Lines.OffsetToPosition(offset);
var (el, ec) = analysis.Lines.OffsetToPosition(offset + length);
return new LspProtocol.Range(new LspProtocol.Position(sl, sc), new LspProtocol.Position(el, ec));
}
private void LogSink(string message)
{
_rpc?.NotifyWithParameterObjectAsync("window/logMessage",
new LspProtocol.LogMessageParams(3, message));
}
private static HeddleLanguageServiceOptions BuildOptions(string root, JsonElement? settings)
{
// File wins over client settings (D18); v1 precedence: a present .heddle-lsp.json is authoritative,
// otherwise the forwarded client settings, otherwise a bare typeless workspace.
var filePath = string.IsNullOrEmpty(root)
? WorkspaceConfig.FileName
: Path.Combine(root, WorkspaceConfig.FileName);
if (File.Exists(filePath))
return WorkspaceConfig.ReadFile(root);
if (settings.HasValue && settings.Value.ValueKind == JsonValueKind.Object)
return WorkspaceConfig.Read(root, settings.Value.GetRawText());
return new HeddleLanguageServiceOptions { RootPath = root };
}
private static string ResolveWorkspaceRoot(LspProtocol.InitializeParams p)
{
var folderUri = p?.WorkspaceFolders != null && p.WorkspaceFolders.Length > 0
? p.WorkspaceFolders[0].Uri
: p?.RootUri;
return string.IsNullOrEmpty(folderUri) ? null : UriToPath(folderUri);
}
internal static string UriToPath(string uri)
{
if (string.IsNullOrEmpty(uri))
return uri;
if (uri.StartsWith("file:", StringComparison.OrdinalIgnoreCase))
{
try { return new Uri(uri).LocalPath; }
catch { return uri; }
}
return uri; // untitled / non-file — analyzed typelessly
}
internal static string PathToUri(string path)
{
if (string.IsNullOrEmpty(path))
return path;
try { return new Uri(Path.GetFullPath(path)).AbsoluteUri; }
catch { return path; }
}
private static string ReadDocument(string path)
{
try { return File.Exists(path) ? File.ReadAllText(path) : string.Empty; }
catch { return string.Empty; }
}
}
}