@mdsvex/source-map 0.0.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 ADDED
@@ -0,0 +1,37 @@
1
+ # @mdsvex/source-map
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
+
27
+ ## 0.1.0-next.0
28
+
29
+ ### Minor Changes
30
+
31
+ - [#799](https://github.com/pngwn/MDsveX/pull/799) [`5b54692`](https://github.com/pngwn/MDsveX/commit/5b54692d6c895f02c947ce620a46c7a9beb856e9) Thanks [@pngwn](https://github.com/pngwn)! - Add language tools
32
+
33
+ ### Patch Changes
34
+
35
+ - Updated dependencies [[`6ac3826`](https://github.com/pngwn/MDsveX/commit/6ac382615eb410230e8423d5ca4202005588869e), [`46f655f`](https://github.com/pngwn/MDsveX/commit/46f655f90a838726eda34e1b07667e86fa61e7cb), [`e5b7abe`](https://github.com/pngwn/MDsveX/commit/e5b7abe1ebf868b1df1e934fb96e1e363696f3e9), [`5b54692`](https://github.com/pngwn/MDsveX/commit/5b54692d6c895f02c947ce620a46c7a9beb856e9)]:
36
+ - @mdsvex/render@1.0.0-next.0
37
+ - @mdsvex/parse@1.0.0-next.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 pngwn
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/package.json CHANGED
@@ -1,15 +1,46 @@
1
1
  {
2
2
  "name": "@mdsvex/source-map",
3
- "version": "0.0.0",
4
- "description": "Placeholder release so npm trusted publishing can be configured.",
5
- "license": "MIT",
6
- "homepage": "https://mdsvex.pngwn.io",
3
+ "version": "0.1.0-next.1",
7
4
  "repository": {
8
5
  "type": "git",
9
- "url": "https://github.com/pngwn/MDsveX",
6
+ "url": "git+https://github.com/pngwn/MDsveX.git",
10
7
  "directory": "language-tools/source-map"
11
8
  },
12
- "publishConfig": {
13
- "access": "public"
9
+ "description": "PFM to Svelte source map pipeline for language tooling",
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./src/index.ts",
14
+ "types": "./src/index.ts"
15
+ },
16
+ "./pfm-to-svelte": {
17
+ "import": "./src/pfm_to_svelte.ts",
18
+ "types": "./src/pfm_to_svelte.ts"
19
+ },
20
+ "./v3-to-volar": {
21
+ "import": "./src/v3_to_volar.ts",
22
+ "types": "./src/v3_to_volar.ts"
23
+ },
24
+ "./compose-mappings": {
25
+ "import": "./src/compose_mappings.ts",
26
+ "types": "./src/compose_mappings.ts"
27
+ }
28
+ },
29
+ "dependencies": {
30
+ "@jridgewell/trace-mapping": "^0.3.31",
31
+ "@mdsvex/parse": "1.0.0-next.1",
32
+ "@mdsvex/render": "1.0.0-next.1",
33
+ "mdsvex": "1.0.0-next.1"
34
+ },
35
+ "devDependencies": {
36
+ "svelte": "^5.0.0",
37
+ "svelte2tsx": "^0.7.53",
38
+ "typescript": "^5.0.0"
39
+ },
40
+ "author": "pngwn <hello@pngwn.io>",
41
+ "license": "MIT",
42
+ "scripts": {
43
+ "test": "vitest --disableConsoleIntercept",
44
+ "test:run": "vitest run --silent"
14
45
  }
15
46
  }
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Compose two sets of Volar mappings: A to B + B to C = A to C.
3
+ *
4
+ * For each mapping in A (PFM to Svelte), find overlapping mappings in B
5
+ * (Svelte to TS), and produce composed mappings (PFM to TS).
6
+ */
7
+
8
+ import type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
9
+
10
+ export type { Mapping, CodeInformation } from "@mdsvex/render/mappings";
11
+
12
+ interface Segment {
13
+ srcStart: number;
14
+ srcEnd: number;
15
+ genStart: number;
16
+ genEnd: number;
17
+ }
18
+
19
+ /** extract flat segments from a mapping (handles multi-entry sourceOffsets). */
20
+ function toSegments<T>(m: Mapping<T>): Segment[] {
21
+ const segs: Segment[] = [];
22
+ for (let i = 0; i < m.sourceOffsets.length; i++) {
23
+ const srcLen = m.lengths[i];
24
+ const genLen = m.generatedLengths ? m.generatedLengths[i] : m.lengths[i];
25
+ segs.push({
26
+ srcStart: m.sourceOffsets[i],
27
+ srcEnd: m.sourceOffsets[i] + srcLen,
28
+ genStart: m.generatedOffsets[i],
29
+ genEnd: m.generatedOffsets[i] + genLen,
30
+ });
31
+ }
32
+ return segs;
33
+ }
34
+
35
+ /** merge CodeInformation via intersection: capability present only if both legs have it. */
36
+ function intersectCapabilities(
37
+ a: CodeInformation,
38
+ b: CodeInformation,
39
+ ): CodeInformation {
40
+ const result: CodeInformation = {};
41
+ if (a.verification && b.verification) result.verification = true;
42
+ if (a.completion && b.completion) result.completion = true;
43
+ if (a.semantic && b.semantic) result.semantic = true;
44
+ if (a.navigation && b.navigation) result.navigation = true;
45
+ if (a.structure && b.structure) result.structure = true;
46
+ if (a.format && b.format) result.format = true;
47
+ return result;
48
+ }
49
+
50
+ /**
51
+ * Compose mappings A to B and B to C into A to C.
52
+ *
53
+ * For each A segment that maps A-source to B (generated), finds overlapping
54
+ * B segments that map B-source to C (generated). The overlap region in B-space
55
+ * determines the composed A to C mapping.
56
+ *
57
+ * @param a - Mappings from A to B (e.g., PFM to Svelte)
58
+ * @param b - Mappings from B to C (e.g., Svelte to TS)
59
+ * @param mergeData - Optional custom data merger. Defaults to intersectCapabilities.
60
+ */
61
+ export function composeMappings<
62
+ DA extends CodeInformation,
63
+ DB extends CodeInformation,
64
+ >(
65
+ a: Mapping<DA>[],
66
+ b: Mapping<DB>[],
67
+ mergeData?: (da: DA, db: DB) => DA,
68
+ ): Mapping<DA>[] {
69
+ // flatten B into sorted segments keyed by source offset
70
+ const bSegs: (Segment & { data: DB })[] = [];
71
+ for (const bm of b) {
72
+ const segs = toSegments(bm);
73
+ for (const s of segs) {
74
+ bSegs.push({ ...s, data: bm.data });
75
+ }
76
+ }
77
+ bSegs.sort((x, y) => x.srcStart - y.srcStart);
78
+
79
+ const merge =
80
+ mergeData ??
81
+ ((da: DA, db: DB) => intersectCapabilities(da, db) as DA);
82
+
83
+ const result: Mapping<DA>[] = [];
84
+
85
+ for (const am of a) {
86
+ const aSegs = toSegments(am);
87
+
88
+ for (const aSeg of aSegs) {
89
+ // A maps A-source [aSeg.srcStart, aSeg.srcEnd) to B [aSeg.genStart, aSeg.genEnd)
90
+ // Find B segments whose source range overlaps with A's generated range
91
+ const aGenStart = aSeg.genStart;
92
+ const aGenEnd = aSeg.genEnd;
93
+ const aIsIdentity = aSeg.srcEnd - aSeg.srcStart === aGenEnd - aGenStart;
94
+
95
+ // binary search for first B segment that might overlap
96
+ let lo = 0,
97
+ hi = bSegs.length;
98
+ while (lo < hi) {
99
+ const mid = (lo + hi) >>> 1;
100
+ if (bSegs[mid].srcEnd <= aGenStart) lo = mid + 1;
101
+ else hi = mid;
102
+ }
103
+
104
+ // scan through potentially overlapping B segments
105
+ for (let j = lo; j < bSegs.length; j++) {
106
+ const bSeg = bSegs[j];
107
+ if (bSeg.srcStart >= aGenEnd) break; // past overlap
108
+
109
+ // compute overlap in B-source space (= A's generated space)
110
+ const overlapStart = Math.max(aGenStart, bSeg.srcStart);
111
+ const overlapEnd = Math.min(aGenEnd, bSeg.srcEnd);
112
+ if (overlapEnd <= overlapStart) continue;
113
+
114
+ const overlapLen = overlapEnd - overlapStart;
115
+
116
+ // map overlap back to A-source
117
+ let composedSrcStart: number;
118
+ let composedSrcLen: number;
119
+ if (aIsIdentity) {
120
+ composedSrcStart = aSeg.srcStart + (overlapStart - aGenStart);
121
+ composedSrcLen = overlapLen;
122
+ } else {
123
+ // non-identity A, can only use the start anchor
124
+ composedSrcStart = aSeg.srcStart;
125
+ composedSrcLen = aSeg.srcEnd - aSeg.srcStart;
126
+ }
127
+
128
+ // map overlap forward to C (B's generated space)
129
+ const bIsIdentity =
130
+ bSeg.srcEnd - bSeg.srcStart === bSeg.genEnd - bSeg.genStart;
131
+ let composedGenStart: number;
132
+ let composedGenLen: number;
133
+ if (bIsIdentity) {
134
+ composedGenStart = bSeg.genStart + (overlapStart - bSeg.srcStart);
135
+ composedGenLen = overlapLen;
136
+ } else {
137
+ composedGenStart = bSeg.genStart;
138
+ composedGenLen = bSeg.genEnd - bSeg.genStart;
139
+ }
140
+
141
+ const m: Mapping<DA> = {
142
+ sourceOffsets: [composedSrcStart],
143
+ generatedOffsets: [composedGenStart],
144
+ lengths: [composedSrcLen],
145
+ data: merge(am.data, bSeg.data),
146
+ };
147
+ if (composedGenLen !== composedSrcLen) {
148
+ m.generatedLengths = [composedGenLen];
149
+ }
150
+ result.push(m);
151
+ }
152
+ }
153
+ }
154
+
155
+ return result;
156
+ }
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/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { pfmToSvelte } from "./pfm_to_svelte";
2
+ export type { PfmToSvelteResult, StyleBlock } from "./pfm_to_svelte";
3
+ export { v3ToVolarMappings } from "./v3_to_volar";
4
+ export { composeMappings } from "./compose_mappings";
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
+ }