Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cc05f2f
Add draft spec: collapsing headings
askaroralkhan Nov 21, 2026
fc02540
Clarify collapse controls, editing and content invariants
Daniil-Chegodayev Nov 24, 2026
d1e4650
Resolve full collapsing-headings specification review
Daniil-Chegodayev Nov 24, 2026
9ee26a9
Spec: apply round 2 findings to rules 7 and 9
askaroralkhan Nov 24, 2026
bb34d61
Simplify Daniil rules for round 2 and record review notes
Daniil-Chegodayev Nov 24, 2026
6248335
Trim examples, add test layers and new example for Server update
zhanapan Nov 26, 2026
8d47bd5
Add submission.md for the draft submission
askaroralkhan Nov 28, 2026
c755d10
submission: build check runs ./bt build and the Server
askaroralkhan Nov 28, 2026
13937fc
Spec: fold source sections with CodeMirror; round 3 findings 1-4
askaroralkhan Nov 28, 2026
7a25521
Spec: document why a cursor in a collapsed section opens it
askaroralkhan Nov 28, 2026
30ef1ff
Spec: open a restored section that still hides the cursor
askaroralkhan Nov 28, 2026
0b85d60
Spec: move tests out of the examples, keep 10 main examples
zhanapan Nov 30, 2026
0833262
submission: build check and review for Zhan
zhanapan Dec 1, 2026
8b9e51e
Revise Daniil rules for view-only disclosures and accessibility
Daniil-Chegodayev Dec 1, 2026
59cabf5
Spec: name required tests, show fold placeholders, open doc-block sec…
askaroralkhan Dec 1, 2026
1e53829
Spec: apply review round 4
askaroralkhan Dec 1, 2026
1799c08
submission: round 4 review for Askar
askaroralkhan Dec 1, 2026
ef09677
Update Daniil build check after installing Rust toolchain
Daniil-Chegodayev Dec 1, 2026
2b4901f
Verify complete Windows build and Server startup for Daniil
Daniil-Chegodayev Dec 1, 2026
896cc68
Merge Daniil's verified build check
askaroralkhan Dec 1, 2026
fc58624
submission: show the hidden text in Zhan's build check
askaroralkhan Dec 1, 2026
cd226cd
Spec: apply review round 5
askaroralkhan Dec 1, 2026
2aec608
submission: round 5 review for Askar
askaroralkhan Dec 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions client/src/collapsing-headings-spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
The CodeChat Editor, a word processor for software developers.<br>Copyright (C)
2024-2026 Bryan A. Jones.

This file is part of the CodeChat Editor.

The CodeChat Editor is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.

The CodeChat Editor is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with the
CodeChat Editor. If not, see
[http://www.gnu.org/licenses](http://www.gnu.org/licenses).

Collapsing headings spec
========================

Each heading in a document gets a disclosure marker (▾) before its text that
collapses its section: all content after the heading and before the next heading
of the same or a higher level, including subheadings, doc blocks and code
blocks. The heading itself stays visible. The marker then changes to ▸, which
shows the section again. This lets a reader fold away the parts of a long
document or source file they aren't working on.

Collapsing changes only what the Client shows; nothing about it is saved (rule
6).

Definitions
-----------

* **Heading:** an `<h1>`–`<h6>` element that, ignoring generated elements (rule
1), is a direct child of the contents of a Markdown document
(`.CodeChat-doc-contents` in [CodeChatEditor.mts](CodeChatEditor.mts)) or of a
doc block (the `.CodeChat-doc-contents` of a `DocBlockWidget` in
[CodeMirror-integration.mts](CodeMirror-integration.mts)). A heading inside a
list, block quote or table isn't one. A heading rendered from a Rust comment
such as `/// # Examples` qualifies on the same basis.
* **Doc block:** documentation that the Server renders as editable HTML in a
source file. Code between doc blocks stays in CodeMirror; heading-like text
in that code isn't a heading.
* **Level:** the heading's number: `<h1>` is level 1, the highest.
* **Document order:** in Markdown, the order of the document's contents; in a
source file, the order of code and doc blocks, including the order of
elements inside each doc block.
* **Section:** everything after a heading in document order, up to but not
including the next heading at the same or a higher level (a lower or equal
number), or to the end of the document. The heading itself isn't part of its
section. A section may be empty.
* **Heading key:** the tuple of a heading's level, its text, and its occurrence
among headings with that same level and text in document order. The text is
the heading's `textContent` as the Client would send it to the Server (no
generated elements, math not yet rendered), with each run of whitespace,
including non-breaking spaces, collapsed to one space. This key only restores
collapsed state after content is replaced (rules 8 and 9); it isn't saved.
* **Cursor:** TinyMCE's, while it has the text cursor; otherwise, in a source
file, the head of CodeMirror's main selection. Focusing a summary neither
moves the cursor nor reports a different one to the IDE.

Rules
-----

1. **Disclosure:** each heading is followed by a view-only `<details>` element
which holds the rest of the heading's section in the same Markdown document
or doc block, including any subheadings and their own `<details>`. Its
`<summary>` holds only the marker, shown before the heading text: ▾ when
open, ▸ when closed. These are the *generated elements*. The heading itself
stays outside them, so it remains editable and remains a heading for screen
readers. In a source file, the same summary also controls the rest of the
section (rule 5).
Visible summaries, including those in inactive doc blocks, can be reached
and left with the keyboard, but can't hold the text cursor; focusing one
leaves TinyMCE in its current block. A click, Enter or Space on a summary
toggles it without editing the document, including in a read-only Client.
Its accessible name identifies the heading, and its state is exposed through
the native disclosure semantics.
2. **Collapse:** closing the disclosure hides the heading's section. The heading
and its summary stay visible. Before closing, if the cursor or either end of
its selection is in the section being hidden, clear the selection and move
the cursor to the end of the heading, without taking focus from the
activating summary. In a source file, if TinyMCE isn't in the heading's doc
block, CodeMirror's cursor moves to the start of that doc block instead.
3. **Expand:** activating ▸ shows the section again. A subheading that was
collapsed before stays collapsed and keeps its section hidden, including code
which both sections contain.
4. **Markdown documents:** the whole section is in the heading's `<details>`.
5. **Source files:** a section may start or end inside a doc block; hiding part
of a doc block leaves its other content visible. Code in a collapsed section
is hidden, not deleted. From the section's first code, its code and whole doc
blocks are folded with CodeMirror's own folding, so each outermost collapsed
section shows as one fold placeholder (…), or none if it has no code.
Clicking a placeholder expands its section and no other. Removing the fold
with the fold gutter, Unfold Code or Unfold All expands its section too.
6. **Never saved:** collapsing and expanding change only the Client's view; they
do not mark the document as edited, send document content or add undo
entries. The generated elements, their state and any other collapse-only
markup are absent from content sent to the Server and from copied content.
Original markup and attributes, including heading ids and any author-written
disclosure elements, are preserved. Cursor paths and offsets describe the
document without the generated elements. Moving the cursor under rule 2 may
send a normal cursor update.
7. **Cursor in hidden content:** the cursor never stays in hidden content. When
it moves there, every collapsed section hiding it opens. This is a design
decision; the alternative was to keep the section collapsed and move the
cursor past the fold. Opening it matches how CodeMirror's folding already
treats code, so collapsed sections and code folds behave the same across the
Client's UI. This covers the cursor the IDE sends, go-to-line, a search's
current match (including TinyMCE's search), and a Client edit or Server
update that leaves the cursor in a collapsed section. Following a link or
cross-reference opens every section hiding its target. If the browser opens a
generated `<details>`, such as for its own find, the section expands. A line
in a partly hidden doc block points to that doc block, which is visible, so
it opens nothing. A scroll position alone never opens a section. Two of these
cases need fixes in the Client first; this spec assumes they will be made
(see the TODOs in Implementation).
8. **Edits:** outside a summary, the generated elements don't change what any
key or edit does. Before a Client edit that would change hidden content,
expand every section needed to reveal it, then apply the edit. This includes
Backspace or Delete merging across a collapsed section and replacing or
deleting a selection that crosses one. Undo and redo are edits too.
A section follows its heading through Client edits. Editing the heading's
text preserves its collapsed state; changing its level recomputes its end.
Deleting a heading or turning it into ordinary text removes its collapsed
state, though other collapsed headings may still hide its former content. A
new heading starts expanded; if a heading is split, its first part keeps the
state. Undo and redo replace content, so they restore collapsed state by
heading key, as in rule 9.
9. **Lifetime:** collapsed state lasts only while the file is open in the
Client. On each content update from the Server, retain the keys of
collapsed headings before applying the update and restore collapsed state
to the headings with matching keys afterwards, before resolving cursor and
scroll targets (rule 7). This covers edits made in the IDE and the
re-translations the Server sends on its own, whether they change part of the
content or replace all of it. Headings with no matching key start expanded,
though a collapsed ancestor may still hide them. Identical headings may
therefore swap state, and a heading which is renamed, changes level or takes
its text from a changed cross-reference may lose it.

Out of scope
------------

* Remembering collapsed sections after the file is closed or the Client reloads.
* New commands to collapse or expand every section at once.
* Changing CodeMirror's folding by syntax, which keeps working alongside the
folds that collapsed sections add.
* Making the browser's own find reach folded content.

Examples
--------

`H1 A` is a level-1 heading with text `A`; `p1` is a paragraph; `code1` is a
code block. Examples whose Document cell starts with "Source file:" describe a
source file; every other example is a Markdown document. Each example starts
with every section shown unless its Document cell specifies otherwise. Results
describe visible content and any observable save or synchronization behavior;
`…` is CodeMirror's fold placeholder (rule 5).

These examples show the main behavior. Unusual cases, such as the cursor move in
rule 2, saves and dirty state in source files, new headings, undo and redo, and
TinyMCE's search, aren't listed here. The implementation must add tests for
them, and for the examples, to the Client tests in
[CodeChatEditor-test.mts](CodeChatEditor-test.mts) or, where a test needs the
Server, to the browser tests in [overall](../../server/tests/overall/).

| # | Document | Action | Expected result |
| -- | -- | -- | -- |
| 1 | `H1 A`, `p1`, `H2 B`, `p2`, `H1 C`, `p3` | Collapse A | `H1 A`, `H1 C`, `p3` |
| 2 | Same as 1 | Collapse B, collapse A, expand A | `H1 A`, `p1`, `H2 B`, `H1 C`, `p3` (B stays collapsed) |
| 3 | Source file: doc block [`H1 A`, `p1`], `code1`, doc block [`H2 B`], `code2`, doc block [`H1 C`] | Collapse A | Doc block [`H1 A`], `…`, doc block [`H1 C`] |
| 4 | Source file: doc block [`H2 A`, `p1`], `code1`, doc block [`p2`, `H2 B`, `p3`] | Collapse A | Doc block [`H2 A`], `…`, doc block [`H2 B`, `p3`] |
| 5 | Same as 1, initially clean | Collapse A, then expand A without editing | No document content sent; no dirty state |
| 6 | Same as 1 | Collapse A, edit p3, wait for autosave | Outgoing document HTML and the saved file equal those from the same edit with A shown; p1, B and p2 remain in the document |
| 7 | Same as 3, A collapsed | The IDE moves the cursor into `code1` | A opens; the cursor is in `code1` |
| 8 | Same as 1, A collapsed | Backspace at the start of C in the Client | A opens before C merges into p2; result equals the same edit with A shown |
| 9 | Same as 1, A collapsed, cursor in A | The IDE inserts a paragraph above A | New paragraph, `H1 A`, `H1 C`, `p3`; A remains collapsed |
| 10 | Same as 1, A collapsed | Edit p3 so that the Server's reply to the autosave changes the content (for example, a line it re-wraps), and wait for that update | The update replaces the content; A remains collapsed and p3 shows the Server's version |
| 11 | Source file: doc block [`H1 A`], doc block [`p1`], `code1`, doc block [`H1 B`], `code2`, doc block [`H1 C`]; A and B collapsed | Click the placeholder after B | Doc block [`H1 A`], `…`, doc block [`H1 B`], `code2`, doc block [`H1 C`] |

Implementation
--------------

These constraints come from reading the Client and from running CodeMirror
(`@codemirror/language` 6.13.1) and TinyMCE 8.10.1.

* **`<details>` in TinyMCE** ([CodeChatEditor.mts](CodeChatEditor.mts) for
Markdown documents; doc blocks behave the same):
* TinyMCE cancels every click inside a `<details>`, and treats Enter or Space
in a `<summary>` as typing. The Client must handle these events on its own
summaries before TinyMCE does, and set `open` itself.
* TinyMCE gives the edges of a `<details>`, even an open one, their own
behavior: Backspace and Delete don't cross them, Enter in an empty last
paragraph leaves the `<details>`, and the down arrow at the end of the
document's last one adds a paragraph. To avoid this (rule 8), remove the
generated elements first, keeping the content, and add them again
afterwards. TinyMCE also outlines a `<details>` that holds the cursor.
* Undo levels, raw saves, `innerHTML` reads and copied selections include the
generated elements and their state, so remove them from each (rule 6). For
example, an arrow key after a toggle adds an undo level and marks the
document as edited.
* TinyMCE's `searchreplace` marks matches in a closed `<details>` without
opening it, and selects one only when its dialog closes. Open the section
holding the current match (rule 7); replacing is an edit (rule 8).
* **Folds in source files**
([CodeMirror-integration.mts](CodeMirror-integration.mts)): use the folding in
`@codemirror/language`, which `basicSetup` already includes; see its
[reference](https://codemirror.net/docs/ref/#language.foldEffect).
* A fold that starts at a doc block leaves that block visible and draws no
placeholder. So start a section's fold at its first code, and hide the doc
blocks before that code like parts of doc blocks.
* Compute the folds from the set of collapsed headings, folding only the
outermost collapsed sections. A fold mapped through a diff can go stale, so
after a toggle or an update, remove all section folds with `unfoldEffect`,
then add the recomputed ones with `foldEffect`, in document order.
* The default placeholder opens the first fold in its line block, and adjacent
sections share a line block. Give each placeholder, including those of folds
by syntax, its own fold, using `codeFolding`'s `preparePlaceholder` and
`placeholderDOM` (rule 5).
* `foldState` opens a fold when the selection's head moves strictly inside it,
so not at its first position, at the end of a file whose last section is
collapsed, or in a doc block hidden before it. When `scrollToLine`,
go-to-line or search puts the cursor there, the Client must open the section
(rule 7). Arrow keys use a fold's first position as the end of the visible
doc block before it (`selectDocBlockEdge`); that doesn't open it.
* `foldState` opens a touched fold for deletions only. Open it for every other
Client edit too, including typing, paste, undo, redo and Replace All
(rule 8). Updates from the Server are exempt.
* **Hidden parts of doc blocks:** change the shown block in place. A redraw
isn't reliable (`DocBlockWidget.eq` ignores collapsed state, and `updateDOM`
skips the block edited in TinyMCE) and loses the summary's focus. Add the
generated elements again wherever the Client rebuilds a block's contents.
* **Updates from the Server:** after each one, restore collapsed state by
heading key (rule 9), leaving out every section that would hide the update's
cursor or, if it has none, the Client's (rule 7).
* **Links and the IDE's cursor:** TODO: apply rule 7 to the IDE's cursor in
Markdown documents once the Client follows it there (`scrollToLine` in
[CodeChatEditor.mts](CodeChatEditor.mts) is a `TODO`). TODO: once same-file
links work in source files (the ignored tests in
[overall_5.rs](../../server/tests/overall/overall_5.rs)), find a link's target
in the doc-block contents held in CodeMirror's state, since `followLink`'s
`document.querySelector` can't see folded content, and open the sections
hiding it. In Markdown documents, `followLink` must open them too.
Loading