Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit fc28abf

Browse files
Fix editable file focus and live syntax highlighting (pingdotgg#3979)
1 parent 85a8986 commit fc28abf

6 files changed

Lines changed: 131 additions & 56 deletions

File tree

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview";
88
import { VirtualizedFile, type SelectedLineRange } from "@pierre/diffs";
99
import { Editor } from "@pierre/diffs/editor";
10-
import { EditorProvider, File, type FileOptions, Virtualizer } from "@pierre/diffs/react";
10+
import { EditProvider, File, type FileOptions, Virtualizer } from "@pierre/diffs/react";
1111
import {
1212
isAtomCommandInterrupted,
1313
squashAtomCommandFailure,
@@ -52,7 +52,7 @@ import {
5252
} from "./fileCommentAnnotations";
5353
import { installFileEditorDismissal } from "./fileEditorDismissal";
5454
import { LocalCommentAnnotation } from "./LocalCommentAnnotation";
55-
import { projectFileCacheKey } from "./fileContentRevision";
55+
import { projectFileCacheKey, projectFileEditorCacheKey } from "./fileContentRevision";
5656
import { fileBreadcrumbs } from "./filePath";
5757
import { isMarkdownPreviewFile, setMarkdownTaskChecked } from "./filePreviewMode";
5858
import { FileSaveCoordinator } from "./fileSaveCoordinator";
@@ -364,6 +364,8 @@ function EditableFileSurface({
364364
const editor = useMemo(
365365
() =>
366366
new Editor<FileCommentAnnotationGroup>({
367+
persistState: true,
368+
persistStateStorage: "inMemory",
367369
onChange: (file, nextLineAnnotations) => {
368370
setProjectFileQueryData(environmentId, cwd, relativePath, file.contents);
369371
saveCoordinator.change(file.contents);
@@ -536,7 +538,7 @@ function EditableFileSurface({
536538
);
537539

538540
return (
539-
<EditorProvider editor={editor}>
541+
<EditProvider editor={editor}>
540542
<div ref={surfaceRef} className="flex min-h-0 flex-1">
541543
<Virtualizer
542544
className="file-preview-virtualizer min-h-0 flex-1 overflow-auto"
@@ -549,7 +551,13 @@ function EditableFileSurface({
549551
file={{
550552
name: relativePath,
551553
contents,
552-
cacheKey: projectFileCacheKey(cwd, relativePath, contents),
554+
cacheKey: projectFileEditorCacheKey(
555+
environmentId,
556+
cwd,
557+
relativePath,
558+
contents,
559+
editor.getFile(),
560+
),
553561
}}
554562
options={{
555563
disableFileHeader: true,
@@ -586,7 +594,7 @@ function EditableFileSurface({
586594
/>
587595
</Virtualizer>
588596
</div>
589-
</EditorProvider>
597+
</EditProvider>
590598
);
591599
}
592600

‎apps/web/src/components/files/fileContentRevision.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { fileContentRevision, projectFileCacheKey } from "./fileContentRevision";
3+
import {
4+
fileContentRevision,
5+
projectFileCacheKey,
6+
projectFileEditorCacheKey,
7+
} from "./fileContentRevision";
48

59
describe("fileContentRevision", () => {
610
it("changes for same-length edits", () => {
@@ -12,4 +16,27 @@ describe("fileContentRevision", () => {
1216
projectFileCacheKey("/repo", "file.json", "contents"),
1317
);
1418
});
19+
20+
it("keeps editor identity stable for locally edited contents", () => {
21+
const cacheKey = projectFileEditorCacheKey("local", "/repo", "file.json", "after", undefined);
22+
23+
expect(
24+
projectFileEditorCacheKey("local", "/repo", "file.json", "after edit", {
25+
cacheKey,
26+
contents: "after edit",
27+
}),
28+
).toBe(cacheKey);
29+
});
30+
31+
it("rotates editor identity for external contents and environments", () => {
32+
const cacheKey = projectFileEditorCacheKey("local", "/repo", "file.json", "before", undefined);
33+
const editorFile = { cacheKey, contents: "before" };
34+
35+
expect(
36+
projectFileEditorCacheKey("local", "/repo", "file.json", "external edit", editorFile),
37+
).not.toBe(cacheKey);
38+
expect(projectFileEditorCacheKey("remote", "/repo", "file.json", "before", undefined)).not.toBe(
39+
cacheKey,
40+
);
41+
});
1542
});

‎apps/web/src/components/files/fileContentRevision.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,21 @@ export function fileContentRevision(contents: string): string {
1010
export function projectFileCacheKey(cwd: string, relativePath: string, contents: string): string {
1111
return `${cwd}:${relativePath}:${fileContentRevision(contents)}`;
1212
}
13+
14+
interface EditorFileIdentity {
15+
readonly cacheKey?: string;
16+
readonly contents: string;
17+
}
18+
19+
export function projectFileEditorCacheKey(
20+
environmentId: string,
21+
cwd: string,
22+
relativePath: string,
23+
contents: string,
24+
editorFile: EditorFileIdentity | undefined,
25+
): string {
26+
if (editorFile?.contents === contents && editorFile.cacheKey) {
27+
return editorFile.cacheKey;
28+
}
29+
return `editor:${environmentId}:${projectFileCacheKey(cwd, relativePath, contents)}`;
30+
}
Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
11
diff --git a/dist/editor/editor.js b/dist/editor/editor.js
2-
index e8013fc6eb6f243a6c912facf3fc0319ac66a8d0..80c82df4cdeb828bd331f5ec2f443d216bedc304 100644
2+
index ff78e2a..f9df318 100644
33
--- a/dist/editor/editor.js
44
+++ b/dist/editor/editor.js
5-
@@ -77,15 +77,12 @@ var Editor = class {
6-
this.#options = options;
7-
}
8-
edit(component) {
9-
- const { useTokenTransformer, enableGutterUtility, enableLineSelection, expandUnchanged, diffStyle, lineHoverHighlight,...rest } = component.options;
10-
+ const { useTokenTransformer, expandUnchanged, diffStyle,...rest } = component.options;
11-
const isDiff = component.type === "file-diff";
12-
- if (useTokenTransformer !== true || enableGutterUtility === true || enableLineSelection === true || lineHoverHighlight !== "disabled" || expandUnchanged !== true && isDiff || diffStyle === "unified" && isDiff) {
13-
+ if (useTokenTransformer !== true || expandUnchanged !== true && isDiff || diffStyle === "unified" && isDiff) {
14-
component.setOptions({
5+
@@ -146,14 +146,11 @@ var Editor = class {
6+
const file = fileInstance.__getCurrentFile?.();
7+
if (file !== void 0) requirePersistedCacheKey(file);
8+
}
9+
- const { useTokenTransformer, enableGutterUtility, enableLineSelection, lineHoverHighlight = "disabled", ...rest } = fileInstance.options;
10+
- if (useTokenTransformer !== true || enableGutterUtility === true || enableLineSelection === true || lineHoverHighlight !== "disabled") {
11+
+ const { useTokenTransformer, ...rest } = fileInstance.options;
12+
+ if (useTokenTransformer !== true) {
13+
fileInstance.setOptions({
1514
...rest,
16-
useTokenTransformer: true,
15+
- useTokenTransformer: true,
1716
- enableGutterUtility: false,
1817
- enableLineSelection: false,
19-
- lineHoverHighlight: "disabled",
20-
expandUnchanged: true,
21-
diffStyle: "split"
18+
- lineHoverHighlight: "disabled"
19+
+ useTokenTransformer: true
2220
});
23-
@@ -511,6 +508,7 @@ var Editor = class {
21+
fileInstance.rerender();
22+
}
23+
@@ -908,6 +905,7 @@ var Editor = class {
2424
return lineNumber - 1;
2525
};
2626
this.#editorEventDisposes.push(addEventListener(gutterEl, "pointerdown", (e) => {
2727
+ if (this.#fileInstance?.options.enableLineSelection === true) return;
28-
const textDocument = this.#textDocument;
29-
const lineIndex = resolveEditableLine(resolveGutterTarget(e.composedPath()[0]));
30-
if (lineIndex === void 0 || textDocument === void 0) return;
28+
const gutterRow = resolveGutterTarget(e.composedPath()[0]);
29+
if (gutterRow?.dataset.lineType === "change-deletion") {
30+
const code = gutterRow.closest("[data-code]");
31+
@@ -1522,6 +1520,7 @@ var Editor = class {
32+
if (gutterEl !== void 0) gutterEl.style.gridRow = "span " + gridRow;
33+
}
34+
fileInstance.updateRenderCache(dirtyLines, tokenizer.themeType, !didLineCountChange, didLineCountChange);
35+
+ if (fileInstance.file !== void 0) fileInstance.file.contents = textDocument.getText();
36+
if (didLineCountChange) fileInstance.applyDocumentChange(textDocument, newLineAnnotations, shouldUpdateBuffer);
37+
if (this.#isDiff && (this.#diffSyle === "unified" || didLineCountChange)) this.#resetCache();
38+
if (newLineAnnotations !== void 0) {
39+
@@ -1788,6 +1787,7 @@ var Editor = class {
40+
}
41+
}
42+
#setSelectedLinesSafe(range, lineNumberOnly = false) {
43+
+ if (this.#fileInstance?.options.controlledSelection === true) return;
44+
try {
45+
this.#fileInstance?.setSelectedLines(range, {
46+
notify: false,
3147
diff --git a/dist/react/utils/useFileInstance.js b/dist/react/utils/useFileInstance.js
32-
index cb8e2026fb5d7a19f489c0a2402efbcb7dff3322..510fad6364d4a2214c7dd65fe2b114f1cce0c815 100644
48+
index e9f62f5..af82a46 100644
3349
--- a/dist/react/utils/useFileInstance.js
3450
+++ b/dist/react/utils/useFileInstance.js
35-
@@ -92,10 +92,7 @@ function mergeFileOptions({ options, controlledSelection, contentEditable, hasCu
51+
@@ -91,10 +91,7 @@ function mergeFileOptions({ options, controlledSelection, contentEditable, hasCu
3652
};
3753
if (needsEditorOptions) merged = {
3854
...merged,
@@ -45,7 +61,7 @@ index cb8e2026fb5d7a19f489c0a2402efbcb7dff3322..510fad6364d4a2214c7dd65fe2b114f1
4561
return merged;
4662
}
4763
diff --git a/package.json b/package.json
48-
index d1558633de87044b7aa96cff09443db11f163cec..c0b16f0a0bec6fba2026f24f38b2c0a8fa06af7c 100644
64+
index ff61c90..1e170e5 100644
4965
--- a/package.json
5066
+++ b/package.json
5167
@@ -55,6 +55,18 @@

‎pnpm-lock.yaml‎

Lines changed: 32 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ catalog:
4141
"@effect/vitest": 4.0.0-beta.102
4242
"@noble/curves": 1.9.1
4343
"@noble/hashes": 1.8.0
44-
"@pierre/diffs": 1.3.0-beta.5
44+
"@pierre/diffs": 1.3.0-beta.10
4545
"@types/node": 24.12.4
4646
"@typescript/native-preview": 7.0.0-dev.20260604.1
4747
effect: 4.0.0-beta.102
@@ -128,7 +128,7 @@ patchedDependencies:
128128
"@expo/[email protected]": patches/@expo%[email protected]
129129
130130
"@legendapp/[email protected]": patches/@[email protected]
131-
"@pierre/[email protected].5": patches/@pierre%[email protected].5.patch
131+
"@pierre/[email protected].10": patches/@pierre%[email protected].10.patch
132132
"@react-native-menu/[email protected]": patches/@[email protected]
133133
"@react-navigation/[email protected]": patches/@react-navigation%[email protected]
134134

0 commit comments

Comments
 (0)