@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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @mdsvex/language-core
|
|
2
2
|
|
|
3
|
+
## 0.1.0-next.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#887](https://github.com/pngwn/MDsveX/pull/887) [`ffaf805`](https://github.com/pngwn/MDsveX/commit/ffaf805cd7ac2ebb07c1b5a33896c36e53dc6c67) Thanks [@pngwn](https://github.com/pngwn)! - The editor now knows your templates, replacement components and directives. The Vite plugin writes what it resolved to `node_modules/.mdsvex/manifest.json` and the editor reads it, falling back to an `mdsvex.config.json` like the playground's:
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"templates": { "docs": "#lib/templates/Docs.svelte" },
|
|
12
|
+
"components": "#lib/markdown.ts"
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Hovering a replaced element or directive shows its component, and go to definition opens it. The frontmatter `template` key is typed as your template names or `false`. Completions offer template names, the template's props as frontmatter keys and their literal values, and a directive's props as args with their values. Directive args and the frontmatter of a templated document are type-checked against the component's props, and `metadata` is typed by the same YAML parser `compile()` uses. Compile errors such as an unknown template show on their line, and `.svx` files are supported alongside `.pfm`.
|
|
17
|
+
|
|
18
|
+
- [#895](https://github.com/pngwn/MDsveX/pull/895) [`57ecb44`](https://github.com/pngwn/MDsveX/commit/57ecb446b292a3406dcf62550986ed15e1aa2b80) Thanks [@pngwn](https://github.com/pngwn)! - The editor reads live expressions in code as references, so type errors, hover, rename and the unused check work on them. It highlights code as the Vite plugin does unless the plugin renders code plain, and `mdsvex.config.json` takes `highlight: false`. A half-typed live expression is an error on that fence rather than on the whole document.
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- Updated dependencies [[`60da683`](https://github.com/pngwn/MDsveX/commit/60da68313df11747bdd6235dffc325c30af60cb2), [`8ede5e2`](https://github.com/pngwn/MDsveX/commit/8ede5e2b409511978562fcec3271e7f5182789db), [`b249bf0`](https://github.com/pngwn/MDsveX/commit/b249bf0dbbfdef330e0e78739b7d44176f9c98b9), [`c81604f`](https://github.com/pngwn/MDsveX/commit/c81604f77f7e2662f42db2dcfb1c2a29a594e726), [`ffaf805`](https://github.com/pngwn/MDsveX/commit/ffaf805cd7ac2ebb07c1b5a33896c36e53dc6c67), [`69b6c62`](https://github.com/pngwn/MDsveX/commit/69b6c624256f1e8fb1cf8e5f348980a043f2c1db), [`fa297f9`](https://github.com/pngwn/MDsveX/commit/fa297f9b3fda7cc11c3f4274ba0b0cc27acedf9a), [`311902d`](https://github.com/pngwn/MDsveX/commit/311902de7c606430e3899440d37def14f5190b6c), [`79bebc9`](https://github.com/pngwn/MDsveX/commit/79bebc937b5c51d7fb50d06ff09f83bf23d44a87), [`6c64d8b`](https://github.com/pngwn/MDsveX/commit/6c64d8b7c13c876dc03668123a9dbfcc9520b885), [`ffaf805`](https://github.com/pngwn/MDsveX/commit/ffaf805cd7ac2ebb07c1b5a33896c36e53dc6c67), [`57ecb44`](https://github.com/pngwn/MDsveX/commit/57ecb446b292a3406dcf62550986ed15e1aa2b80), [`1219ceb`](https://github.com/pngwn/MDsveX/commit/1219ceb0c6805ac8b169dadb13694f354d96a619), [`98a1b87`](https://github.com/pngwn/MDsveX/commit/98a1b87b1c45f192c93bf3b910d6ebc8284b94f4), [`31aa6e2`](https://github.com/pngwn/MDsveX/commit/31aa6e20390c6fa656845add1d19e0439dda48c7), [`fd54579`](https://github.com/pngwn/MDsveX/commit/fd5457966ce23d1eb29a7935a550394adf4498e4), [`8bd14fe`](https://github.com/pngwn/MDsveX/commit/8bd14fe77aed1ff532757a06e934fa6238ecb2da), [`98a1b87`](https://github.com/pngwn/MDsveX/commit/98a1b87b1c45f192c93bf3b910d6ebc8284b94f4), [`045aca9`](https://github.com/pngwn/MDsveX/commit/045aca94077183b8b58bb59c45813f0aa4f33cde), [`59bbad3`](https://github.com/pngwn/MDsveX/commit/59bbad3d1905ea378b8745d916af0dca3e63b042), [`f2ff5fe`](https://github.com/pngwn/MDsveX/commit/f2ff5fe144a9009a79e0aa7a6f08f1bed5a7696c), [`57ecb44`](https://github.com/pngwn/MDsveX/commit/57ecb446b292a3406dcf62550986ed15e1aa2b80), [`fd54579`](https://github.com/pngwn/MDsveX/commit/fd5457966ce23d1eb29a7935a550394adf4498e4), [`4c55f5e`](https://github.com/pngwn/MDsveX/commit/4c55f5eea023e687202fa97626b401f09bffd6f7), [`e708e1d`](https://github.com/pngwn/MDsveX/commit/e708e1d4c66478efd8606fa2e1ff2fee40e0732a), [`4927fd8`](https://github.com/pngwn/MDsveX/commit/4927fd87ef00ae106f081cf4315e425ec71a790c), [`98a1b87`](https://github.com/pngwn/MDsveX/commit/98a1b87b1c45f192c93bf3b910d6ebc8284b94f4), [`60da683`](https://github.com/pngwn/MDsveX/commit/60da68313df11747bdd6235dffc325c30af60cb2), [`2867d9b`](https://github.com/pngwn/MDsveX/commit/2867d9b0df02ae44f0475403ce62393bca4f64b9), [`266f24d`](https://github.com/pngwn/MDsveX/commit/266f24d652a2bf03795b5397696790eb016890b4), [`8ede5e2`](https://github.com/pngwn/MDsveX/commit/8ede5e2b409511978562fcec3271e7f5182789db), [`b35c5ff`](https://github.com/pngwn/MDsveX/commit/b35c5ff2011f9b35515edd24a11027c7a410ba56), [`1dceed1`](https://github.com/pngwn/MDsveX/commit/1dceed1b863e202066c943d7b3e463db7e8fdb05), [`c56db79`](https://github.com/pngwn/MDsveX/commit/c56db798f4f43e437956bb3afb2919c09076cd6e), [`57ecb44`](https://github.com/pngwn/MDsveX/commit/57ecb446b292a3406dcf62550986ed15e1aa2b80), [`2927382`](https://github.com/pngwn/MDsveX/commit/29273823dc39bf4ab9b422210ee8acd867c29bc9), [`69b6c62`](https://github.com/pngwn/MDsveX/commit/69b6c624256f1e8fb1cf8e5f348980a043f2c1db), [`ed11417`](https://github.com/pngwn/MDsveX/commit/ed11417775290f85a0b14dda03d1c11d48bbd239)]:
|
|
23
|
+
- @mdsvex/render@1.0.0-next.1
|
|
24
|
+
- mdsvex@1.0.0-next.1
|
|
25
|
+
- @mdsvex/source-map@0.1.0-next.1
|
|
26
|
+
|
|
3
27
|
## 0.1.0-next.0
|
|
4
28
|
|
|
5
29
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdsvex/language-core",
|
|
3
|
-
"version": "0.1.0-next.
|
|
3
|
+
"version": "0.1.0-next.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/pngwn/MDsveX.git",
|
|
@@ -18,8 +18,9 @@
|
|
|
18
18
|
"@volar/language-core": "^2.4.28",
|
|
19
19
|
"@volar/typescript": "^2.4.28",
|
|
20
20
|
"svelte2tsx": "^0.7.53",
|
|
21
|
-
"@mdsvex/source-map": "0.1.0-next.
|
|
22
|
-
"@mdsvex/render": "1.0.0-next.
|
|
21
|
+
"@mdsvex/source-map": "0.1.0-next.1",
|
|
22
|
+
"@mdsvex/render": "1.0.0-next.1",
|
|
23
|
+
"mdsvex": "1.0.0-next.1"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"svelte": "^5.0.0",
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* what to complete in frontmatter and directive args, the props come from
|
|
3
|
+
* the type alias pfm_to_svelte emits for each template and directive
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type * as TS from 'typescript';
|
|
7
|
+
import { template_value_at, yaml_keys } from '@mdsvex/source-map/pfm-to-svelte';
|
|
8
|
+
import type { PropsProbe } from '@mdsvex/source-map/pfm-to-svelte';
|
|
9
|
+
|
|
10
|
+
interface Range {
|
|
11
|
+
start: number;
|
|
12
|
+
end: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type CompletionContext =
|
|
16
|
+
| { kind: 'template'; range: Range }
|
|
17
|
+
| { kind: 'frontmatter_key'; range: Range; existing: string[] }
|
|
18
|
+
| { kind: 'frontmatter_value'; key: string; range: Range }
|
|
19
|
+
| {
|
|
20
|
+
kind: 'arg_key';
|
|
21
|
+
directive: string;
|
|
22
|
+
inline: boolean;
|
|
23
|
+
range: Range;
|
|
24
|
+
existing: string[];
|
|
25
|
+
}
|
|
26
|
+
| { kind: 'arg_value'; directive: string; key: string; range: Range };
|
|
27
|
+
|
|
28
|
+
const WORD = /[\w$-]/;
|
|
29
|
+
|
|
30
|
+
function word_end(source: string, at: number): number {
|
|
31
|
+
while (at < source.length && WORD.test(source[at])) at++;
|
|
32
|
+
return at;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function line_of(source: string, offset: number) {
|
|
36
|
+
const start = source.lastIndexOf('\n', offset - 1) + 1;
|
|
37
|
+
let end = source.indexOf('\n', offset);
|
|
38
|
+
if (end < 0) end = source.length;
|
|
39
|
+
return { start, end };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** the end of a value token from start, before a comment, separator or space */
|
|
43
|
+
function value_end(source: string, start: number, stop: RegExp): number {
|
|
44
|
+
let at = start;
|
|
45
|
+
const q = source[at];
|
|
46
|
+
if (q === '"' || q === "'") {
|
|
47
|
+
const close = source.indexOf(q, at + 1);
|
|
48
|
+
return close < 0 ? at + 1 : close + 1;
|
|
49
|
+
}
|
|
50
|
+
while (at < source.length && !stop.test(source[at])) at++;
|
|
51
|
+
return at;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// a directive name and label, then the open paren of its args
|
|
55
|
+
const DIRECTIVE =
|
|
56
|
+
/(?<![\w:])(:{1,3})([A-Za-z_][\w-]*)\[(?:[^[\]\n\\]|\\.|\[[^[\]\n]*\])*\]\(/g;
|
|
57
|
+
|
|
58
|
+
/** what the offset completes, null for nothing this module offers */
|
|
59
|
+
export function completion_context(
|
|
60
|
+
source: string,
|
|
61
|
+
offset: number,
|
|
62
|
+
frontmatter: Range | null
|
|
63
|
+
): CompletionContext | null {
|
|
64
|
+
if (
|
|
65
|
+
frontmatter !== null &&
|
|
66
|
+
offset >= frontmatter.start &&
|
|
67
|
+
offset <= frontmatter.end
|
|
68
|
+
) {
|
|
69
|
+
const template = template_value_at(source, offset, frontmatter);
|
|
70
|
+
if (template !== null) return { kind: 'template', range: template };
|
|
71
|
+
const line = line_of(source, offset);
|
|
72
|
+
const before = source.slice(line.start, offset);
|
|
73
|
+
if (/^[A-Za-z_$][\w$-]*$|^$/.test(before)) {
|
|
74
|
+
const end = word_end(source, offset);
|
|
75
|
+
// a key already written on this line takes a value, not a new key
|
|
76
|
+
if (/^[ \t]*:/.test(source.slice(end, line.end))) return null;
|
|
77
|
+
const keys = yaml_keys(
|
|
78
|
+
source.slice(frontmatter.start, frontmatter.end),
|
|
79
|
+
frontmatter.start
|
|
80
|
+
);
|
|
81
|
+
const existing = [...keys.keys()].filter(
|
|
82
|
+
(k) => keys.get(k)!.start !== line.start
|
|
83
|
+
);
|
|
84
|
+
return {
|
|
85
|
+
kind: 'frontmatter_key',
|
|
86
|
+
range: { start: line.start, end },
|
|
87
|
+
existing,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const pair = /^([A-Za-z_$][\w$-]*)[ \t]*:[ \t]*(\S*)$/.exec(before);
|
|
91
|
+
if (pair === null) return null;
|
|
92
|
+
const start = offset - pair[2].length;
|
|
93
|
+
return {
|
|
94
|
+
kind: 'frontmatter_value',
|
|
95
|
+
key: pair[1],
|
|
96
|
+
range: {
|
|
97
|
+
start,
|
|
98
|
+
end: Math.max(offset, value_end(source, start, /[\s#]/)),
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const line = line_of(source, offset);
|
|
104
|
+
const text = source.slice(line.start, line.end);
|
|
105
|
+
DIRECTIVE.lastIndex = 0;
|
|
106
|
+
let m: RegExpExecArray | null;
|
|
107
|
+
while ((m = DIRECTIVE.exec(text)) !== null) {
|
|
108
|
+
const args_start = line.start + m.index + m[0].length;
|
|
109
|
+
const close = text.indexOf(')', args_start - line.start);
|
|
110
|
+
const args_end = close < 0 ? line.end : line.start + close;
|
|
111
|
+
if (offset < args_start || offset > args_end) continue;
|
|
112
|
+
const args = source.slice(args_start, args_end);
|
|
113
|
+
const before = source.slice(args_start, offset);
|
|
114
|
+
const segment_start = args_start + before.lastIndexOf(',') + 1;
|
|
115
|
+
const segment = source.slice(segment_start, offset);
|
|
116
|
+
const eq = segment.indexOf('=');
|
|
117
|
+
if (eq >= 0) {
|
|
118
|
+
let start = segment_start + eq + 1;
|
|
119
|
+
while (start < offset && /[ \t]/.test(source[start])) start++;
|
|
120
|
+
return {
|
|
121
|
+
kind: 'arg_value',
|
|
122
|
+
directive: m[2],
|
|
123
|
+
key: segment.slice(0, eq).trim(),
|
|
124
|
+
range: {
|
|
125
|
+
start,
|
|
126
|
+
end: Math.max(offset, value_end(source, start, /[\s,)]/)),
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
let start = segment_start;
|
|
131
|
+
while (start < offset && /[ \t]/.test(source[start])) start++;
|
|
132
|
+
const existing: string[] = [];
|
|
133
|
+
for (const k of args.matchAll(/([A-Za-z_][\w-]*)[ \t]*=/g))
|
|
134
|
+
if (args_start + k.index! !== start) existing.push(k[1]);
|
|
135
|
+
return {
|
|
136
|
+
kind: 'arg_key',
|
|
137
|
+
directive: m[2],
|
|
138
|
+
inline: m[1].length === 1,
|
|
139
|
+
range: { start, end: word_end(source, offset) },
|
|
140
|
+
existing,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** the template a frontmatter names, undefined when it names none */
|
|
147
|
+
export function template_named(
|
|
148
|
+
source: string,
|
|
149
|
+
frontmatter: Range | null
|
|
150
|
+
): string | false | undefined {
|
|
151
|
+
if (frontmatter === null) return undefined;
|
|
152
|
+
const m =
|
|
153
|
+
/^(["']?)template\1[ \t]*:[ \t]*(["']?)([^"'#\n]*?)\2[ \t]*(?:#.*)?$/m.exec(
|
|
154
|
+
source.slice(frontmatter.start, frontmatter.end)
|
|
155
|
+
);
|
|
156
|
+
if (m === null || m[3] === '') return undefined;
|
|
157
|
+
return m[3] === 'false' ? false : m[3];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** the probe of a template, or of a directive in the scope of a template */
|
|
161
|
+
export function probe_for(
|
|
162
|
+
probes: PropsProbe[],
|
|
163
|
+
kind: PropsProbe['kind'],
|
|
164
|
+
name: string,
|
|
165
|
+
template: string | undefined
|
|
166
|
+
): PropsProbe | undefined {
|
|
167
|
+
if (kind === 'template')
|
|
168
|
+
return probes.find((p) => p.kind === 'template' && p.name === name);
|
|
169
|
+
return (
|
|
170
|
+
probes.find(
|
|
171
|
+
(p) =>
|
|
172
|
+
p.kind === 'directive' && p.name === name && p.template === template
|
|
173
|
+
) ??
|
|
174
|
+
probes.find(
|
|
175
|
+
(p) =>
|
|
176
|
+
p.kind === 'directive' && p.name === name && p.template === undefined
|
|
177
|
+
)
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface Prop {
|
|
182
|
+
name: string;
|
|
183
|
+
/** the type as typescript prints it */
|
|
184
|
+
type: string;
|
|
185
|
+
optional: boolean;
|
|
186
|
+
documentation: string;
|
|
187
|
+
/** the literal values the type allows, as yaml or an arg writes them */
|
|
188
|
+
values: string[];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** the props a probe names, null when the program does not hold it */
|
|
192
|
+
export function props_of(
|
|
193
|
+
ts: typeof TS,
|
|
194
|
+
program: TS.Program,
|
|
195
|
+
file: string,
|
|
196
|
+
alias: string
|
|
197
|
+
): Prop[] | null {
|
|
198
|
+
const sf = program.getSourceFile(file);
|
|
199
|
+
if (sf === undefined) return null;
|
|
200
|
+
let found: TS.TypeAliasDeclaration | undefined;
|
|
201
|
+
const visit = (node: TS.Node): void => {
|
|
202
|
+
if (found) return;
|
|
203
|
+
if (ts.isTypeAliasDeclaration(node) && node.name.text === alias)
|
|
204
|
+
found = node;
|
|
205
|
+
else ts.forEachChild(node, visit);
|
|
206
|
+
};
|
|
207
|
+
visit(sf);
|
|
208
|
+
if (found === undefined) return null;
|
|
209
|
+
const checker = program.getTypeChecker();
|
|
210
|
+
const type = checker.getTypeFromTypeNode(found.type);
|
|
211
|
+
if (type.flags & ts.TypeFlags.Any) return null;
|
|
212
|
+
const props: Prop[] = [];
|
|
213
|
+
for (const symbol of checker.getPropertiesOfType(type)) {
|
|
214
|
+
const prop_type = checker.getTypeOfSymbolAtLocation(symbol, found);
|
|
215
|
+
props.push({
|
|
216
|
+
name: symbol.name,
|
|
217
|
+
type: checker.typeToString(
|
|
218
|
+
checker.getNonNullableType(prop_type),
|
|
219
|
+
found,
|
|
220
|
+
ts.TypeFormatFlags.NoTruncation
|
|
221
|
+
),
|
|
222
|
+
optional: (symbol.flags & ts.SymbolFlags.Optional) !== 0,
|
|
223
|
+
documentation: ts.displayPartsToString(
|
|
224
|
+
symbol.getDocumentationComment(checker)
|
|
225
|
+
),
|
|
226
|
+
values: literal_values(ts, prop_type),
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
return props;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function literal_values(ts: typeof TS, type: TS.Type): string[] {
|
|
233
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
234
|
+
const values: string[] = [];
|
|
235
|
+
for (const t of parts) {
|
|
236
|
+
if (t.isStringLiteral()) values.push(t.value);
|
|
237
|
+
else if (t.isNumberLiteral()) values.push(String(t.value));
|
|
238
|
+
else if (t.flags & ts.TypeFlags.BooleanLiteral)
|
|
239
|
+
values.push((t as { intrinsicName?: string }).intrinsicName ?? '');
|
|
240
|
+
}
|
|
241
|
+
return values.filter((v) => v !== '');
|
|
242
|
+
}
|