Repository navigation
feat(files): highlight .diff files and pick the highlighting language in the file viewer #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
216ecd7
fecdac7
7eaa262
c34385b
be196e9
95253f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1593,6 +1593,10 @@ html[data-touch-targets="accessible"] :is(.image-lightbox-stepper button, .image | |
| min-height: 35px; | ||
| } | ||
|
|
||
| .file-viewer-shell { | ||
| container: file-viewer / inline-size; | ||
| } | ||
|
|
||
| .file-viewer-path { | ||
| flex: 1 1 auto; | ||
| min-width: 48px; | ||
|
|
@@ -1661,6 +1665,45 @@ html[data-touch-targets="accessible"] :is(.image-lightbox-stepper button, .image | |
| filter: brightness(1.08); | ||
| } | ||
|
|
||
| .file-viewer-language-select { | ||
| height: 24px; | ||
| max-width: 140px; | ||
| padding: 0 6px; | ||
| border: 1px solid var(--border); | ||
| border-radius: var(--radius-control); | ||
| background: var(--bg); | ||
| color: var(--text-muted); | ||
| font: inherit; | ||
| font-size: 11px; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .file-viewer-language-select:hover { | ||
| color: var(--text); | ||
| } | ||
|
|
||
| /* A narrow panel moves the controls to their own row, wrapping them again | ||
| rather than clipping the view modes or Download. */ | ||
| @container file-viewer (max-width: 440px) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: CSS Container Queries ( 🩹 The Fix: Wrap the container query in 📏 Severity: suggestion
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not adding a fallback: the repository already relies on unguarded |
||
| .file-viewer-toolbar { | ||
| flex-wrap: wrap; | ||
| row-gap: 5px; | ||
| } | ||
|
|
||
| .file-viewer-meta { | ||
| display: none; | ||
| } | ||
|
|
||
| .file-viewer-controls { | ||
| flex-shrink: 1; | ||
| flex-wrap: wrap; | ||
| justify-content: flex-end; | ||
| row-gap: 5px; | ||
| min-width: 0; | ||
| margin-left: auto; | ||
| } | ||
| } | ||
|
|
||
| .file-viewer-icon-button { | ||
| display: inline-flex; | ||
| align-items: center; | ||
|
|
@@ -1695,7 +1738,8 @@ html[data-touch-targets="accessible"] :is(.image-lightbox-stepper button, .image | |
| } | ||
|
|
||
| .file-viewer-mode-button:focus-visible, | ||
| .file-viewer-icon-button:focus-visible { | ||
| .file-viewer-icon-button:focus-visible, | ||
| .file-viewer-language-select:focus-visible { | ||
| outline: 2px solid var(--accent); | ||
| outline-offset: -2px; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import assert from "node:assert/strict"; | ||
| import "../tests/setup-dom.mjs"; | ||
| import test, { afterEach } from "node:test"; | ||
| import React from "react"; | ||
| const { cleanup, fireEvent, render, waitFor } = await import("@testing-library/react/pure.js"); | ||
|
|
||
| const { FileViewer } = await import("./FileViewer.tsx"); | ||
|
|
||
| const originalFetch = globalThis.fetch; | ||
| const originalWorker = globalThis.Worker; | ||
| afterEach(async () => { | ||
| await React.act(async () => { | ||
| await new Promise((resolve) => setTimeout(resolve, 50)); | ||
| cleanup(); | ||
| }); | ||
| globalThis.fetch = originalFetch; | ||
| globalThis.Worker = originalWorker; | ||
| delete window.matchMedia; | ||
| }); | ||
|
|
||
| const PATCH = "--- a/x\n+++ b/x\n-old\n+new\n"; | ||
|
|
||
| function setup() { | ||
| globalThis.fetch = async (url) => { | ||
| const body = String(url).startsWith("/api/git/") ? { supported: false } : { content: PATCH, language: "text", size: PATCH.length }; | ||
| return { ok: true, json: async () => body }; | ||
| }; | ||
| globalThis.Worker = class { onmessage = null; onerror = null; postMessage() {} }; | ||
| window.matchMedia = () => ({ matches: false, addEventListener() {}, removeEventListener() {} }); | ||
| } | ||
|
|
||
| const props = (filePath) => ({ filePath, hostId: "local", cwd: "/ws", sourceSessionId: "s1" }); | ||
|
|
||
| test("the language picker highlights a file whose extension maps to no grammar, and resets for the next file", async () => { | ||
| setup(); | ||
| const view = render(React.createElement(FileViewer, props("/ws/change.txt"))); | ||
| const picker = await waitFor(() => { | ||
| const select = view.container.querySelector("select.file-viewer-language-select"); | ||
| assert.ok(select); | ||
| return select; | ||
| }); | ||
| assert.equal(picker.value, ""); | ||
| assert.equal(view.container.querySelector(".file-source-view .token"), null); | ||
|
|
||
| fireEvent.change(picker, { target: { value: "diff" } }); | ||
| await waitFor(() => assert.ok(view.container.querySelector(".file-source-view .token"))); | ||
|
|
||
| // Leaving the tab and coming back to the same file keeps the pick. | ||
| view.rerender(React.createElement(FileViewer, { ...props("/ws/change.txt"), active: false })); | ||
| view.rerender(React.createElement(FileViewer, props("/ws/change.txt"))); | ||
| await waitFor(() => assert.ok(view.container.querySelector(".file-source-view .token"))); | ||
| assert.equal(view.container.querySelector("select.file-viewer-language-select").value, "diff"); | ||
|
|
||
| view.rerender(React.createElement(FileViewer, props("/ws/other.txt"))); | ||
| await waitFor(() => assert.equal(view.container.querySelector(".file-source-view .token"), null)); | ||
| assert.equal(view.container.querySelector("select.file-viewer-language-select").value, ""); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import type { SyntaxHighlighterProps } from "react-syntax-highlighter"; | |
| import { | ||
| createSyntaxElement as renderSyntaxNode, | ||
| ensureLanguageRegistered, | ||
| HIGHLIGHT_LANGUAGES, | ||
| isLanguageRegistered, | ||
| SyntaxHighlighter, | ||
| vs, | ||
|
|
@@ -823,6 +824,8 @@ function TextFileViewer({ filePath, hostId, cwd, sourceSessionId, onOpenFile, on | |
| }, [data]); | ||
| const [displayMode, setDisplayMode] = useState<DisplayMode>("source"); | ||
| const [wrapLines, setWrapLines] = useState(false); | ||
| // Picked by the user when the extension maps to the wrong grammar or none; null = detected. | ||
| const [languageOverride, setLanguageOverride] = useState<string | null>(null); | ||
| const [watching, setWatching] = useState(false); | ||
| const gitDiffRequestRef = useRef(0); | ||
| const contentRequestRef = useRef(0); | ||
|
|
@@ -876,6 +879,11 @@ function TextFileViewer({ filePath, hostId, cwd, sourceSessionId, onOpenFile, on | |
| } | ||
| }, [cwd, hostId]); | ||
|
|
||
| // Reset per file, not per activation: switching tabs back keeps the user's pick. | ||
| useEffect(() => { | ||
| setLanguageOverride(null); | ||
| }, [filePath]); | ||
|
|
||
| // Initial load + SSE watch setup | ||
| useEffect(() => { | ||
| if (!active) { | ||
|
|
@@ -1121,6 +1129,18 @@ function TextFileViewer({ filePath, hostId, cwd, sourceSessionId, onOpenFile, on | |
| <div className="file-viewer-actions"> | ||
| {displayMode === "source" && ( | ||
| <> | ||
| <Tooltip content={t("fileViewer.highlightLanguage")}> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: Wrapping a native 🩹 The Fix: Drop the 📏 Severity: warning
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not changing: this is the house |
||
| <select | ||
| value={languageOverride ?? ""} | ||
| onChange={(event) => setLanguageOverride(event.target.value || null)} | ||
| aria-label={t("fileViewer.highlightLanguage")} | ||
| className="file-viewer-language-select" | ||
| > | ||
| <option value="">{t("fileViewer.languageAuto", { language: data.language })}</option> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: The "Auto" option renders as 🩹 The Fix: Map the detected language to a friendly name, or just show the file extension: 📏 Severity: suggestion
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keeping as is: the label shows the detected grammar exactly as the toolbar's metadata line does ( |
||
| <option value="plaintext">{t("fileViewer.plainText")}</option> | ||
| {HIGHLIGHT_LANGUAGES.map((language) => <option key={language} value={language}>{language}</option>)} | ||
| </select> | ||
| </Tooltip> | ||
| <Tooltip content={t("fileViewer.mentionSelectedLines")}> | ||
| <button | ||
| type="button" | ||
|
|
@@ -1241,7 +1261,7 @@ function TextFileViewer({ filePath, hostId, cwd, sourceSessionId, onOpenFile, on | |
| ) : highlightReady ? ( | ||
| <SyntaxHighlighter | ||
| className={wrapLines ? "file-source-view is-wrapped" : "file-source-view"} | ||
| language={data.language === "text" ? "plaintext" : data.language} | ||
| language={languageOverride ?? (data.language === "text" ? "plaintext" : data.language)} | ||
| style={isDark ? vscDarkPlus : vs} | ||
| showLineNumbers | ||
| lineNumberStyle={{ | ||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,16 +89,18 @@ export function isDocumentPreviewPath(filePath: string): boolean { | |
| return documentPreviewKind(filePath) !== null; | ||
| } | ||
| const EXT_TO_LANGUAGE: Record<string, string> = { | ||
| ts: "typescript", tsx: "typescript", js: "javascript", jsx: "javascript", | ||
| ts: "typescript", tsx: "tsx", js: "javascript", jsx: "jsx", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: Changed 🩹 The Fix: At minimum, call this out in the changelog (you did, good). For extra credit, keep a 📏 Severity: nitpick
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentional, and called out in the changelog: the |
||
| mjs: "javascript", cjs: "javascript", py: "python", rb: "ruby", | ||
| go: "go", rs: "rust", java: "java", kt: "kotlin", swift: "swift", | ||
| c: "c", cpp: "cpp", h: "c", hpp: "cpp", cs: "csharp", | ||
| html: "html", htm: "html", css: "css", scss: "css", less: "css", | ||
| go: "go", rs: "rust", java: "java", kt: "kotlin", kts: "kotlin", swift: "swift", | ||
| c: "c", cpp: "cpp", cc: "cpp", cxx: "cpp", h: "c", hpp: "cpp", hh: "cpp", cs: "csharp", | ||
| html: "html", htm: "html", css: "css", scss: "scss", less: "css", | ||
| json: "json", jsonl: "json", yaml: "yaml", yml: "yaml", | ||
| toml: "toml", xml: "xml", md: "markdown", mdx: "markdown", | ||
| sh: "bash", bash: "bash", zsh: "bash", fish: "bash", | ||
| sql: "sql", graphql: "graphql", gql: "graphql", | ||
| dockerfile: "dockerfile", tf: "hcl", hcl: "hcl", | ||
| diff: "diff", patch: "diff", proto: "protobuf", | ||
| ini: "ini", cfg: "ini", conf: "ini", mk: "makefile", | ||
| env: "bash", gitignore: "bash", txt: "text", | ||
| pdf: "pdf", docx: "word", | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,9 @@ for (const [name, grammar] of Object.entries(GRAMMARS)) { | |
| PrismLight.registerLanguage(name, grammar); | ||
| } | ||
|
|
||
| /** Grammar names the file viewer offers in its language picker. */ | ||
| export const HIGHLIGHT_LANGUAGES = Object.keys(GRAMMARS); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 The Roast: 🩹 The Fix: Sort the keys: 📏 Severity: suggestion
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not changing: |
||
|
|
||
| /** | ||
| * True for every language — all grammars are registered eagerly, so nothing | ||
| * ever needs a pending load. Unknown languages are passed through and fall | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the supported 300px desktop panel width (
RIGHT_PANEL_MIN_WIDTH), viewing a modified Markdown/HTML file in Source mode now clips the mode switch and Download button. The new selector joins.file-viewer-actionsand.file-viewer-controls, both of which haveflex-shrink: 0, in a toolbar that never wraps;.file-viewer-shellthen hides the overflow. This pushes Preview/Diff outside the visible panel after switching to Source. Reflow or collapse the controls based on the panel width so the selector does not displace existing actions.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 2dce731: below 440px of panel width (a container query on the viewer, so it follows the panel, not the viewport) the toolbar wraps: the path keeps the first row, the controls move to their own right-aligned row and wrap again rather than shrink, and the metadata label hides. Checked at 300/360/440/900px in Source and Preview: no control extends past the panel.