dsh-remote-vs 0.24.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/README.en.md +50 -16
  2. package/README.md +40 -15
  3. package/THIRD-PARTY-NOTICES.md +188 -0
  4. package/lib/client.js +6512 -77887
  5. package/lib/index.js +1461 -1024
  6. package/lib/p2-router-fs.js +1 -1
  7. package/lib/types/client/CodeEditor.d.ts +25 -0
  8. package/lib/types/client/EditorPane.d.ts +16 -5
  9. package/lib/types/client/FloatingPlaceholder.d.ts +16 -5
  10. package/lib/types/client/RemoteEditorView.d.ts +13 -10
  11. package/lib/types/client/RemoteExplorer.d.ts +53 -0
  12. package/lib/types/client/RemoteRoot.d.ts +5 -5
  13. package/lib/types/client/TerminalSnippets.d.ts +36 -0
  14. package/lib/types/client/TerminalView.d.ts +1 -1
  15. package/lib/types/client/XTerm.d.ts +33 -0
  16. package/lib/types/client/api.d.ts +37 -2
  17. package/lib/types/client/changes/review-state.d.ts +19 -0
  18. package/lib/types/client/connection.d.ts +16 -0
  19. package/lib/types/client/editor-capability.d.ts +27 -0
  20. package/lib/types/client/i18n.d.ts +1 -1
  21. package/lib/types/client/request-gate.d.ts +110 -0
  22. package/lib/types/client/scm/DiffView.d.ts +45 -7
  23. package/lib/types/client/scm/actions.d.ts +31 -3
  24. package/lib/types/client/store.d.ts +112 -4
  25. package/lib/types/client/terminal-input.d.ts +23 -0
  26. package/lib/types/client/terminal-snippets.d.ts +86 -0
  27. package/lib/types/client/vsc/FindWidget.d.ts +6 -0
  28. package/lib/types/client/vsc/breadcrumb.d.ts +30 -0
  29. package/lib/types/client/vsc/change-marks.d.ts +16 -6
  30. package/lib/types/client/vsc/editor-caret.d.ts +32 -0
  31. package/lib/types/client/vsc/editor-keymap.d.ts +35 -1
  32. package/lib/types/client/vsc/editor-langs.d.ts +9 -0
  33. package/lib/types/client/vsc/editor-settings.d.ts +55 -0
  34. package/lib/types/client/vsc/image-preview.d.ts +36 -0
  35. package/lib/types/client/vsc/kit.d.ts +13 -0
  36. package/lib/types/client/vsc/save-shortcut.d.ts +21 -0
  37. package/lib/types/client/vsc/terminal-clipboard.d.ts +15 -0
  38. package/lib/types/client/vsc/terminal-line.d.ts +25 -0
  39. package/lib/types/client/vsc/terminal-links.d.ts +62 -0
  40. package/lib/types/client/vsc/terminal-theme.d.ts +48 -0
  41. package/lib/types/client/vsc/terminal-writer.d.ts +52 -0
  42. package/lib/types/forward.d.ts +12 -0
  43. package/lib/types/fs-ops.d.ts +42 -0
  44. package/lib/types/http.d.ts +2 -0
  45. package/lib/types/keygen.d.ts +9 -0
  46. package/lib/types/monitor.d.ts +8 -0
  47. package/lib/types/notices.d.ts +16 -1
  48. package/lib/types/p2/helper-rpc.d.ts +45 -0
  49. package/lib/types/pool.d.ts +25 -0
  50. package/lib/types/routes.d.ts +2 -0
  51. package/lib/types/store.d.ts +7 -0
  52. package/lib/types/terminal.d.ts +33 -1
  53. package/lib/types/view-settings.d.ts +5 -0
  54. package/lib/{worlds-B7gEDBzC.js → worlds-RG8K5eom.js} +113 -15
  55. package/package.json +2 -1
  56. package/src/client/CodeEditor.tsx +205 -10
  57. package/src/client/DirectoryFlow.tsx +16 -0
  58. package/src/client/DoctorPanel.tsx +70 -7
  59. package/src/client/EditorPane.tsx +545 -118
  60. package/src/client/FloatingPlaceholder.tsx +34 -5
  61. package/src/client/HostsView.tsx +12 -3
  62. package/src/client/RemoteDirectoryPicker.tsx +12 -0
  63. package/src/client/RemoteEditorView.tsx +14 -11
  64. package/src/client/RemoteExplorer.tsx +929 -40
  65. package/src/client/RemoteRoot.tsx +167 -147
  66. package/src/client/SearchView.tsx +8 -0
  67. package/src/client/TerminalSnippets.tsx +432 -0
  68. package/src/client/TerminalView.tsx +50 -1
  69. package/src/client/XTerm.tsx +228 -58
  70. package/src/client/api.ts +106 -13
  71. package/src/client/auto-open-remote.ts +11 -9
  72. package/src/client/changes/ChangesDock.tsx +188 -6
  73. package/src/client/changes/changes.css +10 -3
  74. package/src/client/changes/review-state.ts +34 -0
  75. package/src/client/connection.ts +29 -0
  76. package/src/client/downloads.ts +49 -4
  77. package/src/client/editor-capability.ts +42 -0
  78. package/src/client/i18n.ts +228 -12
  79. package/src/client/index.tsx +22 -6
  80. package/src/client/request-gate.ts +221 -0
  81. package/src/client/scm/CommitPane.tsx +20 -2
  82. package/src/client/scm/DiffView.tsx +343 -117
  83. package/src/client/scm/HistoryPanel.tsx +23 -2
  84. package/src/client/scm/RepoPicker.tsx +1 -1
  85. package/src/client/scm/ScmPanel.tsx +391 -87
  86. package/src/client/scm/actions.ts +95 -12
  87. package/src/client/store.tsx +537 -22
  88. package/src/client/terminal-input.ts +44 -0
  89. package/src/client/terminal-snippets.ts +216 -0
  90. package/src/client/ui.tsx +4 -0
  91. package/src/client/vsc/FindWidget.tsx +24 -9
  92. package/src/client/vsc/breadcrumb.tsx +339 -0
  93. package/src/client/vsc/change-marks.ts +93 -44
  94. package/src/client/vsc/editor-caret.ts +30 -0
  95. package/src/client/vsc/editor-keymap.ts +59 -1
  96. package/src/client/vsc/editor-langs.ts +20 -0
  97. package/src/client/vsc/editor-settings.ts +146 -0
  98. package/src/client/vsc/editor-theme.ts +4 -5
  99. package/src/client/vsc/file-icons.tsx +17 -3
  100. package/src/client/vsc/image-preview.tsx +110 -0
  101. package/src/client/vsc/kit.tsx +46 -10
  102. package/src/client/vsc/save-shortcut.ts +47 -1
  103. package/src/client/vsc/terminal-clipboard.ts +19 -0
  104. package/src/client/vsc/terminal-line.ts +101 -0
  105. package/src/client/vsc/terminal-links.ts +166 -0
  106. package/src/client/vsc/terminal-theme.ts +97 -0
  107. package/src/client/vsc/terminal-writer.ts +112 -0
  108. package/src/client/vsc/vsc.css +1078 -181
  109. package/src/forward.ts +47 -9
  110. package/src/fs-ops.ts +124 -4
  111. package/src/git-ops.ts +20 -3
  112. package/src/http.ts +35 -4
  113. package/src/keygen.ts +55 -4
  114. package/src/monitor.ts +13 -0
  115. package/src/notices.ts +23 -1
  116. package/src/p2/helper-rpc.ts +113 -7
  117. package/src/p2/worlds.ts +14 -1
  118. package/src/pool.ts +99 -27
  119. package/src/routes.ts +212 -43
  120. package/src/sftp.ts +51 -14
  121. package/src/store.ts +10 -0
  122. package/src/terminal.ts +225 -24
  123. package/src/view-settings.ts +50 -10
  124. package/lib/types/client/Dialog.d.ts +0 -14
  125. package/src/client/Dialog.tsx +0 -76
@@ -8,13 +8,15 @@
8
8
  * already-staged diff can only be un-staged. 变更后 switches to a real
9
9
  * CodeMirror buffer so the file can be edited in place.
10
10
  *
11
- * The row count is the WHOLE file, so the split mode is windowed (see
12
- * `useRowWindow`): before that, a 3000-line file put 9000 line containers and
13
- * ~2N tokenized lines in the DOM at once, and every store publish rebuilt all
14
- * of them. Note which element scrolls: the two panes only own their HORIZONTAL
15
- * scroll (`.rvs-diff-col`), while vertical scrolling belongs to `.rvs-diff-grid`
16
- * outside them — so ONE window serves all three columns and no column can
17
- * drift out of lockstep with another.
11
+ * The row count is the WHOLE file, so every pane is windowed (see
12
+ * `useScrollWindow`): before that, a 3000-line file put 9000 line containers and
13
+ * ~2N tokenized lines in the DOM at once, and every store publish rebuilt all of
14
+ * them — the "变更前 / 变更后" panes mounted every line of one side as well. In
15
+ * the split mode note which element scrolls: the two panes only own their
16
+ * HORIZONTAL scroll (`.rvs-diff-col`), while vertical scrolling belongs to
17
+ * `.rvs-diff-grid` outside them — so ONE window serves all three columns and no
18
+ * column can drift out of lockstep with another. The whole-file panes scroll
19
+ * their own `.rvs-code` box and take the same window from the same hook.
18
20
  */
19
21
  import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react'
20
22
  import type { EditorTab } from '../store.tsx'
@@ -28,7 +30,7 @@ import { CodeEditor, type CodeEditorHandle } from '../CodeEditor.tsx'
28
30
  import { buildReviewPrompt, diffByteLength, pickReviewMode, reviewScopeOf } from '../ai-review.ts'
29
31
  import { appendToDraft, canInsertDraft, sameLocalDir } from '../composer.ts'
30
32
  import { useRemoteChatTarget } from '../use-remote-chat-target.ts'
