@rs-x/typescript-plugin 2.0.0-next.0
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.
- package/LICENSE +21 -0
- package/dist/index.js +330 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 robert-sanders-software-ontwikkeling
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
|
|
5
|
+
// lib/index.ts
|
|
6
|
+
var import_compiler = require("@rs-x/compiler");
|
|
7
|
+
function init(modules) {
|
|
8
|
+
const ts = modules.typescript;
|
|
9
|
+
function create(info) {
|
|
10
|
+
const languageService = info.languageService;
|
|
11
|
+
const proxy = /* @__PURE__ */ Object.create(null);
|
|
12
|
+
for (const key of Object.keys(languageService)) {
|
|
13
|
+
const value = languageService[key];
|
|
14
|
+
proxy[key] = typeof value === "function" ? value.bind(languageService) : value;
|
|
15
|
+
}
|
|
16
|
+
proxy.getCompletionsAtPosition = (fileName, position, options, formattingSettings) => {
|
|
17
|
+
const baseCompletions = languageService.getCompletionsAtPosition(fileName, position, options, formattingSettings);
|
|
18
|
+
const program = languageService.getProgram?.();
|
|
19
|
+
if (!program) {
|
|
20
|
+
return baseCompletions;
|
|
21
|
+
}
|
|
22
|
+
const rsxRegion = (0, import_compiler.findRsxExpressionRegionAtPosition)(program, fileName, position);
|
|
23
|
+
if (!rsxRegion) {
|
|
24
|
+
return baseCompletions;
|
|
25
|
+
}
|
|
26
|
+
const rsxCompletions = (0, import_compiler.getRsxCompletionsAtPosition)(program, fileName, position);
|
|
27
|
+
const pluginEntries = rsxCompletions.map((completion) => ({
|
|
28
|
+
name: completion.name,
|
|
29
|
+
kind: completion.kind === "method" ? ts.ScriptElementKind.memberFunctionElement : completion.kind === "constructor" ? ts.ScriptElementKind.classElement : ts.ScriptElementKind.memberVariableElement,
|
|
30
|
+
kindModifiers: "",
|
|
31
|
+
sortText: "0"
|
|
32
|
+
}));
|
|
33
|
+
const uniquePluginEntries = dedupeCompletionEntries(pluginEntries);
|
|
34
|
+
return {
|
|
35
|
+
entries: uniquePluginEntries,
|
|
36
|
+
isGlobalCompletion: false,
|
|
37
|
+
isMemberCompletion: true,
|
|
38
|
+
isNewIdentifierLocation: false
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
proxy.getQuickInfoAtPosition = (fileName, position) => {
|
|
42
|
+
const program = languageService.getProgram?.();
|
|
43
|
+
if (!program) {
|
|
44
|
+
return languageService.getQuickInfoAtPosition(fileName, position);
|
|
45
|
+
}
|
|
46
|
+
const hover = (0, import_compiler.getRsxHoverAtPosition)(program, fileName, position);
|
|
47
|
+
if (!hover) {
|
|
48
|
+
return languageService.getQuickInfoAtPosition(fileName, position);
|
|
49
|
+
}
|
|
50
|
+
const sourceFile = program.getSourceFile(fileName);
|
|
51
|
+
const hoveredIdentifier = sourceFile?.text.slice(hover.start, hover.end) ?? "";
|
|
52
|
+
const hoverLabel = hoveredIdentifier && !hover.text.startsWith(`${hoveredIdentifier}:`) ? `${hoveredIdentifier}: ${hover.text}` : hover.text;
|
|
53
|
+
return {
|
|
54
|
+
// Use a neutral symbol kind and an explicit label to avoid duplicated
|
|
55
|
+
// type-only renderings in VS Code tooltips.
|
|
56
|
+
kind: ts.ScriptElementKind.unknown,
|
|
57
|
+
kindModifiers: "",
|
|
58
|
+
textSpan: {
|
|
59
|
+
start: hover.start,
|
|
60
|
+
length: hover.end - hover.start
|
|
61
|
+
},
|
|
62
|
+
displayParts: [
|
|
63
|
+
{
|
|
64
|
+
kind: "text",
|
|
65
|
+
text: hoverLabel
|
|
66
|
+
}
|
|
67
|
+
],
|
|
68
|
+
documentation: []
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
proxy.getSignatureHelpItems = (fileName, position, options) => {
|
|
72
|
+
const baseSignatureHelp = languageService.getSignatureHelpItems(fileName, position, options);
|
|
73
|
+
const program = languageService.getProgram?.();
|
|
74
|
+
if (!program) {
|
|
75
|
+
return baseSignatureHelp;
|
|
76
|
+
}
|
|
77
|
+
const rsxRegion = (0, import_compiler.findRsxExpressionRegionAtPosition)(program, fileName, position);
|
|
78
|
+
if (!rsxRegion) {
|
|
79
|
+
return baseSignatureHelp;
|
|
80
|
+
}
|
|
81
|
+
const rsxSignatureHelp = (0, import_compiler.getRsxSignatureHelpAtPosition)(program, fileName, position);
|
|
82
|
+
if (!rsxSignatureHelp) {
|
|
83
|
+
return void 0;
|
|
84
|
+
}
|
|
85
|
+
const signatureItems = rsxSignatureHelp.items.map((item) => ({
|
|
86
|
+
isVariadic: item.parameters.some((parameter) => parameter.isRest),
|
|
87
|
+
prefixDisplayParts: [
|
|
88
|
+
{
|
|
89
|
+
kind: "punctuation",
|
|
90
|
+
text: "("
|
|
91
|
+
}
|
|
92
|
+
],
|
|
93
|
+
suffixDisplayParts: [
|
|
94
|
+
{
|
|
95
|
+
kind: "text",
|
|
96
|
+
text: `): ${item.returnTypeText}`
|
|
97
|
+
}
|
|
98
|
+
],
|
|
99
|
+
separatorDisplayParts: [
|
|
100
|
+
{
|
|
101
|
+
kind: "punctuation",
|
|
102
|
+
text: ", "
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
parameters: item.parameters.map((parameter) => ({
|
|
106
|
+
name: parameter.name,
|
|
107
|
+
isOptional: parameter.isOptional,
|
|
108
|
+
isRest: parameter.isRest,
|
|
109
|
+
documentation: [],
|
|
110
|
+
displayParts: [
|
|
111
|
+
{
|
|
112
|
+
kind: "parameterName",
|
|
113
|
+
text: parameter.name
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
kind: "text",
|
|
117
|
+
text: ": "
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
kind: "text",
|
|
121
|
+
text: parameter.typeText
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
})),
|
|
125
|
+
documentation: [],
|
|
126
|
+
tags: []
|
|
127
|
+
}));
|
|
128
|
+
return {
|
|
129
|
+
items: signatureItems,
|
|
130
|
+
applicableSpan: {
|
|
131
|
+
start: rsxSignatureHelp.applicableStart,
|
|
132
|
+
length: Math.max(1, rsxSignatureHelp.applicableEnd - rsxSignatureHelp.applicableStart)
|
|
133
|
+
},
|
|
134
|
+
selectedItemIndex: 0,
|
|
135
|
+
argumentIndex: rsxSignatureHelp.argumentIndex,
|
|
136
|
+
argumentCount: rsxSignatureHelp.argumentCount
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
proxy.getEncodedSemanticClassifications = (fileName, span, format) => {
|
|
140
|
+
const base = languageService.getEncodedSemanticClassifications(fileName, span, format) ?? {
|
|
141
|
+
spans: [],
|
|
142
|
+
endOfLineState: ts.EndOfLineState.None
|
|
143
|
+
};
|
|
144
|
+
const program = languageService.getProgram?.();
|
|
145
|
+
if (!program) {
|
|
146
|
+
return base;
|
|
147
|
+
}
|
|
148
|
+
const pluginSpans = getRsxEncodedClassifications({
|
|
149
|
+
ts,
|
|
150
|
+
program,
|
|
151
|
+
fileName,
|
|
152
|
+
span,
|
|
153
|
+
format
|
|
154
|
+
});
|
|
155
|
+
if (pluginSpans.length === 0) {
|
|
156
|
+
return base;
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
...base,
|
|
160
|
+
spans: [
|
|
161
|
+
...base.spans,
|
|
162
|
+
...pluginSpans
|
|
163
|
+
]
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
proxy.getSemanticDiagnostics = (fileName) => {
|
|
167
|
+
const baseDiagnostics = languageService.getSemanticDiagnostics(fileName);
|
|
168
|
+
const program = languageService.getProgram?.();
|
|
169
|
+
if (!program) {
|
|
170
|
+
return baseDiagnostics;
|
|
171
|
+
}
|
|
172
|
+
const sourceFile = program.getSourceFile(fileName);
|
|
173
|
+
if (!sourceFile) {
|
|
174
|
+
return baseDiagnostics;
|
|
175
|
+
}
|
|
176
|
+
const rsxDiagnostics = (0, import_compiler.getRsxDiagnosticsForFile)(program, fileName).map((diagnostic) => ({
|
|
177
|
+
file: sourceFile,
|
|
178
|
+
start: diagnostic.start,
|
|
179
|
+
length: diagnostic.end - diagnostic.start,
|
|
180
|
+
category: toTsDiagnosticCategory(ts, diagnostic.category),
|
|
181
|
+
code: diagnosticCode(diagnostic.category),
|
|
182
|
+
messageText: diagnostic.message,
|
|
183
|
+
source: "@rs-x/typescript-plugin"
|
|
184
|
+
}));
|
|
185
|
+
return [
|
|
186
|
+
...baseDiagnostics,
|
|
187
|
+
...rsxDiagnostics
|
|
188
|
+
];
|
|
189
|
+
};
|
|
190
|
+
return proxy;
|
|
191
|
+
}
|
|
192
|
+
__name(create, "create");
|
|
193
|
+
return {
|
|
194
|
+
create
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
__name(init, "init");
|
|
198
|
+
function dedupeCompletionEntries(entries) {
|
|
199
|
+
const seen = /* @__PURE__ */ new Set();
|
|
200
|
+
return entries.filter((entry) => {
|
|
201
|
+
if (seen.has(entry.name)) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
seen.add(entry.name);
|
|
205
|
+
return true;
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
__name(dedupeCompletionEntries, "dedupeCompletionEntries");
|
|
209
|
+
function getRsxEncodedClassifications(args) {
|
|
210
|
+
const { ts, program, fileName, span, format } = args;
|
|
211
|
+
const sourceFile = program.getSourceFile(fileName);
|
|
212
|
+
if (!sourceFile) {
|
|
213
|
+
return [];
|
|
214
|
+
}
|
|
215
|
+
const checker = program.getTypeChecker();
|
|
216
|
+
const sites = (0, import_compiler.detectExpressionSitesInSourceFile)(sourceFile, checker);
|
|
217
|
+
if (sites.length === 0) {
|
|
218
|
+
return [];
|
|
219
|
+
}
|
|
220
|
+
const spanStart = span.start;
|
|
221
|
+
const spanEnd = span.start + span.length;
|
|
222
|
+
const encoded = [];
|
|
223
|
+
for (const site of sites) {
|
|
224
|
+
const expressionStart = site.expressionLiteral.getStart(sourceFile) + 1;
|
|
225
|
+
const expressionEnd = site.expressionLiteral.getEnd() - 1;
|
|
226
|
+
if (expressionEnd <= spanStart || expressionStart >= spanEnd) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const expressionText = sourceFile.text.slice(expressionStart, expressionEnd);
|
|
230
|
+
const tokens = (0, import_compiler.tokenizeRsxExpression)(expressionText);
|
|
231
|
+
if (tokens.length === 0) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
for (const token of tokens) {
|
|
235
|
+
const tokenStart = expressionStart + token.start;
|
|
236
|
+
const tokenEnd = expressionStart + token.end;
|
|
237
|
+
if (tokenEnd <= spanStart || tokenStart >= spanEnd) {
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const clippedStart = tokenStart < spanStart ? spanStart : tokenStart;
|
|
241
|
+
const clippedEnd = tokenEnd > spanEnd ? spanEnd : tokenEnd;
|
|
242
|
+
if (clippedEnd <= clippedStart) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const classification = encodeClassification({
|
|
246
|
+
ts,
|
|
247
|
+
token,
|
|
248
|
+
format,
|
|
249
|
+
text: expressionText
|
|
250
|
+
});
|
|
251
|
+
if (classification === null) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
encoded.push(clippedStart, clippedEnd - clippedStart, classification);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return encoded;
|
|
258
|
+
}
|
|
259
|
+
__name(getRsxEncodedClassifications, "getRsxEncodedClassifications");
|
|
260
|
+
function encodeClassification(args) {
|
|
261
|
+
const { ts, token, format, text } = args;
|
|
262
|
+
if (token.kind !== "identifier" && token.kind !== "keyword") {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
if (format === ts.SemanticClassificationFormat.TwentyTwenty) {
|
|
266
|
+
const semanticTokenType = resolveSemanticTokenTypeForIdentifier(text, token);
|
|
267
|
+
return semanticTokenType + 1 << 8;
|
|
268
|
+
}
|
|
269
|
+
return token.kind === "keyword" ? ts.ClassificationType.keyword : ts.ClassificationType.identifier;
|
|
270
|
+
}
|
|
271
|
+
__name(encodeClassification, "encodeClassification");
|
|
272
|
+
function resolveSemanticTokenTypeForIdentifier(text, token) {
|
|
273
|
+
const prev = previousNonWhitespaceChar(text, token.start - 1);
|
|
274
|
+
const next = nextNonWhitespaceChar(text, token.end);
|
|
275
|
+
if (prev === ".") {
|
|
276
|
+
return 9;
|
|
277
|
+
}
|
|
278
|
+
if (next === "(") {
|
|
279
|
+
return 10;
|
|
280
|
+
}
|
|
281
|
+
return 7;
|
|
282
|
+
}
|
|
283
|
+
__name(resolveSemanticTokenTypeForIdentifier, "resolveSemanticTokenTypeForIdentifier");
|
|
284
|
+
function previousNonWhitespaceChar(text, from) {
|
|
285
|
+
for (let i = from; i >= 0; i--) {
|
|
286
|
+
if (!isWhitespace(text[i])) {
|
|
287
|
+
return text[i];
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
__name(previousNonWhitespaceChar, "previousNonWhitespaceChar");
|
|
293
|
+
function nextNonWhitespaceChar(text, from) {
|
|
294
|
+
for (let i = from; i < text.length; i++) {
|
|
295
|
+
if (!isWhitespace(text[i])) {
|
|
296
|
+
return text[i];
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
__name(nextNonWhitespaceChar, "nextNonWhitespaceChar");
|
|
302
|
+
function isWhitespace(char) {
|
|
303
|
+
return /\s/u.test(char);
|
|
304
|
+
}
|
|
305
|
+
__name(isWhitespace, "isWhitespace");
|
|
306
|
+
function toTsDiagnosticCategory(ts, category) {
|
|
307
|
+
switch (category) {
|
|
308
|
+
case "syntax":
|
|
309
|
+
case "semantic":
|
|
310
|
+
case "unsupported":
|
|
311
|
+
return ts.DiagnosticCategory.Error;
|
|
312
|
+
default:
|
|
313
|
+
return ts.DiagnosticCategory.Warning;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
__name(toTsDiagnosticCategory, "toTsDiagnosticCategory");
|
|
317
|
+
function diagnosticCode(category) {
|
|
318
|
+
switch (category) {
|
|
319
|
+
case "syntax":
|
|
320
|
+
return 97001;
|
|
321
|
+
case "semantic":
|
|
322
|
+
return 97002;
|
|
323
|
+
case "unsupported":
|
|
324
|
+
return 97003;
|
|
325
|
+
default:
|
|
326
|
+
return 97e3;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
__name(diagnosticCode, "diagnosticCode");
|
|
330
|
+
module.exports = init;
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rs-x/typescript-plugin",
|
|
3
|
+
"version": "2.0.0-next.0",
|
|
4
|
+
"description": "TypeScript server plugin that adds RS-X IntelliSense inside expression strings",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@rs-x/compiler": "2.0.0-next.0"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"rimraf": "^6.1.2",
|
|
14
|
+
"tsup": "^8.5.1",
|
|
15
|
+
"typescript": "^5.9.3"
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"typescript",
|
|
22
|
+
"tsserver",
|
|
23
|
+
"plugin",
|
|
24
|
+
"rs-x"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup lib/index.ts --format cjs --tsconfig ../tsconfig.build.json",
|
|
28
|
+
"clean": "rimraf dist"
|
|
29
|
+
}
|
|
30
|
+
}
|