@mdsvex/source-map 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/edit.ts +95 -0
- package/src/keys.ts +101 -0
- package/src/pfm_to_svelte.ts +799 -220
- package/test/pfm_to_svelte.spec.ts +34 -25
- package/test/pfm_to_svelte_config.spec.ts +339 -0
- package/test/pfm_to_svelte_extended.spec.ts +21 -14
- package/test/pfm_to_svelte_live.spec.ts +84 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @mdsvex/source-map
|
|
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 [[`cfee8d1`](https://github.com/pngwn/MDsveX/commit/cfee8d1a968ac78329e314650c51785968bf1dff), [`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), [`29b6dd1`](https://github.com/pngwn/MDsveX/commit/29b6dd127efc0d00dff650d320d4d003f6d2b1b6), [`69b6c62`](https://github.com/pngwn/MDsveX/commit/69b6c624256f1e8fb1cf8e5f348980a043f2c1db), [`7815ee9`](https://github.com/pngwn/MDsveX/commit/7815ee9d0993774f6f648d46d35ce3f900a6eed4), [`db0418d`](https://github.com/pngwn/MDsveX/commit/db0418d50bcd286a062cd0b6243a234167d11781), [`fa297f9`](https://github.com/pngwn/MDsveX/commit/fa297f9b3fda7cc11c3f4274ba0b0cc27acedf9a), [`ebdb9fb`](https://github.com/pngwn/MDsveX/commit/ebdb9fb687933c69da0260eb50e8a4d2fae2bbca), [`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), [`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), [`e804671`](https://github.com/pngwn/MDsveX/commit/e804671afb412c81ce5cb38c40588257d34ab99d), [`e804671`](https://github.com/pngwn/MDsveX/commit/e804671afb412c81ce5cb38c40588257d34ab99d), [`045aca9`](https://github.com/pngwn/MDsveX/commit/045aca94077183b8b58bb59c45813f0aa4f33cde), [`2f981d8`](https://github.com/pngwn/MDsveX/commit/2f981d84b04a718dae82f79e6a9243868444f88c), [`cfee8d1`](https://github.com/pngwn/MDsveX/commit/cfee8d1a968ac78329e314650c51785968bf1dff), [`59bbad3`](https://github.com/pngwn/MDsveX/commit/59bbad3d1905ea378b8745d916af0dca3e63b042), [`6f4a27d`](https://github.com/pngwn/MDsveX/commit/6f4a27da11241056ef4912618cefee521525a82a), [`585a698`](https://github.com/pngwn/MDsveX/commit/585a698f25fd0b6160e25bf0022e3cdabde65172), [`65c9784`](https://github.com/pngwn/MDsveX/commit/65c9784b51209c7706eebaada3e9908bda9d5fa4), [`31aa6e2`](https://github.com/pngwn/MDsveX/commit/31aa6e20390c6fa656845add1d19e0439dda48c7), [`585a698`](https://github.com/pngwn/MDsveX/commit/585a698f25fd0b6160e25bf0022e3cdabde65172), [`585a698`](https://github.com/pngwn/MDsveX/commit/585a698f25fd0b6160e25bf0022e3cdabde65172), [`98a1b87`](https://github.com/pngwn/MDsveX/commit/98a1b87b1c45f192c93bf3b910d6ebc8284b94f4), [`fd54579`](https://github.com/pngwn/MDsveX/commit/fd5457966ce23d1eb29a7935a550394adf4498e4), [`585a698`](https://github.com/pngwn/MDsveX/commit/585a698f25fd0b6160e25bf0022e3cdabde65172), [`585a698`](https://github.com/pngwn/MDsveX/commit/585a698f25fd0b6160e25bf0022e3cdabde65172), [`29b6dd1`](https://github.com/pngwn/MDsveX/commit/29b6dd127efc0d00dff650d320d4d003f6d2b1b6), [`31aa6e2`](https://github.com/pngwn/MDsveX/commit/31aa6e20390c6fa656845add1d19e0439dda48c7), [`9903c3a`](https://github.com/pngwn/MDsveX/commit/9903c3ad8d764085285486816736c4f3ea140a99), [`7e69d52`](https://github.com/pngwn/MDsveX/commit/7e69d52284e20892752041fb42726480d88cf31c), [`fd54579`](https://github.com/pngwn/MDsveX/commit/fd5457966ce23d1eb29a7935a550394adf4498e4), [`31aa6e2`](https://github.com/pngwn/MDsveX/commit/31aa6e20390c6fa656845add1d19e0439dda48c7), [`f2ff5fe`](https://github.com/pngwn/MDsveX/commit/f2ff5fe144a9009a79e0aa7a6f08f1bed5a7696c), [`57ecb44`](https://github.com/pngwn/MDsveX/commit/57ecb446b292a3406dcf62550986ed15e1aa2b80), [`fd54579`](https://github.com/pngwn/MDsveX/commit/fd5457966ce23d1eb29a7935a550394adf4498e4), [`e804671`](https://github.com/pngwn/MDsveX/commit/e804671afb412c81ce5cb38c40588257d34ab99d), [`98a1b87`](https://github.com/pngwn/MDsveX/commit/98a1b87b1c45f192c93bf3b910d6ebc8284b94f4), [`f2e4911`](https://github.com/pngwn/MDsveX/commit/f2e4911db89f7c66f684c0da839deeb3bc46fa55), [`4c55f5e`](https://github.com/pngwn/MDsveX/commit/4c55f5eea023e687202fa97626b401f09bffd6f7), [`e804671`](https://github.com/pngwn/MDsveX/commit/e804671afb412c81ce5cb38c40588257d34ab99d), [`e708e1d`](https://github.com/pngwn/MDsveX/commit/e708e1d4c66478efd8606fa2e1ff2fee40e0732a), [`2f981d8`](https://github.com/pngwn/MDsveX/commit/2f981d84b04a718dae82f79e6a9243868444f88c), [`e804671`](https://github.com/pngwn/MDsveX/commit/e804671afb412c81ce5cb38c40588257d34ab99d), [`ed11417`](https://github.com/pngwn/MDsveX/commit/ed11417775290f85a0b14dda03d1c11d48bbd239), [`4927fd8`](https://github.com/pngwn/MDsveX/commit/4927fd87ef00ae106f081cf4315e425ec71a790c), [`98a1b87`](https://github.com/pngwn/MDsveX/commit/98a1b87b1c45f192c93bf3b910d6ebc8284b94f4), [`60da683`](https://github.com/pngwn/MDsveX/commit/60da68313df11747bdd6235dffc325c30af60cb2), [`fd54579`](https://github.com/pngwn/MDsveX/commit/fd5457966ce23d1eb29a7935a550394adf4498e4), [`2867d9b`](https://github.com/pngwn/MDsveX/commit/2867d9b0df02ae44f0475403ce62393bca4f64b9), [`266f24d`](https://github.com/pngwn/MDsveX/commit/266f24d652a2bf03795b5397696790eb016890b4), [`60da683`](https://github.com/pngwn/MDsveX/commit/60da68313df11747bdd6235dffc325c30af60cb2), [`8ede5e2`](https://github.com/pngwn/MDsveX/commit/8ede5e2b409511978562fcec3271e7f5182789db), [`430cf34`](https://github.com/pngwn/MDsveX/commit/430cf3493575c706b31b5e02b4c219f4b6ea0569), [`eefaff5`](https://github.com/pngwn/MDsveX/commit/eefaff563e6ce05026f92c03fce21b3ba4bd5e77), [`3f5bbbb`](https://github.com/pngwn/MDsveX/commit/3f5bbbb069ca12e16b3bb96afb60c41362161109), [`60da683`](https://github.com/pngwn/MDsveX/commit/60da68313df11747bdd6235dffc325c30af60cb2), [`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), [`65c9784`](https://github.com/pngwn/MDsveX/commit/65c9784b51209c7706eebaada3e9908bda9d5fa4), [`585a698`](https://github.com/pngwn/MDsveX/commit/585a698f25fd0b6160e25bf0022e3cdabde65172), [`585a698`](https://github.com/pngwn/MDsveX/commit/585a698f25fd0b6160e25bf0022e3cdabde65172), [`9903c3a`](https://github.com/pngwn/MDsveX/commit/9903c3ad8d764085285486816736c4f3ea140a99), [`2f981d8`](https://github.com/pngwn/MDsveX/commit/2f981d84b04a718dae82f79e6a9243868444f88c), [`cbc0029`](https://github.com/pngwn/MDsveX/commit/cbc00291119021655765fd4f4e05d9f4fb320727), [`ed11417`](https://github.com/pngwn/MDsveX/commit/ed11417775290f85a0b14dda03d1c11d48bbd239)]:
|
|
23
|
+
- @mdsvex/parse@1.0.0-next.1
|
|
24
|
+
- @mdsvex/render@1.0.0-next.1
|
|
25
|
+
- mdsvex@1.0.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/source-map",
|
|
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",
|
|
@@ -28,8 +28,9 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@jridgewell/trace-mapping": "^0.3.31",
|
|
31
|
-
"@mdsvex/parse": "1.0.0-next.
|
|
32
|
-
"@mdsvex/render": "1.0.0-next.
|
|
31
|
+
"@mdsvex/parse": "1.0.0-next.1",
|
|
32
|
+
"@mdsvex/render": "1.0.0-next.1",
|
|
33
|
+
"mdsvex": "1.0.0-next.1"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
36
|
"svelte": "^5.0.0",
|
package/src/edit.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* text edits over generated code that carry its mappings along, edits only
|
|
3
|
+
* replace or insert generated text, so a mapping either moves past an edit or
|
|
4
|
+
* contains it whole
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Mapping } from '@mdsvex/render/mappings';
|
|
8
|
+
|
|
9
|
+
export interface Edit<D> {
|
|
10
|
+
/** offsets in the code before any edit, start === end inserts */
|
|
11
|
+
start: number;
|
|
12
|
+
end: number;
|
|
13
|
+
text: string;
|
|
14
|
+
/** mappings into text, their generated offsets relative to its start */
|
|
15
|
+
mappings?: Mapping<D>[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** a plain copy, core mappings read their arrays through accessors */
|
|
19
|
+
export function plain<D>(m: Mapping<D>): Mapping<D> {
|
|
20
|
+
const out: Mapping<D> = {
|
|
21
|
+
sourceOffsets: m.sourceOffsets.slice(),
|
|
22
|
+
generatedOffsets: m.generatedOffsets.slice(),
|
|
23
|
+
lengths: m.lengths.slice(),
|
|
24
|
+
data: m.data,
|
|
25
|
+
};
|
|
26
|
+
if (m.generatedLengths) out.generatedLengths = m.generatedLengths.slice();
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function apply_edits<D>(
|
|
31
|
+
code: string,
|
|
32
|
+
mappings: Mapping<D>[],
|
|
33
|
+
edits: Edit<D>[]
|
|
34
|
+
): { code: string; mappings: Mapping<D>[] } {
|
|
35
|
+
if (edits.length === 0) return { code, mappings };
|
|
36
|
+
const sorted = edits
|
|
37
|
+
.slice()
|
|
38
|
+
.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
39
|
+
|
|
40
|
+
let out = '';
|
|
41
|
+
let at = 0;
|
|
42
|
+
const placed: number[] = [];
|
|
43
|
+
for (const e of sorted) {
|
|
44
|
+
out += code.slice(at, e.start);
|
|
45
|
+
placed.push(out.length);
|
|
46
|
+
out += e.text;
|
|
47
|
+
at = e.end;
|
|
48
|
+
}
|
|
49
|
+
out += code.slice(at);
|
|
50
|
+
|
|
51
|
+
// a start moves past every edit ending at or before it, an end past every edit starting before it
|
|
52
|
+
const start_of = (p: number) => {
|
|
53
|
+
let d = 0;
|
|
54
|
+
for (const e of sorted) {
|
|
55
|
+
if (e.end <= p) d += e.text.length - (e.end - e.start);
|
|
56
|
+
else break;
|
|
57
|
+
}
|
|
58
|
+
return p + d;
|
|
59
|
+
};
|
|
60
|
+
const end_of = (p: number) => {
|
|
61
|
+
let d = 0;
|
|
62
|
+
for (const e of sorted) {
|
|
63
|
+
if (e.start < p) d += e.text.length - (e.end - e.start);
|
|
64
|
+
else break;
|
|
65
|
+
}
|
|
66
|
+
return p + d;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const result: Mapping<D>[] = [];
|
|
70
|
+
for (const m of mappings) {
|
|
71
|
+
const moved = plain(m);
|
|
72
|
+
for (let i = 0; i < moved.generatedOffsets.length; i++) {
|
|
73
|
+
const g = moved.generatedOffsets[i];
|
|
74
|
+
const len = moved.generatedLengths
|
|
75
|
+
? moved.generatedLengths[i]
|
|
76
|
+
: moved.lengths[i];
|
|
77
|
+
const s = start_of(g);
|
|
78
|
+
const e = len === 0 ? s : end_of(g + len);
|
|
79
|
+
moved.generatedOffsets[i] = s;
|
|
80
|
+
if (e - s !== len) moved.generatedLengths ??= moved.lengths.slice();
|
|
81
|
+
if (moved.generatedLengths) moved.generatedLengths[i] = e - s;
|
|
82
|
+
}
|
|
83
|
+
result.push(moved);
|
|
84
|
+
}
|
|
85
|
+
for (let k = 0; k < sorted.length; k++) {
|
|
86
|
+
const e = sorted[k];
|
|
87
|
+
if (!e.mappings) continue;
|
|
88
|
+
for (const m of e.mappings) {
|
|
89
|
+
const moved = plain(m);
|
|
90
|
+
moved.generatedOffsets = moved.generatedOffsets.map((g) => g + placed[k]);
|
|
91
|
+
result.push(moved);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { code: out, mappings: result };
|
|
95
|
+
}
|
package/src/keys.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* where the top level keys of frontmatter sit in the yaml source, so each
|
|
3
|
+
* maps to the key of the metadata export
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface KeyRange {
|
|
7
|
+
/** the key text without quotes */
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// a plain key runs to the first colon followed by a space or the line end
|
|
13
|
+
const PLAIN_KEY = /^([^\s#'"\-?:[\]{},&*!|>%@`][^\n]*?)[ \t]*:(?=[ \t]|$)/;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* the first range of each top level key of yaml, offset by base, a key the
|
|
17
|
+
* parser reads some other way, such as a complex key, is left out
|
|
18
|
+
*/
|
|
19
|
+
export function yaml_keys(yaml: string, base: number): Map<string, KeyRange> {
|
|
20
|
+
const keys = new Map<string, KeyRange>();
|
|
21
|
+
let line_start = 0;
|
|
22
|
+
while (line_start <= yaml.length) {
|
|
23
|
+
let line_end = yaml.indexOf('\n', line_start);
|
|
24
|
+
if (line_end < 0) line_end = yaml.length;
|
|
25
|
+
const line = yaml.slice(line_start, line_end).replace(/\r$/, '');
|
|
26
|
+
const found = top_key(line);
|
|
27
|
+
if (found !== null && !keys.has(found.name)) {
|
|
28
|
+
keys.set(found.name, {
|
|
29
|
+
start: base + line_start + found.start,
|
|
30
|
+
end: base + line_start + found.end,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
line_start = line_end + 1;
|
|
34
|
+
}
|
|
35
|
+
return keys;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function top_key(
|
|
39
|
+
line: string
|
|
40
|
+
): { name: string; start: number; end: number } | null {
|
|
41
|
+
const q = line[0];
|
|
42
|
+
if (q === '"' || q === "'") {
|
|
43
|
+
let i = 1;
|
|
44
|
+
let name = '';
|
|
45
|
+
for (; i < line.length; i++) {
|
|
46
|
+
const c = line[i];
|
|
47
|
+
if (q === '"' && c === '\\') {
|
|
48
|
+
i++;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (c === q) {
|
|
52
|
+
if (q === "'" && line[i + 1] === "'") {
|
|
53
|
+
i++;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (i >= line.length) return null;
|
|
60
|
+
const inner = line.slice(1, i);
|
|
61
|
+
if (!/^[ \t]*:(?:[ \t]|$)/.test(line.slice(i + 1))) return null;
|
|
62
|
+
if (q === "'") name = inner.replace(/''/g, "'");
|
|
63
|
+
else {
|
|
64
|
+
try {
|
|
65
|
+
name = JSON.parse('"' + inner + '"');
|
|
66
|
+
} catch {
|
|
67
|
+
name = inner;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { name, start: 1, end: i };
|
|
71
|
+
}
|
|
72
|
+
const m = PLAIN_KEY.exec(line);
|
|
73
|
+
if (m === null) return null;
|
|
74
|
+
return { name: m[1], start: 0, end: m[1].length };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const TEMPLATE_KEY = /^(["']?)template\1[ \t]*:[ \t]*/;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* the value of a top level template key on the line holding offset, the
|
|
81
|
+
* range a completion replaces, null when offset is not in one
|
|
82
|
+
*/
|
|
83
|
+
export function template_value_at(
|
|
84
|
+
source: string,
|
|
85
|
+
offset: number,
|
|
86
|
+
frontmatter: KeyRange
|
|
87
|
+
): KeyRange | null {
|
|
88
|
+
if (offset < frontmatter.start || offset > frontmatter.end) return null;
|
|
89
|
+
const line_start = source.lastIndexOf('\n', offset - 1) + 1;
|
|
90
|
+
let line_end = source.indexOf('\n', offset);
|
|
91
|
+
if (line_end < 0 || line_end > frontmatter.end) line_end = frontmatter.end;
|
|
92
|
+
const line = source.slice(line_start, line_end).replace(/\r$/, '');
|
|
93
|
+
const key = TEMPLATE_KEY.exec(line);
|
|
94
|
+
if (key === null) return null;
|
|
95
|
+
const start = line_start + key[0].length;
|
|
96
|
+
if (offset < start) return null;
|
|
97
|
+
// a comment ends the value, trailing space is not part of it
|
|
98
|
+
const rest = line.slice(key[0].length).replace(/[ \t]+#.*$/, '');
|
|
99
|
+
const end = Math.max(start + rest.trimEnd().length, offset);
|
|
100
|
+
return { start, end };
|
|
101
|
+
}
|