GeezerVison is showing github.com — refactor: live-preview の共通 helper 集約 + collectCodeRanges cache 化 (#209 ②) by ymnao · Pull Request #278 · ymnao/scripta
Conversation
概要
live-preview の 13 デコレーションで共通していた「visibleRanges × tree.iterate」パターンと IME composing check を helper に集約、collectCodeRanges を StateField cache 化する。#209 backlog の項目 ② 対応。
関連 Issue
refs #209 (項目 ②)
移行 Stage
該当なし (内部 refactor、Stage 6 相当の運用補強)。
変更内容
新規 helper (src/components/editor/live-preview/plugin-utils.ts)
iterateVisibleSyntax(view, enter) — for-of (visibleRanges) → tree.iterate({from, to, enter}) を集約。第 2 引数 ctx: { tree, from, to } は enter 呼び出し中のみ有効な shared object (loop 外で 1 個 hoist、node ごとの allocation を回避)
handleComposingUpdate(update, target) — if (update.view.composing) { if (docChanged) map; return; } を集約。atomicRanges を持つ target (lists.ts) にも対応
13 デコレーションに適用
blockquotes / code-block-copy / code-blocks / emphasis / headings / horizontal-rules / images / link-cards / links / lists / strikethrough / wikilinks の buildDecorations と update() を helper 呼び出しに置換。mermaid は StateField ベースで composing check を持たないので対象外。
codeRangesField StateField cache (math.ts)
- ドキュメント全体の code ranges を tree 変更時のみ再計算するキャッシュ (selection / focus 変化では再計算しない)
getCodeRanges(state) helper で field 読み取り + fallback を封じ込め、buildMathDecorations / wikilinks / link-cards の 3 重コピーを 1 呼び出しに集約
/simplify 適用 (別 commit)
getCodeRanges helper 集約 (3 重コピー → 1 呼び出し)
code-block-copy.ts の buildCopyDecorations に iterateVisibleSyntax 適用漏れを修正
iterateVisibleSyntax の ctx object を hoist して mutate (per-node allocation 削減)
codeRangesField.update guard を tree identity 比較のみに短縮 (lazy parse 中の無駄計算を回避)
plugin-utils.test.ts の dummy sanity test を削除
テスト
plugin-utils.test.ts を新規追加、helper 単体テスト 8 件:
iterateVisibleSyntax: multi visibleRanges 対応 / enter = false で子孫 skip / ctx 伝播
handleComposingUpdate: composing 分岐 / docChanged 有無 / atomicRanges optional map
動作確認
-
biome check src/components/editor/live-preview/: clean
-
tsc (web / node / e2e): 全 pass
-
vitest: 2233 pass, 2 skipped, 0 fail
-
electron-vite build: 成功
- 実 Electron e2e (
pnpm test:e2e:electron): CI で確認
スクリーンショット
UI 変更なし。
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…項目 ②)
- src/components/editor/live-preview/plugin-utils.ts を新規追加。iterateVisibleSyntax
(visibleRanges × tree.iterate パターン) と handleComposingUpdate (IME composing
中の decorations map + 早期 return) を集約する
- 13 デコレーションを両 helper で置換 (blockquotes, code-block-copy, code-blocks,
emphasis, headings, horizontal-rules, images, link-cards, links, lists,
strikethrough, wikilinks の buildDecorations / update()。mermaid は
StateField ベースで composing check を持たないので対象外)
- math.ts に codeRangesField (docLength 全体を StateField cache 化) を追加。
buildMathDecorations / wikilinks / link-cards から重複していた collectCodeRanges
呼び出しを field 参照に統一し、selection/focus のみの変化では再計算しない
- handleComposingUpdate は lists.ts の atomicRanges にも対応するよう optional
property を受け取る (target: { decorations; atomicRanges?; })
- plugin-utils.test.ts で iterateVisibleSyntax / handleComposingUpdate の単体テスト
を追加 (multi visibleRanges / enter=false の子ノード skip / composing 分岐 /
atomicRanges optional map)
refs #209 (項目 ②)
…ctx hoist / 他 3 件
- math.ts: getCodeRanges(state) helper を追加、wikilinks / link-cards /
buildMathDecorations で 3 重コピーされていた
`state.field(codeRangesField, false) ?? fallback` を 1 呼び出しに集約 (Reuse/Altitude
F1)
- code-block-copy.ts: buildCopyDecorations に残っていた for-of / tree.iterate
パターンを iterateVisibleSyntax で置換 (Reuse F2)
- plugin-utils.ts: iterateVisibleSyntax の ctx object を loop 外で 1 個 hoist し
per-range で mutate。node ごとの object literal 生成 (O(nodes) × 13 plugins) を
O(visibleRanges) に削減。ctx は enter 呼び出し中のみ有効という制約を docstring
に明記 (Efficiency F3)
- math.ts: codeRangesField.update の guard を tree change の identity 比較のみに
短縮。!tr.docChanged 半分は tree 同一なら redundant なので削除 (lazy parse
中に docChanged だけ立つケースの無駄計算を回避、Efficiency F4)
- plugin-utils.test.ts: EditorState import の dummy 参照 sanity test を削除
(createTestState 経由で既に import は使われているので sanity は不要、
Simplification F5)
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Jul 5, 2026
ymnao
added a commit
that referenced
this pull request
Jul 5, 2026
- package.json の version を 0.6.0 → 0.7.0 に bump
- CHANGELOG.md に v0.7.0 エントリを追加 (#243 〜 #280)
- Fixed: FileTree scroll regression (#276)、electron e2e launch teardown timeout 拡大 (#247)
- Internal: #209 3 項目 (drift-lock 系 refactor #278/#279/#280)、settings setter 集約 (#273 → #277)、search/workspace/backlink 系 helper 抽出 15 件
- Dependencies: electron 43.0.0 / p-limit 7.3.0 major bump (#275)、Dependabot 7 件 combine (#274)、pnpm-workspace.yaml overrides で @codemirror dual copy を恒久防止
Co-authored-by: Claude Opus 4.7 <[email protected]>
This was referenced Jul 6, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|