-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathSnapshotDiffFormatter.ts
More file actions
148 lines (133 loc) · 4.38 KB
/
SnapshotDiffFormatter.ts
File metadata and controls
148 lines (133 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {TextSnapshot, TextSnapshotNode} from '../McpContext.js';
import {TreeDiff, type DiffNode} from '../utils/TreeDiff.js';
import {SnapshotFormatter} from './SnapshotFormatter.js';
export class SnapshotDiffFormatter {
#root: DiffNode<TextSnapshotNode>;
#oldFormatter: SnapshotFormatter;
#newFormatter: SnapshotFormatter;
constructor(
root: DiffNode<TextSnapshotNode>,
oldFormatter: SnapshotFormatter,
newFormatter: SnapshotFormatter,
) {
this.#root = root;
this.#oldFormatter = oldFormatter;
this.#newFormatter = newFormatter;
}
toString(): string {
const lines = this.#formatDiffNode(this.#root, 0);
const hasChanges = lines.some(l => l.startsWith('+') || l.startsWith('-'));
if (!hasChanges) {
return '';
}
return lines.join('').trimEnd();
}
#formatDiffNode(
diffNode: DiffNode<TextSnapshotNode>,
depth: number,
): string[] {
const chunks: string[] = [];
if (diffNode.type === 'same') {
const oldLine = this.#oldFormatter.formatNodeSelf(
diffNode.oldNode!,
depth,
);
const newLine = this.#newFormatter.formatNodeSelf(diffNode.node, depth);
if (oldLine === newLine) {
chunks.push(' ' + newLine);
} else {
chunks.push('- ' + oldLine);
chunks.push('+ ' + newLine);
}
// Children
for (const child of diffNode.children) {
chunks.push(...this.#formatDiffNode(child, depth + 1));
}
} else if (diffNode.type === 'added') {
chunks.push(
'+ ' + this.#newFormatter.formatNodeSelf(diffNode.node, depth),
);
// Recursively add children (they are also 'added' in the tree)
for (const child of diffNode.children) {
chunks.push(...this.#formatDiffNode(child, depth + 1));
}
} else if (diffNode.type === 'removed') {
chunks.push(
'- ' + this.#oldFormatter.formatNodeSelf(diffNode.node, depth),
);
// Recursively remove children (they are also 'removed' in the tree)
for (const child of diffNode.children) {
chunks.push(...this.#formatDiffNode(child, depth + 1));
}
} else if (diffNode.type === 'modified') {
chunks.push(
'- ' + this.#oldFormatter.formatNodeSelf(diffNode.oldNode!, depth),
);
chunks.push(
'+ ' + this.#newFormatter.formatNodeSelf(diffNode.node, depth),
);
}
return chunks;
}
toJSON(): object {
return this.#nodeToJSON(this.#root) ?? {};
}
#nodeToJSON(diffNode: DiffNode<TextSnapshotNode>): object | null {
if (diffNode.type === 'same') {
const oldJson = this.#oldFormatter.nodeToJSON(diffNode.oldNode!);
const newJson = this.#newFormatter.nodeToJSON(diffNode.node);
const childrenDiff = diffNode.children
.map(child => this.#nodeToJSON(child))
.filter(x => x !== null);
const contentChanged =
JSON.stringify(oldJson) !== JSON.stringify(newJson);
if (!contentChanged && childrenDiff.length === 0) {
return null;
}
const result: Record<string, unknown> = {};
if (contentChanged) {
result.type = 'modified';
result.oldAttributes = oldJson;
result.newAttributes = newJson;
} else {
result.type = 'unchanged';
result.id = diffNode.node.id;
}
if (childrenDiff.length > 0) {
result.children = childrenDiff;
}
return result;
} else if (diffNode.type === 'added') {
return {
type: 'added',
node: this.#newFormatter.nodeToJSON(diffNode.node),
};
} else if (diffNode.type === 'removed') {
return {
type: 'removed',
node: this.#oldFormatter.nodeToJSON(diffNode.node),
};
} else if (diffNode.type === 'modified') {
return {
type: 'modified',
oldNode: this.#oldFormatter.nodeToJSON(diffNode.oldNode!),
newNode: this.#newFormatter.nodeToJSON(diffNode.node),
};
}
return null;
}
static diff(
oldSnapshot: TextSnapshot,
newSnapshot: TextSnapshot,
): SnapshotDiffFormatter {
const diffRoot = TreeDiff.compute(oldSnapshot.root, newSnapshot.root);
const oldFormatter = new SnapshotFormatter(oldSnapshot);
const newFormatter = new SnapshotFormatter(newSnapshot);
return new SnapshotDiffFormatter(diffRoot, oldFormatter, newFormatter);
}
}