@kabmo-labs/kabmo 0.9.0 → 0.10.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 (180) hide show
  1. package/README.md +45 -0
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +333 -333
  4. package/dist/client/static/js/index-18xe7655.css +1 -0
  5. package/dist/client/static/js/{index-bpptfkz2.js → index-nxpe7835.js} +768 -763
  6. package/package.json +3 -1
  7. package/src/client/components/common/code-surface/bracket-guide.test.ts +249 -0
  8. package/src/client/components/common/code-surface/bracket-guide.ts +131 -0
  9. package/src/client/components/common/code-surface/documents.test.ts +5 -3
  10. package/src/client/components/common/code-surface/documents.ts +85 -0
  11. package/src/client/components/common/code-surface/extensions.test.ts +5 -0
  12. package/src/client/components/common/code-surface/extensions.ts +140 -7
  13. package/src/client/components/common/code-surface/fold-stack.test.ts +218 -0
  14. package/src/client/components/common/code-surface/fold.test.ts +148 -0
  15. package/src/client/components/common/code-surface/fold.ts +118 -0
  16. package/src/client/components/common/code-surface/indent-guide.ts +104 -0
  17. package/src/client/components/common/code-surface/indent.test.ts +239 -0
  18. package/src/client/components/common/code-surface/indent.ts +124 -0
  19. package/src/client/components/common/code-surface/index.tsx +76 -46
  20. package/src/client/components/common/code-surface/keymap.ts +12 -23
  21. package/src/client/components/common/code-surface/line-commands.test.ts +90 -0
  22. package/src/client/components/common/code-surface/line-commands.ts +103 -0
  23. package/src/client/components/common/code-surface/props.ts +87 -0
  24. package/src/client/components/common/code-surface/stream-language.test.ts +118 -0
  25. package/src/client/components/common/code-surface/stream-language.ts +123 -0
  26. package/src/client/components/common/code-surface/theme.test.ts +40 -5
  27. package/src/client/components/common/code-surface/theme.ts +113 -0
  28. package/src/client/components/common/code-surface/typing.test.ts +97 -0
  29. package/src/client/components/common/code-surface/typing.ts +111 -0
  30. package/src/client/components/common/toolbar-row/index.test.tsx +53 -4
  31. package/src/client/components/common/toolbar-row/index.tsx +101 -30
  32. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +17 -0
  33. package/src/client/components/layout/desktop-layout/index.tsx +18 -0
  34. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +35 -33
  35. package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +20 -1
  36. package/src/client/components/mobile/mobile-right-sidebar/editor-props.ts +25 -0
  37. package/src/client/components/settings/categories/AppearanceSettings.tsx +6 -0
  38. package/src/client/components/settings/categories/appearance-settings/EditorIndentSection.tsx +87 -0
  39. package/src/client/components/settings/categories/appearance-settings/EditorTypingSection.tsx +99 -0
  40. package/src/client/components/workspace/chat-timeline/chat-input/expansion.ts +9 -4
  41. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +15 -11
  42. package/src/client/components/workspace/chat-timeline/chat-input/stretch-send.test.tsx +165 -0
  43. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.test.ts +17 -3
  44. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.tsx +10 -6
  45. package/src/client/components/workspace/editor/Body.tsx +24 -0
  46. package/src/client/components/workspace/editor/CommandPalette.tsx +4 -4
  47. package/src/client/components/workspace/editor/ConflictStrip.tsx +77 -0
  48. package/src/client/components/workspace/editor/Dialogs.tsx +53 -0
  49. package/src/client/components/workspace/editor/EmptyEditor.tsx +23 -0
  50. package/src/client/components/workspace/editor/ExternalChangeStrip.tsx +61 -0
  51. package/src/client/components/workspace/editor/FileDocument.tsx +89 -1
  52. package/src/client/components/workspace/editor/FilePane.tsx +43 -0
  53. package/src/client/components/workspace/editor/FindWidget.test.ts +9 -5
  54. package/src/client/components/workspace/editor/FindWidget.tsx +29 -0
  55. package/src/client/components/workspace/editor/GoToLineModal.tsx +123 -0
  56. package/src/client/components/workspace/editor/Notices.tsx +71 -0
  57. package/src/client/components/workspace/editor/Pane.tsx +118 -0
  58. package/src/client/components/workspace/editor/PluginColumn.tsx +32 -0
  59. package/src/client/components/workspace/editor/QuickOpenModal.tsx +146 -0
  60. package/src/client/components/workspace/editor/SaveAsModal.tsx +175 -0
  61. package/src/client/components/workspace/editor/SplitColumn.tsx +98 -0
  62. package/src/client/components/workspace/editor/SplitPane.tsx +57 -0
  63. package/src/client/components/workspace/editor/StatusBar.tsx +106 -0
  64. package/src/client/components/workspace/editor/TabContextMenu.tsx +75 -0
  65. package/src/client/components/workspace/editor/Tabs.tsx +195 -18
  66. package/src/client/components/workspace/editor/Toolbar.tsx +54 -181
  67. package/src/client/components/workspace/editor/index.tsx +199 -189
  68. package/src/client/components/workspace/editor/props.ts +64 -0
  69. package/src/client/components/workspace/editor/toolbar-clusters.tsx +300 -0
  70. package/src/client/components/workspace/file-explorer/Modals.tsx +14 -1
  71. package/src/client/components/workspace/file-explorer/TreeItem.tsx +2 -1
  72. package/src/client/components/workspace/file-explorer/clipboard.test.ts +3 -1
  73. package/src/client/components/workspace/file-explorer/index.test.ts +61 -0
  74. package/src/client/components/workspace/file-explorer/index.tsx +10 -48
  75. package/src/client/components/workspace/file-explorer/local-edit.test.ts +8 -3
  76. package/src/client/components/workspace/search-panel/Body.tsx +6 -1
  77. package/src/client/hooks/chat/composer/file-drop.ts +22 -37
  78. package/src/client/hooks/chat/composer/pipeline.test.ts +76 -0
  79. package/src/client/hooks/chat/composer/pipeline.ts +12 -1
  80. package/src/client/hooks/editor/caret-position.test.ts +58 -0
  81. package/src/client/hooks/editor/caret-position.ts +48 -0
  82. package/src/client/hooks/editor/file-editor-types.ts +200 -0
  83. package/src/client/hooks/editor/find-history.test.ts +132 -0
  84. package/src/client/hooks/editor/find-history.ts +46 -0
  85. package/src/client/hooks/editor/find-selection-range.test.ts +100 -0
  86. package/src/client/hooks/editor/find-selection-range.ts +49 -0
  87. package/src/client/hooks/editor/find-state.ts +13 -0
  88. package/src/client/hooks/editor/font.test.ts +0 -93
  89. package/src/client/hooks/editor/indent-settings.test.ts +104 -0
  90. package/src/client/hooks/editor/indent-settings.ts +88 -0
  91. package/src/client/hooks/editor/quick-open.test.ts +141 -0
  92. package/src/client/hooks/editor/quick-open.ts +127 -0
  93. package/src/client/hooks/editor/save-as.test.ts +156 -0
  94. package/src/client/hooks/editor/save-as.ts +122 -0
  95. package/src/client/hooks/editor/save-file.test.ts +179 -0
  96. package/src/client/hooks/editor/save-file.ts +73 -28
  97. package/src/client/hooks/editor/typing-settings.test.ts +73 -0
  98. package/src/client/hooks/editor/typing-settings.ts +65 -0
  99. package/src/client/hooks/editor/use-close-guard.ts +98 -0
  100. package/src/client/hooks/editor/use-editor-drop-overlay.test.ts +159 -0
  101. package/src/client/hooks/editor/use-editor-drop.shell.test.ts +224 -0
  102. package/src/client/hooks/editor/use-editor-drop.ts +58 -68
  103. package/src/client/hooks/editor/use-editor-find.test.ts +2 -2
  104. package/src/client/hooks/editor/use-editor-find.ts +38 -3
  105. package/src/client/hooks/editor/use-editor-panel-keys.ts +152 -0
  106. package/src/client/hooks/editor/use-editor-tab-state.ts +71 -0
  107. package/src/client/hooks/editor/use-external-change-notice.ts +71 -0
  108. package/src/client/hooks/editor/use-external-change.test.ts +88 -0
  109. package/src/client/hooks/editor/use-external-change.ts +146 -0
  110. package/src/client/hooks/editor/use-file-editor.ts +83 -116
  111. package/src/client/hooks/editor/use-file-read.ts +16 -8
  112. package/src/client/hooks/editor/use-file-writes.ts +28 -3
  113. package/src/client/hooks/editor/use-mobile-editor-find.ts +69 -0
  114. package/src/client/hooks/editor/use-mobile-reveal.ts +53 -0
  115. package/src/client/hooks/editor/use-search-reveal-jump.ts +57 -0
  116. package/src/client/hooks/editor/use-split-editor.test.tsx +169 -0
  117. package/src/client/hooks/editor/use-split-editor.ts +91 -0
  118. package/src/client/hooks/editor/use-unload-guard.tsx +117 -0
  119. package/src/client/hooks/editor/use-write-conflict.test.ts +164 -0
  120. package/src/client/hooks/editor/use-write-conflict.ts +152 -0
  121. package/src/client/hooks/editor/wheel-zoom.test.ts +141 -0
  122. package/src/client/hooks/editor/wheel-zoom.ts +96 -0
  123. package/src/client/hooks/ui/drag-presence.test.ts +193 -0
  124. package/src/client/hooks/ui/drag-presence.ts +108 -0
  125. package/src/client/hooks/ui/settings-modal.test.ts +115 -0
  126. package/src/client/hooks/workspace/file-listing.ts +36 -24
  127. package/src/client/hooks/workspace/file-tabs.test.ts +90 -0
  128. package/src/client/hooks/workspace/file-tabs.ts +46 -0
  129. package/src/client/hooks/workspace/file-tree-edits.ts +103 -0
  130. package/src/client/lib/desktop/dropped-paths.ts +83 -0
  131. package/src/client/lib/editor/tab-menu.test.ts +127 -0
  132. package/src/client/lib/editor/tab-menu.ts +127 -0
  133. package/src/client/lib/editor-dirty-tabs.test.ts +102 -0
  134. package/src/client/lib/editor-dirty-tabs.ts +103 -0
  135. package/src/client/lib/native/resume.test.ts +66 -0
  136. package/src/client/lib/native/resume.ts +42 -0
  137. package/src/client/main.tsx +8 -0
  138. package/src/client/tailwind.css +38 -0
  139. package/src/server/lib/bundler/css.test.ts +31 -0
  140. package/src/server/lib/fs/listing.test.ts +81 -16
  141. package/src/server/lib/fs/listing.ts +72 -5
  142. package/src/server/lib/fs/write-conflict.test.ts +68 -0
  143. package/src/server/lib/fs/write-conflict.ts +84 -0
  144. package/src/server/routes/fs/action-save-as.test.ts +82 -0
  145. package/src/server/routes/fs/action.test.ts +82 -0
  146. package/src/server/routes/fs/action.ts +54 -1
  147. package/src/server/routes/fs/exists.ts +11 -1
  148. package/src/server/routes/fs/read.ts +6 -1
  149. package/src/shared/lib/code/editor/find-replace.test.ts +1 -1
  150. package/src/shared/lib/code/editor/find-scan.test.ts +1 -1
  151. package/src/shared/lib/code/editor/find-scan.ts +22 -1
  152. package/src/shared/lib/code/editor/find-selection.test.ts +56 -0
  153. package/src/shared/lib/code/editor/indent-guide-rule.test.ts +65 -0
  154. package/src/shared/lib/code/editor/indent-guide-rule.ts +47 -0
  155. package/src/shared/lib/code/editor/indent-rules.test.ts +219 -0
  156. package/src/shared/lib/code/editor/indent-rules.ts +198 -0
  157. package/src/shared/lib/code/editor/keymap.test.ts +23 -0
  158. package/src/shared/lib/code/editor/keymap.ts +40 -3
  159. package/src/shared/lib/code/editor/palette.test.ts +25 -2
  160. package/src/shared/lib/code/editor/palette.ts +10 -0
  161. package/src/shared/lib/code/editor/stream-modes.ts +225 -0
  162. package/src/shared/lib/code/syntax-highlight.test.ts +1 -1
  163. package/src/shared/lib/fs/file-tree.test.ts +41 -0
  164. package/src/shared/lib/fs/file-tree.ts +126 -34
  165. package/src/shared/lib/fs/folder-chain.test.ts +87 -0
  166. package/src/shared/lib/fs/folder-chain.ts +91 -0
  167. package/src/shared/lib/fs/fs-helpers.test.ts +4 -50
  168. package/src/shared/lib/realtime/connection.test.ts +5 -1
  169. package/src/shared/lib/realtime/connection.ts +25 -119
  170. package/src/shared/lib/realtime/frozen-recovery.test.ts +93 -0
  171. package/src/shared/lib/realtime/resume.ts +223 -0
  172. package/src/shared/lib/realtime/state.ts +22 -0
  173. package/src/shared/lib/realtime/watchdog-wiring.test.ts +224 -0
  174. package/src/shared/lib/realtime/watchdog.test.ts +99 -0
  175. package/src/shared/lib/realtime/watchdog.ts +72 -0
  176. package/src/shared/lib/workspace/split-editor.test.ts +104 -0
  177. package/src/shared/lib/workspace/split-editor.ts +129 -0
  178. package/src/test-support/file-explorer-disk.test-util.ts +30 -3
  179. package/src/test-support/pristine-globals.ts +7 -0
  180. package/dist/client/static/js/index-k315p1e8.css +0 -1
