@singapore-editor/lsp 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +26 -0
  2. package/dist/capabilities.d.ts +24 -0
  3. package/dist/capabilities.d.ts.map +1 -0
  4. package/dist/capabilities.js +191 -0
  5. package/dist/capabilities.js.map +1 -0
  6. package/dist/client.d.ts +92 -0
  7. package/dist/client.d.ts.map +1 -0
  8. package/dist/client.js +391 -0
  9. package/dist/client.js.map +1 -0
  10. package/dist/index.d.ts +11 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +9 -0
  13. package/dist/performanceDiagnostics.d.ts +5 -0
  14. package/dist/performanceDiagnostics.d.ts.map +1 -0
  15. package/dist/performanceDiagnostics.js +64 -0
  16. package/dist/performanceDiagnostics.js.map +1 -0
  17. package/dist/positions.d.ts +17 -0
  18. package/dist/positions.d.ts.map +1 -0
  19. package/dist/positions.js +268 -0
  20. package/dist/positions.js.map +1 -0
  21. package/dist/protocol.d.ts +53 -0
  22. package/dist/protocol.d.ts.map +1 -0
  23. package/dist/protocol.js +95 -0
  24. package/dist/protocol.js.map +1 -0
  25. package/dist/semanticTokens.d.ts +77 -0
  26. package/dist/semanticTokens.d.ts.map +1 -0
  27. package/dist/semanticTokens.js +94 -0
  28. package/dist/semanticTokens.js.map +1 -0
  29. package/dist/transports.d.ts +54 -0
  30. package/dist/transports.d.ts.map +1 -0
  31. package/dist/transports.js +200 -0
  32. package/dist/transports.js.map +1 -0
  33. package/dist/types.d.ts +129 -0
  34. package/dist/types.d.ts.map +1 -0
  35. package/dist/types.js +0 -0
  36. package/dist/workspace.d.ts +29 -0
  37. package/dist/workspace.d.ts.map +1 -0
  38. package/dist/workspace.js +248 -0
  39. package/dist/workspace.js.map +1 -0
  40. package/dist/workspaceFactory.d.ts +4 -0
  41. package/dist/workspaceFactory.d.ts.map +1 -0
  42. package/dist/workspaceFactory.js +13 -0
  43. package/dist/workspaceFactory.js.map +1 -0
  44. package/package.json +51 -0
@@ -0,0 +1,268 @@
1
+ import { recordLspPerformanceDiagnostic } from "./performanceDiagnostics.js";
2
+ //#region src/positions.ts
3
+ function offsetToLspPosition(text, offset) {
4
+ if (offset < 0 || offset > text.length) throw new RangeError("invalid offset");
5
+ return positionForOffset(text, offset);
6
+ }
7
+ function lspPositionToOffset(text, position) {
8
+ return offsetInLine(text, lineStartForLspLine(text, nonNegativeInteger(position.line)), nonNegativeInteger(position.character));
9
+ }
10
+ function offsetToLspPositionInSnapshot(document, offset) {
11
+ if (offset < 0 || offset > document.textSnapshot.length) throw new RangeError("invalid offset");
12
+ const line = rowForOffset(document.lineStarts, offset);
13
+ const lineStart = lineStartForSnapshotLine(document, line);
14
+ const lineEnd = lineEndOffsetInSnapshot(document, line);
15
+ return {
16
+ line,
17
+ character: Math.min(offset, lineEnd) - lineStart
18
+ };
19
+ }
20
+ function lspPositionToOffsetInSnapshot(document, position) {
21
+ const line = snapshotLineForLspLine(document, nonNegativeInteger(position.line));
22
+ const lineStart = lineStartForSnapshotLine(document, line);
23
+ const lineEnd = lineEndOffsetInSnapshot(document, line);
24
+ return Math.min(lineStart + nonNegativeInteger(position.character), lineEnd);
25
+ }
26
+ var textEditToLspContentChange = (previousText, edit) => {
27
+ validateTextEdit(previousText, edit);
28
+ return {
29
+ range: lspRangeForTextEdit(previousText, edit),
30
+ text: edit.text
31
+ };
32
+ };
33
+ var textEditsToLspContentChanges = (previousText, edits) => {
34
+ if (edits.length === 0) return [];
35
+ if (!areValidBatchEdits(previousText, edits)) return [];
36
+ const changes = [];
37
+ for (const edit of edits.toSorted(compareTextEditsDescending)) changes.push({
38
+ range: lspRangeForTextEdit(previousText, edit),
39
+ text: edit.text
40
+ });
41
+ return changes;
42
+ };
43
+ var textEditToLspContentChangeInSnapshot = (previousDocument, edit) => {
44
+ validateTextEditForLength(previousDocument.textSnapshot.length, edit);
45
+ return {
46
+ range: lspRangeForTextEditInSnapshot(previousDocument, edit),
47
+ text: edit.text
48
+ };
49
+ };
50
+ var textEditsToLspContentChangesInSnapshot = (previousDocument, edits) => {
51
+ if (edits.length === 0) return [];
52
+ if (!areValidBatchEditsForLength(previousDocument.textSnapshot.length, edits)) return [];
53
+ const changes = [];
54
+ for (const edit of edits.toSorted(compareTextEditsDescending)) changes.push({
55
+ range: lspRangeForTextEditInSnapshot(previousDocument, edit),
56
+ text: edit.text
57
+ });
58
+ return changes;
59
+ };
60
+ var createLspContentChanges = (previousText, nextText, options = {}) => {
61
+ if (!options.incremental) return [createFullContentChange(nextText)];
62
+ if (!options.edits || options.edits.length === 0) return [createFullContentChange(nextText)];
63
+ const changes = textEditsToLspContentChanges(previousText, options.edits);
64
+ const editedText = applyTextEdits(previousText, options.edits);
65
+ if (changes.length === 0 || editedText !== nextText) return [createFullContentChange(nextText)];
66
+ return changes;
67
+ };
68
+ var createLspContentChangesInSnapshot = (previousDocument, nextDocument, options = {}) => {
69
+ if (!options.incremental) return fullSnapshotContentChange(nextDocument, "full-sync");
70
+ if (!options.edits || options.edits.length === 0) return fullSnapshotContentChange(nextDocument, "missing-edits");
71
+ const changes = textEditsToLspContentChangesInSnapshot(previousDocument, options.edits);
72
+ if (changes.length === 0) return fullSnapshotContentChange(nextDocument, "invalid-edits");
73
+ if (!editsMatchNextLength(previousDocument, nextDocument, options.edits)) return fullSnapshotContentChange(nextDocument, "length-mismatch");
74
+ recordContentChangePath("snapshot-incremental", previousDocument, nextDocument, options.edits);
75
+ return changes;
76
+ };
77
+ var LF = 10;
78
+ var CR = 13;
79
+ function positionForOffset(text, offset) {
80
+ let index = 0;
81
+ let line = 0;
82
+ let lineStart = 0;
83
+ while (index < offset) {
84
+ const code = text.charCodeAt(index);
85
+ if (!(code === LF || code === CR)) {
86
+ index += 1;
87
+ continue;
88
+ }
89
+ const breakEnd = lineBreakEnd(text, index, code);
90
+ if (offset < breakEnd) return {
91
+ line,
92
+ character: index - lineStart
93
+ };
94
+ index = breakEnd;
95
+ lineStart = breakEnd;
96
+ line += 1;
97
+ }
98
+ return {
99
+ line,
100
+ character: offset - lineStart
101
+ };
102
+ }
103
+ function lspRangeForTextEdit(text, edit) {
104
+ let index = 0;
105
+ let line = 0;
106
+ let lineStart = 0;
107
+ function readPosition(offset) {
108
+ while (index < offset) {
109
+ const code = text.charCodeAt(index);
110
+ if (!(code === LF || code === CR)) {
111
+ index += 1;
112
+ continue;
113
+ }
114
+ const breakEnd = lineBreakEnd(text, index, code);
115
+ if (offset < breakEnd) return {
116
+ line,
117
+ character: index - lineStart
118
+ };
119
+ index = breakEnd;
120
+ lineStart = breakEnd;
121
+ line += 1;
122
+ }
123
+ return {
124
+ line,
125
+ character: offset - lineStart
126
+ };
127
+ }
128
+ return {
129
+ start: readPosition(edit.from),
130
+ end: readPosition(edit.to)
131
+ };
132
+ }
133
+ function lspRangeForTextEditInSnapshot(document, edit) {
134
+ return {
135
+ start: offsetToLspPositionInSnapshot(document, edit.from),
136
+ end: offsetToLspPositionInSnapshot(document, edit.to)
137
+ };
138
+ }
139
+ function lineStartForLspLine(text, targetLine) {
140
+ let index = 0;
141
+ let line = 0;
142
+ let lineStart = 0;
143
+ while (line < targetLine && index < text.length) {
144
+ const code = text.charCodeAt(index);
145
+ if (!(code === LF || code === CR)) {
146
+ index += 1;
147
+ continue;
148
+ }
149
+ lineStart = lineBreakEnd(text, index, code);
150
+ index = lineStart;
151
+ line += 1;
152
+ }
153
+ return lineStart;
154
+ }
155
+ function offsetInLine(text, lineStart, character) {
156
+ const requestedOffset = lineStart + character;
157
+ let index = lineStart;
158
+ while (index < requestedOffset && index < text.length) {
159
+ const code = text.charCodeAt(index);
160
+ if (code === LF || code === CR) return index;
161
+ index += 1;
162
+ }
163
+ return Math.min(requestedOffset, text.length);
164
+ }
165
+ function lineBreakEnd(text, index, code) {
166
+ if (code !== CR) return index + 1;
167
+ if (text.charCodeAt(index + 1) === LF) return index + 2;
168
+ return index + 1;
169
+ }
170
+ function rowForOffset(lineStarts, offset) {
171
+ let low = 0;
172
+ let high = lineStarts.length - 1;
173
+ let row = 0;
174
+ while (low <= high) {
175
+ const middle = Math.floor((low + high) / 2);
176
+ if ((lineStarts.at(middle) ?? 0) <= offset) {
177
+ row = middle;
178
+ low = middle + 1;
179
+ continue;
180
+ }
181
+ high = middle - 1;
182
+ }
183
+ return row;
184
+ }
185
+ function snapshotLineForLspLine(document, line) {
186
+ if (document.lineStarts.length === 0) return 0;
187
+ return Math.min(line, document.lineStarts.length - 1);
188
+ }
189
+ function lineStartForSnapshotLine(document, line) {
190
+ return document.lineStarts.at(line) ?? document.textSnapshot.length;
191
+ }
192
+ function lineEndOffsetInSnapshot(document, line) {
193
+ const nextStart = document.lineStarts.at(line + 1);
194
+ if (nextStart === void 0) return document.textSnapshot.length;
195
+ if (lineBreakProbe(document, line, nextStart).endsWith("\r\n")) return nextStart - 2;
196
+ return Math.max(lineStartForSnapshotLine(document, line), nextStart - 1);
197
+ }
198
+ function lineBreakProbe(document, line, nextStart) {
199
+ const lineStart = lineStartForSnapshotLine(document, line);
200
+ const probeStart = Math.max(lineStart, nextStart - 2);
201
+ return document.textSnapshot.readRange(probeStart, nextStart);
202
+ }
203
+ function nonNegativeInteger(value) {
204
+ if (!Number.isFinite(value)) return 0;
205
+ return Math.max(0, Math.trunc(value));
206
+ }
207
+ var createFullContentChange = (text) => ({ text });
208
+ var fullSnapshotContentChange = (document, reason) => {
209
+ recordContentChangePath(reason, document, document, []);
210
+ return [createFullContentChange(fullSyncPayloadText(document))];
211
+ };
212
+ function fullSyncPayloadText(document) {
213
+ return document.textSnapshot.readRange(0, document.textSnapshot.length);
214
+ }
215
+ var applyTextEdits = (text, edits) => {
216
+ if (!areValidBatchEdits(text, edits)) return null;
217
+ let nextText = text;
218
+ for (const edit of edits.toSorted(compareTextEditsDescending)) nextText = applyTextEdit(nextText, edit);
219
+ return nextText;
220
+ };
221
+ var applyTextEdit = (text, edit) => `${text.slice(0, edit.from)}${edit.text}${text.slice(edit.to)}`;
222
+ var areValidBatchEdits = (text, edits) => {
223
+ let previousEnd = -1;
224
+ for (const edit of edits.toSorted(compareTextEditsAscending)) {
225
+ if (!isValidTextEdit(text, edit)) return false;
226
+ if (edit.from < previousEnd) return false;
227
+ previousEnd = edit.to;
228
+ }
229
+ return true;
230
+ };
231
+ var areValidBatchEditsForLength = (length, edits) => {
232
+ let previousEnd = -1;
233
+ for (const edit of edits.toSorted(compareTextEditsAscending)) {
234
+ if (!isValidTextEditForLength(length, edit)) return false;
235
+ if (edit.from < previousEnd) return false;
236
+ previousEnd = edit.to;
237
+ }
238
+ return true;
239
+ };
240
+ var validateTextEdit = (text, edit) => {
241
+ if (isValidTextEdit(text, edit)) return;
242
+ throw new RangeError("invalid text edit");
243
+ };
244
+ var validateTextEditForLength = (length, edit) => {
245
+ if (isValidTextEditForLength(length, edit)) return;
246
+ throw new RangeError("invalid text edit");
247
+ };
248
+ var isValidTextEdit = (text, edit) => edit.from >= 0 && edit.to >= edit.from && edit.to <= text.length;
249
+ var isValidTextEditForLength = (length, edit) => edit.from >= 0 && edit.to >= edit.from && edit.to <= length;
250
+ var editsMatchNextLength = (previousDocument, nextDocument, edits) => {
251
+ let length = previousDocument.textSnapshot.length;
252
+ for (const edit of edits) length += edit.text.length - (edit.to - edit.from);
253
+ return length === nextDocument.textSnapshot.length;
254
+ };
255
+ function recordContentChangePath(path, previousDocument, nextDocument, edits) {
256
+ recordLspPerformanceDiagnostic("lsp.contentChanges.path", {
257
+ path,
258
+ editCount: edits.length,
259
+ previousLength: previousDocument.textSnapshot.length,
260
+ nextLength: nextDocument.textSnapshot.length
261
+ });
262
+ }
263
+ var compareTextEditsAscending = (left, right) => left.from - right.from || left.to - right.to;
264
+ var compareTextEditsDescending = (left, right) => right.from - left.from || right.to - left.to;
265
+ //#endregion
266
+ export { createLspContentChanges, createLspContentChangesInSnapshot, lspPositionToOffset, lspPositionToOffsetInSnapshot, offsetToLspPosition, offsetToLspPositionInSnapshot, textEditToLspContentChange, textEditToLspContentChangeInSnapshot, textEditsToLspContentChanges, textEditsToLspContentChangesInSnapshot };
267
+
268
+ //# sourceMappingURL=positions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"positions.js","names":[],"sources":["../src/positions.ts"],"sourcesContent":["import type * as lsp from 'vscode-languageserver-protocol'\nimport { recordLspPerformanceDiagnostic } from './performanceDiagnostics'\nimport type { LspLineStarts, LspTextDocumentSnapshot, LspTextEdit } from './types'\n\nexport type LspContentChangeOptions = {\n readonly incremental?: boolean\n readonly edits?: readonly LspTextEdit[]\n}\n\nexport function offsetToLspPosition(text: string, offset: number): lsp.Position {\n if (offset < 0 || offset > text.length) throw new RangeError('invalid offset')\n\n return positionForOffset(text, offset)\n}\n\nexport function lspPositionToOffset(text: string, position: lsp.Position): number {\n const lineStart = lineStartForLspLine(text, nonNegativeInteger(position.line))\n return offsetInLine(text, lineStart, nonNegativeInteger(position.character))\n}\n\nexport function offsetToLspPositionInSnapshot(\n document: LspTextDocumentSnapshot,\n offset: number,\n): lsp.Position {\n if (offset < 0 || offset > document.textSnapshot.length) throw new RangeError('invalid offset')\n\n const line = rowForOffset(document.lineStarts, offset)\n const lineStart = lineStartForSnapshotLine(document, line)\n const lineEnd = lineEndOffsetInSnapshot(document, line)\n return { line, character: Math.min(offset, lineEnd) - lineStart }\n}\n\nexport function lspPositionToOffsetInSnapshot(\n document: LspTextDocumentSnapshot,\n position: lsp.Position,\n): number {\n const line = snapshotLineForLspLine(document, nonNegativeInteger(position.line))\n const lineStart = lineStartForSnapshotLine(document, line)\n const lineEnd = lineEndOffsetInSnapshot(document, line)\n return Math.min(lineStart + nonNegativeInteger(position.character), lineEnd)\n}\n\nexport const textEditToLspContentChange = (\n previousText: string,\n edit: LspTextEdit,\n): lsp.TextDocumentContentChangeEvent => {\n validateTextEdit(previousText, edit)\n return {\n range: lspRangeForTextEdit(previousText, edit),\n text: edit.text,\n }\n}\n\nexport const textEditsToLspContentChanges = (\n previousText: string,\n edits: readonly LspTextEdit[],\n): readonly lsp.TextDocumentContentChangeEvent[] => {\n if (edits.length === 0) return []\n if (!areValidBatchEdits(previousText, edits)) return []\n\n const changes: lsp.TextDocumentContentChangeEvent[] = []\n\n for (const edit of edits.toSorted(compareTextEditsDescending)) {\n changes.push({\n range: lspRangeForTextEdit(previousText, edit),\n text: edit.text,\n })\n }\n\n return changes\n}\n\nexport const textEditToLspContentChangeInSnapshot = (\n previousDocument: LspTextDocumentSnapshot,\n edit: LspTextEdit,\n): lsp.TextDocumentContentChangeEvent => {\n validateTextEditForLength(previousDocument.textSnapshot.length, edit)\n return {\n range: lspRangeForTextEditInSnapshot(previousDocument, edit),\n text: edit.text,\n }\n}\n\nexport const textEditsToLspContentChangesInSnapshot = (\n previousDocument: LspTextDocumentSnapshot,\n edits: readonly LspTextEdit[],\n): readonly lsp.TextDocumentContentChangeEvent[] => {\n if (edits.length === 0) return []\n if (!areValidBatchEditsForLength(previousDocument.textSnapshot.length, edits)) return []\n\n const changes: lsp.TextDocumentContentChangeEvent[] = []\n\n for (const edit of edits.toSorted(compareTextEditsDescending)) {\n changes.push({\n range: lspRangeForTextEditInSnapshot(previousDocument, edit),\n text: edit.text,\n })\n }\n\n return changes\n}\n\nexport const createLspContentChanges = (\n previousText: string,\n nextText: string,\n options: LspContentChangeOptions = {},\n): readonly lsp.TextDocumentContentChangeEvent[] => {\n if (!options.incremental) return [createFullContentChange(nextText)]\n if (!options.edits || options.edits.length === 0) return [createFullContentChange(nextText)]\n\n const changes = textEditsToLspContentChanges(previousText, options.edits)\n const editedText = applyTextEdits(previousText, options.edits)\n if (changes.length === 0 || editedText !== nextText) return [createFullContentChange(nextText)]\n\n return changes\n}\n\nexport const createLspContentChangesInSnapshot = (\n previousDocument: LspTextDocumentSnapshot,\n nextDocument: LspTextDocumentSnapshot,\n options: LspContentChangeOptions = {},\n): readonly lsp.TextDocumentContentChangeEvent[] => {\n if (!options.incremental) return fullSnapshotContentChange(nextDocument, 'full-sync')\n if (!options.edits || options.edits.length === 0) {\n return fullSnapshotContentChange(nextDocument, 'missing-edits')\n }\n\n const changes = textEditsToLspContentChangesInSnapshot(previousDocument, options.edits)\n if (changes.length === 0) return fullSnapshotContentChange(nextDocument, 'invalid-edits')\n if (!editsMatchNextLength(previousDocument, nextDocument, options.edits)) {\n return fullSnapshotContentChange(nextDocument, 'length-mismatch')\n }\n\n recordContentChangePath('snapshot-incremental', previousDocument, nextDocument, options.edits)\n return changes\n}\n\nconst LF = 10\nconst CR = 13\n\nfunction positionForOffset(text: string, offset: number): lsp.Position {\n let index = 0\n let line = 0\n let lineStart = 0\n\n while (index < offset) {\n const code = text.charCodeAt(index)\n if (!(code === LF || code === CR)) {\n index += 1\n continue\n }\n\n const breakEnd = lineBreakEnd(text, index, code)\n if (offset < breakEnd) return { line, character: index - lineStart }\n\n index = breakEnd\n lineStart = breakEnd\n line += 1\n }\n\n return { line, character: offset - lineStart }\n}\n\nfunction lspRangeForTextEdit(text: string, edit: LspTextEdit): lsp.Range {\n let index = 0\n let line = 0\n let lineStart = 0\n\n function readPosition(offset: number): lsp.Position {\n while (index < offset) {\n const code = text.charCodeAt(index)\n if (!(code === LF || code === CR)) {\n index += 1\n continue\n }\n\n const breakEnd = lineBreakEnd(text, index, code)\n if (offset < breakEnd) return { line, character: index - lineStart }\n\n index = breakEnd\n lineStart = breakEnd\n line += 1\n }\n\n return { line, character: offset - lineStart }\n }\n\n return {\n start: readPosition(edit.from),\n end: readPosition(edit.to),\n }\n}\n\nfunction lspRangeForTextEditInSnapshot(\n document: LspTextDocumentSnapshot,\n edit: LspTextEdit,\n): lsp.Range {\n return {\n start: offsetToLspPositionInSnapshot(document, edit.from),\n end: offsetToLspPositionInSnapshot(document, edit.to),\n }\n}\n\nfunction lineStartForLspLine(text: string, targetLine: number): number {\n let index = 0\n let line = 0\n let lineStart = 0\n\n while (line < targetLine && index < text.length) {\n const code = text.charCodeAt(index)\n if (!(code === LF || code === CR)) {\n index += 1\n continue\n }\n\n lineStart = lineBreakEnd(text, index, code)\n index = lineStart\n line += 1\n }\n\n return lineStart\n}\n\nfunction offsetInLine(text: string, lineStart: number, character: number): number {\n const requestedOffset = lineStart + character\n let index = lineStart\n\n while (index < requestedOffset && index < text.length) {\n const code = text.charCodeAt(index)\n if (code === LF || code === CR) return index\n index += 1\n }\n\n return Math.min(requestedOffset, text.length)\n}\n\nfunction lineBreakEnd(text: string, index: number, code: number): number {\n if (code !== CR) return index + 1\n if (text.charCodeAt(index + 1) === LF) return index + 2\n return index + 1\n}\n\nfunction rowForOffset(lineStarts: LspLineStarts, offset: number): number {\n let low = 0\n let high = lineStarts.length - 1\n let row = 0\n\n while (low <= high) {\n const middle = Math.floor((low + high) / 2)\n const start = lineStarts.at(middle) ?? 0\n if (start <= offset) {\n row = middle\n low = middle + 1\n continue\n }\n\n high = middle - 1\n }\n\n return row\n}\n\nfunction snapshotLineForLspLine(document: LspTextDocumentSnapshot, line: number): number {\n if (document.lineStarts.length === 0) return 0\n return Math.min(line, document.lineStarts.length - 1)\n}\n\nfunction lineStartForSnapshotLine(document: LspTextDocumentSnapshot, line: number): number {\n return document.lineStarts.at(line) ?? document.textSnapshot.length\n}\n\nfunction lineEndOffsetInSnapshot(document: LspTextDocumentSnapshot, line: number): number {\n const nextStart = document.lineStarts.at(line + 1)\n if (nextStart === undefined) return document.textSnapshot.length\n if (lineBreakProbe(document, line, nextStart).endsWith('\\r\\n')) return nextStart - 2\n return Math.max(lineStartForSnapshotLine(document, line), nextStart - 1)\n}\n\nfunction lineBreakProbe(\n document: LspTextDocumentSnapshot,\n line: number,\n nextStart: number,\n): string {\n const lineStart = lineStartForSnapshotLine(document, line)\n const probeStart = Math.max(lineStart, nextStart - 2)\n return document.textSnapshot.readRange(probeStart, nextStart)\n}\n\nfunction nonNegativeInteger(value: number): number {\n if (!Number.isFinite(value)) return 0\n return Math.max(0, Math.trunc(value))\n}\n\nconst createFullContentChange = (text: string): lsp.TextDocumentContentChangeEvent => ({ text })\n\nconst fullSnapshotContentChange = (\n document: LspTextDocumentSnapshot,\n reason: string,\n): readonly lsp.TextDocumentContentChangeEvent[] => {\n recordContentChangePath(reason, document, document, [])\n return [createFullContentChange(fullSyncPayloadText(document))]\n}\n\n// Full sync and incremental recovery send the whole document by protocol definition.\nfunction fullSyncPayloadText(document: LspTextDocumentSnapshot): string {\n return document.textSnapshot.readRange(0, document.textSnapshot.length)\n}\n\nconst applyTextEdits = (text: string, edits: readonly LspTextEdit[]): string | null => {\n if (!areValidBatchEdits(text, edits)) return null\n\n let nextText = text\n for (const edit of edits.toSorted(compareTextEditsDescending)) {\n nextText = applyTextEdit(nextText, edit)\n }\n\n return nextText\n}\n\nconst applyTextEdit = (text: string, edit: LspTextEdit): string =>\n `${text.slice(0, edit.from)}${edit.text}${text.slice(edit.to)}`\n\nconst areValidBatchEdits = (text: string, edits: readonly LspTextEdit[]): boolean => {\n let previousEnd = -1\n for (const edit of edits.toSorted(compareTextEditsAscending)) {\n if (!isValidTextEdit(text, edit)) return false\n if (edit.from < previousEnd) return false\n previousEnd = edit.to\n }\n\n return true\n}\n\nconst areValidBatchEditsForLength = (length: number, edits: readonly LspTextEdit[]): boolean => {\n let previousEnd = -1\n for (const edit of edits.toSorted(compareTextEditsAscending)) {\n if (!isValidTextEditForLength(length, edit)) return false\n if (edit.from < previousEnd) return false\n previousEnd = edit.to\n }\n\n return true\n}\n\nconst validateTextEdit = (text: string, edit: LspTextEdit): void => {\n if (isValidTextEdit(text, edit)) return\n throw new RangeError('invalid text edit')\n}\n\nconst validateTextEditForLength = (length: number, edit: LspTextEdit): void => {\n if (isValidTextEditForLength(length, edit)) return\n throw new RangeError('invalid text edit')\n}\n\nconst isValidTextEdit = (text: string, edit: LspTextEdit): boolean =>\n edit.from >= 0 && edit.to >= edit.from && edit.to <= text.length\n\nconst isValidTextEditForLength = (length: number, edit: LspTextEdit): boolean =>\n edit.from >= 0 && edit.to >= edit.from && edit.to <= length\n\nconst editsMatchNextLength = (\n previousDocument: LspTextDocumentSnapshot,\n nextDocument: LspTextDocumentSnapshot,\n edits: readonly LspTextEdit[],\n): boolean => {\n let length = previousDocument.textSnapshot.length\n for (const edit of edits) length += edit.text.length - (edit.to - edit.from)\n return length === nextDocument.textSnapshot.length\n}\n\nfunction recordContentChangePath(\n path: string,\n previousDocument: LspTextDocumentSnapshot,\n nextDocument: LspTextDocumentSnapshot,\n edits: readonly LspTextEdit[],\n): void {\n recordLspPerformanceDiagnostic('lsp.contentChanges.path', {\n path,\n editCount: edits.length,\n previousLength: previousDocument.textSnapshot.length,\n nextLength: nextDocument.textSnapshot.length,\n })\n}\n\nconst compareTextEditsAscending = (left: LspTextEdit, right: LspTextEdit): number =>\n left.from - right.from || left.to - right.to\n\nconst compareTextEditsDescending = (left: LspTextEdit, right: LspTextEdit): number =>\n right.from - left.from || right.to - left.to\n"],"mappings":";;AASA,SAAgB,oBAAoB,MAAc,QAA8B;CAC9E,IAAI,SAAS,KAAK,SAAS,KAAK,QAAQ,MAAM,IAAI,WAAW,gBAAgB;CAE7E,OAAO,kBAAkB,MAAM,MAAM;AACvC;AAEA,SAAgB,oBAAoB,MAAc,UAAgC;CAEhF,OAAO,aAAa,MADF,oBAAoB,MAAM,mBAAmB,SAAS,IAAI,CAClD,GAAW,mBAAmB,SAAS,SAAS,CAAC;AAC7E;AAEA,SAAgB,8BACd,UACA,QACc;CACd,IAAI,SAAS,KAAK,SAAS,SAAS,aAAa,QAAQ,MAAM,IAAI,WAAW,gBAAgB;CAE9F,MAAM,OAAO,aAAa,SAAS,YAAY,MAAM;CACrD,MAAM,YAAY,yBAAyB,UAAU,IAAI;CACzD,MAAM,UAAU,wBAAwB,UAAU,IAAI;CACtD,OAAO;EAAE;EAAM,WAAW,KAAK,IAAI,QAAQ,OAAO,IAAI;CAAU;AAClE;AAEA,SAAgB,8BACd,UACA,UACQ;CACR,MAAM,OAAO,uBAAuB,UAAU,mBAAmB,SAAS,IAAI,CAAC;CAC/E,MAAM,YAAY,yBAAyB,UAAU,IAAI;CACzD,MAAM,UAA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M,KAAK;AAE5D,IAAM,4BAA4B,QAAgB,SAChD,KAAK,QAAQ,KAAK,KAAK,MAAM,KAAK,QAAQ,KAAK,MAAM;AAEvD,IAAM,wBACJ,kBACA,cACA,UACY;CACZ,IAAI,SAAS,iBAAiB,aAAa;CAC3C,KAAK,MAAM,QAAQ,OAAO,UAAU,KAAK,KAAK,UAAU,KAAK,KAAK,KAAK;CACvE,OAAO,WAAW,aAAa,aAAa;AAC9C;AAEA,SAAS,wBACP,MACA,kBACA,cACA,OACM;CACN,+BAA+B,2BAA2B;EACxD;EACA,WAAW,MAAM;EACjB,gBAAgB,iBAAiB,aAAa;EAC9C,YAAY,aAAa,aAAa;CACxC,CAAC;AACH;AAEA,IAAM,6BAA6B,MAAmB,UACpD,KAAK,OAAO,MAAM,QAAQ,KAAK,KAAK,MAAM;AAE5C,IAAM,8BAA8B,MAAmB,UACrD,MAAM,OAAO,KAAK,QAAQ,MAAM,KAAK,KAAK"}
@@ -0,0 +1,53 @@
1
+ import type * as lsp from 'vscode-languageserver-protocol';
2
+ export declare const METHOD_NOT_FOUND = -32601;
3
+ export declare const REQUEST_CANCELLED = -32800;
4
+ export type LspRequestId = number | string;
5
+ export declare class LspResponseError extends Error {
6
+ readonly code: number;
7
+ readonly data: unknown;
8
+ constructor(error: {
9
+ readonly code: number;
10
+ readonly message: string;
11
+ readonly data?: unknown;
12
+ });
13
+ }
14
+ /**
15
+ * Sent just before a server's transport closes, by the server or on its behalf, saying why. A bare
16
+ * close reads the same as a healthy idle one, so without this a dead server looks like a quiet one.
17
+ * `$/` because it is implementation-defined, which the protocol permits clients to ignore.
18
+ */
19
+ export declare const LSP_SERVER_EXITED = "$/serverExited";
20
+ export type LspServerExitedParams = {
21
+ /** How the server ended: `crashed` for a worker that threw, a process outcome otherwise. */
22
+ readonly outcome: string;
23
+ readonly serverId?: string;
24
+ readonly exitCode?: number | null;
25
+ readonly exitSignal?: string | null;
26
+ readonly stderrTail?: string;
27
+ /** Guidance for the user; absent when the host closed the server on purpose. */
28
+ readonly error?: {
29
+ readonly code?: string;
30
+ readonly message: string;
31
+ readonly why?: string;
32
+ readonly fix?: string;
33
+ };
34
+ };
35
+ /** A transport that closed after the server said why; the close is reported as this. */
36
+ export declare class LspServerExitedError extends Error {
37
+ readonly params: LspServerExitedParams;
38
+ constructor(params: LspServerExitedParams);
39
+ }
40
+ export declare class LspRequestCancelledError extends Error {
41
+ readonly code = -32800;
42
+ constructor(message?: string);
43
+ }
44
+ export declare const createRequestMessage: (id: LspRequestId, method: string, params: unknown) => lsp.RequestMessage;
45
+ export declare const createNotificationMessage: (method: string, params?: unknown) => lsp.NotificationMessage;
46
+ export declare const createMethodNotFoundResponse: (id: LspRequestId | null, method: string) => lsp.ResponseMessage;
47
+ export declare const createResponseMessage: (id: LspRequestId | null, result: unknown) => lsp.ResponseMessage;
48
+ export declare const createInternalErrorResponse: (id: LspRequestId | null, error: unknown) => lsp.ResponseMessage;
49
+ export declare const isResponseMessage: (message: unknown) => message is lsp.ResponseMessage;
50
+ export declare const isRequestMessage: (message: unknown) => message is lsp.RequestMessage;
51
+ export declare const isNotificationMessage: (message: unknown) => message is lsp.NotificationMessage;
52
+ export declare const responseResult: <TResult>(message: lsp.ResponseMessage) => TResult;
53
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,GAAG,MAAM,gCAAgC,CAAA;AAG1D,eAAO,MAAM,gBAAgB,SAAS,CAAA;AACtC,eAAO,MAAM,iBAAiB,SAAS,CAAA;AAEvC,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,CAAA;AAE1C,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,SAAgB,IAAI,EAAE,MAAM,CAAA;IAC5B,SAAgB,IAAI,EAAE,OAAO,CAAA;gBAEV,KAAK,EAAE;QACxB,QAAQ,CAAC,IAAI,EAAE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AAQ,MAAM,KACb,GAAG,CAAC,eAOL,CAAA;AAEF,eAAO,MAAM,qBAAqB,GAChC,IAAI,YAAY,GAAG,IAAI,EACvB,QAAQ,OAAO,KACd,GAAG,CAAC,eAIL,CAAA;AAEF,eAAO,MAAM,2BAA2B,GACtC,IAAI,YAAY,GAAG,IAAI,EACvB,OAAO,OAAO,KACb,GAAG,CAAC,eAOL,CAAA;AAEF,eAAO,MAAM,iBAAiB,GAAI,SAAS,OAAO,KAAG,OAAO,IAAI,GAAG,CAAC,eAGnE,CAAA;AAED,eAAO,MAAM,gBAAgB,GAAI,SAAS,OAAO,KAAG,OAAO,IAAI,GAAG,CAAC,cAGlE,CAAA;AAED,eAAO,MAAM,qBAAqB,GAAI,SAAS,OAAO,KAAG,OAAO,IAAI,GAAG,CAAC,mBAGvE,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,OAAO,EAAE,SAAS,GAAG,CAAC,eAAe,KAAG,OAGtE,CAAA"}
@@ -0,0 +1,95 @@
1
+ //#region src/protocol.ts
2
+ var JSON_RPC_VERSION = "2.0";
3
+ var METHOD_NOT_FOUND = -32601;
4
+ var REQUEST_CANCELLED = -32800;
5
+ var LspResponseError = class extends Error {
6
+ code;
7
+ data;
8
+ constructor(error) {
9
+ super(error.message);
10
+ this.name = "LspResponseError";
11
+ this.code = error.code;
12
+ this.data = error.data;
13
+ }
14
+ };
15
+ /**
16
+ * Sent just before a server's transport closes, by the server or on its behalf, saying why. A bare
17
+ * close reads the same as a healthy idle one, so without this a dead server looks like a quiet one.
18
+ * `$/` because it is implementation-defined, which the protocol permits clients to ignore.
19
+ */
20
+ var LSP_SERVER_EXITED = "$/serverExited";
21
+ /** A transport that closed after the server said why; the close is reported as this. */
22
+ var LspServerExitedError = class extends Error {
23
+ params;
24
+ constructor(params) {
25
+ super(params.error?.message ?? `Language server ${params.outcome}`);
26
+ this.name = "LspServerExitedError";
27
+ this.params = params;
28
+ }
29
+ };
30
+ var LspRequestCancelledError = class extends Error {
31
+ code = REQUEST_CANCELLED;
32
+ constructor(message = "LSP request cancelled") {
33
+ super(message);
34
+ this.name = "LspRequestCancelledError";
35
+ }
36
+ };
37
+ var createRequestMessage = (id, method, params) => {
38
+ const message = {
39
+ jsonrpc: JSON_RPC_VERSION,
40
+ id,
41
+ method
42
+ };
43
+ if (params !== void 0) message.params = params;
44
+ return message;
45
+ };
46
+ var createNotificationMessage = (method, params) => {
47
+ const message = {
48
+ jsonrpc: JSON_RPC_VERSION,
49
+ method
50
+ };
51
+ if (params !== void 0) message.params = params;
52
+ return message;
53
+ };
54
+ var createMethodNotFoundResponse = (id, method) => ({
55
+ jsonrpc: JSON_RPC_VERSION,
56
+ id,
57
+ error: {
58
+ code: METHOD_NOT_FOUND,
59
+ message: `Method not implemented: ${method}`
60
+ }
61
+ });
62
+ var createResponseMessage = (id, result) => ({
63
+ jsonrpc: JSON_RPC_VERSION,
64
+ id,
65
+ result: result === void 0 ? null : result
66
+ });
67
+ var createInternalErrorResponse = (id, error) => ({
68
+ jsonrpc: JSON_RPC_VERSION,
69
+ id,
70
+ error: {
71
+ code: -32603,
72
+ message: error instanceof Error ? error.message : String(error)
73
+ }
74
+ });
75
+ var isResponseMessage = (message) => {
76
+ if (!isObject(message)) return false;
77
+ return "id" in message && !("method" in message);
78
+ };
79
+ var isRequestMessage = (message) => {
80
+ if (!isObject(message)) return false;
81
+ return "id" in message && typeof message.method === "string";
82
+ };
83
+ var isNotificationMessage = (message) => {
84
+ if (!isObject(message)) return false;
85
+ return !("id" in message) && typeof message.method === "string";
86
+ };
87
+ var responseResult = (message) => {
88
+ if (message.error) throw new LspResponseError(message.error);
89
+ return message.result;
90
+ };
91
+ var isObject = (value) => typeof value === "object" && value !== null;
92
+ //#endregion
93
+ export { LSP_SERVER_EXITED, LspRequestCancelledError, LspResponseError, LspServerExitedError, METHOD_NOT_FOUND, REQUEST_CANCELLED, createInternalErrorResponse, createMethodNotFoundResponse, createNotificationMessage, createRequestMessage, createResponseMessage, isNotificationMessage, isRequestMessage, isResponseMessage, responseResult };
94
+
95
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","names":[],"sources":["../src/protocol.ts"],"sourcesContent":["import type * as lsp from 'vscode-languageserver-protocol'\n\nconst JSON_RPC_VERSION = '2.0'\nexport const METHOD_NOT_FOUND = -32601\nexport const REQUEST_CANCELLED = -32800\n\nexport type LspRequestId = number | string\n\nexport class LspResponseError extends Error {\n public readonly code: number\n public readonly data: unknown\n\n public constructor(error: {\n readonly code: number\n readonly message: string\n readonly data?: unknown\n }) {\n super(error.message)\n this.name = 'LspResponseError'\n this.code = error.code\n this.data = error.data\n }\n}\n\n/**\n * Sent just before a server's transport closes, by the server or on its behalf, saying why. A bare\n * close reads the same as a healthy idle one, so without this a dead server looks like a quiet one.\n * `$/` because it is implementation-defined, which the protocol permits clients to ignore.\n */\nexport const LSP_SERVER_EXITED = '$/serverExited'\n\nexport type LspServerExitedParams = {\n /** How the server ended: `crashed` for a worker that threw, a process outcome otherwise. */\n readonly outcome: string\n readonly serverId?: string\n readonly exitCode?: number | null\n readonly exitSignal?: string | null\n readonly stderrTail?: string\n /** Guidance for the user; absent when the host closed the server on purpose. */\n readonly error?: {\n readonly code?: string\n readonly message: string\n readonly why?: string\n readonly fix?: string\n }\n}\n\n/** A transport that closed after the server said why; the close is reported as this. */\nexport class LspServerExitedError extends Error {\n public readonly params: LspServerExitedParams\n\n public constructor(params: LspServerExitedParams) {\n super(params.error?.message ?? `Language server ${params.outcome}`)\n this.name = 'LspServerExitedError'\n this.params = params\n }\n}\n\nexport class LspRequestCancelledError extends Error {\n public readonly code = REQUEST_CANCELLED\n\n public constructor(message = 'LSP request cancelled') {\n super(message)\n this.name = 'LspRequestCancelledError'\n }\n}\n\nexport const createRequestMessage = (\n id: LspRequestId,\n method: string,\n params: unknown,\n): lsp.RequestMessage => {\n const message: lsp.RequestMessage = {\n jsonrpc: JSON_RPC_VERSION,\n id,\n method,\n }\n if (params !== undefined) message.params = params as lsp.RequestMessage['params']\n return message\n}\n\nexport const createNotificationMessage = (\n method: string,\n params?: unknown,\n): lsp.NotificationMessage => {\n const message: lsp.NotificationMessage = {\n jsonrpc: JSON_RPC_VERSION,\n method,\n }\n if (params !== undefined) message.params = params as lsp.NotificationMessage['params']\n return message\n}\n\nexport const createMethodNotFoundResponse = (\n id: LspRequestId | null,\n method: string,\n): lsp.ResponseMessage => ({\n jsonrpc: JSON_RPC_VERSION,\n id,\n error: {\n code: METHOD_NOT_FOUND,\n message: `Method not implemented: ${method}`,\n },\n})\n\nexport const createResponseMessage = (\n id: LspRequestId | null,\n result: unknown,\n): lsp.ResponseMessage => ({\n jsonrpc: JSON_RPC_VERSION,\n id,\n result: result === undefined ? null : result,\n})\n\nexport const createInternalErrorResponse = (\n id: LspRequestId | null,\n error: unknown,\n): lsp.ResponseMessage => ({\n jsonrpc: JSON_RPC_VERSION,\n id,\n error: {\n code: -32603,\n message: error instanceof Error ? error.message : String(error),\n },\n})\n\nexport const isResponseMessage = (message: unknown): message is lsp.ResponseMessage => {\n if (!isObject(message)) return false\n return 'id' in message && !('method' in message)\n}\n\nexport const isRequestMessage = (message: unknown): message is lsp.RequestMessage => {\n if (!isObject(message)) return false\n return 'id' in message && typeof message.method === 'string'\n}\n\nexport const isNotificationMessage = (message: unknown): message is lsp.NotificationMessage => {\n if (!isObject(message)) return false\n return !('id' in message) && typeof message.method === 'string'\n}\n\nexport const responseResult = <TResult>(message: lsp.ResponseMessage): TResult => {\n if (message.error) throw new LspResponseError(message.error)\n return message.result as TResult\n}\n\nconst isObject = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null\n"],"mappings":";AAEA,IAAM,mBAAmB;AACzB,IAAa,mBAAmB;AAChC,IAAa,oBAAoB;AAIjC,IAAa,mBAAb,cAAsC,MAAM;CAC1C;CACA;CAEA,YAAmB,OAIhB;EACD,MAAM,MAAM,OAAO;EACnB,KAAK,OAAO;EACZ,KAAK,OAAO,MAAM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AAO;CAC/B,OAAO,EAAE,QAAQ,YAAY,OAAO,QAAQ,WAAW;AACzD;AAEA,IAAa,kBAA2B,YAA0C;CAChF,IAAI,QAAQ,OAAO,MAAM,IAAI,iBAAiB,QAAQ,KAAK;CAC3D,OAAO,QAAQ;AACjB;AAEA,IAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU"}
@@ -0,0 +1,77 @@
1
+ import type * as lsp from 'vscode-languageserver-protocol';
2
+ /**
3
+ * The token types LSP itself defines, in the order the specification lists them.
4
+ *
5
+ * Exported so a host can declare the standard set plus whatever its own servers add, rather than
6
+ * retyping twenty-three strings. The list is a *vocabulary*, not a legend: a legend is per-server,
7
+ * index-addressed, and arrives in that server's `initializeResult`. Nothing in this package ever
8
+ * holds one.
9
+ */
10
+ export declare const SEMANTIC_TOKEN_TYPES: readonly string[];
11
+ /** The token modifiers LSP itself defines, in specification order. See SEMANTIC_TOKEN_TYPES. */
12
+ export declare const SEMANTIC_TOKEN_MODIFIERS: readonly string[];
13
+ /** The protocol's own `TokenFormat`, which its package does not re-export from its entry point. */
14
+ export type SemanticTokenFormat = lsp.SemanticTokensClientCapabilities['formats'][number];
15
+ export type SemanticTokensRequestOptions = {
16
+ /** `true` for whole-document requests; `{ delta: true }` to also accept `SemanticTokensDelta`. */
17
+ readonly full?: boolean | {
18
+ readonly delta?: boolean;
19
+ };
20
+ readonly range?: boolean;
21
+ };
22
+ export type SemanticTokensClientCapabilityOptions = {
23
+ readonly requests: SemanticTokensRequestOptions;
24
+ /** Defaults to SEMANTIC_TOKEN_TYPES. */
25
+ readonly tokenTypes?: readonly string[];
26
+ /** Defaults to SEMANTIC_TOKEN_MODIFIERS. */
27
+ readonly tokenModifiers?: readonly string[];
28
+ /** Defaults to `['relative']`, which is the only format the protocol defines. */
29
+ readonly formats?: readonly SemanticTokenFormat[];
30
+ /**
31
+ * Whether the client uses semantic tokens *alongside* the colour it already has rather than
32
+ * instead of it. Deliberately the host's answer and per server, because whether this editor
33
+ * already has syntactic colour for a file depends on which grammar the host registered for that
34
+ * language id — which the host knows and this package does not.
35
+ */
36
+ readonly augmentsSyntaxTokens?: boolean;
37
+ /**
38
+ * Whether the client accepts a token whose span crosses a newline.
39
+ *
40
+ * Declarable since the paint layer was proved to render one across two mounted rows. It stays
41
+ * opt-in rather than default-on because it is a statement about the *host's* pipeline as much as
42
+ * the editor's: a host that re-derives spans from `{line, character}` pairs of its own may not be
43
+ * able to honour it even though the layer can.
44
+ */
45
+ readonly multilineTokenSupport?: boolean;
46
+ };
47
+ /**
48
+ * Builds a `textDocument.semanticTokens` block that declares exactly what this editor honours.
49
+ *
50
+ * There is no such block in `defaultClientCapabilities()` and there will not be: a client that
51
+ * declares semantic tokens makes every server it speaks to compute them, and a host that paints no
52
+ * tokens would be paying for answers nobody draws. So the block is opt-in, and it is built here
53
+ * rather than hand-written by each host because three of its flags are ones a client must not claim
54
+ * unless it can actually honour them — and a server acts on all three.
55
+ *
56
+ * **`overlappingTokenSupport` is not an option.** Where two spans overlap this editor truncates the
57
+ * earlier one; it does not paint both. Declaring the flag would invite a server to send overlapping
58
+ * tokens on the understanding that they would be layered, and they would not be.
59
+ *
60
+ * **`dynamicRegistration` is not an option.** Several servers answer `initialize` with *no*
61
+ * semantic-tokens provider at all when a client declares it, expecting to register later through
62
+ * `client/registerCapability` — a request `LspClient` answers with method-not-found and always
63
+ * will. Declaring it would turn a working server into a silent one.
64
+ *
65
+ * **`multilineTokenSupport` is declarable but not assumed.** It was refused until the paint layer
66
+ * was proved to render a span crossing a newline across two mounted rows; that gate is now open, so
67
+ * the flag is an ordinary option. It is still opt-in, because it is a statement about the host's
68
+ * pipeline as well as the editor's.
69
+ *
70
+ * Everything the block *does* say — which requests, which token types, which modifiers — is the
71
+ * caller's to choose, because at least one real server computes its advertised legend as the
72
+ * intersection of its own token types with the ones the client declared. That makes the declared
73
+ * block an input to the server rather than a local decoding table, and therefore a property of a
74
+ * connection, which is host-owned.
75
+ */
76
+ export declare function semanticTokensClientCapability(options: SemanticTokensClientCapabilityOptions): lsp.ClientCapabilities;
77
+ //# sourceMappingURL=semanticTokens.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"semanticTokens.d.ts","sourceRoot":"","sources":["../src/semanticTokens.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,GAAG,MAAM,gCAAgC,CAAA;AAE1D;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,EAAE,SAAS,MAAM,EAwBjD,CAAA;AAED,gGAAgG;AAChG,eAAO,MAAM,wBAAwB,EAAE,SAAS,MAAM,EAWrD,CAAA;AAED,mGAAmG;AACnG,MAAM,MAAM,mBAAmB,GAAG,GAAG,CAAC,gCAAgC,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAA;AAEzF,MAAM,MAAM,4BAA4B,GAAG;IACzC,kGAAkG;IAClG,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,GAAG;QAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;IACtD,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,qCAAqC,GAAG;IAClD,QAAQ,CAAC,QAAQ,EAAE,4BAA4B,CAAA;IAC/C,wCAAwC;IACxC,QAAQ,CAAC,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IACvC,4CAA4C;IAC5C,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC3C,iFAAiF;IACjF,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,mBAAmB,EAAE,CAAA;IACjD;;;;;OAKG;IACH,QAAQ,CAAC,oBAAoB,CAAC,EAAE,OAAO,CAAA;IACvC;;;;;;;OAOG;IACH,QAAQ,CAAC,qBAAqB,CAAC,EAAE,OAAO,CAAA;CACzC,CAAA;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,EAAE,qCAAqC,GAC7C,GAAG,CAAC,kBAAkB,CA0BxB"}
@@ -0,0 +1,94 @@
1
+ //#region src/semanticTokens.ts
2
+ /**
3
+ * The token types LSP itself defines, in the order the specification lists them.
4
+ *
5
+ * Exported so a host can declare the standard set plus whatever its own servers add, rather than
6
+ * retyping twenty-three strings. The list is a *vocabulary*, not a legend: a legend is per-server,
7
+ * index-addressed, and arrives in that server's `initializeResult`. Nothing in this package ever
8
+ * holds one.
9
+ */
10
+ var SEMANTIC_TOKEN_TYPES = [
11
+ "namespace",
12
+ "type",
13
+ "class",
14
+ "enum",
15
+ "interface",
16
+ "struct",
17
+ "typeParameter",
18
+ "parameter",
19
+ "variable",
20
+ "property",
21
+ "enumMember",
22
+ "event",
23
+ "function",
24
+ "method",
25
+ "macro",
26
+ "keyword",
27
+ "modifier",
28
+ "comment",
29
+ "string",
30
+ "number",
31
+ "regexp",
32
+ "operator",
33
+ "decorator"
34
+ ];
35
+ /** The token modifiers LSP itself defines, in specification order. See SEMANTIC_TOKEN_TYPES. */
36
+ var SEMANTIC_TOKEN_MODIFIERS = [
37
+ "declaration",
38
+ "definition",
39
+ "readonly",
40
+ "static",
41
+ "deprecated",
42
+ "abstract",
43
+ "async",
44
+ "modification",
45
+ "documentation",
46
+ "defaultLibrary"
47
+ ];
48
+ /**
49
+ * Builds a `textDocument.semanticTokens` block that declares exactly what this editor honours.
50
+ *
51
+ * There is no such block in `defaultClientCapabilities()` and there will not be: a client that
52
+ * declares semantic tokens makes every server it speaks to compute them, and a host that paints no
53
+ * tokens would be paying for answers nobody draws. So the block is opt-in, and it is built here
54
+ * rather than hand-written by each host because three of its flags are ones a client must not claim
55
+ * unless it can actually honour them — and a server acts on all three.
56
+ *
57
+ * **`overlappingTokenSupport` is not an option.** Where two spans overlap this editor truncates the
58
+ * earlier one; it does not paint both. Declaring the flag would invite a server to send overlapping
59
+ * tokens on the understanding that they would be layered, and they would not be.
60
+ *
61
+ * **`dynamicRegistration` is not an option.** Several servers answer `initialize` with *no*
62
+ * semantic-tokens provider at all when a client declares it, expecting to register later through
63
+ * `client/registerCapability` — a request `LspClient` answers with method-not-found and always
64
+ * will. Declaring it would turn a working server into a silent one.
65
+ *
66
+ * **`multilineTokenSupport` is declarable but not assumed.** It was refused until the paint layer
67
+ * was proved to render a span crossing a newline across two mounted rows; that gate is now open, so
68
+ * the flag is an ordinary option. It is still opt-in, because it is a statement about the host's
69
+ * pipeline as well as the editor's.
70
+ *
71
+ * Everything the block *does* say — which requests, which token types, which modifiers — is the
72
+ * caller's to choose, because at least one real server computes its advertised legend as the
73
+ * intersection of its own token types with the ones the client declared. That makes the declared
74
+ * block an input to the server rather than a local decoding table, and therefore a property of a
75
+ * connection, which is host-owned.
76
+ */
77
+ function semanticTokensClientCapability(options) {
78
+ const requests = {};
79
+ if (options.requests.full !== void 0) requests.full = typeof options.requests.full === "boolean" ? options.requests.full : { delta: options.requests.full.delta === true };
80
+ if (options.requests.range !== void 0) requests.range = options.requests.range;
81
+ const semanticTokens = {
82
+ formats: [...options.formats ?? ["relative"]],
83
+ requests,
84
+ tokenModifiers: [...options.tokenModifiers ?? SEMANTIC_TOKEN_MODIFIERS],
85
+ tokenTypes: [...options.tokenTypes ?? SEMANTIC_TOKEN_TYPES]
86
+ };
87
+ if (options.augmentsSyntaxTokens !== void 0) semanticTokens.augmentsSyntaxTokens = options.augmentsSyntaxTokens;
88
+ if (options.multilineTokenSupport !== void 0) semanticTokens.multilineTokenSupport = options.multilineTokenSupport;
89
+ return { textDocument: { semanticTokens } };
90
+ }
91
+ //#endregion
92
+ export { SEMANTIC_TOKEN_MODIFIERS, SEMANTIC_TOKEN_TYPES, semanticTokensClientCapability };
93
+
94
+ //# sourceMappingURL=semanticTokens.js.map