31
- import { langFromPath, tokenizeDocument, type Token } from '../vsc/tokenize.ts'
33
+ import { langFromPath, lexState, tokenizeLine, type LangId, type Token } from '../vsc/tokenize.ts'
32
34
  import {
33
35
  IconButton,
34
36
  IconChevronDown,
@@ -44,27 +46,45 @@ import {
44
46
  type ViewMode = 'split' | 'before' | 'after'
45
47
 
46
48
  /**
47
- * Height of one diff row, in px. Must track `.rvs-dline`'s declared height
48
- * (`vsc.css`), because the window below converts scroll offsets to row indices
49
- * with it: if the CSS box is not exactly this tall the rendered slice drifts
50
- * from the spacer that stands in for the rest of the file.
49
+ * Height of one row, in px, in EVERY windowed pane.
50
+ *
51
+ * Must track the row heights in `vsc.css` — `.rvs-dline` (split) and
52
+ * `.rvs-code-line` (the whole-file panes) both render at 18px, the latter via
53
+ * its inherited line-height (no explicit `height`; it is `min-width: max-content`
54
+ * and never wraps) — because the window below
55
+ * converts scroll offsets to line indices with it: if the box is not exactly
56
+ * this tall the rendered slice drifts from the spacer that stands in for the
57
+ * rest of the file.
51
58
  */
52
59
  const ROW_HEIGHT = 18
53
60
 
54
61
  /** Rows rendered above and below the viewport, so a wheel step is already ready. */
55
62
  const OVERSCAN_ROWS = 8
56
63
 
57
- /** Rows assumed to fit before the grid has been measured (one frame at most). */
64
+ /** Rows assumed to fit before a scroller has been measured (one frame at most). */
58
65
  const FALLBACK_VISIBLE_ROWS = 30
59
66
 
60
- /** The half-open row range the diff columns paint, plus what they leave blank. */
61
- interface RowWindow {
62
- /** First rendered row index into `model.rows`. */
67
+ /**
68
+ * `.rvs-code`'s `padding-top`, in px (`vsc.css`).
69
+ *
70
+ * The whole-file panes scroll the same fixed 18px rows as the split columns, but
71
+ * their scroller is `.rvs-code`, whose content box starts 6px down: that offset
72
+ * has to come out of `scrollTop` before it is turned into a line index, or the
73
+ * pane paints one row behind the scrollbar and the first line cannot be reached
74
+ * at the top. The 16px bottom padding needs no arithmetic — it belongs to the
75
+ * same scroller, so it stays inside the scroll range on its own.
76
+ */
77
+ const CODE_PAD_TOP = 6
78
+
79
+ /** The half-open line range a windowed pane paints, plus what it leaves blank. */
80
+ export interface LineWindow {
81
+ /** First rendered line index. */
63
82
  from: number
64
- /** One past the last rendered row index. */
83
+ /** One past the last rendered line index. */
65
84
  to: number
66
- /** Height of the rows above `from` / below `to`, in px. */
85
+ /** Height of the lines above `from`, in px. */
67
86
  before: number
87
+ /** Height of the lines below `to`, in px. */
68
88
  after: number
69
89
  }
70
90
 
@@ -76,101 +96,169 @@ interface RowWindowing {
76
96
  /** The rows to paint, already sliced and clamped. */
77
97
  rows: DiffRow[]
78
98
  /** The range that slice came from, for effects that must re-measure with it. */
79
- range: RowWindow
99
+ range: LineWindow
80
100
  /** Height of the rows above / below the slice, in px. */
81
101
  before: number
82
102
  after: number
83
103
  }
84
104
 
85
105
  /**
86
- * The rows to render for one scroll position.
106
+ * The lines to paint for one scroll offset — arithmetic, not measurement,
107
+ * because every windowed row is a hard `ROW_HEIGHT`.
87
108
  *
88
- * Memoised against the row LIST, not just the numbers: a reload hands over a
89
- * new `model.rows` while `scrollTop` looks unchanged, and a window remembered
90
- * from the previous file would render the new file from the old offset. Handing
91
- * back the settled object in every other case is what lets a scroll event that
92
- * stays inside one row cost nothing.
109
+ * `total` is the number of lines the FULL pane would have rendered, which is
110
+ * what the scroll range has to cover: `before + painted + after` is exactly
111
+ * `total * ROW_HEIGHT`, so the scrollbar behaves as it did when every line was
112
+ * mounted.
113
+ *
114
+ * `offset` is the scroller's own top padding (`CODE_PAD_TOP` on `.rvs-code`, 0
115
+ * on `.rvs-diff-grid`).
116
+ *
117
+ * The offset is clamped to the LAST line, not merely guarded against negatives:
118
+ * a reload can replace a 3000-line side with a 40-line one while the browser
119
+ * still holds the old offset (it re-clamps the scroller only once the new
120
+ * spacer height is in the DOM). An unclamped slice would ask for lines that no
121
+ * longer exist, `from` would exceed the text, and the two spacers would then add
122
+ * up to less than the file — the bottom of the new text would be unreachable.
123
+ * Clamping keeps `before + painted + after` exactly equal to
124
+ * `total * ROW_HEIGHT`, which is what the scroller's range was before this
125
+ * window existed.
126
+ *
127
+ * Pure and exported so a probe can pin the boundaries without a browser.
93
128
  */
94
- function computeRowWindow(
95
- rows: DiffRow[],
129
+ export function computeLineWindow(
130
+ total: number,
96
131
  scrollTop: number,
97
132
  height: number,
98
- last: React.MutableRefObject<{ rows: DiffRow[]; window: RowWindow }>,
99
- ): RowWindow {
100
- const visibleRows = Math.ceil(height / ROW_HEIGHT)
133
+ offset = 0,
134
+ overscan = OVERSCAN_ROWS,
135
+ ): LineWindow {
136
+ // A pane can be measured while hidden (height 0) and a browser can hand back a
137
+ // non-finite offset during teardown; the arithmetic must stay total.
138
+ const lines = Number.isFinite(total) ? Math.max(0, Math.trunc(total)) : 0
139
+ const view = Number.isFinite(height) ? Math.max(0, height) : 0
140
+ const top = Number.isFinite(scrollTop) ? scrollTop : 0
141
+ const visibleRows = Math.ceil(view / ROW_HEIGHT)
142
+ const scrolledRows = Math.max(0, Math.min(Math.floor((top - offset) / ROW_HEIGHT), Math.max(0, lines - 1)))
143
+ const from = Math.max(0, scrolledRows - overscan)
144
+ const to = Math.min(lines, Math.max(from + 1, scrolledRows + visibleRows + overscan))
145
+ return { from, to, before: from * ROW_HEIGHT, after: Math.max(0, (lines - to) * ROW_HEIGHT) }
146
+ }
147
+
148
+ /** The window a pane last settled on, so a slice that did not move keeps its identity. */
149
+ interface WindowCache {
101
150
  /**
102
- * The offset is clamped to the last row, not merely guarded against
103
- * negatives: a reload can replace a 3000-line model with a 40-line one while
104
- * the browser still holds the old offset (it re-clamps the scroller only once
105
- * the new spacer height is in the DOM). An unclamped slice would ask for rows
106
- * that no longer exist, `from` would exceed the model, and the two spacers
107
- * would then add up to less than the file — the bottom of the new file would
108
- * be unreachable. Clamping keeps `before + slice + after` exactly equal to
109
- * `rows.length * ROW_HEIGHT`, which is what the scroller's range was before
110
- * this window existed.
151
+ * The line count that window was computed for: part of the identity because a
152
+ * reload can change the count under an unchanged offset (the case
153
+ * `computeLineWindow` clamps for).
111
154
  */
112
- const scrolledRows = Math.max(0, Math.min(Math.floor(scrollTop / ROW_HEIGHT), Math.max(0, rows.length - 1)))
113
- const from = Math.max(0, scrolledRows - OVERSCAN_ROWS)
114
- const to = Math.min(rows.length, Math.max(from + 1, scrolledRows + visibleRows + OVERSCAN_ROWS))
155
+ total: number
156
+ window: LineWindow
157
+ }
158
+
159
+ /**
160
+ * `computeLineWindow`, but handing back the PREVIOUS object while the slice did
161
+ * not move.
162
+ *
163
+ * That identity is what makes a wheel storm cheap: `onScroll` bumps the revision
164
+ * counter only when the returned object differs from the one the last render
165
+ * used, so a scroll that stays inside the same lines re-renders nothing. What it
166
+ * costs is one 4-field object per scroll event, against a render that would
167
+ * remount up to `visibleRows + 2 * OVERSCAN_ROWS` rows.
168
+ */
169
+ function settledLineWindow(
170
+ total: number,
171
+ scrollTop: number,
172
+ height: number,
173
+ offset: number,
174
+ last: React.MutableRefObject<WindowCache | undefined>,
175
+ ): LineWindow {
176
+ const next = computeLineWindow(total, scrollTop, height, offset)
115
177
  const settled = last.current
116
- if (settled.rows === rows && settled.window.from === from && settled.window.to === to) return settled.window
117
- const window = { from, to, before: from * ROW_HEIGHT, after: Math.max(0, (rows.length - to) * ROW_HEIGHT) }
118
- last.current = { rows, window }
119
- return window
178
+ if (settled !== undefined && settled.total === total && settled.window.from === next.from && settled.window.to === next.to) {
179
+ return settled.window
180
+ }
181
+ last.current = { total, window: next }
182
+ return next
183
+ }
184
+
185
+ /** What one render pass gets from `useScrollWindow`. */
186
+ interface Windowing {
187
+ /** Goes on the element that owns the vertical scroll. */
188
+ attach: (node: HTMLDivElement | null) => void
189
+ onScroll: (event: React.UIEvent<HTMLDivElement>) => void
190
+ /** The lines to paint, already clamped. */
191
+ range: LineWindow
120
192
  }
121
193
 
122
194
  /**
123
- * Fixed-height windowing for the side-by-side columns.
195
+ * Fixed-height windowing for every pane that paints a whole file.
124
196
  *
125
- * `.rvs-diff-grid` is the only vertical scroller in the diff view, and
126
- * `.rvs-dline` is a hard 18px, so the visible slice is arithmetic rather than
127
- * measurement: offset → first row, viewport height → row count. What is NOT
128
- * rendered is covered by a spacer of the right height, so the scroll range
129
- * covers the whole file and the scrollbar behaves exactly as it did when every
130
- * row was mounted.
197
+ * ONE implementation, because the arithmetic, the resize bookkeeping and the
198
+ * "only a crossing re-renders" policy are the parts that are easy to get subtly
199
+ * different: the split view needs the window's ROWS (it slices `model.rows`),
200
+ * the "变更前 / 变更后" panes need only the window's LINE INDEXES, and both take
201
+ * the range from here — see `useRowWindow` for the split adapter.
131
202
  *
132
- * Scroll geometry is read through refs instead of React state: a `scrollTop`
133
- * per wheel event would re-render on every pixel, while only crossing a row
203
+ * A scroller owns a fixed-row-height list, so the visible slice is arithmetic
204
+ * rather than measurement: offset → first line, viewport height → line count.
205
+ * What is NOT rendered is covered by a spacer of the right height, so the scroll
206
+ * range covers the whole file and the scrollbar behaves exactly as it did when
207
+ * every line was mounted.
208
+ *
209
+ * Scroll geometry is read through refs instead of React state: a `scrollTop` per
210
+ * wheel event would re-render on every pixel, while only crossing a line
134
211
  * boundary changes anything on screen. The state here is just a revision
135
212
  * counter, and the arithmetic above is what decides whether it moves.
136
213
  *
137
214
  * Nothing is pinned outside the window: an off-screen hunk button is reached by
138
215
  * scrolling to its row, the same gesture as before.
139
216
  */
140
- function useRowWindow(rows: DiffRow[]): RowWindowing {
217
+ function useScrollWindow(total: number, offset = 0): Windowing {
141
218
  const scrollTopRef = useRef(0)
142
219
  const heightRef = useRef(FALLBACK_VISIBLE_ROWS * ROW_HEIGHT)
143
- const lastWindowRef = useRef<{ rows: DiffRow[]; window: RowWindow }>({
144
- rows: [],
145
- window: { from: -1, to: -1, before: 0, after: 0 },
146
- })
220
+ const lastWindowRef = useRef<WindowCache | undefined>(undefined)
147
221
  const observerRef = useRef<ResizeObserver | null>(null)
148
222
  const [, bumpWindow] = useState(0)
149
223
 
150
224
  /**
151
- * A CALLBACK ref, so the grid is measured whenever it actually mounts.
225
+ * A CALLBACK ref, so the scroller is measured whenever it actually mounts.
152
226
  *
153
- * A layout effect cannot do this job: a diff opens on its loading branch,
154
- * where there is no grid to point at, and the grid only appears once the
155
- * model arrives — at which point no dependency has changed and the effect
156
- * would never run. The height would stay at the fallback and the bottom of a
157
- * tall viewport would sit blank until the first scroll. React calls this on
158
- * mount and with `null` on the way out, which is also the observer teardown.
227
+ * A layout effect cannot do this job: a diff opens on its loading branch, where
228
+ * there is no scroller to point at, and it only appears once the model arrives
229
+ * — at which point no dependency has changed and the effect would never run.
230
+ * The height would stay at the fallback and the bottom of a tall viewport would
231
+ * sit blank until the first scroll. React calls this on mount and with `null`
232
+ * on the way out, which is also the observer teardown.
159
233
  *
160
234
  * Measuring here keeps the guarantee that motivated the layout effect: the
161
- * first painted frame already carries the right slice, so opening or resizing
162
- * the split does not flash the opening rows and then jump. A 0-height reading
163
- * is the grid being hidden, not a collapsed viewport, so the last real height
164
- * is kept. The pane can also change size without the window following (a
165
- * sidebar split, a window resize, the editor header appearing), which is what
166
- * the observer below is for.
235
+ * first painted frame already carries the right slice, so opening or resizing a
236
+ * pane does not flash the opening rows and then jump. A 0-height reading is the
237
+ * pane being hidden (or a scroller with no layout at all — jsdom), NOT a
238
+ * collapsed viewport, so the last real height is kept and the window falls back
239
+ * to `FALLBACK_VISIBLE_ROWS`: a fixed slice, never the whole file.
240
+ *
241
+ * The offset is re-read here as well, and a change repaints: a mode switch
242
+ * UNMOUNTS one scroller and mounts another, so the ref can still hold the
243
+ * previous pane's offset while this render has already laid the window out from
244
+ * it. A fresh node starts at 0, and getting that wrong would paint a blank
245
+ * spacer above the first lines until the next scroll event.
167
246
  */
168
- const attachGrid = useCallback((node: HTMLDivElement | null): void => {
247
+ const attach = useCallback((node: HTMLDivElement | null): void => {
169
248
  observerRef.current?.disconnect()
170
249
  observerRef.current = null
171
- if (node === null || typeof ResizeObserver === 'undefined') return
250
+ if (node === null) return
251
+ let repaint = node.scrollTop !== scrollTopRef.current
172
252
  scrollTopRef.current = node.scrollTop
173
- if (node.clientHeight > 0) heightRef.current = node.clientHeight
253
+ if (node.clientHeight > 0 && node.clientHeight !== heightRef.current) {
254
+ heightRef.current = node.clientHeight
255
+ repaint = true
256
+ }
257
+ if (repaint) bumpWindow(step => step + 1)
258
+ // A scroller can also be mounted somewhere without `ResizeObserver` (jsdom,
259
+ // an older embedded webview): the measurement above is then the only one,
260
+ // and there is simply nothing to follow later.
261
+ if (typeof ResizeObserver === 'undefined') return
174
262
  const observer = new ResizeObserver((entries) => {
175
263
  const box = entries[0]?.borderBoxSize[0]
176
264
  const measured = box === undefined ? node.clientHeight : box.blockSize
@@ -183,35 +271,41 @@ function useRowWindow(rows: DiffRow[]): RowWindowing {
183
271
  observerRef.current = observer
184
272
  }, [])
185
273
 
186
- const range = computeRowWindow(rows, scrollTopRef.current, heightRef.current, lastWindowRef)
274
+ const range = settledLineWindow(total, scrollTopRef.current, heightRef.current, offset, lastWindowRef)
187
275
  /**
188
276
  * Recomputed on every scroll event and handed to React as the same object
189
- * whenever the slice did not move (`computeRowWindow`), so a wheel storm that
190
- * stays inside one row re-renders nothing. The measured `clientHeight` comes
277
+ * whenever the slice did not move (`settledLineWindow`), so a wheel storm that
278
+ * stays inside one line re-renders nothing. The measured `clientHeight` comes
191
279
  * along because it tracks a pane resize the window itself cannot see.
192
280
  */
193
281
  const onScroll = useCallback((event: React.UIEvent<HTMLDivElement>): void => {
194
- const grid = event.currentTarget
195
- scrollTopRef.current = grid.scrollTop
196
- if (grid.clientHeight > 0) heightRef.current = grid.clientHeight
197
- // Only a slice change is worth a render. `computeRowWindow` hands back the
282
+ const scroller = event.currentTarget
283
+ scrollTopRef.current = scroller.scrollTop
284
+ if (scroller.clientHeight > 0) heightRef.current = scroller.clientHeight
285
+ // Only a slice change is worth a render. `settledLineWindow` hands back the
198
286
  // very object the last render used while the wheel stays inside the same
199
- // rows, so comparing against the window that was current BEFORE the call is
287
+ // lines, so comparing against the window that was current BEFORE the call is
200
288
  // the whole test — without it every scroll event re-rendered the diff,
201
289
  // which is the cost the windowing exists to remove.
202
- const previous = lastWindowRef.current.window
203
- if (computeRowWindow(rows, grid.scrollTop, heightRef.current, lastWindowRef) !== previous) {
290
+ const previous = lastWindowRef.current?.window
291
+ if (settledLineWindow(total, scroller.scrollTop, heightRef.current, offset, lastWindowRef) !== previous) {
204
292
  bumpWindow(step => step + 1)
205
293
  }
206
- }, [rows])
294
+ }, [total, offset])
295
+
296
+ return { attach, onScroll, range }
297
+ }
207
298
 
299
+ /** The split view's window: `useScrollWindow` plus the `model.rows` slice. */
300
+ function useRowWindow(rows: DiffRow[]): RowWindowing {
301
+ const windowing = useScrollWindow(rows.length)
208
302
  return {
209
- attachGrid,
210
- onScroll,
211
- rows: rows.slice(range.from, range.to),
212
- range,
213
- before: range.before,
214
- after: range.after,
303
+ attachGrid: windowing.attach,
304
+ onScroll: windowing.onScroll,
305
+ rows: rows.slice(windowing.range.from, windowing.range.to),
306
+ range: windowing.range,
307
+ before: windowing.range.before,
308
+ after: windowing.range.after,
215
309
  }
216
310
  }
217
311
 
@@ -227,6 +321,94 @@ function Line(props: { tokens: Token[]; empty?: boolean }): ReactNode {
227
321
  )
228
322
  }
229
323
 
324
+ /**
325
+ * The lines of one whole-file side ("变更前" / a staged "变更后").
326
+ *
327
+ * The split columns' treatment, one pane deep: only the window's lines are
328
+ * mounted, the lines above and below it are spacers of the right height, and the
329
+ * gutter numbers are the side's own line indexes + 1. `.rvs-code-line` is a hard
330
+ * 18px row and does not wrap (`min-width: max-content`), which is what lets the
331
+ * shared window use the same arithmetic here as in the split view.
332
+ *
333
+ * Rendered inside `.rvs-code`, whose 6px top padding is the window's `offset`
334
+ * (see `CODE_PAD_TOP`), so the first rendered line sits exactly where the scroll
335
+ * offset says it does.
336
+ */
337
+ function CodeLines(props: { tokens: SideTokens; range: LineWindow; emptyText: string }): ReactNode {
338
+ if (props.range.to <= props.range.from) return <div className="rvs-empty">{props.emptyText}</div>
339
+ return (
340
+ <>
341
+ <div className="rvs-drows-blank" style={{ height: props.range.before }} />
342
+ {Array.from({ length: props.range.to - props.range.from }, (_, offset) => props.range.from + offset).map(line => (
343
+ <div key={line} className="rvs-code-line">
344
+ <span className="rvs-num">{line + 1}</span>
345
+ <span className="rvs-code-text"><Line tokens={props.tokens.at(line) ?? []} /></span>
346
+ </div>
347
+ ))}
348
+ <div className="rvs-drows-blank" style={{ height: props.range.after }} />
349
+ </>
350
+ )
351
+ }
352
+
353
+ /**
354
+ * One side of the diff, tokenized ON DEMAND down to the deepest line asked for.
355
+ *
356
+ * Why not `tokenizeDocument` on open: the split view paints only the rows in
357
+ * its window, but the old code tokenized BOTH whole sides before painting a
358
+ * single line — on a 2 MB diff (the host's `GIT_CAP`) that is tens of thousands
359
+ * of lines of work spent before the first screenful appears.
360
+ *
361
+ * Why it cannot simply tokenize the window's slice: `tokenizeLine` is stateful
362
+ * (a line inside a `/* … *\/` block comment only tokenizes correctly after the
363
+ * lines that opened it), so this walks the side from line 0, keeps the block
364
+ * state and the arrays produced so far, and stops as soon as the deepest
365
+ * requested line is covered. Opening a file at the top therefore tokenizes a
366
+ * screenful, scrolling or jumping deeper extends the cache incrementally, and
367
+ * every line is still tokenized exactly once per reload.
368
+ *
369
+ * The string split is deferred to the first line actually asked for, and a side
370
+ * that is never painted (its pane is not on screen) now costs nothing at all.
371
+ *
372
+ * Both halves of the view read through this: the split columns take the lines
373
+ * their rows name, and the windowed whole-file panes take their visible range.
374
+ * `tokenizeDocument` walks exactly the same lines with the same state, so the
375
+ * per-line arrays are identical either way.
376
+ */
377
+ interface SideTokens {
378
+ /** Number of lines in the text (a trailing newline ends with an empty line). */
379
+ readonly lineCount: number
380
+ /** Tokens for the 0-based line, or `undefined` past the end of the text. */
381
+ at(lineIndex: number): Token[] | undefined
382
+ }
383
+
384
+ /** Lines in `text`, counted without allocating the split array. */
385
+ function countLines(text: string): number {
386
+ let count = 1
387
+ for (let index = text.indexOf('\n'); index >= 0; index = text.indexOf('\n', index + 1)) count += 1
388
+ return count
389
+ }
390
+
391
+ /** A side with nothing to show yet: never allocates and never tokenizes. */
392
+ const NO_TOKENS: SideTokens = { lineCount: 0, at: () => undefined }
393
+
394
+ function createSideTokens(text: string, lang: LangId | undefined): SideTokens {
395
+ const lineCount = countLines(text)
396
+ let lines: string[] | undefined
397
+ const tokens: Token[][] = []
398
+ const state = lexState()
399
+ return {
400
+ lineCount,
401
+ at(lineIndex: number): Token[] | undefined {
402
+ if (lineIndex < 0 || lineIndex >= lineCount) return undefined
403
+ const source = lines ?? (lines = text.split('\n'))
404
+ while (tokens.length <= lineIndex) {
405
+ tokens.push(tokenizeLine(source[tokens.length] ?? '', lang, state))
406
+ }
407
+ return tokens[lineIndex]
408
+ },
409
+ }
410
+ }
411
+
230
412
  /**
231
413
  * The top horizontal scrollbar: a scroll container whose only content is a
232
414
  * spacer as wide as the widest line. Dragging it drives both panes, so a long
@@ -462,14 +644,69 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
462
644
  const sideLang = useMemo(() => langFromPath(tab.rel), [tab.rel])
463
645
  const model = tab.model
464
646
  const counts = useMemo(() => (model === undefined ? { added: 0, removed: 0 } : diffCounts(model)), [model])
465
- const beforeLines = useMemo(
466
- () => (model === undefined ? [] : tokenizeDocument(tab.diff?.oldText ?? '', sideLang)),
647
+ /**
648
+ * The two sides, tokenized lazily as a window asks for lines (see
649
+ * `createSideTokens`). Memoized on the TEXT, not the tab: a store publish hands
650
+ * over a fresh tab object for a dirty/saving flip while the diff bytes are
651
+ * unchanged, and rebuilding both caches would re-tokenize the file.
652
+ *
653
+ * The memo refuses to run while `model` is missing (F7): the payload arrives in
654
+ * two publishes, so `tab.diff` can be populated a publish before `tab.model`,
655
+ * and the old memo walked the whole side text for a pane that could not be
656
+ * painted yet. `NO_TOKENS` is the stand-in for that state.
657
+ */
658
+ const beforeSide = useMemo(
659
+ () => (model === undefined ? NO_TOKENS : createSideTokens(tab.diff?.oldText ?? '', sideLang)),
467
660
  [model, tab.diff?.oldText, sideLang],
468
661
  )
469
- const afterLines = useMemo(
470
- () => (model === undefined ? [] : tokenizeDocument(tab.diff?.newText ?? '', sideLang)),
662
+ const afterSide = useMemo(
663
+ () => (model === undefined ? NO_TOKENS : createSideTokens(tab.diff?.newText ?? '', sideLang)),
471
664
  [model, tab.diff?.newText, sideLang],
472
665
  )
666
+ /**
667
+ * The whole-file pane's side and its window.
668
+ *
669
+ * Both "变更前 / 变更后" panes read through the SAME lazy tokenizer as the split
670
+ * columns instead of `tokenizeDocument`-ing the whole side, which is what makes
671
+ * windowing them pay off: only the lines near the viewport are ever tokenized.
672
+ * An UNSTAGED "变更后" is the editable CodeMirror buffer, not a text pane, so it
673
+ * gets `NO_TOKENS` and tokenizes nothing.
674
+ */
675
+ const codeSide = mode === 'before'
676
+ ? beforeSide
677
+ : mode === 'after' && tab.staged
678
+ ? afterSide
679
+ : NO_TOKENS
680
+ const codeWindow = useScrollWindow(codeSide.lineCount, CODE_PAD_TOP)
681
+ /**
682
+ * Materialise every token array this frame's rows will read (F7).
683
+ *
684
+ * `SideTokens.at` is not a pure read: the first call for a line advances an
685
+ * append-only cache over the stateful `tokenizeLine`. While it was called from
686
+ * the row JSX, a render pass mutated that cache, so a double render (StrictMode,
687
+ * a discarded concurrent render) interleaved advancement with reading. The token
688
+ * ARRAYS are a pure function of the side's text and language, so the output
689
+ * never differed — what was wrong is that the render phase changed state nobody
690
+ * could see.
691
+ *
692
+ * The prefetch asks the same questions ONCE per window (this memo is keyed by
693
+ * the window, not by the scroll event) and the row loops below are then pure
694
+ * cache hits. It mirrors their guards deliberately; if one ever drifts the cost
695
+ * is a render-time miss, not a wrong token.
696
+ */
697
+ useMemo(() => {
698
+ if (mode === 'split') {
699
+ const rows = model?.rows ?? []
700
+ for (let index = rowsInView.range.from; index < rowsInView.range.to; index += 1) {
701
+ const row = rows[index]
702
+ if (row === undefined) continue
703
+ if (row.old !== '') beforeSide.at((row.oldNum ?? 1) - 1)
704
+ if (row.inNew !== false && row.new !== '') afterSide.at((row.newNum ?? 1) - 1)
705
+ }
706
+ return
707
+ }
708
+ for (let line = codeWindow.range.from; line < codeWindow.range.to; line += 1) codeSide.at(line)
709
+ }, [mode, model, beforeSide, afterSide, codeSide, rowsInView.range, codeWindow.range])
473
710
 
474
711
  const runHunk = async (index: number, kind: 'stage' | 'unstage' | 'revert'): Promise<void> => {
475
712
  // A hunk action is a remote write, and it ships a patch parsed out of a
@@ -636,8 +873,9 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
636
873
  <span style={{ color: 'var(--rvs-git-d)' }}>-{counts.removed}</span>
637
874
  {tab.staged && <span className="rvs-pill">{t('scm.stagedReadonly')}</span>}
638
875
  <span className="rvs-einfo-spacer" />
639
- {/* Prev/next change: only the split view is windowed, so only it has a
640
- row offset to jump to. The counter doubles as the "where am I" cue. */}
876
+ {/* Prev/next change: the hunk list indexes the SPLIT model's rows, so the
877
+ counter belongs to the split view; the side panes paint file lines.
878
+ The counter doubles as the "where am I" cue. */}
641
879
  {mode === 'split' && hunkRows.length > 0 && (
642
880
  <span className="rvs-hunknav" title={t('scm.hunkNavHint')}>
643
881
  <IconButton title={t('scm.prevHunk')} onClick={() => jumpHunk(-1)}>
@@ -705,7 +943,7 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
705
943
  <span className="rvs-dline-text">
706
944
  {row.old === ''
707
945
  ? <span className="rvs-row-empty">{'\u00a0'}</span>
708
- : <Line tokens={beforeLines[(row.oldNum ?? 1) - 1] ?? [{ text: row.old, kind: 'plain' }]} />}
946
+ : <Line tokens={beforeSide.at((row.oldNum ?? 1) - 1) ?? [{ text: row.old, kind: 'plain' }]} />}
709
947
  </span>
710
948
  </div>
711
949
  ))}
@@ -767,7 +1005,7 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
767
1005
  <span className="rvs-dline-text">
768
1006
  {row.inNew === false || row.new === ''
769
1007
  ? <span className="rvs-row-empty">{'\u00a0'}</span>
770
- : <Line tokens={afterLines[(row.newNum ?? 1) - 1] ?? [{ text: row.new, kind: 'plain' }]} />}
1008
+ : <Line tokens={afterSide.at((row.newNum ?? 1) - 1) ?? [{ text: row.new, kind: 'plain' }]} />}
771
1009
  </span>
772
1010
  </div>
773
1011
  ))}
@@ -778,27 +1016,15 @@ export function DiffView(props: { tab: EditorTab; visible: boolean; offline?: bo
778
1016
  </>
779
1017
  )}
780
1018
  {mode === 'before' && (
781
- <div className="rvs-code rvs-scroll">
782
- {beforeLines.length === 0
783
- ? <div className="rvs-empty">{t('scm.emptySide')}</div>
784
- : beforeLines.map((tokens, index) => (
785
- <div key={index} className="rvs-code-line">
786
- <span className="rvs-num">{index + 1}</span>
787
- <span className="rvs-code-text"><Line tokens={tokens} /></span>
788
- </div>
789
- ))}
1019
+ <div className="rvs-code rvs-scroll" ref={codeWindow.attach} onScroll={codeWindow.onScroll}>
1020
+ <CodeLines tokens={codeSide} range={codeWindow.range} emptyText={t('scm.emptySide')} />
790
1021
  </div>
791
1022
  )}
792
1023
  {mode === 'after' && (
793
1024
  tab.staged
794
1025
  ? (
795
- <div className="rvs-code rvs-scroll">
796
- {afterLines.map((tokens, index) => (
797
- <div key={index} className="rvs-code-line">
798
- <span className="rvs-num">{index + 1}</span>
799
- <span className="rvs-code-text"><Line tokens={tokens} /></span>
800
- </div>
801
- ))}
1026
+ <div className="rvs-code rvs-scroll" ref={codeWindow.attach} onScroll={codeWindow.onScroll}>
1027
+ <CodeLines tokens={codeSide} range={codeWindow.range} emptyText={t('scm.emptySide')} />
802
1028
  </div>
803
1029
  )
804
1030
  : (
@@ -25,7 +25,7 @@ import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } fro
25
25
  import type { GitCommit, GitCommitFile } from '../../protocol.ts'
26
26
  import { GIT_LOG_MAX } from '../../git-format.ts'
27
27
  import { api } from '../api.ts'
28
- import { gitReadSettleMs, useExplorer } from '../store.tsx'
28
+ import { gitReadSettleMs, shallowEqual, useExplorerSelector, type ExplorerValue } from '../store.tsx'
29
29
  import { checkoutCommit, joinRemote, loadCommitFiles, loadCommitMessage, loadCommitPatch } from './actions.ts'
30
30
  import { CommitPane } from './CommitPane.tsx'
31
31
  import { isHistoryTab, needsPatchLoad, refBadgeClass, refBadgeLabel, type HistoryTab } from './commit-model.ts'
@@ -207,8 +207,29 @@ interface CommitPatchState {
207
207
  error?: string
208
208
  }
209
209
 
210
+ /**
211
+ * The store fields this panel reads — and only those.
212
+ *
213
+ * Same reasoning as `ScmPanel`'s slice: the commit graph (up to `GIT_LOG_MAX`
214
+ * rows) and the pane below it used to be rebuilt on EVERY store publish, which
215
+ * includes saves, git refreshes and rail switches that this view cannot show.
216
+ * The slice below is what a graph/pane actually depends on: the repository
217
+ * identity the log is read for, the branch pill, and the actions.
218
+ */
219
+ const selectHistoryStore = (snapshot: ExplorerValue) => ({
220
+ t: snapshot.t,
221
+ lang: snapshot.lang,
222
+ alias: snapshot.alias,
223
+ repoRoot: snapshot.repoRoot,
224
+ head: snapshot.head,
225
+ afterWrite: snapshot.afterWrite,
226
+ openCommitFile: snapshot.openCommitFile,
227
+ revealInExplorer: snapshot.revealInExplorer,
228
+ openTerminalAt: snapshot.openTerminalAt,
229
+ })
230
+
210
231
  export function HistoryPanel(): ReactNode {
211
- const store = useExplorer()
232
+ const store = useExplorerSelector(selectHistoryStore, shallowEqual)
212
233
  const { t, lang, alias, repoRoot } = store
213
234
  const [commits, setCommits] = useState<GitCommit[]>([])
214
235
  /**