@@ -1,16 +1,5 @@
1
1
  import type { FsNode } from '@/shared/types';
2
-
3
- /** Replace the children of the node at `path`, rebuilding only that branch. */
4
- export function setChildrenAt(nodes: FsNode[], path: string, children: FsNode[]): FsNode[] {
5
- return nodes.map(node => {
6
- if (node.path === path) return { ...node, children };
7
- if (Array.isArray(node.children)) {
8
- const nested = setChildrenAt(node.children, path, children);
9
- if (nested !== node.children) return { ...node, children: nested };
10
- }
11
- return node;
12
- });
13
- }
2
+ import { foldSingleChildFolders } from '@/shared/lib/fs/folder-chain';
14
3
 
15
4
  /**
16
5
  * Re-attach the children already loaded for a folder, so a re-listing of the
@@ -20,14 +9,21 @@ export function setChildrenAt(nodes: FsNode[], path: string, children: FsNode[])
20
9
  * RELATIVE to the listed root — which is why the caller must drop both that set
21
10
  * and the cache when the listed root changes: `src/` under one workspace would
22
11
  * otherwise rehydrate into `src/` under the next.
12
+ *
13
+ * The fold runs HERE because this is the one function that builds a rendered
14
+ * tree — every server read, every realtime snapshot and every local edit goes
15
+ * through it. Folding at a call site instead would let a create draw a tree the
16
+ * next read undoes.
23
17
  */
24
18
  export function rehydrateTree(nodes: FsNode[], cache: Record<string, FsNode[]>): FsNode[] {
25
- return nodes.map(node => {
26
- if (node.type !== 'folder') return node;
27
- const cached = cache[node.path];
28
- if (!cached) return node;
29
- return { ...node, children: rehydrateTree(cached, cache) };
30
- });
19
+ return foldSingleChildFolders(nodes.map(node => rehydrateNode(node, cache)));
20
+ }
21
+
22
+ function rehydrateNode(node: FsNode, cache: Record<string, FsNode[]>): FsNode {
23
+ if (node.type !== 'folder') return node;
24
+ const cached = cache[node.path];
25
+ if (!cached) return node;
26
+ return { ...node, children: rehydrateTree(cached, cache) };
31
27
  }
32
28
 
33
29
  /** The folder a path lives in: `.` for a top-level entry. */
@@ -75,54 +71,150 @@ export interface TreeEdit {
75
71
  * reads the real directory, which by then holds the new entry. The chain nodes
76
72
  * that ARE inserted carry no `children` array, so the row's own expand effect
77
73
  * reads them from disk like any other unread folder.
74
+ *
75
+ * A row may already stand for a CHAIN of directories (`lib/deep`), because a
76
+ * single-child folder is collapsed — by the server when it lists, and by
77
+ * `rehydrateTree` for the chains this function creates. Descending into that
78
+ * row by its own path is what keeps a create from inserting a SECOND row for a
79
+ * directory the tree already draws; a chain row that covers the destination but
80
+ * is not the destination itself is skipped, so `lib/other/x.ts` never lands
81
+ * inside `lib/deep`.
78
82
  */
79
83
  export function placeEntry(nodes: FsNode[], cache: Record<string, FsNode[]>, entry: FsNode): TreeEdit {
80
84
  const segments = entry.path.split('/');
81
- const nextNodes = [...nodes];
85
+ const parentPath = segments.slice(0, -1).join('/');
86
+ let nextNodes = [...nodes];
82
87
  const nextCache = { ...cache };
83
88
  let container = nextNodes;
84
89
  let containerKey: string | null = null;
85
90
 
86
- for (let i = 0; i < segments.length - 1; i += 1) {
91
+ for (let i = 0; i < segments.length - 1;) {
87
92
  const folderPath = segments.slice(0, i + 1).join('/');
88
- const existing = container.find((node) => node.path === folderPath);
89
- const known = existing ? (nextCache[folderPath] ?? existing.children) : null;
90
- if (existing && !Array.isArray(known)) return { nodes, cache };
93
+ const existing = container.find((node) => node.type === 'folder' && node.path === folderPath);
94
+ const chain = existing ? null : container.find((node) => node.type === 'folder'
95
+ && node.path.startsWith(`${folderPath}/`)
96
+ && (parentPath === node.path || parentPath.startsWith(`${node.path}/`)));
97
+ const node = existing ?? chain;
98
+ const known = node ? (nextCache[node.path] ?? node.children) : null;
99
+ if (node && !Array.isArray(known)) return { nodes, cache };
100
+ if (!node) {
101
+ // No row names `folderPath`, but a collapsed chain row may still STAND for
102
+ // it (`lib/deep` while the entry is `lib/other/x.ts`). Inserting a fresh
103
+ // `lib` beside it would draw `deep` twice — once inside the new row and
104
+ // once as its own — so the chain is unfolded back into the rows the
105
+ // server would list: `lib` holding `deep`, whose children stay in the
106
+ // cache under their unchanged paths.
107
+ const unfolded = unfoldRow(container, nextCache, folderPath);
108
+ if (unfolded) {
109
+ container = unfolded;
110
+ const top = container.find((node) => node.path === folderPath);
111
+ const children = Array.isArray(top?.children) ? [...top.children] : [];
112
+ nextCache[folderPath] = children;
113
+ container = children;
114
+ containerKey = folderPath;
115
+ i += 1;
116
+ continue;
117
+ }
118
+ }
91
119
  const children = Array.isArray(known) ? [...known] : [];
92
- if (!existing) container.push({ name: segments[i], path: folderPath, type: 'folder', ignored: false });
120
+ if (node) {
121
+ nextCache[node.path] = children;
122
+ container = children;
123
+ containerKey = node.path;
124
+ // The row names every directory of its chain, so the walk resumes past
125
+ // all of them rather than re-deriving a folder that is already drawn.
126
+ i = node.path.split('/').length;
127
+ continue;
128
+ }
129
+ container.push({ name: segments[i], path: folderPath, type: 'folder', ignored: false });
93
130
  nextCache[folderPath] = children;
94
131
  container = children;
95
132
  containerKey = folderPath;
133
+ i += 1;
96
134
  }
97
135
 
98
136
  container.push(entry);
99
137
  if (containerKey !== null) nextCache[containerKey] = sortEntries(container);
100
- return { nodes: sortEntries(nextNodes), cache: nextCache };
138
+ nextNodes = sortEntries(nextNodes);
139
+ return { nodes: nextNodes, cache: nextCache };
140
+ }
141
+
142
+ /**
143
+ * Replace a collapsed chain row with the rows the server would list for it.
144
+ *
145
+ * `folderPath` names the OUTERMOST directory of the chain, so the row is
146
+ * rebuilt as that directory holding the rest of the chain one level at a time.
147
+ * Mutates `container` in place (the caller owns a copy) and returns it, or
148
+ * `null` when no row in it stands for `folderPath`.
149
+ */
150
+ function unfoldRow(container: FsNode[], cache: Record<string, FsNode[]>, folderPath: string): FsNode[] | null {
151
+ const index = container.findIndex((node) => node.type === 'folder' && node.path.startsWith(`${folderPath}/`));
152
+ if (index === -1) return null;
153
+ const row = container[index]!;
154
+ const rest = row.path.slice(folderPath.length + 1).split('/');
155
+ const deepestChildren = cache[row.path] ?? row.children;
156
+ if (Array.isArray(deepestChildren)) cache[row.path] = deepestChildren;
157
+ let built: FsNode = {
158
+ name: rest[rest.length - 1]!,
159
+ path: row.path,
160
+ type: 'folder',
161
+ ignored: row.ignored,
162
+ ...(Array.isArray(deepestChildren) ? { children: deepestChildren } : {}),
163
+ };
164
+ for (let i = rest.length - 2; i >= 0; i -= 1) {
165
+ built = {
166
+ name: rest[i]!,
167
+ path: `${folderPath}/${rest.slice(0, i + 1).join('/')}`,
168
+ type: 'folder',
169
+ ignored: false,
170
+ children: [built],
171
+ };
172
+ }
173
+ // The outermost row is the directory the entry needed, holding the chain
174
+ // beneath it — its own name and path, so a later step finds it by
175
+ // `folderPath`, and the chain's leaf keeps its own children.
176
+ container[index] = { name: baseNameOf(folderPath), path: folderPath, type: 'folder', ignored: false, children: [built] };
177
+ return container;
101
178
  }
102
179
 
103
180
  /**
104
181
  * Remove the entry at `path` from wherever it lives, leaving the rest of the
105
182
  * tree untouched. Returns `entry: null` when the path is not in this tree —
106
183
  * a collapsed folder's contents are not, and that is not an error.
184
+ *
185
+ * The search walks EVERY container rather than looking the parent up by path:
186
+ * a collapsed chain row's own path is the deepest directory, so its parent key
187
+ * is one the cache does not hold (`com/java/app/package` lives in `com/java`,
188
+ * and no `com/java/app` key exists). Deriving the container from the path would
189
+ * report a rename of that row as "not in this tree" and fall back to a reload.
107
190
  */
108
191
  export function takeEntry(
109
192
  nodes: FsNode[],
110
193
  cache: Record<string, FsNode[]>,
111
194
  path: string,
112
195
  ): TreeEdit & { entry: FsNode | null } {
196
+ const atRoot = nodes.find((node) => node.path === path);
197
+ if (atRoot) return { entry: atRoot, nodes: nodes.filter((node) => node.path !== path), cache };
198
+ // The container the server would have listed it in, when the tree holds it.
113
199
  const parent = parentPathOf(path);
114
- if (parent === '.') {
115
- const entry = nodes.find((node) => node.path === path) ?? null;
116
- if (!entry) return { entry: null, nodes, cache };
117
- return { entry, nodes: nodes.filter((node) => node.path !== path), cache };
200
+ const direct = cache[parent];
201
+ const directEntry = direct?.find((node) => node.path === path);
202
+ if (direct && directEntry) {
203
+ return { entry: directEntry, nodes, cache: { ...cache, [parent]: direct.filter((node) => node.path !== path) } };
118
204
  }
119
- const siblings = cache[parent];
120
- const entry = siblings?.find((node) => node.path === path) ?? null;
121
- if (!entry || !siblings) return { entry: null, nodes, cache };
205
+ // Otherwise the DEEPEST drawn container above it: a chain row's key is its
206
+ // deepest directory, so its children live under a key the path's own parent
207
+ // does not name.
208
+ const candidates = Object.keys(cache)
209
+ .filter((key) => path.startsWith(`${key}/`) && cache[key]!.some((node) => node.path === path))
210
+ .sort((a, b) => b.length - a.length);
211
+ const key = candidates[0];
212
+ if (!key) return { entry: null, nodes, cache };
213
+ const siblings = cache[key]!;
122
214
  return {
123
- entry,
215
+ entry: siblings.find((node) => node.path === path)!,
124
216
  nodes,
125
- cache: { ...cache, [parent]: siblings.filter((node) => node.path !== path) },
217
+ cache: { ...cache, [key]: siblings.filter((node) => node.path !== path) },
126
218
  };
127
219
  }
128
220
 
@@ -0,0 +1,87 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The Files panel's collapsed folder chains.
8
+ *
9
+ * Two rules, and each is a way the panel would show the wrong thing:
10
+ *
11
+ * 1. **A folder holding exactly one folder is ONE row.** A row per directory
12
+ * level spends four rows on a package tree (`com`, `java`, `app`,
13
+ * `package`) and makes the reader scroll past all of them to reach the files
14
+ * inside. The row's IDENTITY is the deepest directory — what an expand
15
+ * reads, and what a rename, delete or drag acts on — while only its `name`
16
+ * carries the chain.
17
+ * 2. **A reveal expands the rows that are DRAWN.** Deriving the ancestors from
18
+ * the path's own segments puts `lib` and `lib/deep` in the expansion set
19
+ * when the single row is `lib/deep`, and the next refresh then reads each of
20
+ * them from disk for nothing.
21
+ */
22
+
23
+ import { describe, expect, test } from 'bun:test';
24
+ import type { FsNode } from '@/shared/types';
25
+ import { drawnAncestors, foldSingleChildFolders } from '@/shared/lib/fs/folder-chain';
26
+
27
+ const baseNameOf = (path: string): string => path.slice(path.lastIndexOf('/') + 1);
28
+ const file = (path: string): FsNode => ({ name: baseNameOf(path), path, type: 'file' });
29
+ const folder = (path: string, children?: FsNode[]): FsNode =>
30
+ children ? { name: baseNameOf(path), path, type: 'folder', children } : { name: baseNameOf(path), path, type: 'folder' };
31
+
32
+ describe('foldSingleChildFolders', () => {
33
+ test('a folder holding exactly one folder becomes one row naming the chain', () => {
34
+ const tree = [folder('com', [folder('com/java', [folder('com/java/app', [file('com/java/app/Main.java')])])])];
35
+ const folded = foldSingleChildFolders(tree);
36
+
37
+ expect(folded).toHaveLength(1);
38
+ expect(folded[0]!.name).toBe('com/java/app');
39
+ expect(folded[0]!.path).toBe('com/java/app');
40
+ expect(folded[0]!.children?.map((node) => node.path)).toEqual(['com/java/app/Main.java']);
41
+ });
42
+
43
+ test('a folder holding a folder AND a file is not folded', () => {
44
+ const tree = [folder('src', [folder('src/deep', []), file('src/a.ts')])];
45
+ const folded = foldSingleChildFolders(tree);
46
+
47
+ expect(folded[0]!.name).toBe('src');
48
+ expect(folded[0]!.children).toHaveLength(2);
49
+ });
50
+
51
+ test('a folder whose children are not loaded cannot fold', () => {
52
+ const unread: FsNode = { name: 'src', path: 'src', type: 'folder' };
53
+ expect(foldSingleChildFolders([unread])[0]).toBe(unread);
54
+ });
55
+
56
+ test('an unchanged tree keeps its own array and node identities', () => {
57
+ const tree = [folder('src', [file('src/a.ts'), file('src/b.ts')])];
58
+ const folded = foldSingleChildFolders(tree);
59
+
60
+ expect(folded).toBe(tree);
61
+ expect(folded[0]).toBe(tree[0]);
62
+ });
63
+
64
+ test('the folded row wears the deepest directory’s ignored flag', () => {
65
+ const tree = [folder('a', [{ name: 'b', path: 'a/b', type: 'folder', ignored: true, children: [] }])];
66
+ expect(foldSingleChildFolders(tree)[0]!.ignored).toBe(true);
67
+ });
68
+ });
69
+
70
+ describe('drawnAncestors', () => {
71
+ test('a reveal expands only the rows that exist, never a chain’s inner levels', () => {
72
+ const tree = [folder('lib/deep', [])];
73
+
74
+ // `lib` is not a row — `lib/deep` is — so expanding it would put a path in
75
+ // the expansion set that nothing draws and the next refresh re-reads.
76
+ expect(drawnAncestors(tree, 'lib/deep/x.ts')).toEqual(['lib/deep']);
77
+ expect(drawnAncestors(tree, 'lib/x.ts')).toEqual([]);
78
+ });
79
+
80
+ test('a nested row’s ancestors are its drawn parents, outermost first', () => {
81
+ const tree = [folder('src', [folder('src/nested', [])])];
82
+
83
+ expect(drawnAncestors(tree, 'src/nested/a.ts')).toEqual(['src', 'src/nested']);
84
+ expect(drawnAncestors(tree, 'a.ts')).toEqual([]);
85
+ });
86
+ });
87
+
@@ -0,0 +1,91 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The Files panel's COLLAPSED folder chains.
8
+ *
9
+ * A folder that holds exactly one folder and nothing else is drawn as a single
10
+ * row naming the whole chain (`com/java/app/package`), because a row per
11
+ * directory level spends four rows to reach the files inside and every editor
12
+ * folds that. The server collapses the same chains when it lists a directory
13
+ * (`folderChain`, `lib/fs/listing.ts`); this half exists for the trees the
14
+ * CLIENT builds — a create or a rename inserts a chain the server has not
15
+ * listed yet — and it is applied by `rehydrateTree`, which is the one function
16
+ * that builds a rendered tree.
17
+ *
18
+ * Split from `file-tree.ts` (which owns the edits) because the two rules are
19
+ * read by different callers: the panel's listing hook needs the fold and the
20
+ * reveal, while the create/rename paths need the placement.
21
+ */
22
+
23
+ import type { FsNode } from '@/shared/types';
24
+
25
+ /**
26
+ * Collapse a folder that holds exactly one FOLDER and nothing else into that
27
+ * child, so one row names the whole chain.
28
+ *
29
+ * The folded row's IDENTITY is the deepest directory — that is what its
30
+ * children are, what an expand reads, and what a rename, delete or drag acts
31
+ * on. Only its `name` carries the chain, so the label says where the row is.
32
+ *
33
+ * A folder whose children are NOT loaded cannot fold: the client does not know
34
+ * whether it holds one folder or forty files, and guessing would hide a listing
35
+ * the server never produced.
36
+ */
37
+ export function foldSingleChildFolders(nodes: FsNode[]): FsNode[] {
38
+ const next = nodes.map(foldNode);
39
+ // Identity is what the row components diff on, so an unchanged list keeps its
40
+ // own array rather than re-rendering every row on every commit.
41
+ return next.some((node, index) => node !== nodes[index]) ? next : nodes;
42
+ }
43
+
44
+ function foldNode(node: FsNode): FsNode {
45
+ if (node.type !== 'folder' || !Array.isArray(node.children)) return node;
46
+ const children = foldSingleChildFolders(node.children);
47
+ let current: FsNode = children === node.children ? node : { ...node, children };
48
+ while (Array.isArray(current.children) && current.children.length === 1) {
49
+ const only = current.children[0];
50
+ if (only.type !== 'folder') break;
51
+ current = {
52
+ ...current,
53
+ name: `${current.name}/${only.name}`,
54
+ path: only.path,
55
+ children: only.children,
56
+ // The row names the deepest directory now, so that is the one whose
57
+ // git-ignore answer it wears (a rule matching an outer level matches it
58
+ // too — verified against `git check-ignore`).
59
+ ignored: only.ignored ?? current.ignored,
60
+ };
61
+ }
62
+ return current;
63
+ }
64
+
65
+ /**
66
+ * The paths of the DRAWN rows that contain `path`, outermost first.
67
+ *
68
+ * This is what a reveal expands. It cannot be the path's own segments: a
69
+ * collapsed chain is one row whose path is the deepest directory, so `lib` and
70
+ * `lib/deep` are not rows at all — expanding them would leave the expansion set
71
+ * holding paths nothing draws, which the next refresh then re-reads from disk
72
+ * for nothing.
73
+ */
74
+ export function drawnAncestors(nodes: FsNode[], path: string): string[] {
75
+ const folders = new Set<string>();
76
+ const walk = (list: FsNode[]) => {
77
+ for (const node of list) {
78
+ if (node.type !== 'folder') continue;
79
+ folders.add(node.path);
80
+ if (Array.isArray(node.children)) walk(node.children);
81
+ }
82
+ };
83
+ walk(nodes);
84
+ const segments = path.split('/').slice(0, -1);
85
+ const ancestors: string[] = [];
86
+ for (let i = 0; i < segments.length; i += 1) {
87
+ const prefix = segments.slice(0, i + 1).join('/');
88
+ if (folders.has(prefix)) ancestors.push(prefix);
89
+ }
90
+ return ancestors;
91
+ }
@@ -22,7 +22,7 @@ import { describe, expect, test } from 'bun:test';
22
22
  import type { FsNode } from '@/shared/types';
23
23
  import { COMMIT_PAGE_SIZE } from '@/shared/lib/fs/commit-page';
24
24
  import { normalizeCommits } from '@/shared/lib/fs/commit-row';
25
- import { rehydrateTree, setChildrenAt } from '@/shared/lib/fs/file-tree';
25
+ import { rehydrateTree } from '@/shared/lib/fs/file-tree';
26
26
  import { buildFsRawUrl, toAbsolutePath } from '@/shared/lib/fs/paths';
27
27
 
28
28
  const file = (path: string): FsNode => ({ name: path.split('/').pop() ?? path, path, type: 'file' });
@@ -111,53 +111,6 @@ describe('toAbsolutePath', () => {
111
111
  });
112
112
  });
113
113
 
114
- describe('setChildrenAt', () => {
115
- test('the matching node receives the new children', () => {
116
- const tree = [folder('a', [file('a/x.ts')]), file('b.ts')];
117
- const result = setChildrenAt(tree, 'a', [file('a/y.ts')]);
118
- expect(result[0].children?.map(c => c.path)).toEqual(['a/y.ts']);
119
- });
120
-
121
- test('a nested folder is patched at any depth', () => {
122
- const tree = [folder('a', [folder('a/b', [file('a/b/c.ts')])])];
123
- const result = setChildrenAt(tree, 'a/b', [file('a/b/d.ts')]);
124
- expect(result[0].children?.[0].children?.map(c => c.path)).toEqual(['a/b/d.ts']);
125
- });
126
-
127
- test('the original tree is never mutated', () => {
128
- const tree = [folder('a', [file('a/x.ts')])];
129
- setChildrenAt(tree, 'a', [file('a/y.ts')]);
130
- expect(tree[0].children?.map(c => c.path)).toEqual(['a/x.ts']);
131
- });
132
-
133
- test('unrelated file nodes keep their identity', () => {
134
- const tree = [folder('a', [file('a/x.ts')]), file('b.ts')];
135
- const result = setChildrenAt(tree, 'a/x.ts', []);
136
- expect(result[1]).toBe(tree[1]);
137
- });
138
-
139
- test('a path that does not exist leaves the shape unchanged', () => {
140
- const tree = [folder('a', [file('a/x.ts')]), file('b.ts')];
141
- const result = setChildrenAt(tree, 'missing', []);
142
- expect(result[0].children?.[0]).toBe(tree[0].children?.[0]);
143
- expect(result[1]).toBe(tree[1]);
144
- });
145
-
146
- test('a folder without a children array is left alone on a miss', () => {
147
- const childless: FsNode = { name: 'a', path: 'a', type: 'folder' };
148
- const result = setChildrenAt([childless], 'other', []);
149
- expect(result[0]).toBe(childless);
150
- expect(result[0].children).toBeUndefined();
151
- });
152
-
153
- test('a folder without a children array can still be given children', () => {
154
- const childless: FsNode = { name: 'a', path: 'a', type: 'folder' };
155
- const result = setChildrenAt([childless], 'a', [file('a/x.ts')]);
156
- expect(result[0].children?.map(c => c.path)).toEqual(['a/x.ts']);
157
- expect(childless.children).toBeUndefined();
158
- });
159
- });
160
-
161
114
  describe('rehydrateTree', () => {
162
115
  test('a cached folder gets its children back', () => {
163
116
  const cache = { a: [file('a/x.ts')] };
@@ -166,9 +119,10 @@ describe('rehydrateTree', () => {
166
119
  });
167
120
 
168
121
  test('nested cached folders are rehydrated recursively', () => {
169
- const cache = { a: [folder('a/b', [])], 'a/b': [file('a/b/c.ts')] };
122
+ const cache = { a: [folder('a/b', []), file('a/y.ts')], 'a/b': [file('a/b/c.ts')] };
170
123
  const result = rehydrateTree([folder('a', [])], cache);
171
- expect(result[0].children?.[0].children?.map(c => c.path)).toEqual(['a/b/c.ts']);
124
+ const nested = result[0].children?.find((child) => child.path === 'a/b');
125
+ expect(nested?.children?.map(c => c.path)).toEqual(['a/b/c.ts']);
172
126
  });
173
127
 
174
128
  test('an uncached folder keeps its own children untouched', () => {
@@ -212,7 +212,10 @@ describe('realtime channel retry lifecycle', () => {
212
212
  expect(ping).toBeDefined();
213
213
  socket.onmessage?.({ data: JSON.stringify({ t: 'pong', id: ping!.id }) });
214
214
 
215
- vi.advanceTimersByTime(SLOW_RETRY_MS);
215
+ // Short of the idle-probe window: an answered probe must not cause a
216
+ // reconnect of its own. (Past `IDLE_PROBE_AFTER_MS` the watchdog probes
217
+ // again by design — see the idle case below.)
218
+ vi.advanceTimersByTime(20_000);
216
219
  expect(FakeSocket.instances).toHaveLength(1);
217
220
  expect(currentStatus()).toBe('open');
218
221
  });
@@ -299,3 +302,4 @@ describe('realtime channel retry lifecycle', () => {
299
302
  expect(() => resetRealtimeClient()).not.toThrow();
300
303
  });
301
304
  });
305
+