@mdsvex/language-core 0.1.0-next.1 → 0.1.0-next.3
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/dist/_svelte_shims.d.ts +1 -0
- package/dist/completions.d.ts +51 -0
- package/dist/config.d.ts +48 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +1254 -0
- package/dist/language_plugin.d.ts +53 -0
- package/dist/svelte_plugin.d.ts +14 -0
- package/package.json +16 -7
- package/CHANGELOG.md +0 -37
- package/src/_svelte_shims.ts +0 -1
- package/src/completions.ts +0 -242
- package/src/config.ts +0 -521
- package/src/index.ts +0 -31
- package/src/language_plugin.ts +0 -556
- package/src/svelte_plugin.ts +0 -183
- package/test/completions.spec.ts +0 -179
- package/test/config.spec.ts +0 -304
- package/test/language_plugin.spec.ts +0 -236
- package/test/language_service.spec.ts +0 -314
- package/test/ts_harness.ts +0 -168
- package/tsconfig.json +0 -11
package/src/language_plugin.ts
DELETED
|
@@ -1,556 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* PFM Volar LanguagePlugin.
|
|
3
|
-
*
|
|
4
|
-
* Teaches Volar how to handle .pfm files by producing TypeScript
|
|
5
|
-
* VirtualCode through the pipeline:
|
|
6
|
-
*
|
|
7
|
-
* PFM source -> pfmToSvelte -> svelte2tsx -> compose mappings -> VirtualCode
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
LanguagePlugin,
|
|
12
|
-
VirtualCode,
|
|
13
|
-
IScriptSnapshot,
|
|
14
|
-
CodeMapping,
|
|
15
|
-
CodegenContext,
|
|
16
|
-
} from '@volar/language-core';
|
|
17
|
-
import { forEachEmbeddedCode } from '@volar/language-core';
|
|
18
|
-
import type { TypeScriptExtraServiceScript } from '@volar/typescript';
|
|
19
|
-
|
|
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';
|
|
47
|
-
|
|
48
|
-
/** Prepend shims content to svelte2tsx output and shift mappings. */
|
|
49
|
-
function inject_shims(
|
|
50
|
-
tsx_code: string,
|
|
51
|
-
raw_mappings: ReturnType<typeof v3ToVolarMappings>
|
|
52
|
-
): { code: string; mappings: typeof raw_mappings } {
|
|
53
|
-
if (!SVELTE_SHIMS) return { code: tsx_code, mappings: raw_mappings };
|
|
54
|
-
const prefix = SVELTE_SHIMS + '\n';
|
|
55
|
-
const prefixLen = prefix.length;
|
|
56
|
-
return {
|
|
57
|
-
code: prefix + tsx_code,
|
|
58
|
-
mappings: raw_mappings.map((m) => ({
|
|
59
|
-
...m,
|
|
60
|
-
generatedOffsets: m.generatedOffsets.map((o) => o + prefixLen),
|
|
61
|
-
})),
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export interface PfmVirtualCode extends VirtualCode {
|
|
66
|
-
id: string;
|
|
67
|
-
languageId: string;
|
|
68
|
-
snapshot: IScriptSnapshot;
|
|
69
|
-
mappings: CodeMapping[];
|
|
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;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** Create a simple IScriptSnapshot from a string. */
|
|
97
|
-
function create_snapshot(text: string): IScriptSnapshot {
|
|
98
|
-
return {
|
|
99
|
-
getText: (start, end) => text.slice(start, end),
|
|
100
|
-
getLength: () => text.length,
|
|
101
|
-
getChangeRange: () => undefined,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Build a minimal VirtualCode with no TS features (used as fallback on error). */
|
|
106
|
-
function create_fallback_virtual_code(source: string): PfmVirtualCode {
|
|
107
|
-
return {
|
|
108
|
-
id: 'root',
|
|
109
|
-
languageId: PFM_LANGUAGE_ID,
|
|
110
|
-
snapshot: create_snapshot(source),
|
|
111
|
-
mappings: [
|
|
112
|
-
{
|
|
113
|
-
sourceOffsets: [0],
|
|
114
|
-
generatedOffsets: [0],
|
|
115
|
-
lengths: [source.length],
|
|
116
|
-
data: { structure: true },
|
|
117
|
-
},
|
|
118
|
-
],
|
|
119
|
-
embeddedCodes: [],
|
|
120
|
-
diagnostics: [],
|
|
121
|
-
frontmatter: null,
|
|
122
|
-
templates: [],
|
|
123
|
-
probes: [],
|
|
124
|
-
};
|
|
125
|
-
}
|
|
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
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Run the full PFM to TypeScript pipeline and produce VirtualCode.
|
|
152
|
-
* On any error (parse failure, svelte2tsx crash), returns a fallback
|
|
153
|
-
* VirtualCode with no TS features instead of crashing the server.
|
|
154
|
-
*/
|
|
155
|
-
function create_virtual_code_from_source(
|
|
156
|
-
source: string,
|
|
157
|
-
script_id: string,
|
|
158
|
-
options: PfmToSvelteOptions
|
|
159
|
-
): PfmVirtualCode {
|
|
160
|
-
let svelte;
|
|
161
|
-
try {
|
|
162
|
-
// Step 1: PFM to Svelte
|
|
163
|
-
svelte = pfmToSvelte(source, options);
|
|
164
|
-
} catch {
|
|
165
|
-
return create_fallback_virtual_code(source);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
let tsx;
|
|
169
|
-
try {
|
|
170
|
-
// Step 2: Svelte to TypeScript via svelte2tsx
|
|
171
|
-
tsx = svelte_2_tsx(svelte.code, {
|
|
172
|
-
filename: script_id.replace(/\.[^./\\]+$/, '.svelte'),
|
|
173
|
-
isTsFile: false,
|
|
174
|
-
mode: 'ts',
|
|
175
|
-
});
|
|
176
|
-
} catch {
|
|
177
|
-
return create_fallback_virtual_code(source);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Step 3: Convert svelte2tsx's v3 source map to Volar format + inject shims
|
|
181
|
-
const v3_map = tsx.map.toJSON ? tsx.map.toJSON() : tsx.map;
|
|
182
|
-
const raw_svelte_to_ts = v3ToVolarMappings(
|
|
183
|
-
v3_map as any,
|
|
184
|
-
tsx.code,
|
|
185
|
-
svelte.code
|
|
186
|
-
);
|
|
187
|
-
const { code: ts_code, mappings: svelte_to_ts_mappings } = inject_shims(
|
|
188
|
-
tsx.code,
|
|
189
|
-
raw_svelte_to_ts
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
// Step 4: Filter mappings for composition.
|
|
193
|
-
// - Exclude "node" role (broadest spans cause duplicate hover results)
|
|
194
|
-
// - Exclude zero-length source mappings
|
|
195
|
-
// - Upgrade capabilities to full, let svelte2tsx's B-side gate features
|
|
196
|
-
const ALL_CAPS: CodeInformation = {
|
|
197
|
-
verification: true,
|
|
198
|
-
completion: true,
|
|
199
|
-
semantic: true,
|
|
200
|
-
navigation: true,
|
|
201
|
-
structure: true,
|
|
202
|
-
};
|
|
203
|
-
const filtered_mappings = svelte.mappings
|
|
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
|
-
}));
|
|
216
|
-
|
|
217
|
-
// Step 5: Compose PFM to Svelte + Svelte to TS = PFM to TS
|
|
218
|
-
// Use B's capabilities but disable format (overlapping ranges crash the formatter).
|
|
219
|
-
const pfm_to_ts = composeMappings(
|
|
220
|
-
filtered_mappings as any,
|
|
221
|
-
svelte_to_ts_mappings,
|
|
222
|
-
merge_capabilities
|
|
223
|
-
).filter((m) => !is_empty(m.data));
|
|
224
|
-
|
|
225
|
-
// Step 5b: Merge single-char non-identity mappings into adjacent
|
|
226
|
-
// neighbors. svelte2tsx splits attribute names at the first character
|
|
227
|
-
// (e.g. "v" to '"' + "alue=" to 'alue"') and maps syntax chars to filler
|
|
228
|
-
// spaces. These 1-char mappings cause two problems via Volar's
|
|
229
|
-
// inclusive-end (<=) translateOffset:
|
|
230
|
-
// 1. Diagnostic highlights bleed through whitespace (char to space anchors)
|
|
231
|
-
// 2. Duplicate hover at attribute-name boundaries (v/alue overlap)
|
|
232
|
-
// Merging them into the next mapping eliminates the boundary overlap
|
|
233
|
-
// while preserving hover on the first character.
|
|
234
|
-
pfm_to_ts.sort((a, b) => a.sourceOffsets[0] - b.sourceOffsets[0]);
|
|
235
|
-
for (let i = pfm_to_ts.length - 2; i >= 0; i--) {
|
|
236
|
-
const m = pfm_to_ts[i];
|
|
237
|
-
if (m.lengths[0] !== 1) continue;
|
|
238
|
-
const gen_len = m.generatedLengths ? m.generatedLengths[0] : 1;
|
|
239
|
-
if (gen_len !== 1) continue;
|
|
240
|
-
const src_ch = source.charCodeAt(m.sourceOffsets[0]);
|
|
241
|
-
const gen_ch = ts_code.charCodeAt(m.generatedOffsets[0]);
|
|
242
|
-
if (src_ch === gen_ch) continue; // identity, leave as-is
|
|
243
|
-
|
|
244
|
-
const next = pfm_to_ts[i + 1];
|
|
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];
|
|
250
|
-
|
|
251
|
-
// Merge into next if source-adjacent and generated gap <= 1
|
|
252
|
-
if (
|
|
253
|
-
m.sourceOffsets[0] + 1 === next.sourceOffsets[0] &&
|
|
254
|
-
next.generatedOffsets[0] - (m.generatedOffsets[0] + 1) <= 1
|
|
255
|
-
) {
|
|
256
|
-
const new_gen_start = m.generatedOffsets[0];
|
|
257
|
-
const new_gen_end = next.generatedOffsets[0] + next_gen_len;
|
|
258
|
-
next.sourceOffsets[0] = m.sourceOffsets[0];
|
|
259
|
-
next.lengths[0] += 1;
|
|
260
|
-
next.generatedOffsets[0] = new_gen_start;
|
|
261
|
-
const new_gen_len = new_gen_end - new_gen_start;
|
|
262
|
-
next.generatedLengths =
|
|
263
|
-
new_gen_len !== next.lengths[0] ? [new_gen_len] : undefined;
|
|
264
|
-
pfm_to_ts.splice(i, 1);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
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
|
-
|
|
277
|
-
// Step 6: Shrink non-identity trailing boundaries at adjacency points.
|
|
278
|
-
// Volar's translateOffset uses inclusive end (<=) so adjacent mappings
|
|
279
|
-
// share boundary offsets. When the last source char differs from the
|
|
280
|
-
// last generated char (e.g. "}" becomes ","), the previous mapping wrongly
|
|
281
|
-
// claims the next token's start, causing diagnostic highlights to
|
|
282
|
-
// bleed through inter-attribute whitespace. We only shrink when the
|
|
283
|
-
// next mapping starts at exactly this mapping's generated end, to
|
|
284
|
-
// avoid dropping diagnostics at the final mapping.
|
|
285
|
-
const by_gen_offset = pfm_to_ts
|
|
286
|
-
.slice()
|
|
287
|
-
.sort((a, b) => a.generatedOffsets[0] - b.generatedOffsets[0]);
|
|
288
|
-
for (let i = 0; i < by_gen_offset.length - 1; i++) {
|
|
289
|
-
const m = by_gen_offset[i];
|
|
290
|
-
const next = by_gen_offset[i + 1];
|
|
291
|
-
if (m.data.editor) continue;
|
|
292
|
-
const src_len = m.lengths[0];
|
|
293
|
-
const gen_len = m.generatedLengths ? m.generatedLengths[0] : src_len;
|
|
294
|
-
if (gen_len <= 1) continue;
|
|
295
|
-
const gen_end = m.generatedOffsets[0] + gen_len;
|
|
296
|
-
if (gen_end !== next.generatedOffsets[0]) continue;
|
|
297
|
-
const last_src = source.charCodeAt(m.sourceOffsets[0] + src_len - 1);
|
|
298
|
-
const last_gen = ts_code.charCodeAt(m.generatedOffsets[0] + gen_len - 1);
|
|
299
|
-
if (last_src !== last_gen) {
|
|
300
|
-
m.generatedLengths = [gen_len - 1];
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// Step 7: Deduplicate composed mappings that share the same source range.
|
|
305
|
-
// svelte2tsx maps identifiers like `Test` to multiple TS positions
|
|
306
|
-
// (import, type alias, constructor). Keeping only the first (lowest
|
|
307
|
-
// generated offset) prevents Volar from combining all their hover
|
|
308
|
-
// results into one noisy tooltip.
|
|
309
|
-
{
|
|
310
|
-
const seen = new Set<string>();
|
|
311
|
-
let write = 0;
|
|
312
|
-
for (let read = 0; read < pfm_to_ts.length; read++) {
|
|
313
|
-
const m = pfm_to_ts[read];
|
|
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);
|
|
317
|
-
if (seen.has(key)) continue;
|
|
318
|
-
seen.add(key);
|
|
319
|
-
pfm_to_ts[write++] = m;
|
|
320
|
-
}
|
|
321
|
-
pfm_to_ts.length = write;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// Build the TypeScript embedded VirtualCode
|
|
325
|
-
const ts_virtual_code: VirtualCode = {
|
|
326
|
-
id: 'ts',
|
|
327
|
-
languageId: 'typescript',
|
|
328
|
-
snapshot: create_snapshot(ts_code),
|
|
329
|
-
mappings: pfm_to_ts as CodeMapping[],
|
|
330
|
-
embeddedCodes: [],
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
// Build CSS embedded VirtualCodes from <style> blocks
|
|
334
|
-
const embedded_codes: VirtualCode[] = [ts_virtual_code];
|
|
335
|
-
const all_caps = {
|
|
336
|
-
verification: true,
|
|
337
|
-
completion: true,
|
|
338
|
-
semantic: true,
|
|
339
|
-
navigation: true,
|
|
340
|
-
structure: true,
|
|
341
|
-
format: true,
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
for (let i = 0; i < svelte.styleBlocks.length; i++) {
|
|
345
|
-
const sb = svelte.styleBlocks[i];
|
|
346
|
-
const css_text = source.slice(sb.sourceStart, sb.sourceEnd);
|
|
347
|
-
embedded_codes.push({
|
|
348
|
-
id: 'style_' + i,
|
|
349
|
-
languageId: 'css',
|
|
350
|
-
snapshot: create_snapshot(css_text),
|
|
351
|
-
mappings: [
|
|
352
|
-
{
|
|
353
|
-
sourceOffsets: [sb.sourceStart],
|
|
354
|
-
generatedOffsets: [0],
|
|
355
|
-
lengths: [css_text.length],
|
|
356
|
-
data: all_caps,
|
|
357
|
-
},
|
|
358
|
-
],
|
|
359
|
-
embeddedCodes: [],
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
// Build markdown VirtualCode: PFM source with excluded regions
|
|
364
|
-
// (frontmatter, script, style, imports) blanked out so the markdown
|
|
365
|
-
// service sees clean markdown without setext-heading false positives.
|
|
366
|
-
let md_source = source;
|
|
367
|
-
for (const region of svelte.excludedRegions) {
|
|
368
|
-
// Replace excluded bytes with spaces, preserving newlines for line alignment
|
|
369
|
-
const chunk = source.slice(region.start, region.end);
|
|
370
|
-
const blanked = chunk.replace(/[^\n]/g, ' ');
|
|
371
|
-
md_source =
|
|
372
|
-
md_source.slice(0, region.start) + blanked + md_source.slice(region.end);
|
|
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
|
-
);
|
|
379
|
-
|
|
380
|
-
embedded_codes.push({
|
|
381
|
-
id: 'md',
|
|
382
|
-
languageId: 'markdown',
|
|
383
|
-
snapshot: create_snapshot(md_source),
|
|
384
|
-
mappings: [
|
|
385
|
-
{
|
|
386
|
-
sourceOffsets: [0],
|
|
387
|
-
generatedOffsets: [0],
|
|
388
|
-
lengths: [source.length],
|
|
389
|
-
data: {
|
|
390
|
-
structure: true,
|
|
391
|
-
navigation: true,
|
|
392
|
-
},
|
|
393
|
-
},
|
|
394
|
-
],
|
|
395
|
-
embeddedCodes: [],
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
return {
|
|
399
|
-
id: 'root',
|
|
400
|
-
languageId: PFM_LANGUAGE_ID,
|
|
401
|
-
snapshot: create_snapshot(source),
|
|
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
|
-
],
|
|
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,
|
|
417
|
-
};
|
|
418
|
-
}
|
|
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
|
-
|
|
452
|
-
/** Simple string hash for content-based caching. */
|
|
453
|
-
function hash_string(s: string): number {
|
|
454
|
-
let h = 0;
|
|
455
|
-
for (let i = 0; i < s.length; i++) {
|
|
456
|
-
h = ((h << 5) - h + s.charCodeAt(i)) | 0;
|
|
457
|
-
}
|
|
458
|
-
return h;
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* Create a PFM LanguagePlugin for Volar.
|
|
463
|
-
*
|
|
464
|
-
* Usage:
|
|
465
|
-
* const plugin = createPfmLanguagePlugin();
|
|
466
|
-
* // Pass to @volar/language-server or @volar/kit
|
|
467
|
-
*/
|
|
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
|
-
}
|
|
493
|
-
|
|
494
|
-
return {
|
|
495
|
-
getLanguageId(scriptId: string): string | undefined {
|
|
496
|
-
return is_document(scriptId, config) ? PFM_LANGUAGE_ID : undefined;
|
|
497
|
-
},
|
|
498
|
-
|
|
499
|
-
createVirtualCode(
|
|
500
|
-
scriptId: string,
|
|
501
|
-
languageId: string,
|
|
502
|
-
snapshot: IScriptSnapshot,
|
|
503
|
-
_ctx: CodegenContext<string>
|
|
504
|
-
): PfmVirtualCode | undefined {
|
|
505
|
-
if (languageId !== PFM_LANGUAGE_ID) return undefined;
|
|
506
|
-
return virtual_code(scriptId, snapshot);
|
|
507
|
-
},
|
|
508
|
-
|
|
509
|
-
updateVirtualCode(
|
|
510
|
-
scriptId: string,
|
|
511
|
-
_virtualCode: PfmVirtualCode,
|
|
512
|
-
newSnapshot: IScriptSnapshot,
|
|
513
|
-
_ctx: CodegenContext<string>
|
|
514
|
-
): PfmVirtualCode | undefined {
|
|
515
|
-
return virtual_code(scriptId, newSnapshot);
|
|
516
|
-
},
|
|
517
|
-
|
|
518
|
-
disposeVirtualCode(scriptId: string) {
|
|
519
|
-
cache.delete(scriptId);
|
|
520
|
-
},
|
|
521
|
-
|
|
522
|
-
typescript: {
|
|
523
|
-
extraFileExtensions: [
|
|
524
|
-
...EXTENSIONS.map((ext) => ({
|
|
525
|
-
extension: ext.slice(1),
|
|
526
|
-
isMixedContent: true,
|
|
527
|
-
scriptKind: 7 as any, // ts.ScriptKind.Deferred
|
|
528
|
-
})),
|
|
529
|
-
],
|
|
530
|
-
getServiceScript(root: VirtualCode) {
|
|
531
|
-
// Return the embedded TS code as the service script:
|
|
532
|
-
// this tells TypeScript "when resolving this .pfm file
|
|
533
|
-
// as a module, use this TS code for its exports"
|
|
534
|
-
const ts_code = root.embeddedCodes?.find(
|
|
535
|
-
(c) => c.languageId === 'typescript'
|
|
536
|
-
);
|
|
537
|
-
if (ts_code) {
|
|
538
|
-
return {
|
|
539
|
-
code: ts_code,
|
|
540
|
-
extension: '.ts' as any,
|
|
541
|
-
scriptKind: 3 as any, // ts.ScriptKind.TS
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
return undefined;
|
|
545
|
-
},
|
|
546
|
-
},
|
|
547
|
-
};
|
|
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
|
-
}
|