Repository navigation
Code lines
How TeXmacs Upsilon opens and edits source files of hundreds of thousands of lines: the tag code-lines, a block of lines with its own bridge and box. This page sums up the goals, what the block does, the timings, the design decisions and what could come next.
- Pull request: #798 (to
upsilon_texmacs). Tracking issue: #797. Origin: #710 (a 19.8 MBparser.cpassed 3 GB of memory while it was typeset). - Notes of the developers, on the branch of the PR and then on
upsilon_texmacs:src/docs/code-lines.md, next tosrc/docs/code-features.md(Code features).
- TeXmacs as a real editor of source files: a large file opens at once and a key costs the same at line 10 and at line 150000.
- No loss of the features of programs: tree-sitter colours, the language servers (diagnostics, semantic colours, inlay hints, folds, edits), markup in a line, folds; the block is one more client of the line state of the core, not a third mechanism.
- Files saved as they were loaded: tabs, control characters and line ends kept byte for byte.
- Paper mode and printing still work: the page breaker gets the same penalties as for an ordinary document of lines.
(document (code-lines (document "line 0" "line 1" ...)))
A source file (source_document_to_tree) or a text file (verbatim_document_to_tree) is loaded as a block of lines unless the preference code lines is off (TEXMACS_CODE_LINES=0 or 1 overrides it, for tests). A plain line is a row of a monospaced font on a grid of cells: it has no line item, and outside paper mode no paragraph of its own.
code_lines_root (Scheme: code-lines-root) is the one definition of "line l, offset o" of a buffer: the document inside the block, or the body when there is no block. The language servers, tree-sitter's region, go-to-line and the tests address lines through it.
| Papyrus mode | the block is one box (code_box), which draws only the visible lines from the live document; a sparse list holds the special lines (wrapped, with markup, folded); a click or a redraw finds its line by a binary search |
| Paper mode | a line is a paragraph of rows made from templates of paragraphs of 1 to 5 rows, so that pages break as in a document; rows are made again only for the lines which changed |
| Records of the lines | kept in place by the notifications of the editor (insert, remove, split, join, assign); TEXMACS_TYPESET_CACHES=check makes them all again and counts the differences (suite code-lines-check) |
| Colours | languages with states at the starts of lines (prog languages, C++ without tree-sitter) use a lexer of the block which keeps those states; the others, tree-sitter included, are asked for the colours of the line when it is drawn |
| Soft line breaks | long lines wrap at the width of the page, after a space when there is one |
| Markup in a line | strong, em, colours, math, images...: the line is typeset by the concater and put on the grid; option code-tall-objects = shrink scales a tall object to the height of a row (clicks, cursor and selections scaled too) |
| Language servers | semantic colours and inlay hints through typeset_layered_string, folds through code_line_fold_state (hidden lines take no room, the line before ends with "..."; not when printing) |
| Tabs and control characters | kept as <#9> and <#x>; tabs stop every prog-tab-width characters and are always shown; Format > Source code > Show invisible characters also shows spaces, line ends and control characters |
| Export | HTML, LaTeX (also for verbatim content, a source or text file), Markdown, ODT/DOCX and plain text export the lines as the paragraphs of a document, as before the block |
A C file of 200000 lines, the cursor at line 150000, s7 build (macOS):
| paper | papyrus | |
|---|---|---|
| a key, with the block | 0.8–0.9 s | 71–78 ms |
| Return, with the block | 1.8–2.0 s | 78 ms |
| a key, without the block | 1.3–5 s | |
| Return, without the block | 4.4–4.8 s |
The first prototype, on Linux (Qt 5 offscreen, Guile 1.8), with a C file of 680001 lines (26.8 MB) opened as text, in papyrus mode:
| without the block | with the block | |
|---|---|---|
| first typesetting | 57 s | 9 ms |
| peak memory | 3.2 GB | 441 MB (the tree itself) |
| a cursor movement | 130 ms (+2.2 GB) | 3 ms |
| Return | 1.5 s | 413 ms |
What is left: in paper mode, mostly the linear passes of the page breaker when the number of lines changes; in papyrus mode, mostly tree-sitter bringing its region up to date at each key.
- A tag of its own rather than a faster document. The lines are strings in a document, as before, so that every tool which edits a document of lines (the editor, undo, search, the converters) works unchanged; only the typesetting differs. The tag has an inner border: the cursor is always in a line, never before or after the block.
-
Nothing per line unless needed. A short plain line has no record and no box; a box is made for a line only when the cursor, a selection or a path goes into it (
subbox). The generic code that walks all the children of a box does not visit the lines (subnris 0). -
One mechanism with the features of programs. The block uses the line state of the core (layers, folds) and
code_lines_root, as described in Code features; tree-sitter and the language servers needed only to ask for the lines throughcode_lines_root. - Paper mode reuses the stacker's rules. Rows are made as those of real paragraphs, so that page breaking, penalties and printing are those of a document of lines.
-
A primitive as the others. Wherever TeXmacs handles its primitives case by case,
code-linesis listed with the tags of the same kind (datoms,dbox..., whose body is a child): the tables of the converters, the verbatim content of LaTeX (tmtex-tt),is_multi_paragraph(a block inserted in a paragraph splits it, as awithof a document does), the source view and the serializer of the grammars. The focus skips the block as it skips a document. A new place which lists the primitives should list it too (src/docs/code-lines.md). -
Checked caches. Every kept record, row and colour state can be compared with what is made again from scratch (
TEXMACS_TYPESET_CACHES=check), and the suitecode-linesruns both ways.
-
The cursor in documents taller than 2^31 units (about 1300 A4 pages):
box_rep::distancecut distances to 32 bits, andgo_up/go_downtook the ends of the document at 2^30 units. -
Lists without deep recursion: the functions of
list.cppare loops (a list of 10^5 cells overflowed the stack). - Tree-sitter's region was the body of the buffer, so that the whole block was one line and was flattened at each key.
The PR is audited by fresh agents until an audit finds nothing; each finding so far was fixed with a check that fails without it:
-
Cursor and focus: the cursor started before the block (what was typed went outside it) and the suite
lspfailed with clangd; the Focus menu of source files. -
Export: HTML of a block was empty, LaTeX gave an unknown
\codelines, and a source or text file gave an emptyalltt. - Colour states: lines inserted or removed at the level of the document (edits of a language server).
- Paper mode: a block of one line with markup was not typeset; a line with markup first or last was typeset twice.
- The box: lines with markup left blank by an interrupted repaint; clicks beyond a wrapped row; extents.
- Primitives: a block inserted in a paragraph went into the line.
- Paper mode in proportion to the change: a page breaker that redoes only the pages around an edit, so that Return in a large file in paper mode costs as much as in papyrus.
-
Tree-sitter's region in proportion to the change (
syntax_region.cpp): the main cost of a key in papyrus mode. - Memory: the tree of the lines is now most of what a large file costs; a more compact representation of plain lines (one string per block, lines as offsets) would cut it further.
-
Lines with markup as children of the box: animations and other boxes that the generic traversals visit (
relocate, the repaint log) would then work inside a line. -
Settings in the interface: the preference
code linesin the Preferences dialog, and the Format > Source code menu also for text files. - A colour-state test with C++: where tree-sitter is built, C++ colours come from tree-sitter, so the suite checks the lexer of the block with Scala.
- Very long lines: the positions of the characters are computed for the whole line for each row; wrapped minified files would gain from computing them per row.