Skip to content

feat(nsgo): edit source mode in NSGo's code editor - #290

Merged
bketelsen merged 1 commit into
mainfrom
feat/nsgo-source-editor
Nov 25, 2026
Merged

bketelsen merged 1 commit into
mainfrom
feat/nsgo-source-editor

Conversation

@bketelsen

Copy link
Copy Markdown
Contributor

Summary

The first half of Phase 2 of plan 0011 ("Source mode on NSGo's code editor"). Source mode in the NSGo client was a multiline ui.Input. It is now NSGo's code editor (github.com/nsgo-dev/nsgo/ui/editor, v0.3.0), bound to the note's notes.Document (cmd/frostnotes-nsgo/source.go).

  • The undo rule (ADR-0022; ADR-0049, decision 3). Typing reaches the document as the user's edits, through Document.Local. NSGo's editor records any new Value as an undo step, and it cannot keep an edit out of its history or clear the history. So any other change of the document's text (a merge, another writer's change, a reload, a load after a failed one) never goes into the live editor. Instead the editor starts again (core.Dynamic on a start counter, DefaultValue) on the new text. Its selections, folds and first line in view are mapped through notes.Diff(old, new) with ChangeSet.MapPos and set again with Ref.SetSelections, SetFolds and SetScrollOffset once the new editor is laid out. The user's undo history ends there. If typing reaches the old editor before it restarts, that typing is mapped over the other writer's change (ChangeSet.Map). The editor also restarts when the note becomes editable or read-only. Tests: TestUndoNeverTakesBackAnotherWritersChange (another writer's change to a saved note, then a notesd merge of an unsaved edit; Ctrl+Z leaves both and undoes only typing done after them) and TestAnotherWritersChangeKeepsTheCaretAndTheView. When the remote change was instead put into the editor with Ref.SetValue, the first of these tests failed; it was run once that way.
  • Obsidian highlighting: an app Highlighter for the lines in view. It runs off the UI goroutine and wraps markup.Highlight, a UI-free port of the MyGo client's editor/syntax.go overlay that also lexes the Markdown MyGo's own lexer used to provide. It covers frontmatter (fences, keys, values), headings, emphasis, strong, strike, code spans, fenced and indented code, Markdown links, URLs and autolinks, wikilinks, embeds and images, tags, highlights, %% comments across lines, callouts, footnote references and definitions, inline and $$ block math, block IDs, quote and list marks, and rules. What is open at a line (frontmatter, a fence, indented code, math, a comment) is read from the lines before it. The colors come from NSGo's palette by kind. NSGo v0.3.0 maps an app's token kinds by name (KindNamed) and has no bold or italic kind for them, so the text of emphasis and strong keeps the text color and only its marks are dimmed.
  • Folds by headings, the frontmatter and indentation (markup.Folds, an O(n) port of MyGo's FoldEnd; a # inside a fence is not a heading), through FoldRanges. The other settings are the MyGo client's defaults: no line numbers, indentation guides, tab size 4, indenting with tabs, paired brackets and soft wrap. They are copied in the client because prefs imports system, which the web build cannot build, and TestSourceTakesTheMyGoDefaults checks they still equal prefs.Default(). The settings UI is Phase 7.
  • Readable line length: at most 700 wide, in the middle of the pane. The kit has no token for a reading width (readableWidth). TestNoLiteralLooks passes, and no other look is written as a number.
  • The typing gate on NSGo: make nsgo-bench runs BenchmarkTypingInALongNote in cmd/frostnotes-nsgo/bench_test.go. It uses the same 10,000-line note and the same limits as desktop-bench (16 ms, or 25 ms with CI=true, ADR-0043), in source mode at the note's end and in live preview on its first line, headless, against a real notesd. A keystroke is one step of nsgotest typing one character: the key and the edit reach the focused field, the client does its work (the editor inserts, the document takes the edit, the folds are worked out again, or in live preview the lines are scanned again), and the host runs what was posted and commits the views that changed to its model of the window. No pixels are drawn, so GTK's layout and painting are not included, and highlighting runs off the UI goroutine. The run then saves the note and checks that the file on disk holds every keystroke. On this host (AMD Ryzen AI Max+ 395): source 8.3 ms, live 4.8–5.0 ms. make desktop-bench on the same host: MyGo source 2.0 ms, live 8.4 ms. desktop-bench stays, and CI does not run nsgo-bench yet: wiring it into .github/workflows/ is L2 and is left for a later PR.
  • Keys before the editor (TestAppKeysAroundTheEditor, passes headless and in GTK). For a key, the newest binding runs, and the editor binds its keys when it gets the keyboard. A binding made around the editor before that gets mod+V, which the editor does not bind, but does not get Enter or Backspace, which the editor handles. A binding made after the editor has the keyboard (in a Show on its focus, placed after the editor) gets all three, and the editor does nothing with them. Smart lists in 2b can bind that way and call Ref.Run(editor.Newline) / DeleteBackward when they have nothing to do. Paste would then have to read the clipboard itself; the editor's own paste goes through its field's OnPaste, which is internal.

Out of scope (2b): completions, find and replace, formatting commands, smart lists and auto-paired marks, following links, the inline title, multiple-cursor commands. Live preview is unchanged.

Other changes: the tests read source mode's text from the editor's rows (sourceText), because the editor's field keeps no value. On a target, line breaks in source mode are typed as Enter keys (replaceSource). Committing them through an input method turned them into spaces in GTK, and a line break inside typed text left the previous line in the editor's field, so it was typed a second time. The web build now links chroma's lexers through nsgo/base/editor, so go-sources.json was regenerated. docs/design/app.md (NSGo section and live suite) and AGENTS.md were updated. Plan 0011's status was not edited.

Risk tier

Risk tier: 3 — Makefile matches .github/risk-paths.json (L2, governance). I matched all 24 changed files with a script against the schema-2 globs, expanding braces and **; the other 23 files are at the default level, L3. The Makefile change only adds a new target, nsgo-bench (a go test -tags integration -run '^$' -bench … in cmd/frostnotes-nsgo, with DISPLAY and WAYLAND_DISPLAY unset as in nsgo-check). It changes no existing target and adds nothing to verify, check, ci or bump. internal/repocheck accepts the -run filter because the run carries the integration tag, and make check passes.

Docs housekeeping

  • Cross-links added in both directions (ADR ↔ design ↔ spec ↔ plan): docs/design/app.md links plan 0011, ADR-0022, ADR-0043 and ADR-0049

Verification

  • make check green (host): exit 0 on the final tree
  • make nsgo-live green (Xvfb, GDK_BACKEND=x11 from the Makefile): exit 0, 35 passed, 0 failed, 0 skipped, on the final tree
  • make nsgo-bench: exit 0 (source 8.38 ms, live 4.99 ms per keystroke)
  • make flatpak-sources run, for the change to cmd/frostnotes-nsgo/go.mod and go.sum
  • make nsgo-pictures: 9 PNGs changed in pixels and are committed (note-source, dark-note-source, the 4 dialog pictures, narrow-note, dark-narrow-note, narrow-menu: each shows source mode). A small Go program compared each one with main pixel by pixel, and every difference is inside the editor's area. The other 16 came out byte-identical. I looked at note-source, dark-note-source and narrow-note.
  • Revert test: TestUndoNeverTakesBackAnotherWritersChange fails when the remote change is put into the editor with Ref.SetValue instead of restarting the editor

Source mode in the NSGo client is NSGo's code editor (nsgo/ui/editor
v0.3.0) on the note's document, in place of a multiline field: the first
half of plan 0011's Phase 2.

- The undo rule (ADR-0022; ADR-0049, decision 3): typing reaches the
  document through Document.Local. Any other change of its text (a
  merge, another writer's change, a reload) never enters the editor as a
  new value, which NSGo records as an undo step: the editor starts again
  on the new text with its carets, folds and first line in view mapped
  through the change, so Ctrl+Z never takes back another writer's change.
- Obsidian's highlighting for the lines in view: markup.Highlight, a
  UI-free port of the MyGo client's overlay, in NSGo's palette.
- Folds by headings, the frontmatter and indentation (markup.Folds), no
  line numbers, indentation guides, tabs of 4 with hard tabs, paired
  brackets, soft wrap and a readable line length of 700: the MyGo
  client's defaults.
- make nsgo-bench, the typing gate on NSGo, in source mode and live
  preview, with desktop-bench's note and limits. CI does not run it yet.
- A test records which keys an app binding takes before the editor.
@github-actions github-actions Bot added level:L2 Read the diff risk:high Touches a path in .github/risk-paths.json labels Nov 25, 2026
@bketelsen
bketelsen merged commit 95495bd into main Nov 25, 2026
15 checks passed
@bketelsen
bketelsen deleted the feat/nsgo-source-editor branch November 25, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L2 Read the diff risk:high Touches a path in .github/risk-paths.json

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants