@mdsvex/language-core 0.1.0-next.0 → 0.1.0-next.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.
- package/CHANGELOG.md +24 -0
- package/package.json +4 -3
- package/src/completions.ts +242 -0
- package/src/config.ts +521 -0
- package/src/index.ts +29 -2
- package/src/language_plugin.ts +265 -114
- package/test/completions.spec.ts +179 -0
- package/test/config.spec.ts +304 -0
- package/test/language_plugin.spec.ts +59 -0
- package/test/language_service.spec.ts +314 -0
- package/test/ts_harness.ts +168 -0
package/src/language_plugin.ts
CHANGED
|
@@ -13,28 +13,45 @@ import type {
|
|
|
13
13
|
IScriptSnapshot,
|
|
14
14
|
CodeMapping,
|
|
15
15
|
CodegenContext,
|
|
16
|
-
} from
|
|
17
|
-
import { forEachEmbeddedCode } from
|
|
18
|
-
import type { TypeScriptExtraServiceScript } from
|
|
16
|
+
} from '@volar/language-core';
|
|
17
|
+
import { forEachEmbeddedCode } from '@volar/language-core';
|
|
18
|
+
import type { TypeScriptExtraServiceScript } from '@volar/typescript';
|
|
19
19
|
|
|
20
|
-
import { pfmToSvelte } from
|
|
21
|
-
import {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
import {
|
|
20
|
+
import { pfmToSvelte } from '@mdsvex/source-map/pfm-to-svelte';
|
|
21
|
+
import type {
|
|
22
|
+
EditorMappingData,
|
|
23
|
+
PfmDiagnostic,
|
|
24
|
+
PfmToSvelteOptions,
|
|
25
|
+
PropsProbe,
|
|
26
|
+
} from '@mdsvex/source-map/pfm-to-svelte';
|
|
27
|
+
import { v3ToVolarMappings } from '@mdsvex/source-map/v3-to-volar';
|
|
28
|
+
import { composeMappings } from '@mdsvex/source-map/compose-mappings';
|
|
29
|
+
import { svelte2tsx as svelte_2_tsx } from 'svelte2tsx';
|
|
30
|
+
import type { CodeInformation } from '@mdsvex/render/mappings';
|
|
31
|
+
import type * as TS from 'typescript';
|
|
32
|
+
|
|
33
|
+
import { create_config_loader } from './config';
|
|
34
|
+
import type { ConfigLoader } from './config';
|
|
35
|
+
|
|
36
|
+
const PFM_LANGUAGE_ID = 'pfm';
|
|
37
|
+
/** always documents, .svx is the default extension of the vite plugin */
|
|
38
|
+
const EXTENSIONS = ['.pfm', '.svx'];
|
|
39
|
+
// a directive up to its label, then its args in parens
|
|
40
|
+
const DIRECTIVE_ARGS =
|
|
41
|
+
/((?<![\w:]):{1,3}[A-Za-z_][\w-]*\[(?:[^[\]\n\\]|\\.|\[[^[\]\n]*\])*\])(\([^)\n]*\))/g;
|
|
42
|
+
|
|
43
|
+
/** never documents, so their language needs no config lookup */
|
|
44
|
+
const NOT_DOCUMENTS = /\.(?:[cm]?[jt]sx?|svelte|json|css|d\.ts|map)$/;
|
|
45
|
+
|
|
46
|
+
import { SVELTE_SHIMS } from './_svelte_shims';
|
|
30
47
|
|
|
31
48
|
/** Prepend shims content to svelte2tsx output and shift mappings. */
|
|
32
49
|
function inject_shims(
|
|
33
50
|
tsx_code: string,
|
|
34
|
-
raw_mappings: ReturnType<typeof v3ToVolarMappings
|
|
51
|
+
raw_mappings: ReturnType<typeof v3ToVolarMappings>
|
|
35
52
|
): { code: string; mappings: typeof raw_mappings } {
|
|
36
53
|
if (!SVELTE_SHIMS) return { code: tsx_code, mappings: raw_mappings };
|
|
37
|
-
const prefix = SVELTE_SHIMS +
|
|
54
|
+
const prefix = SVELTE_SHIMS + '\n';
|
|
38
55
|
const prefixLen = prefix.length;
|
|
39
56
|
return {
|
|
40
57
|
code: prefix + tsx_code,
|
|
@@ -51,6 +68,29 @@ export interface PfmVirtualCode extends VirtualCode {
|
|
|
51
68
|
snapshot: IScriptSnapshot;
|
|
52
69
|
mappings: CodeMapping[];
|
|
53
70
|
embeddedCodes: VirtualCode[];
|
|
71
|
+
/** compile errors and warnings, typescript reports the rest */
|
|
72
|
+
diagnostics: PfmDiagnostic[];
|
|
73
|
+
/** the yaml of the frontmatter, null without any */
|
|
74
|
+
frontmatter: { start: number; end: number } | null;
|
|
75
|
+
/** the templates the frontmatter template key can name */
|
|
76
|
+
templates: TemplateChoice[];
|
|
77
|
+
/** the template the document compiled with */
|
|
78
|
+
template?: string;
|
|
79
|
+
/** type aliases in the typescript code holding the props of each template and directive */
|
|
80
|
+
probes: PropsProbe[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface TemplateChoice {
|
|
84
|
+
name: string;
|
|
85
|
+
/** the template file relative to the document, when the config resolves it */
|
|
86
|
+
file?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface PfmLanguagePluginOptions {
|
|
90
|
+
/** reads the exports of templates a mdsvex.config.json names, without it they replace nothing */
|
|
91
|
+
typescript?: typeof TS;
|
|
92
|
+
/** where the mdsvex config of a document comes from, by default the nearest manifest or json config */
|
|
93
|
+
config?: ConfigLoader;
|
|
54
94
|
}
|
|
55
95
|
|
|
56
96
|
/** Create a simple IScriptSnapshot from a string. */
|
|
@@ -65,19 +105,48 @@ function create_snapshot(text: string): IScriptSnapshot {
|
|
|
65
105
|
/** Build a minimal VirtualCode with no TS features (used as fallback on error). */
|
|
66
106
|
function create_fallback_virtual_code(source: string): PfmVirtualCode {
|
|
67
107
|
return {
|
|
68
|
-
id:
|
|
108
|
+
id: 'root',
|
|
69
109
|
languageId: PFM_LANGUAGE_ID,
|
|
70
110
|
snapshot: create_snapshot(source),
|
|
71
|
-
mappings: [
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
111
|
+
mappings: [
|
|
112
|
+
{
|
|
113
|
+
sourceOffsets: [0],
|
|
114
|
+
generatedOffsets: [0],
|
|
115
|
+
lengths: [source.length],
|
|
116
|
+
data: { structure: true },
|
|
117
|
+
},
|
|
118
|
+
],
|
|
77
119
|
embeddedCodes: [],
|
|
120
|
+
diagnostics: [],
|
|
121
|
+
frontmatter: null,
|
|
122
|
+
templates: [],
|
|
123
|
+
probes: [],
|
|
78
124
|
};
|
|
79
125
|
}
|
|
80
126
|
|
|
127
|
+
function template_choices(options: PfmToSvelteOptions): TemplateChoice[] {
|
|
128
|
+
const templates = options.compile?.templates ?? {};
|
|
129
|
+
return Object.keys(templates).map((name) => {
|
|
130
|
+
const file = options.resolve?.(templates[name].specifier, 'default');
|
|
131
|
+
return file === undefined ? { name } : { name, file };
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** the composed capabilities, a mapping the converter added keeps only what it allows */
|
|
136
|
+
function merge_capabilities(
|
|
137
|
+
a: CodeInformation & { editor?: true },
|
|
138
|
+
b: CodeInformation
|
|
139
|
+
): CodeInformation & { editor?: true } {
|
|
140
|
+
if (!a.editor) return { ...b, format: false };
|
|
141
|
+
const out: CodeInformation & { editor?: true } = { editor: true };
|
|
142
|
+
if (a.verification && b.verification) out.verification = b.verification;
|
|
143
|
+
if (a.completion && b.completion) out.completion = b.completion;
|
|
144
|
+
if (a.semantic && b.semantic) out.semantic = b.semantic;
|
|
145
|
+
if (a.navigation && b.navigation) out.navigation = b.navigation;
|
|
146
|
+
if (a.structure && b.structure) out.structure = b.structure;
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
81
150
|
/**
|
|
82
151
|
* Run the full PFM to TypeScript pipeline and produce VirtualCode.
|
|
83
152
|
* On any error (parse failure, svelte2tsx crash), returns a fallback
|
|
@@ -86,11 +155,12 @@ function create_fallback_virtual_code(source: string): PfmVirtualCode {
|
|
|
86
155
|
function create_virtual_code_from_source(
|
|
87
156
|
source: string,
|
|
88
157
|
script_id: string,
|
|
158
|
+
options: PfmToSvelteOptions
|
|
89
159
|
): PfmVirtualCode {
|
|
90
160
|
let svelte;
|
|
91
161
|
try {
|
|
92
162
|
// Step 1: PFM to Svelte
|
|
93
|
-
svelte = pfmToSvelte(source);
|
|
163
|
+
svelte = pfmToSvelte(source, options);
|
|
94
164
|
} catch {
|
|
95
165
|
return create_fallback_virtual_code(source);
|
|
96
166
|
}
|
|
@@ -99,9 +169,9 @@ function create_virtual_code_from_source(
|
|
|
99
169
|
try {
|
|
100
170
|
// Step 2: Svelte to TypeScript via svelte2tsx
|
|
101
171
|
tsx = svelte_2_tsx(svelte.code, {
|
|
102
|
-
filename: script_id.replace(
|
|
172
|
+
filename: script_id.replace(/\.[^./\\]+$/, '.svelte'),
|
|
103
173
|
isTsFile: false,
|
|
104
|
-
mode:
|
|
174
|
+
mode: 'ts',
|
|
105
175
|
});
|
|
106
176
|
} catch {
|
|
107
177
|
return create_fallback_virtual_code(source);
|
|
@@ -109,10 +179,14 @@ function create_virtual_code_from_source(
|
|
|
109
179
|
|
|
110
180
|
// Step 3: Convert svelte2tsx's v3 source map to Volar format + inject shims
|
|
111
181
|
const v3_map = tsx.map.toJSON ? tsx.map.toJSON() : tsx.map;
|
|
112
|
-
const raw_svelte_to_ts = v3ToVolarMappings(
|
|
182
|
+
const raw_svelte_to_ts = v3ToVolarMappings(
|
|
183
|
+
v3_map as any,
|
|
184
|
+
tsx.code,
|
|
185
|
+
svelte.code
|
|
186
|
+
);
|
|
113
187
|
const { code: ts_code, mappings: svelte_to_ts_mappings } = inject_shims(
|
|
114
188
|
tsx.code,
|
|
115
|
-
raw_svelte_to_ts
|
|
189
|
+
raw_svelte_to_ts
|
|
116
190
|
);
|
|
117
191
|
|
|
118
192
|
// Step 4: Filter mappings for composition.
|
|
@@ -127,19 +201,26 @@ function create_virtual_code_from_source(
|
|
|
127
201
|
structure: true,
|
|
128
202
|
};
|
|
129
203
|
const filtered_mappings = svelte.mappings
|
|
130
|
-
.filter((m) => (m.data as any).role !==
|
|
131
|
-
|
|
204
|
+
.filter((m) => (m.data as any).role !== 'node' && m.lengths[0] > 0)
|
|
205
|
+
// mappings read through accessors, so a spread would drop the arrays
|
|
206
|
+
.map((m) => ({
|
|
207
|
+
sourceOffsets: m.sourceOffsets,
|
|
208
|
+
generatedOffsets: m.generatedOffsets,
|
|
209
|
+
lengths: m.lengths,
|
|
210
|
+
...(m.generatedLengths ? { generatedLengths: m.generatedLengths } : {}),
|
|
211
|
+
// a mapping the converter added keeps its own capabilities
|
|
212
|
+
data: (m.data as EditorMappingData).editor
|
|
213
|
+
? editor_caps(m.data as EditorMappingData)
|
|
214
|
+
: ALL_CAPS,
|
|
215
|
+
}));
|
|
132
216
|
|
|
133
217
|
// Step 5: Compose PFM to Svelte + Svelte to TS = PFM to TS
|
|
134
218
|
// Use B's capabilities but disable format (overlapping ranges crash the formatter).
|
|
135
219
|
const pfm_to_ts = composeMappings(
|
|
136
220
|
filtered_mappings as any,
|
|
137
221
|
svelte_to_ts_mappings,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
format: false,
|
|
141
|
-
}),
|
|
142
|
-
);
|
|
222
|
+
merge_capabilities
|
|
223
|
+
).filter((m) => !is_empty(m.data));
|
|
143
224
|
|
|
144
225
|
// Step 5b: Merge single-char non-identity mappings into adjacent
|
|
145
226
|
// neighbors. svelte2tsx splits attribute names at the first character
|
|
@@ -150,9 +231,7 @@ function create_virtual_code_from_source(
|
|
|
150
231
|
// 2. Duplicate hover at attribute-name boundaries (v/alue overlap)
|
|
151
232
|
// Merging them into the next mapping eliminates the boundary overlap
|
|
152
233
|
// while preserving hover on the first character.
|
|
153
|
-
pfm_to_ts.sort(
|
|
154
|
-
(a, b) => a.sourceOffsets[0] - b.sourceOffsets[0],
|
|
155
|
-
);
|
|
234
|
+
pfm_to_ts.sort((a, b) => a.sourceOffsets[0] - b.sourceOffsets[0]);
|
|
156
235
|
for (let i = pfm_to_ts.length - 2; i >= 0; i--) {
|
|
157
236
|
const m = pfm_to_ts[i];
|
|
158
237
|
if (m.lengths[0] !== 1) continue;
|
|
@@ -163,22 +242,38 @@ function create_virtual_code_from_source(
|
|
|
163
242
|
if (src_ch === gen_ch) continue; // identity, leave as-is
|
|
164
243
|
|
|
165
244
|
const next = pfm_to_ts[i + 1];
|
|
166
|
-
|
|
245
|
+
// a mapping the converter added keeps its capabilities apart
|
|
246
|
+
if (!m.data.editor !== !next.data.editor) continue;
|
|
247
|
+
const next_gen_len = next.generatedLengths
|
|
248
|
+
? next.generatedLengths[0]
|
|
249
|
+
: next.lengths[0];
|
|
167
250
|
|
|
168
251
|
// Merge into next if source-adjacent and generated gap <= 1
|
|
169
|
-
if (
|
|
170
|
-
|
|
252
|
+
if (
|
|
253
|
+
m.sourceOffsets[0] + 1 === next.sourceOffsets[0] &&
|
|
254
|
+
next.generatedOffsets[0] - (m.generatedOffsets[0] + 1) <= 1
|
|
255
|
+
) {
|
|
171
256
|
const new_gen_start = m.generatedOffsets[0];
|
|
172
257
|
const new_gen_end = next.generatedOffsets[0] + next_gen_len;
|
|
173
258
|
next.sourceOffsets[0] = m.sourceOffsets[0];
|
|
174
259
|
next.lengths[0] += 1;
|
|
175
260
|
next.generatedOffsets[0] = new_gen_start;
|
|
176
261
|
const new_gen_len = new_gen_end - new_gen_start;
|
|
177
|
-
next.generatedLengths =
|
|
262
|
+
next.generatedLengths =
|
|
263
|
+
new_gen_len !== next.lengths[0] ? [new_gen_len] : undefined;
|
|
178
264
|
pfm_to_ts.splice(i, 1);
|
|
179
265
|
}
|
|
180
266
|
}
|
|
181
267
|
|
|
268
|
+
// svelte2tsx quotes a prop key and typescript spans both quotes, so an arg takes the closing one
|
|
269
|
+
for (const m of pfm_to_ts) {
|
|
270
|
+
if (!m.data.editor) continue;
|
|
271
|
+
const start = m.generatedOffsets[0];
|
|
272
|
+
const len = m.generatedLengths ? m.generatedLengths[0] : m.lengths[0];
|
|
273
|
+
if (ts_code[start] === '"' && ts_code[start + len] === '"' && len > 1)
|
|
274
|
+
m.generatedLengths = [len + 1];
|
|
275
|
+
}
|
|
276
|
+
|
|
182
277
|
// Step 6: Shrink non-identity trailing boundaries at adjacency points.
|
|
183
278
|
// Volar's translateOffset uses inclusive end (<=) so adjacent mappings
|
|
184
279
|
// share boundary offsets. When the last source char differs from the
|
|
@@ -187,12 +282,13 @@ function create_virtual_code_from_source(
|
|
|
187
282
|
// bleed through inter-attribute whitespace. We only shrink when the
|
|
188
283
|
// next mapping starts at exactly this mapping's generated end, to
|
|
189
284
|
// avoid dropping diagnostics at the final mapping.
|
|
190
|
-
const by_gen_offset = pfm_to_ts
|
|
191
|
-
(
|
|
192
|
-
|
|
285
|
+
const by_gen_offset = pfm_to_ts
|
|
286
|
+
.slice()
|
|
287
|
+
.sort((a, b) => a.generatedOffsets[0] - b.generatedOffsets[0]);
|
|
193
288
|
for (let i = 0; i < by_gen_offset.length - 1; i++) {
|
|
194
289
|
const m = by_gen_offset[i];
|
|
195
290
|
const next = by_gen_offset[i + 1];
|
|
291
|
+
if (m.data.editor) continue;
|
|
196
292
|
const src_len = m.lengths[0];
|
|
197
293
|
const gen_len = m.generatedLengths ? m.generatedLengths[0] : src_len;
|
|
198
294
|
if (gen_len <= 1) continue;
|
|
@@ -215,7 +311,9 @@ function create_virtual_code_from_source(
|
|
|
215
311
|
let write = 0;
|
|
216
312
|
for (let read = 0; read < pfm_to_ts.length; read++) {
|
|
217
313
|
const m = pfm_to_ts[read];
|
|
218
|
-
|
|
314
|
+
// a frontmatter key maps once to hover and once to check
|
|
315
|
+
const key =
|
|
316
|
+
m.sourceOffsets[0] + ':' + m.lengths[0] + ':' + caps_key(m.data);
|
|
219
317
|
if (seen.has(key)) continue;
|
|
220
318
|
seen.add(key);
|
|
221
319
|
pfm_to_ts[write++] = m;
|
|
@@ -225,8 +323,8 @@ function create_virtual_code_from_source(
|
|
|
225
323
|
|
|
226
324
|
// Build the TypeScript embedded VirtualCode
|
|
227
325
|
const ts_virtual_code: VirtualCode = {
|
|
228
|
-
id:
|
|
229
|
-
languageId:
|
|
326
|
+
id: 'ts',
|
|
327
|
+
languageId: 'typescript',
|
|
230
328
|
snapshot: create_snapshot(ts_code),
|
|
231
329
|
mappings: pfm_to_ts as CodeMapping[],
|
|
232
330
|
embeddedCodes: [],
|
|
@@ -247,15 +345,17 @@ function create_virtual_code_from_source(
|
|
|
247
345
|
const sb = svelte.styleBlocks[i];
|
|
248
346
|
const css_text = source.slice(sb.sourceStart, sb.sourceEnd);
|
|
249
347
|
embedded_codes.push({
|
|
250
|
-
id:
|
|
251
|
-
languageId:
|
|
348
|
+
id: 'style_' + i,
|
|
349
|
+
languageId: 'css',
|
|
252
350
|
snapshot: create_snapshot(css_text),
|
|
253
|
-
mappings: [
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
351
|
+
mappings: [
|
|
352
|
+
{
|
|
353
|
+
sourceOffsets: [sb.sourceStart],
|
|
354
|
+
generatedOffsets: [0],
|
|
355
|
+
lengths: [css_text.length],
|
|
356
|
+
data: all_caps,
|
|
357
|
+
},
|
|
358
|
+
],
|
|
259
359
|
embeddedCodes: [],
|
|
260
360
|
});
|
|
261
361
|
}
|
|
@@ -267,40 +367,88 @@ function create_virtual_code_from_source(
|
|
|
267
367
|
for (const region of svelte.excludedRegions) {
|
|
268
368
|
// Replace excluded bytes with spaces, preserving newlines for line alignment
|
|
269
369
|
const chunk = source.slice(region.start, region.end);
|
|
270
|
-
const blanked = chunk.replace(/[^\n]/g,
|
|
271
|
-
md_source =
|
|
370
|
+
const blanked = chunk.replace(/[^\n]/g, ' ');
|
|
371
|
+
md_source =
|
|
372
|
+
md_source.slice(0, region.start) + blanked + md_source.slice(region.end);
|
|
272
373
|
}
|
|
374
|
+
// the args of a directive would read as the url of a link
|
|
375
|
+
md_source = md_source.replace(
|
|
376
|
+
DIRECTIVE_ARGS,
|
|
377
|
+
(_, head: string, args: string) => head + ' '.repeat(args.length)
|
|
378
|
+
);
|
|
273
379
|
|
|
274
380
|
embedded_codes.push({
|
|
275
|
-
id:
|
|
276
|
-
languageId:
|
|
381
|
+
id: 'md',
|
|
382
|
+
languageId: 'markdown',
|
|
277
383
|
snapshot: create_snapshot(md_source),
|
|
278
|
-
mappings: [
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
384
|
+
mappings: [
|
|
385
|
+
{
|
|
386
|
+
sourceOffsets: [0],
|
|
387
|
+
generatedOffsets: [0],
|
|
388
|
+
lengths: [source.length],
|
|
389
|
+
data: {
|
|
390
|
+
structure: true,
|
|
391
|
+
navigation: true,
|
|
392
|
+
},
|
|
285
393
|
},
|
|
286
|
-
|
|
394
|
+
],
|
|
287
395
|
embeddedCodes: [],
|
|
288
396
|
});
|
|
289
397
|
|
|
290
398
|
return {
|
|
291
|
-
id:
|
|
399
|
+
id: 'root',
|
|
292
400
|
languageId: PFM_LANGUAGE_ID,
|
|
293
401
|
snapshot: create_snapshot(source),
|
|
294
|
-
mappings: [
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
402
|
+
mappings: [
|
|
403
|
+
{
|
|
404
|
+
sourceOffsets: [0],
|
|
405
|
+
generatedOffsets: [0],
|
|
406
|
+
lengths: [source.length],
|
|
407
|
+
// so a service reports compile diagnostics and completes frontmatter on the root
|
|
408
|
+
data: { structure: true, verification: true, completion: true },
|
|
409
|
+
},
|
|
410
|
+
],
|
|
300
411
|
embeddedCodes: embedded_codes,
|
|
412
|
+
diagnostics: svelte.diagnostics,
|
|
413
|
+
frontmatter: svelte.frontmatter,
|
|
414
|
+
templates: template_choices(options),
|
|
415
|
+
template: svelte.template,
|
|
416
|
+
probes: svelte.probes,
|
|
301
417
|
};
|
|
302
418
|
}
|
|
303
419
|
|
|
420
|
+
function editor_caps(
|
|
421
|
+
data: EditorMappingData
|
|
422
|
+
): CodeInformation & { editor: true } {
|
|
423
|
+
const out: CodeInformation & { editor: true } = { editor: true };
|
|
424
|
+
if (data.verification) out.verification = true;
|
|
425
|
+
if (data.completion) out.completion = true;
|
|
426
|
+
if (data.semantic) out.semantic = true;
|
|
427
|
+
if (data.navigation) out.navigation = true;
|
|
428
|
+
if (data.structure) out.structure = true;
|
|
429
|
+
return out;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function is_empty(data: CodeInformation): boolean {
|
|
433
|
+
return (
|
|
434
|
+
!data.verification &&
|
|
435
|
+
!data.completion &&
|
|
436
|
+
!data.semantic &&
|
|
437
|
+
!data.navigation &&
|
|
438
|
+
!data.structure &&
|
|
439
|
+
!data.format
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function caps_key(data: CodeInformation): string {
|
|
444
|
+
return (
|
|
445
|
+
(data.verification ? 'v' : '') +
|
|
446
|
+
(data.semantic ? 's' : '') +
|
|
447
|
+
(data.navigation ? 'n' : '') +
|
|
448
|
+
(data.completion ? 'c' : '')
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
|
|
304
452
|
/** Simple string hash for content-based caching. */
|
|
305
453
|
function hash_string(s: string): number {
|
|
306
454
|
let h = 0;
|
|
@@ -317,54 +465,54 @@ function hash_string(s: string): number {
|
|
|
317
465
|
* const plugin = createPfmLanguagePlugin();
|
|
318
466
|
* // Pass to @volar/language-server or @volar/kit
|
|
319
467
|
*/
|
|
320
|
-
export function create_pfm_language_plugin(
|
|
321
|
-
|
|
322
|
-
|
|
468
|
+
export function create_pfm_language_plugin(
|
|
469
|
+
plugin_options: PfmLanguagePluginOptions = {}
|
|
470
|
+
): LanguagePlugin<string, PfmVirtualCode> {
|
|
471
|
+
// a file converts again only when its content or config changed
|
|
472
|
+
const cache = new Map<
|
|
473
|
+
string,
|
|
474
|
+
{ hash: number; stamp: string; result: PfmVirtualCode }
|
|
475
|
+
>();
|
|
476
|
+
const config =
|
|
477
|
+
plugin_options.config ??
|
|
478
|
+
create_config_loader({ typescript: plugin_options.typescript });
|
|
479
|
+
|
|
480
|
+
function virtual_code(scriptId: string, snapshot: IScriptSnapshot) {
|
|
481
|
+
const source = snapshot.getText(0, snapshot.getLength());
|
|
482
|
+
const hash = hash_string(source);
|
|
483
|
+
const { stamp, options } = config.options_for(scriptId);
|
|
484
|
+
const cached = cache.get(scriptId);
|
|
485
|
+
if (cached && cached.hash === hash && cached.stamp === stamp) {
|
|
486
|
+
return cached.result;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const result = create_virtual_code_from_source(source, scriptId, options);
|
|
490
|
+
cache.set(scriptId, { hash, stamp, result });
|
|
491
|
+
return result;
|
|
492
|
+
}
|
|
323
493
|
|
|
324
494
|
return {
|
|
325
495
|
getLanguageId(scriptId: string): string | undefined {
|
|
326
|
-
|
|
327
|
-
return PFM_LANGUAGE_ID;
|
|
328
|
-
}
|
|
329
|
-
return undefined;
|
|
496
|
+
return is_document(scriptId, config) ? PFM_LANGUAGE_ID : undefined;
|
|
330
497
|
},
|
|
331
498
|
|
|
332
499
|
createVirtualCode(
|
|
333
500
|
scriptId: string,
|
|
334
501
|
languageId: string,
|
|
335
502
|
snapshot: IScriptSnapshot,
|
|
336
|
-
_ctx: CodegenContext<string
|
|
503
|
+
_ctx: CodegenContext<string>
|
|
337
504
|
): PfmVirtualCode | undefined {
|
|
338
505
|
if (languageId !== PFM_LANGUAGE_ID) return undefined;
|
|
339
|
-
|
|
340
|
-
const source = snapshot.getText(0, snapshot.getLength());
|
|
341
|
-
const hash = hash_string(source);
|
|
342
|
-
const cached = cache.get(scriptId);
|
|
343
|
-
if (cached && cached.hash === hash) {
|
|
344
|
-
return cached.result;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
const result = create_virtual_code_from_source(source, scriptId);
|
|
348
|
-
cache.set(scriptId, { hash, result });
|
|
349
|
-
return result;
|
|
506
|
+
return virtual_code(scriptId, snapshot);
|
|
350
507
|
},
|
|
351
508
|
|
|
352
509
|
updateVirtualCode(
|
|
353
510
|
scriptId: string,
|
|
354
511
|
_virtualCode: PfmVirtualCode,
|
|
355
512
|
newSnapshot: IScriptSnapshot,
|
|
356
|
-
_ctx: CodegenContext<string
|
|
513
|
+
_ctx: CodegenContext<string>
|
|
357
514
|
): PfmVirtualCode | undefined {
|
|
358
|
-
|
|
359
|
-
const hash = hash_string(source);
|
|
360
|
-
const cached = cache.get(scriptId);
|
|
361
|
-
if (cached && cached.hash === hash) {
|
|
362
|
-
return cached.result;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
const result = create_virtual_code_from_source(source, scriptId);
|
|
366
|
-
cache.set(scriptId, { hash, result });
|
|
367
|
-
return result;
|
|
515
|
+
return virtual_code(scriptId, newSnapshot);
|
|
368
516
|
},
|
|
369
517
|
|
|
370
518
|
disposeVirtualCode(scriptId: string) {
|
|
@@ -373,33 +521,36 @@ export function create_pfm_language_plugin(): LanguagePlugin<string, PfmVirtualC
|
|
|
373
521
|
|
|
374
522
|
typescript: {
|
|
375
523
|
extraFileExtensions: [
|
|
376
|
-
{
|
|
377
|
-
extension:
|
|
524
|
+
...EXTENSIONS.map((ext) => ({
|
|
525
|
+
extension: ext.slice(1),
|
|
378
526
|
isMixedContent: true,
|
|
379
527
|
scriptKind: 7 as any, // ts.ScriptKind.Deferred
|
|
380
|
-
},
|
|
528
|
+
})),
|
|
381
529
|
],
|
|
382
530
|
getServiceScript(root: VirtualCode) {
|
|
383
531
|
// Return the embedded TS code as the service script:
|
|
384
532
|
// this tells TypeScript "when resolving this .pfm file
|
|
385
533
|
// as a module, use this TS code for its exports"
|
|
386
534
|
const ts_code = root.embeddedCodes?.find(
|
|
387
|
-
(c) => c.languageId ===
|
|
535
|
+
(c) => c.languageId === 'typescript'
|
|
388
536
|
);
|
|
389
537
|
if (ts_code) {
|
|
390
538
|
return {
|
|
391
539
|
code: ts_code,
|
|
392
|
-
extension:
|
|
540
|
+
extension: '.ts' as any,
|
|
393
541
|
scriptKind: 3 as any, // ts.ScriptKind.TS
|
|
394
542
|
};
|
|
395
543
|
}
|
|
396
544
|
return undefined;
|
|
397
545
|
},
|
|
398
|
-
getExtraServiceScripts(_fileName: string, _root: VirtualCode) {
|
|
399
|
-
// The main TS code is already returned by getServiceScript.
|
|
400
|
-
// No extra scripts needed, returning it again causes duplicates.
|
|
401
|
-
return [];
|
|
402
|
-
},
|
|
403
546
|
},
|
|
404
547
|
};
|
|
405
548
|
}
|
|
549
|
+
|
|
550
|
+
/** .pfm and .svx always, another extension when the config of the file lists it */
|
|
551
|
+
export function is_document(file: string, config: ConfigLoader): boolean {
|
|
552
|
+
for (const ext of EXTENSIONS) if (file.endsWith(ext)) return true;
|
|
553
|
+
if (NOT_DOCUMENTS.test(file) || !/\.[^./\\]+$/.test(file)) return false;
|
|
554
|
+
const extensions = config.load(file)?.manifest.extensions;
|
|
555
|
+
return extensions !== undefined && extensions.some((e) => file.endsWith(e));
|
|
556
|
+
}
|