Fix editable file focus and live syntax highlighting (#3979)

This commit is contained in:
Jake Leventhal authored and GitHub committed 2026-07-29 19:06:39 +02:00
1 parent 85a8986870
commit fc28abf24b
6 files changed
+131 -56

No files matched your search

@@ -7,7 +7,7 @@ import type {
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview";
import { VirtualizedFile, type SelectedLineRange } from "@pierre/diffs";
import { Editor } from "@pierre/diffs/editor";
import { EditorProvider, File, type FileOptions, Virtualizer } from "@pierre/diffs/react";
import { EditProvider, File, type FileOptions, Virtualizer } from "@pierre/diffs/react";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
@@ -52,7 +52,7 @@ import {
} from "./fileCommentAnnotations";
import { installFileEditorDismissal } from "./fileEditorDismissal";
import { LocalCommentAnnotation } from "./LocalCommentAnnotation";
import { projectFileCacheKey } from "./fileContentRevision";
import { projectFileCacheKey, projectFileEditorCacheKey } from "./fileContentRevision";
import { fileBreadcrumbs } from "./filePath";
import { isMarkdownPreviewFile, setMarkdownTaskChecked } from "./filePreviewMode";
import { FileSaveCoordinator } from "./fileSaveCoordinator";
@@ -364,6 +364,8 @@ function EditableFileSurface({
const editor = useMemo(
() =>
new Editor<FileCommentAnnotationGroup>({
persistState: true,
persistStateStorage: "inMemory",
onChange: (file, nextLineAnnotations) => {
setProjectFileQueryData(environmentId, cwd, relativePath, file.contents);
saveCoordinator.change(file.contents);
@@ -536,7 +538,7 @@ function EditableFileSurface({
);
return (
<EditorProvider editor={editor}>
<EditProvider editor={editor}>
<div ref={surfaceRef} className="flex min-h-0 flex-1">
<Virtualizer
className="file-preview-virtualizer min-h-0 flex-1 overflow-auto"
@@ -549,7 +551,13 @@ function EditableFileSurface({
file={{
name: relativePath,
contents,
cacheKey: projectFileCacheKey(cwd, relativePath, contents),
cacheKey: projectFileEditorCacheKey(
environmentId,
cwd,
relativePath,
contents,
editor.getFile(),
),
}}
options={{
disableFileHeader: true,
@@ -586,7 +594,7 @@ function EditableFileSurface({
/>
</Virtualizer>
</div>
</EditorProvider>
</EditProvider>
);
}
@@ -1,6 +1,10 @@
import { describe, expect, it } from "vite-plus/test";
import { fileContentRevision, projectFileCacheKey } from "./fileContentRevision";
import {
fileContentRevision,
projectFileCacheKey,
projectFileEditorCacheKey,
} from "./fileContentRevision";
describe("fileContentRevision", () => {
it("changes for same-length edits", () => {
@@ -12,4 +16,27 @@ describe("fileContentRevision", () => {
projectFileCacheKey("/repo", "file.json", "contents"),
);
});
it("keeps editor identity stable for locally edited contents", () => {
const cacheKey = projectFileEditorCacheKey("local", "/repo", "file.json", "after", undefined);
expect(
projectFileEditorCacheKey("local", "/repo", "file.json", "after edit", {
cacheKey,
contents: "after edit",
}),
).toBe(cacheKey);
});
it("rotates editor identity for external contents and environments", () => {
const cacheKey = projectFileEditorCacheKey("local", "/repo", "file.json", "before", undefined);
const editorFile = { cacheKey, contents: "before" };
expect(
projectFileEditorCacheKey("local", "/repo", "file.json", "external edit", editorFile),
).not.toBe(cacheKey);
expect(projectFileEditorCacheKey("remote", "/repo", "file.json", "before", undefined)).not.toBe(
cacheKey,
);
});
});
@@ -10,3 +10,21 @@ export function fileContentRevision(contents: string): string {
export function projectFileCacheKey(cwd: string, relativePath: string, contents: string): string {
return `${cwd}:${relativePath}:${fileContentRevision(contents)}`;
}
interface EditorFileIdentity {
readonly cacheKey?: string;
readonly contents: string;
}
export function projectFileEditorCacheKey(
environmentId: string,
cwd: string,
relativePath: string,
contents: string,
editorFile: EditorFileIdentity | undefined,
): string {
if (editorFile?.contents === contents && editorFile.cacheKey) {
return editorFile.cacheKey;
}
return `editor:${environmentId}:${projectFileCacheKey(cwd, relativePath, contents)}`;
}