Skip to content

feat: read source code in a read-only CodeMirror view - #60

Merged
bencode merged 2 commits into
mainfrom
feat/code-reader
Oct 1, 2026
Merged

bencode merged 2 commits into
mainfrom
feat/code-reader

Conversation

@bencode

@bencode bencode commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Why

Source code opened in the prose reader. There was no highlighting and no line numbers, and long lines wrapped. Folder import (#58) makes reading code a main use, so code needs a proper reader.

What changes

  • features/reader/code-reader.tsx (new): a read-only CodeMirror view.
    • Built from basicSetup with EditorState.readOnly. Line numbers, folding, Cmd/Ctrl+F search, and selection and copying all work.
    • It keeps its reading place as a flow position: line plus the share of it scrolled past. The place is restored when the file is shown, and survives content changes, for example after the agent rewrites the file.
    • It reports the first and last visible lines to the assistant (get_reader_state viewport), the same way the prose reader does.
  • Lazy loading
    • The reader is React.lazy in document-tabs.tsx, so CodeMirror stays out of the main bundle.
    • @codemirror/language-data is imported dynamically and matches a language by file name. Each language is its own chunk, loaded when a file in it is opened.
    • I checked with a production build: the main chunk has no CodeMirror code, only the dynamic import of code-reader, and python-*.js etc. are separate chunks.
  • core/files.ts: isCodePath.
    • Code extensions such as ts, js, py, go, rs, java, c/cpp, rb, sh, sql, css, vue, clj, scm, hs and the JSON/YAML/TOML/XML family, plus Makefile and Dockerfile.
    • Markdown, lab, HTML, SVG, CSV and p5 keep their own readers. .txt and .log stay prose.
  • No Source view for code: the reader is enough on its own, so code files open read-only, without the Source editor beside them and without drafts. The toolbar's Source and Save buttons do not appear.
    • TextReaderDefinition.sourceLanguage is now optional. When it is absent, TextFileReader renders the reader directly instead of TextDocumentWorkspace.
    • As a result, the agent's read_active_source and edit_active_source do not apply to code files. It reads them with read and can rewrite them with write.
    • SourceEditor is unchanged from main. The language lookup by path lives in the code reader.
  • Dependencies: @codemirror/language-data, plus @codemirror/language as a direct dependency because pnpm requires it for the import. The lockfile has a single version each of @codemirror/language, /state and /view.

Not in this PR

Dark theme, go-to-definition, a wrap toggle.

Tests

  • Integration test: opening tools/run.py shows the code in the reader. It is aria-readonly, and once Python loads, def is a highlighted token. There is no Source button.
  • Checked by hand in Chromium: highlighting and line numbers on an imported TypeScript folder, and the reading place restored after a reload.
  • pnpm check passes.

Code files opened as plain prose: no highlighting, no line numbers, long lines
wrapped. They now open in CodeMirror, read-only but selectable, with line
numbers, folding, search and highlighting for the file's language. CodeMirror
loads only when a code file is first opened, and each language only when a
file in it is. The reader keeps its place and tells the assistant which lines
are on screen, as the prose reader did. The Source view highlights any
language the same way.
The code reader is enough on its own, so code files no longer open the Source
editor beside it or keep drafts. A text reader may now leave out its source
language to open read-only; the source editor is back as it was, and the
language lookup by path lives in the code reader.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant