@motion-script/code 0.1.0
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/README.md +85 -0
- package/dist/code-fragment.d.ts +42 -0
- package/dist/code-fragment.d.ts.map +1 -0
- package/dist/code-fragment.js +72 -0
- package/dist/code-fragment.js.map +1 -0
- package/dist/code-metrics.d.ts +11 -0
- package/dist/code-metrics.d.ts.map +1 -0
- package/dist/code-metrics.js +29 -0
- package/dist/code-metrics.js.map +1 -0
- package/dist/code-range.d.ts +44 -0
- package/dist/code-range.d.ts.map +1 -0
- package/dist/code-range.js +70 -0
- package/dist/code-range.js.map +1 -0
- package/dist/diff.d.ts +31 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +236 -0
- package/dist/diff.js.map +1 -0
- package/dist/highlighter.d.ts +69 -0
- package/dist/highlighter.d.ts.map +1 -0
- package/dist/highlighter.js +2 -0
- package/dist/highlighter.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/node.d.ts +96 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +882 -0
- package/dist/node.js.map +1 -0
- package/dist/props.d.ts +14 -0
- package/dist/props.d.ts.map +1 -0
- package/dist/props.js +2 -0
- package/dist/props.js.map +1 -0
- package/dist/tokenizer.d.ts +8 -0
- package/dist/tokenizer.d.ts.map +1 -0
- package/dist/tokenizer.js +50 -0
- package/dist/tokenizer.js.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @motion-script/code
|
|
2
|
+
|
|
3
|
+
Animated, syntax-highlighted code blocks for [Motion Script](https://motionscript.dev).
|
|
4
|
+
The `Code` node tokenizes source with [Shiki](https://shiki.style) and animates
|
|
5
|
+
edits at the token level, so appending, inserting, removing, replacing, and
|
|
6
|
+
highlighting code all morph smoothly instead of snapping.
|
|
7
|
+
|
|
8
|
+
```tsx
|
|
9
|
+
import { Scene, createRef } from '@motion-script/core';
|
|
10
|
+
import { Code, lines, word } from '@motion-script/code';
|
|
11
|
+
|
|
12
|
+
export class CodeScene extends Scene {
|
|
13
|
+
*build() {
|
|
14
|
+
const code = createRef<Code>();
|
|
15
|
+
|
|
16
|
+
this.add(
|
|
17
|
+
<Code
|
|
18
|
+
ref={code}
|
|
19
|
+
language="typescript"
|
|
20
|
+
theme="github-dark"
|
|
21
|
+
fontSize={28}
|
|
22
|
+
code={`function greet(name) {\n return "hi";\n}`}
|
|
23
|
+
/>,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
yield* code().append('\ngreet("world");', 0.6);
|
|
27
|
+
yield* code().highlight(lines(2), 0.4);
|
|
28
|
+
yield* code().replace(word(2, 10, 4), '`hi, ${name}`', 0.6);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## What's in here
|
|
34
|
+
|
|
35
|
+
- **`Code`** node, a hugging block of syntax-highlighted source. Set `code`,
|
|
36
|
+
`language`, and `theme` to switch what is shown; tune `fontSize`,
|
|
37
|
+
`fontFamily`, `lineHeight`, `letterSpacing`, `showLineNumbers`,
|
|
38
|
+
`lineNumberGap`, and `padding` for layout.
|
|
39
|
+
- **Token-level transitions**, generator methods you `yield*` from a scene:
|
|
40
|
+
- `append(code, duration)` / `prepend(code, duration)`, fade in lines at the
|
|
41
|
+
end or start.
|
|
42
|
+
- `insert([line, col], code, duration)`, splice code into a line or grow new
|
|
43
|
+
lines mid-document.
|
|
44
|
+
- `remove(range, duration)`, collapse tokens (and whole lines) so surrounding
|
|
45
|
+
text reflows.
|
|
46
|
+
- `replace(range, next, duration)`, cross-fade old tokens out and new ones in.
|
|
47
|
+
- `highlight(range, duration)` / `resetHighlight(duration)`, dim everything
|
|
48
|
+
outside the range, then restore it.
|
|
49
|
+
- **Range helpers**, `word(line, col, length)`, `lines(from, to)`, and
|
|
50
|
+
`range(startLine, startCol, endLine, endCol)` build the `CodeRange` values the
|
|
51
|
+
transitions take. `findFirstRange`, `findRangeAt`, and `findAllRanges` locate
|
|
52
|
+
ranges by matching literal text.
|
|
53
|
+
- **`initSyntaxHighlighter(themes, langs)`**, optional preloading of Shiki
|
|
54
|
+
themes and languages so the first frame renders fully highlighted.
|
|
55
|
+
|
|
56
|
+
Every token keeps a stable identity across edits, so concurrent animations at
|
|
57
|
+
different positions do not clobber each other while one is mid-flight.
|
|
58
|
+
|
|
59
|
+
This package builds on [`@motion-script/core`](../../core) (the scene graph and
|
|
60
|
+
animation runtime) and renders through whatever backend draws the scene, such as
|
|
61
|
+
[`@motion-script/web`](../../web).
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install @motion-script/code
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
For a guided setup, scaffold a project instead with:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npm create motion-script@latest
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
See the [docs](https://motionscript.dev/docs) for the full feature set and API
|
|
76
|
+
reference.
|
|
77
|
+
|
|
78
|
+
## Development
|
|
79
|
+
|
|
80
|
+
From the monorepo root:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pnpm --filter @motion-script/code build
|
|
84
|
+
pnpm --filter @motion-script/code test
|
|
85
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { CodeMetrics } from './code-metrics';
|
|
2
|
+
export interface CodeFragment {
|
|
3
|
+
before: CodeMetrics;
|
|
4
|
+
after: CodeMetrics;
|
|
5
|
+
}
|
|
6
|
+
export interface RawCodeFragment {
|
|
7
|
+
before: string;
|
|
8
|
+
after: string;
|
|
9
|
+
}
|
|
10
|
+
export type PossibleCodeFragment = CodeFragment | CodeMetrics | RawCodeFragment | string;
|
|
11
|
+
export declare function metricsToFragment(value: CodeMetrics): CodeFragment;
|
|
12
|
+
export declare function parseCodeFragment(value: PossibleCodeFragment, context: CanvasRenderingContext2D, monoWidth: number): CodeFragment;
|
|
13
|
+
/**
|
|
14
|
+
* Create a code fragment that represents an insertion of code.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.
|
|
18
|
+
*
|
|
19
|
+
* @param code - The code to insert.
|
|
20
|
+
*/
|
|
21
|
+
export declare function insert(code: string): RawCodeFragment;
|
|
22
|
+
/**
|
|
23
|
+
* Create a code fragment that represents a change from one piece of code to
|
|
24
|
+
* another.
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.
|
|
28
|
+
*
|
|
29
|
+
* @param before - The code to change from.
|
|
30
|
+
* @param after - The code to change to.
|
|
31
|
+
*/
|
|
32
|
+
export declare function replace(before: string, after: string): RawCodeFragment;
|
|
33
|
+
/**
|
|
34
|
+
* Create a code fragment that represents a removal of code.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.
|
|
38
|
+
*
|
|
39
|
+
* @param code - The code to remove.
|
|
40
|
+
*/
|
|
41
|
+
export declare function remove(code: string): RawCodeFragment;
|
|
42
|
+
//# sourceMappingURL=code-fragment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-fragment.d.ts","sourceRoot":"","sources":["../src/code-fragment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAgC,MAAM,gBAAgB,CAAC;AAE3E,MAAM,WAAW,YAAY;IACzB,MAAM,EAAE,WAAW,CAAC;IACpB,KAAK,EAAE,WAAW,CAAC;CACtB;AACD,MAAM,WAAW,eAAe;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,oBAAoB,GAC1B,YAAY,GACZ,WAAW,GACX,eAAe,GACf,MAAM,CAAC;AAEb,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,YAAY,CAKlE;AAED,wBAAgB,iBAAiB,CAC7B,KAAK,EAAE,oBAAoB,EAC3B,OAAO,EAAE,wBAAwB,EACjC,SAAS,EAAE,MAAM,GAClB,YAAY,CAoBd;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,CAKpD;AAED;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,eAAe,CAKtE;AAED;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,CAKpD"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { isCodeMetrics, measureString } from './code-metrics';
|
|
2
|
+
export function metricsToFragment(value) {
|
|
3
|
+
return {
|
|
4
|
+
before: value,
|
|
5
|
+
after: value,
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export function parseCodeFragment(value, context, monoWidth) {
|
|
9
|
+
let fragment;
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
fragment = metricsToFragment(measureString(context, monoWidth, value));
|
|
12
|
+
}
|
|
13
|
+
else if (isCodeMetrics(value)) {
|
|
14
|
+
fragment = metricsToFragment(value);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
fragment = {
|
|
18
|
+
before: typeof value.before === 'string'
|
|
19
|
+
? measureString(context, monoWidth, value.before)
|
|
20
|
+
: value.before,
|
|
21
|
+
after: typeof value.after === 'string'
|
|
22
|
+
? measureString(context, monoWidth, value.after)
|
|
23
|
+
: value.after,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return fragment;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a code fragment that represents an insertion of code.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.
|
|
33
|
+
*
|
|
34
|
+
* @param code - The code to insert.
|
|
35
|
+
*/
|
|
36
|
+
export function insert(code) {
|
|
37
|
+
return {
|
|
38
|
+
before: '',
|
|
39
|
+
after: code,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create a code fragment that represents a change from one piece of code to
|
|
44
|
+
* another.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.
|
|
48
|
+
*
|
|
49
|
+
* @param before - The code to change from.
|
|
50
|
+
* @param after - The code to change to.
|
|
51
|
+
*/
|
|
52
|
+
export function replace(before, after) {
|
|
53
|
+
return {
|
|
54
|
+
before,
|
|
55
|
+
after,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a code fragment that represents a removal of code.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.
|
|
63
|
+
*
|
|
64
|
+
* @param code - The code to remove.
|
|
65
|
+
*/
|
|
66
|
+
export function remove(code) {
|
|
67
|
+
return {
|
|
68
|
+
before: code,
|
|
69
|
+
after: '',
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=code-fragment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-fragment.js","sourceRoot":"","sources":["../src/code-fragment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,aAAa,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAiB3E,MAAM,UAAU,iBAAiB,CAAC,KAAkB;IAChD,OAAO;QACH,MAAM,EAAE,KAAK;QACb,KAAK,EAAE,KAAK;KACf,CAAC;AACN,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC7B,KAA2B,EAC3B,OAAiC,EACjC,SAAi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Column: number;
|
|
5
|
+
firstWidth: number;
|
|
6
|
+
maxWidth: number;
|
|
7
|
+
lastWidth: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function measureString(context: CanvasRenderingContext2D, monoWidth: number, value: string): CodeMetrics;
|
|
10
|
+
export declare function isCodeMetrics(value: any): value is CodeMetrics;
|
|
11
|
+
//# sourceMappingURL=code-metrics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-metrics.d.ts","sourceRoot":"","sources":["../src/code-metrics.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED,wBAAgB,aAAa,CACzB,OAAO,EAAE,wBAAwB,EACjC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,GACd,WAAW,CA6Bb;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,WAAW,CAE9D"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function measureString(context, monoWidth, value) {
|
|
2
|
+
const lines = value.split('\n');
|
|
3
|
+
const lastLine = lines[lines.length - 1];
|
|
4
|
+
const firstWidth = Math.round(context.measureText(lines[0]).width / monoWidth);
|
|
5
|
+
let lastWidth = firstWidth;
|
|
6
|
+
let maxWidth = firstWidth;
|
|
7
|
+
for (let i = 1; i < lines.length; i++) {
|
|
8
|
+
const line = lines[i];
|
|
9
|
+
const width = Math.round(context.measureText(line).width / monoWidth);
|
|
10
|
+
if (width > maxWidth) {
|
|
11
|
+
maxWidth = width;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if (lines.length > 0) {
|
|
15
|
+
lastWidth = Math.round(context.measureText(lastLine).width / monoWidth);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
content: value,
|
|
19
|
+
newRows: lines.length - 1,
|
|
20
|
+
endColumn: lastLine.length,
|
|
21
|
+
firstWidth,
|
|
22
|
+
maxWidth,
|
|
23
|
+
lastWidth,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function isCodeMetrics(value) {
|
|
27
|
+
return value?.content !== undefined;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=code-metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-metrics.js","sourceRoot":"","sources":["../src/code-metrics.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,aAAa,CACzB,OAAiC,EACjC,SAAiB,EACjB,KAAa;IAEb,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACzC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CACzB,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,SAAS,CAClD,CAAC;IACF,IAAI,SAAS,GAAG,UAAU,CAAC;IAC3B,IAAI,QAAQ,GAAG,UAAU,CAAC;IAE1B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACpC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACtB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC;QACtE,IAAI,KAAK,GAAG,QAAQ,EAAE,CAAC;YACnB,QAAQ,GAAG,KAAK,CAAC;QACrB,CAAC;IACL,CAAC;IAED,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACnB,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC;IAC5E,CAAC;IAED,OAAO;QACH,OAAO,EAAE,KAAK;QACd,OAAO,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC;QACzB,SAAS,EAAE,QAAQ,CAAC,MAAM;QAC1B,UAAU;QACV,QAAQ;QACR,SAAS;KACZ,CAAC;AACN,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAU;IACpC,OAAO,KAAK,EAAE,OAAO,KAAK,SAAS,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A range over a code document. Lines and columns are 1-indexed.
|
|
3
|
+
*
|
|
4
|
+
* `endLine`/`endCol` may be `Infinity` to mean "to the end of the line" or
|
|
5
|
+
* "to the end of the document".
|
|
6
|
+
*/
|
|
7
|
+
export interface CodeRange {
|
|
8
|
+
startLine: number;
|
|
9
|
+
startCol: number;
|
|
10
|
+
endLine: number;
|
|
11
|
+
endCol: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create a range that spans `length` characters starting at (line, col).
|
|
15
|
+
* If `length` is omitted, the range extends to the end of the line.
|
|
16
|
+
*
|
|
17
|
+
* Example:
|
|
18
|
+
* word(1, 3, 3) // line 1, column 3, spans 3 characters
|
|
19
|
+
* word(1, 3) // line 1, column 3, to end of line
|
|
20
|
+
*/
|
|
21
|
+
export declare function word(line: number, col: number, length?: number): CodeRange;
|
|
22
|
+
/**
|
|
23
|
+
* Create a range that covers entire lines from `from` to `to` (inclusive).
|
|
24
|
+
* If `to` is omitted, only line `from` is covered.
|
|
25
|
+
*
|
|
26
|
+
* Example:
|
|
27
|
+
* lines(1, 3) // lines 1 through 3
|
|
28
|
+
* lines(2) // line 2 only
|
|
29
|
+
*/
|
|
30
|
+
export declare function lines(from: number, to?: number): CodeRange;
|
|
31
|
+
/**
|
|
32
|
+
* Create an arbitrary range from (startLine, startCol) to (endLine, endCol).
|
|
33
|
+
*/
|
|
34
|
+
export declare function range(startLine: number, startCol: number, endLine: number, endCol: number): CodeRange;
|
|
35
|
+
/**
|
|
36
|
+
* Convert a CodeRange to absolute character offsets [start, end) in the joined
|
|
37
|
+
* source text, given the per-line lengths of that source. Lines/columns are
|
|
38
|
+
* clamped into the document so out-of-bounds inputs produce a valid range.
|
|
39
|
+
*/
|
|
40
|
+
export declare function rangeToCharOffsets(r: CodeRange, lineLengths: number[]): {
|
|
41
|
+
start: number;
|
|
42
|
+
end: number;
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=code-range.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-range.d.ts","sourceRoot":"","sources":["../src/code-range.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAO1E;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAO1D;AAED;;GAEG;AACH,wBAAgB,KAAK,CACjB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,GACf,SAAS,CAEX;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAC9B,CAAC,EAAE,SAAS,EACZ,WAAW,EAAE,MAAM,EAAE,GACtB;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CA8BhC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a range that spans `length` characters starting at (line, col).
|
|
3
|
+
* If `length` is omitted, the range extends to the end of the line.
|
|
4
|
+
*
|
|
5
|
+
* Example:
|
|
6
|
+
* word(1, 3, 3) // line 1, column 3, spans 3 characters
|
|
7
|
+
* word(1, 3) // line 1, column 3, to end of line
|
|
8
|
+
*/
|
|
9
|
+
export function word(line, col, length) {
|
|
10
|
+
return {
|
|
11
|
+
startLine: line,
|
|
12
|
+
startCol: col,
|
|
13
|
+
endLine: line,
|
|
14
|
+
endCol: length === undefined ? Infinity : col + length,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a range that covers entire lines from `from` to `to` (inclusive).
|
|
19
|
+
* If `to` is omitted, only line `from` is covered.
|
|
20
|
+
*
|
|
21
|
+
* Example:
|
|
22
|
+
* lines(1, 3) // lines 1 through 3
|
|
23
|
+
* lines(2) // line 2 only
|
|
24
|
+
*/
|
|
25
|
+
export function lines(from, to) {
|
|
26
|
+
return {
|
|
27
|
+
startLine: from,
|
|
28
|
+
startCol: 1,
|
|
29
|
+
endLine: to ?? from,
|
|
30
|
+
endCol: Infinity,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Create an arbitrary range from (startLine, startCol) to (endLine, endCol).
|
|
35
|
+
*/
|
|
36
|
+
export function range(startLine, startCol, endLine, endCol) {
|
|
37
|
+
return { startLine, startCol, endLine, endCol };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Convert a CodeRange to absolute character offsets [start, end) in the joined
|
|
41
|
+
* source text, given the per-line lengths of that source. Lines/columns are
|
|
42
|
+
* clamped into the document so out-of-bounds inputs produce a valid range.
|
|
43
|
+
*/
|
|
44
|
+
export function rangeToCharOffsets(r, lineLengths) {
|
|
45
|
+
if (lineLengths.length === 0)
|
|
46
|
+
return { start: 0, end: 0 };
|
|
47
|
+
// Cumulative line starts in the joined string (lines joined with '\n').
|
|
48
|
+
const lineStart = new Array(lineLengths.length);
|
|
49
|
+
let acc = 0;
|
|
50
|
+
for (let i = 0; i < lineLengths.length; i++) {
|
|
51
|
+
lineStart[i] = acc;
|
|
52
|
+
acc += lineLengths[i] + 1; // +1 for the joining newline
|
|
53
|
+
}
|
|
54
|
+
const docEnd = acc > 0 ? acc - 1 : 0; // strip trailing newline
|
|
55
|
+
const clampLine = (n) => Math.max(0, Math.min(lineLengths.length - 1, n - 1)); // to 0-indexed
|
|
56
|
+
const sLine = clampLine(r.startLine);
|
|
57
|
+
const eLine = clampLine(r.endLine);
|
|
58
|
+
const sColMax = lineLengths[sLine];
|
|
59
|
+
const eColMax = lineLengths[eLine];
|
|
60
|
+
// Columns are 1-indexed; clamp to [1, lineLength+1] so endCol can sit past
|
|
61
|
+
// the last char (one-past-end).
|
|
62
|
+
const sCol = Math.max(0, Math.min(sColMax, r.startCol - 1));
|
|
63
|
+
const eCol = r.endCol === Infinity
|
|
64
|
+
? eColMax
|
|
65
|
+
: Math.max(0, Math.min(eColMax, r.endCol - 1));
|
|
66
|
+
const start = Math.min(docEnd, lineStart[sLine] + sCol);
|
|
67
|
+
const end = Math.min(docEnd, lineStart[eLine] + eCol);
|
|
68
|
+
return { start: Math.min(start, end), end: Math.max(start, end) };
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=code-range.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code-range.js","sourceRoot":"","sources":["../src/code-range.ts"],"names":[],"mappings":"AAaA;;;;;;;GAOG;AACH,MAAM,UAAU,IAAI,CAAC,IAAY,EAAE,GAAW,EAAE,MAAe;IAC3D,OAAO;QACH,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,GAAG;QACb,OAAO,EAAE,IAAI;QACb,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,CAA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ing the lines that were deleted, inserted, and potentially moved
|
|
4
|
+
* lines. The plus parameter can result in a significant performance hit due
|
|
5
|
+
* to additional Longest Common Substring searches.
|
|
6
|
+
*
|
|
7
|
+
* @param aLines - The original array of strings
|
|
8
|
+
* @param bLines - The new array of strings
|
|
9
|
+
* @param plus - Whether to return the moved lines
|
|
10
|
+
*
|
|
11
|
+
* Adapted from Jonathan "jonTrent" Trent's patience-diff algorithm.
|
|
12
|
+
* Types and tests added by Hunter "hhenrichsen" Henrichsen.
|
|
13
|
+
*
|
|
14
|
+
* {@link https://github.com/jonTrent/PatienceDiff}
|
|
15
|
+
*/
|
|
16
|
+
export declare function patienceDiff(aLines: string[], bLines: string[]): {
|
|
17
|
+
lines: {
|
|
18
|
+
line: string;
|
|
19
|
+
aIndex: number;
|
|
20
|
+
bIndex: number;
|
|
21
|
+
}[];
|
|
22
|
+
lineCountDeleted: number;
|
|
23
|
+
lineCountInserted: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Utility function for debugging patienceDiff.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export declare function printDiff(diff: ReturnType<typeof patienceDiff>): void;
|
|
31
|
+
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CACxB,MAAM,EAAE,MAAM,EAAE,EAChB,MAAM,EAAE,MAAM,EAAE,GACjB;IACC,KAAK,EAAE;QACH,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;KAClB,EAAE,CAAC;IACJ,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;CAC7B,CA6QA;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,UAAU,CAAC,OAAO,YAAY,CAAC,QAU9D"}
|
package/dist/diff.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performs a patience diff on two arrays of strings, returning an object
|
|
3
|
+
* containing the lines that were deleted, inserted, and potentially moved
|
|
4
|
+
* lines. The plus parameter can result in a significant performance hit due
|
|
5
|
+
* to additional Longest Common Substring searches.
|
|
6
|
+
*
|
|
7
|
+
* @param aLines - The original array of strings
|
|
8
|
+
* @param bLines - The new array of strings
|
|
9
|
+
* @param plus - Whether to return the moved lines
|
|
10
|
+
*
|
|
11
|
+
* Adapted from Jonathan "jonTrent" Trent's patience-diff algorithm.
|
|
12
|
+
* Types and tests added by Hunter "hhenrichsen" Henrichsen.
|
|
13
|
+
*
|
|
14
|
+
* {@link https://github.com/jonTrent/PatienceDiff}
|
|
15
|
+
*/
|
|
16
|
+
export function patienceDiff(aLines, bLines) {
|
|
17
|
+
/**
|
|
18
|
+
* Finds all unique values in lines[start...end], inclusive. This
|
|
19
|
+
* function is used in preparation for determining the longest common
|
|
20
|
+
* subsequence.
|
|
21
|
+
*
|
|
22
|
+
* @param lines - The array to search
|
|
23
|
+
* @param start - The starting index (inclusive)
|
|
24
|
+
* @param end - The ending index (inclusive)
|
|
25
|
+
* @returns A map of the unique lines to their index
|
|
26
|
+
*/
|
|
27
|
+
function findUnique(lines, start, end) {
|
|
28
|
+
const lineMap = new Map();
|
|
29
|
+
for (let i = start; i <= end; i++) {
|
|
30
|
+
const line = lines[i];
|
|
31
|
+
const data = lineMap.get(line);
|
|
32
|
+
if (data) {
|
|
33
|
+
data.count++;
|
|
34
|
+
data.index = i;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
lineMap.set(line, { count: 1, index: i });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const newMap = new Map();
|
|
41
|
+
for (const [key, value] of lineMap) {
|
|
42
|
+
if (value.count === 1) {
|
|
43
|
+
newMap.set(key, value.index);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return newMap;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Finds all the unique common entries between aArray[aStart...aEnd] and
|
|
50
|
+
* bArray[bStart...bEnd], inclusive. This function uses findUnique to pare
|
|
51
|
+
* down the aArray and bArray ranges first, before then walking the
|
|
52
|
+
* comparison between the two arrays.
|
|
53
|
+
*
|
|
54
|
+
*
|
|
55
|
+
* @param aArray - The original array
|
|
56
|
+
* @param aStart - The start of the original array to search
|
|
57
|
+
* @param aEnd - The end of the original array to search, inclusive
|
|
58
|
+
* @param bArray - The new array
|
|
59
|
+
* @param bStart - the start of the new array to search
|
|
60
|
+
* @param bEnd - The end of the new array to search, inclusive
|
|
61
|
+
* @returns a Map, with the key as the common line between aArray and
|
|
62
|
+
* bArray, with the value as an object containing the array indices of the
|
|
63
|
+
* matching uniqe lines.
|
|
64
|
+
*/
|
|
65
|
+
function uniqueCommon(aArray, aStart, aEnd, bArray, bStart, bEnd) {
|
|
66
|
+
const aUnique = findUnique(aArray, aStart, aEnd);
|
|
67
|
+
const bUnique = findUnique(bArray, bStart, bEnd);
|
|
68
|
+
return [...aUnique.entries()].reduce((paired, [key, value]) => {
|
|
69
|
+
const bIndex = bUnique.get(key);
|
|
70
|
+
if (bIndex !== undefined) {
|
|
71
|
+
paired.set(key, {
|
|
72
|
+
aIndex: value,
|
|
73
|
+
bIndex,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return paired;
|
|
77
|
+
}, new Map());
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Takes a map from the unique common lines between two arrays and determines
|
|
81
|
+
* the longest common subsequence.
|
|
82
|
+
*
|
|
83
|
+
* @see uniqueCommon
|
|
84
|
+
*
|
|
85
|
+
* @param abMap - The map of unique common lines between two arrays.
|
|
86
|
+
* @returns An array of objects containing the indices of the longest common
|
|
87
|
+
* subsequence.
|
|
88
|
+
*/
|
|
89
|
+
function longestCommonSubsequence(abMap) {
|
|
90
|
+
const jagged = [];
|
|
91
|
+
abMap.forEach(value => {
|
|
92
|
+
let i = 0;
|
|
93
|
+
while (jagged[i] && jagged[i].at(-1).bIndex < value.bIndex) {
|
|
94
|
+
i++;
|
|
95
|
+
}
|
|
96
|
+
if (i > 0) {
|
|
97
|
+
value.prev = jagged[i - 1].at(-1);
|
|
98
|
+
}
|
|
99
|
+
if (!jagged[i]) {
|
|
100
|
+
jagged[i] = [value];
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
jagged[i].push(value);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
// Pull out the longest common subsequence
|
|
107
|
+
let lcs = [];
|
|
108
|
+
if (jagged.length > 0) {
|
|
109
|
+
lcs = [jagged.at(-1).at(-1)];
|
|
110
|
+
let cursor = lcs.at(-1);
|
|
111
|
+
while (cursor?.prev) {
|
|
112
|
+
cursor = cursor.prev;
|
|
113
|
+
lcs.push(cursor);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return lcs.reverse();
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Keeps track of the aLines that have been deleted, are shared between aLines
|
|
120
|
+
* and bLines, and bLines that have been inserted.
|
|
121
|
+
*/
|
|
122
|
+
const result = [];
|
|
123
|
+
let deleted = 0;
|
|
124
|
+
let inserted = 0;
|
|
125
|
+
function addToResult(aIndex, bIndex) {
|
|
126
|
+
if (bIndex < 0) {
|
|
127
|
+
deleted++;
|
|
128
|
+
}
|
|
129
|
+
else if (aIndex < 0) {
|
|
130
|
+
inserted++;
|
|
131
|
+
}
|
|
132
|
+
result.push({
|
|
133
|
+
line: 0 <= aIndex ? aLines[aIndex] : bLines[bIndex],
|
|
134
|
+
aIndex,
|
|
135
|
+
bIndex,
|
|
136
|
+
moved: false,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
function addSubMatch(aStart, aEnd, bStart, bEnd) {
|
|
140
|
+
// Match any lines at the beginning of aLines and bLines.
|
|
141
|
+
while (aStart <= aEnd &&
|
|
142
|
+
bStart <= bEnd &&
|
|
143
|
+
aLines[aStart] === bLines[bStart]) {
|
|
144
|
+
addToResult(aStart++, bStart++);
|
|
145
|
+
}
|
|
146
|
+
// Match any lines at the end of aLines and bLines, but don't place them
|
|
147
|
+
// in the "result" array just yet, as the lines between these matches at
|
|
148
|
+
// the beginning and the end need to be analyzed first.
|
|
149
|
+
const aEndTemp = aEnd;
|
|
150
|
+
while (aStart <= aEnd && bStart <= bEnd && aLines[aEnd] === bLines[bEnd]) {
|
|
151
|
+
aEnd--;
|
|
152
|
+
bEnd--;
|
|
153
|
+
}
|
|
154
|
+
// Now, check to determine with the remaining lines in the subsequence
|
|
155
|
+
// whether there are any unique common lines between aLines and bLines.
|
|
156
|
+
//
|
|
157
|
+
// If not, add the subsequence to the result (all aLines having been
|
|
158
|
+
// deleted, and all bLines having been inserted).
|
|
159
|
+
//
|
|
160
|
+
// If there are unique common lines between aLines and bLines, then let's
|
|
161
|
+
// recursively perform the patience diff on the subsequence.
|
|
162
|
+
const uniqueCommonMap = uniqueCommon(aLines, aStart, aEnd, bLines, bStart, bEnd);
|
|
163
|
+
if (uniqueCommonMap.size === 0) {
|
|
164
|
+
while (aStart <= aEnd) {
|
|
165
|
+
addToResult(aStart++, -1);
|
|
166
|
+
}
|
|
167
|
+
while (bStart <= bEnd) {
|
|
168
|
+
addToResult(-1, bStart++);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
recurseLCS(aStart, aEnd, bStart, bEnd, uniqueCommonMap);
|
|
173
|
+
}
|
|
174
|
+
// Finally, let's add the matches at the end to the result.
|
|
175
|
+
while (aEnd < aEndTemp) {
|
|
176
|
+
addToResult(++aEnd, ++bEnd);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Finds the longest common subsequence between the arrays
|
|
181
|
+
* aLines[aStart...aEnd] and bLines[bStart...bEnd], inclusive. Then for each
|
|
182
|
+
* subsequence, recursively performs another LCS search (via addSubMatch),
|
|
183
|
+
* until there are none found, at which point the subsequence is dumped to
|
|
184
|
+
* the result.
|
|
185
|
+
*
|
|
186
|
+
* @param aStart - The start of the original array to search
|
|
187
|
+
* @param aEnd - The end of the original array to search, inclusive
|
|
188
|
+
* @param bStart - The start of the new array to search
|
|
189
|
+
* @param bEnd - The end of the new array to search, inclusive
|
|
190
|
+
* @param uniqueCommonMap - A map of the unique common lines between
|
|
191
|
+
* aLines[aStart...aEnd] and bLines[bStart...bEnd], inclusive.
|
|
192
|
+
*/
|
|
193
|
+
function recurseLCS(aStart, aEnd, bStart, bEnd, uniqueCommonMap = uniqueCommon(aLines, aStart, aEnd, bLines, bStart, bEnd)) {
|
|
194
|
+
const lcs = longestCommonSubsequence(uniqueCommonMap);
|
|
195
|
+
if (lcs.length === 0) {
|
|
196
|
+
addSubMatch(aStart, aEnd, bStart, bEnd);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
if (aStart < lcs[0].aIndex || bStart < lcs[0].bIndex) {
|
|
200
|
+
addSubMatch(aStart, lcs[0].aIndex - 1, bStart, lcs[0].bIndex - 1);
|
|
201
|
+
}
|
|
202
|
+
let i;
|
|
203
|
+
for (i = 0; i < lcs.length - 1; i++) {
|
|
204
|
+
addSubMatch(lcs[i].aIndex, lcs[i + 1].aIndex - 1, lcs[i].bIndex, lcs[i + 1].bIndex - 1);
|
|
205
|
+
}
|
|
206
|
+
if (lcs[i].aIndex <= aEnd || lcs[i].bIndex <= bEnd) {
|
|
207
|
+
addSubMatch(lcs[i].aIndex, aEnd, lcs[i].bIndex, bEnd);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
recurseLCS(0, aLines.length - 1, 0, bLines.length - 1);
|
|
212
|
+
return {
|
|
213
|
+
lines: result,
|
|
214
|
+
lineCountDeleted: deleted,
|
|
215
|
+
lineCountInserted: inserted,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Utility function for debugging patienceDiff.
|
|
220
|
+
*
|
|
221
|
+
* @internal
|
|
222
|
+
*/
|
|
223
|
+
export function printDiff(diff) {
|
|
224
|
+
diff.lines.forEach(line => {
|
|
225
|
+
if (line.bIndex < 0) {
|
|
226
|
+
console.log(`- ${line.line}`);
|
|
227
|
+
}
|
|
228
|
+
else if (line.aIndex < 0) {
|
|
229
|
+
console.log(`+ ${line.line}`);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
console.log(` ${line.line}`);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=diff.js.map
|
package/dist/diff.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff.js","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,YAAY,CACxB,MAAgB,EAChB,MAAgB;IAUhB;;;;;;;;;OASG;IACH,SAAS,UAAU,CAAC,KAAe,EAAE,KAAa,EAAE,GAAW;QAC3D,MAAM,OAAO,GAAG,IAAI,GAAG,EAA4C,CAAC;QACpE,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;YAChC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YACtB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC/B,IAAI,IAAI,EAAE,CAAC;gBACP,IAAI,CAAC,KAAK,EAAE,CAAC;gBACb,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;YACnB,CAAC;iBAAM,CAAC;gBACJ,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC9C,CAAC;QACL,CAAC;QAED,MAAM,M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