@kabmo-labs/kabmo 0.8.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 (391) hide show
  1. package/README.md +52 -0
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +336 -336
  4. package/dist/client/static/js/index-18xe7655.css +1 -0
  5. package/dist/client/static/js/{index-qwpvd9fv.js → index-nxpe7835.js} +933 -898
  6. package/package.json +11 -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 +122 -0
  10. package/src/client/components/common/code-surface/documents.ts +286 -0
  11. package/src/client/components/common/code-surface/extensions.test.ts +115 -0
  12. package/src/client/components/common/code-surface/extensions.ts +295 -0
  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/handle.ts +102 -0
  17. package/src/client/components/common/code-surface/indent-guide.ts +104 -0
  18. package/src/client/components/common/code-surface/indent.test.ts +239 -0
  19. package/src/client/components/common/code-surface/indent.ts +124 -0
  20. package/src/client/components/common/code-surface/index.tsx +283 -314
  21. package/src/client/components/common/code-surface/keymap.test.ts +133 -0
  22. package/src/client/components/common/code-surface/keymap.ts +256 -0
  23. package/src/client/components/common/code-surface/line-commands.test.ts +90 -0
  24. package/src/client/components/common/code-surface/line-commands.ts +103 -0
  25. package/src/client/components/common/code-surface/marks.ts +74 -0
  26. package/src/client/components/common/code-surface/props.ts +87 -0
  27. package/src/client/components/common/code-surface/shiki.ts +234 -0
  28. package/src/client/components/common/code-surface/stream-language.test.ts +118 -0
  29. package/src/client/components/common/code-surface/stream-language.ts +123 -0
  30. package/src/client/components/common/code-surface/sync.test.ts +185 -0
  31. package/src/client/components/common/code-surface/sync.ts +58 -0
  32. package/src/client/components/common/code-surface/theme.test.ts +237 -0
  33. package/src/client/components/common/code-surface/theme.ts +224 -0
  34. package/src/client/components/common/code-surface/typing.test.ts +97 -0
  35. package/src/client/components/common/code-surface/typing.ts +111 -0
  36. package/src/client/components/common/editor-view-buttons/index.test.tsx +148 -0
  37. package/src/client/components/common/editor-view-buttons/index.tsx +109 -0
  38. package/src/client/components/common/shell-update-button/Modal.tsx +163 -0
  39. package/src/client/components/common/shell-update-button/index.test.tsx +251 -0
  40. package/src/client/components/common/shell-update-button/index.tsx +125 -0
  41. package/src/client/components/common/toolbar-row/index.test.tsx +215 -0
  42. package/src/client/components/common/toolbar-row/index.tsx +260 -0
  43. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +58 -78
  44. package/src/client/components/layout/desktop-layout/index.tsx +65 -67
  45. package/src/client/components/layout/desktop-layout/resizer/index.tsx +6 -3
  46. package/src/client/components/layout/session-sidebar/Footer.tsx +8 -15
  47. package/src/client/components/mobile/LayoutWrapper.tsx +1 -0
  48. package/src/client/components/mobile/mobile-right-sidebar/FullDiff.tsx +3 -0
  49. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +98 -71
  50. package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +20 -1
  51. package/src/client/components/mobile/mobile-right-sidebar/OutlineButton.test.tsx +137 -0
  52. package/src/client/components/mobile/mobile-right-sidebar/OutlineButton.tsx +14 -3
  53. package/src/client/components/mobile/mobile-right-sidebar/editor-props.ts +25 -0
  54. package/src/client/components/mobile/mobile-session-sidebar/Footer.tsx +9 -21
  55. package/src/client/components/settings/Modal.tsx +8 -0
  56. package/src/client/components/settings/categories/AppearanceSettings.tsx +9 -0
  57. package/src/client/components/settings/categories/appearance-settings/EditorIndentSection.tsx +87 -0
  58. package/src/client/components/settings/categories/appearance-settings/EditorThemeSection.test.tsx +167 -0
  59. package/src/client/components/settings/categories/appearance-settings/EditorThemeSection.tsx +184 -0
  60. package/src/client/components/settings/categories/appearance-settings/EditorTypingSection.tsx +99 -0
  61. package/src/client/components/settings/categories/project-settings/WorktreeSettings.tsx +8 -2
  62. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +16 -8
  63. package/src/client/components/workspace/chat-timeline/SystemNotice.tsx +9 -4
  64. package/src/client/components/workspace/chat-timeline/TaskResultContent.tsx +9 -7
  65. package/src/client/components/workspace/chat-timeline/chat-input/expansion.ts +9 -4
  66. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +15 -11
  67. package/src/client/components/workspace/chat-timeline/chat-input/stretch-send.test.tsx +165 -0
  68. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.test.tsx +132 -0
  69. package/src/client/components/workspace/chat-timeline/target-picker/WorkspacePicker.tsx +120 -0
  70. package/src/client/components/workspace/chat-timeline/target-picker/WorktreePicker.test.tsx +319 -0
  71. package/src/client/components/workspace/chat-timeline/target-picker/WorktreePicker.tsx +285 -0
  72. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.test.ts +2 -0
  73. package/src/client/components/workspace/chat-timeline/tool-renderers/hashline-patch/index.tsx +34 -29
  74. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/AstEdit.tsx +11 -9
  75. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.tsx +10 -7
  76. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Edit.tsx +25 -8
  77. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Eval.tsx +8 -11
  78. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/FileMatches.test.ts +114 -0
  79. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/FileMatches.tsx +111 -0
  80. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.tsx +7 -7
  81. package/src/client/components/workspace/chat-timeline/tool-renderers/panels/SearchTool.tsx +9 -59
  82. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ArtifactBody.tsx +2 -7
  83. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.test.ts +43 -30
  84. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.tsx +49 -32
  85. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ExcerptCode.tsx +8 -8
  86. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/HighlightedCode.tsx +35 -0
  87. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/JsonCodeBlock.tsx +4 -4
  88. package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +179 -0
  89. package/src/client/components/workspace/diff-panel/ThemedMergeDiffView.tsx +24 -0
  90. package/src/client/components/workspace/diff-panel/Toolbar.tsx +224 -186
  91. package/src/client/components/workspace/diff-panel/ToolbarSegments.tsx +98 -0
  92. package/src/client/components/workspace/diff-panel/index.tsx +76 -145
  93. package/src/client/components/workspace/diff-panel/merge.ts +315 -0
  94. package/src/client/components/workspace/editor/Body.tsx +28 -7
  95. package/src/client/components/workspace/editor/CommandPalette.tsx +6 -6
  96. package/src/client/components/workspace/editor/ConflictStrip.tsx +77 -0
  97. package/src/client/components/workspace/editor/Dialogs.tsx +53 -0
  98. package/src/client/components/workspace/editor/EmptyEditor.tsx +23 -0
  99. package/src/client/components/workspace/editor/ExternalChangeStrip.tsx +61 -0
  100. package/src/client/components/workspace/editor/FileDocument.tsx +146 -63
  101. package/src/client/components/workspace/editor/FilePane.tsx +47 -7
  102. package/src/client/components/workspace/editor/FindWidget.test.ts +9 -5
  103. package/src/client/components/workspace/editor/FindWidget.tsx +36 -2
  104. package/src/client/components/workspace/editor/GoToLineModal.tsx +123 -0
  105. package/src/client/components/workspace/editor/Notices.tsx +71 -0
  106. package/src/client/components/workspace/editor/Pane.tsx +118 -0
  107. package/src/client/components/workspace/editor/PluginColumn.tsx +32 -0
  108. package/src/client/components/workspace/editor/QuickOpenModal.tsx +146 -0
  109. package/src/client/components/workspace/editor/SaveAsModal.tsx +175 -0
  110. package/src/client/components/workspace/editor/SplitColumn.tsx +98 -0
  111. package/src/client/components/workspace/editor/SplitPane.tsx +57 -0
  112. package/src/client/components/workspace/editor/StatusBar.tsx +106 -0
  113. package/src/client/components/workspace/editor/TabContextMenu.tsx +75 -0
  114. package/src/client/components/workspace/editor/Tabs.tsx +195 -18
  115. package/src/client/components/workspace/editor/Toolbar.tsx +110 -140
  116. package/src/client/components/workspace/editor/index.tsx +201 -193
  117. package/src/client/components/workspace/editor/props.ts +64 -0
  118. package/src/client/components/workspace/editor/toolbar-clusters.tsx +300 -0
  119. package/src/client/components/workspace/file-explorer/Modals.tsx +14 -1
  120. package/src/client/components/workspace/file-explorer/OutlineSection.test.tsx +18 -0
  121. package/src/client/components/workspace/file-explorer/OutlineSection.tsx +10 -3
  122. package/src/client/components/workspace/file-explorer/TreeItem.tsx +2 -1
  123. package/src/client/components/workspace/file-explorer/clipboard.test.ts +3 -1
  124. package/src/client/components/workspace/file-explorer/index.test.ts +61 -0
  125. package/src/client/components/workspace/file-explorer/index.tsx +10 -48
  126. package/src/client/components/workspace/file-explorer/local-edit.test.ts +8 -3
  127. package/src/client/components/workspace/search-panel/Body.tsx +7 -2
  128. package/src/client/components/workspace/worktree-modal/index.test.tsx +118 -0
  129. package/src/client/hooks/chat/composer/file-drop.ts +22 -37
  130. package/src/client/hooks/chat/composer/pipeline.test.ts +76 -0
  131. package/src/client/hooks/chat/composer/pipeline.ts +12 -1
  132. package/src/client/hooks/chat/omp/reconnect.test.ts +338 -0
  133. package/src/client/hooks/chat/omp/reconnect.ts +187 -0
  134. package/src/client/hooks/chat/omp/run-liveness.test.ts +71 -0
  135. package/src/client/hooks/chat/omp/run-liveness.ts +65 -0
  136. package/src/client/hooks/chat/omp/session-list.activity.test.ts +114 -0
  137. package/src/client/hooks/chat/omp/session-list.test-util.ts +164 -0
  138. package/src/client/hooks/chat/omp/session-list.test.ts +30 -150
  139. package/src/client/hooks/chat/omp/session-list.tsx +69 -9
  140. package/src/client/hooks/chat/timeline/generating.ts +19 -2
  141. package/src/client/hooks/chat/timeline/index.ts +19 -24
  142. package/src/client/hooks/chat/timeline/lost-run.test.ts +329 -0
  143. package/src/client/hooks/chat/timeline/lost-run.ts +177 -0
  144. package/src/client/hooks/chat/timeline/session-load.test-util.ts +205 -0
  145. package/src/client/hooks/chat/timeline/session-load.test.ts +61 -162
  146. package/src/client/hooks/chat/timeline/session-load.ts +69 -37
  147. package/src/client/hooks/chat/timeline/session-switch.test.ts +65 -0
  148. package/src/client/hooks/chat/timeline/session-switch.ts +63 -0
  149. package/src/client/hooks/code/deferred-highlight.test.ts +182 -0
  150. package/src/client/hooks/code/deferred-highlight.ts +129 -0
  151. package/src/client/hooks/editor/caret-position.test.ts +58 -0
  152. package/src/client/hooks/editor/caret-position.ts +48 -0
  153. package/src/client/hooks/editor/file-editor-types.ts +200 -0
  154. package/src/client/hooks/editor/find-history.test.ts +132 -0
  155. package/src/client/hooks/editor/find-history.ts +46 -0
  156. package/src/client/hooks/editor/find-selection-range.test.ts +100 -0
  157. package/src/client/hooks/editor/find-selection-range.ts +49 -0
  158. package/src/client/hooks/editor/find-state.ts +14 -1
  159. package/src/client/hooks/editor/font.test.ts +0 -93
  160. package/src/client/hooks/editor/indent-settings.test.ts +104 -0
  161. package/src/client/hooks/editor/indent-settings.ts +88 -0
  162. package/src/client/hooks/editor/quick-open.test.ts +141 -0
  163. package/src/client/hooks/editor/quick-open.ts +127 -0
  164. package/src/client/hooks/editor/save-as.test.ts +156 -0
  165. package/src/client/hooks/editor/save-as.ts +122 -0
  166. package/src/client/hooks/editor/save-file.test.ts +179 -0
  167. package/src/client/hooks/editor/save-file.ts +73 -28
  168. package/src/client/hooks/editor/theme.test.ts +206 -0
  169. package/src/client/hooks/editor/theme.ts +82 -0
  170. package/src/client/hooks/editor/typing-settings.test.ts +73 -0
  171. package/src/client/hooks/editor/typing-settings.ts +65 -0
  172. package/src/client/hooks/editor/use-close-guard.ts +98 -0
  173. package/src/client/hooks/editor/use-editor-drop-overlay.test.ts +159 -0
  174. package/src/client/hooks/editor/use-editor-drop.shell.test.ts +224 -0
  175. package/src/client/hooks/editor/use-editor-drop.test.ts +101 -0
  176. package/src/client/hooks/editor/use-editor-drop.ts +120 -77
  177. package/src/client/hooks/editor/use-editor-find.test.ts +2 -2
  178. package/src/client/hooks/editor/use-editor-find.ts +45 -10
  179. package/src/client/hooks/editor/use-editor-panel-keys.ts +152 -0
  180. package/src/client/hooks/editor/use-editor-tab-state.ts +71 -0
  181. package/src/client/hooks/editor/use-external-change-notice.ts +71 -0
  182. package/src/client/hooks/editor/use-external-change.test.ts +88 -0
  183. package/src/client/hooks/editor/use-external-change.ts +146 -0
  184. package/src/client/hooks/editor/use-file-editor.ts +83 -116
  185. package/src/client/hooks/editor/use-file-read.ts +16 -8
  186. package/src/client/hooks/editor/use-file-writes.ts +28 -3
  187. package/src/client/hooks/editor/use-mobile-editor-find.ts +69 -0
  188. package/src/client/hooks/editor/use-mobile-reveal.ts +53 -0
  189. package/src/client/hooks/editor/use-outline-view.test.ts +182 -0
  190. package/src/client/hooks/editor/use-outline-view.ts +94 -7
  191. package/src/client/hooks/editor/use-scroll-restore.test.ts +55 -2
  192. package/src/client/hooks/editor/use-scroll-restore.ts +124 -4
  193. package/src/client/hooks/editor/use-search-reveal-jump.ts +57 -0
  194. package/src/client/hooks/editor/use-search-reveal.ts +1 -1
  195. package/src/client/hooks/editor/use-split-editor.test.tsx +169 -0
  196. package/src/client/hooks/editor/use-split-editor.ts +91 -0
  197. package/src/client/hooks/editor/use-unload-guard.tsx +117 -0
  198. package/src/client/hooks/editor/use-write-conflict.test.ts +164 -0
  199. package/src/client/hooks/editor/use-write-conflict.ts +152 -0
  200. package/src/client/hooks/editor/wheel-zoom.test.ts +141 -0
  201. package/src/client/hooks/editor/wheel-zoom.ts +96 -0
  202. package/src/client/hooks/ui/drag-presence.test.ts +193 -0
  203. package/src/client/hooks/ui/drag-presence.ts +108 -0
  204. package/src/client/hooks/ui/settings-modal.test.ts +115 -0
  205. package/src/client/hooks/ui/settings-modal.ts +56 -0
  206. package/src/client/hooks/ui/updates/shell-download.test.ts +2 -0
  207. package/src/client/hooks/ui/updates/shell-download.ts +55 -14
  208. package/src/client/hooks/ui/updates/shell-offer.test.ts +26 -0
  209. package/src/client/hooks/ui/updates/shell-update.test.ts +140 -0
  210. package/src/client/hooks/ui/updates/shell-update.ts +119 -0
  211. package/src/client/hooks/workspace/diff-file.ts +29 -10
  212. package/src/client/hooks/workspace/diff-git-actions.ts +147 -0
  213. package/src/client/hooks/workspace/editor-view.ts +61 -0
  214. package/src/client/hooks/workspace/file-listing.ts +36 -24
  215. package/src/client/hooks/workspace/file-tabs.test.ts +152 -5
  216. package/src/client/hooks/workspace/file-tabs.ts +70 -11
  217. package/src/client/hooks/workspace/file-tree-edits.ts +103 -0
  218. package/src/client/hooks/workspace/navbar-session-actions.ts +72 -0
  219. package/src/client/hooks/workspace/worktree/index.ts +20 -5
  220. package/src/client/lib/desktop/bridge.ts +45 -0
  221. package/src/client/lib/desktop/dropped-paths.ts +83 -0
  222. package/src/client/lib/editor/tab-menu.test.ts +127 -0
  223. package/src/client/lib/editor/tab-menu.ts +127 -0
  224. package/src/client/lib/editor-dirty-tabs.test.ts +102 -0
  225. package/src/client/lib/editor-dirty-tabs.ts +103 -0
  226. package/src/client/lib/native/resume.test.ts +66 -0
  227. package/src/client/lib/native/resume.ts +42 -0
  228. package/src/client/lib/search-reveal.ts +1 -1
  229. package/src/client/main.tsx +13 -1
  230. package/src/client/tailwind.css +75 -8
  231. package/src/server/lib/auth/guard.test.ts +28 -0
  232. package/src/server/lib/auth/guard.ts +10 -0
  233. package/src/server/lib/bundler/css.test.ts +31 -0
  234. package/src/server/lib/fs/listing.test.ts +81 -16
  235. package/src/server/lib/fs/listing.ts +72 -5
  236. package/src/server/lib/fs/worktree/create.ts +2 -3
  237. package/src/server/lib/fs/worktree/list.ts +13 -0
  238. package/src/server/lib/fs/worktree/remove.ts +3 -3
  239. package/src/server/lib/fs/write-conflict.test.ts +68 -0
  240. package/src/server/lib/fs/write-conflict.ts +84 -0
  241. package/src/server/lib/lifecycle/claim-port.test.ts +156 -0
  242. package/src/server/lib/lifecycle/port-guard.test.ts +20 -164
  243. package/src/server/lib/lifecycle/port-guard.ts +39 -7
  244. package/src/server/lib/lifecycle/probe-health.test.ts +166 -0
  245. package/src/server/lib/lifecycle/probe.test.ts +77 -164
  246. package/src/server/lib/lifecycle/probe.ts +121 -13
  247. package/src/server/lib/omp/rpc/frame-fold-signals.test.ts +112 -0
  248. package/src/server/lib/omp/rpc/frame-fold.ts +10 -5
  249. package/src/server/lib/omp/session/sidebar-activity.server.test.ts +162 -0
  250. package/src/server/lib/omp/session/sidebar-activity.server.ts +70 -0
  251. package/src/server/lib/omp/session/telemetry/compute.ts +13 -1
  252. package/src/server/lib/omp/session/telemetry.test.ts +33 -0
  253. package/src/server/lib/realtime/signals.server.ts +10 -0
  254. package/src/server/lib/realtime/topics/catalog.server.ts +4 -1
  255. package/src/server/lib/realtime/topics.server.ts +16 -0
  256. package/src/server/lib/updates/shell-assets.ts +94 -3
  257. package/src/server/lib/updates/shell-cache.ts +105 -0
  258. package/src/server/lib/updates/shell-pages.test.ts +33 -2
  259. package/src/server/lib/updates/shell-pages.ts +17 -5
  260. package/src/server/lib/updates/shell-release-desktop.test.ts +166 -0
  261. package/src/server/lib/updates/shell-release.test.ts +10 -27
  262. package/src/server/lib/updates/shell-release.ts +107 -111
  263. package/src/server/routes/fs/action-save-as.test.ts +82 -0
  264. package/src/server/routes/fs/action.test.ts +82 -0
  265. package/src/server/routes/fs/action.ts +54 -1
  266. package/src/server/routes/fs/exists.ts +11 -1
  267. package/src/server/routes/fs/git-merge.test.ts +11 -1
  268. package/src/server/routes/fs/read.test.ts +24 -7
  269. package/src/server/routes/fs/read.ts +6 -1
  270. package/src/server/routes/fs/scope.test.ts +15 -10
  271. package/src/server/routes/fs/scope.ts +3 -14
  272. package/src/server/routes/updates/android.ts +6 -1
  273. package/src/server/routes/updates/desktop.ts +29 -7
  274. package/src/server/routes/updates/ios.ts +5 -1
  275. package/src/server/routes/worktrees/list.ts +11 -1
  276. package/src/server/routes/worktrees/routes.test.ts +17 -0
  277. package/src/shared/lib/chat/timeline/omp-callbacks.test.ts +36 -0
  278. package/src/shared/lib/chat/timeline/omp-callbacks.ts +18 -0
  279. package/src/shared/lib/chat/timeline/run-signals.test.ts +2 -0
  280. package/src/shared/lib/code/editor/comment-syntax.test.ts +37 -0
  281. package/src/shared/lib/code/editor/comment-syntax.ts +55 -0
  282. package/src/shared/lib/code/editor/find-replace.test.ts +134 -0
  283. package/src/shared/lib/code/editor/{find.ts → find-replace.ts} +23 -107
  284. package/src/shared/lib/code/editor/{find.test.ts → find-scan.test.ts} +20 -74
  285. package/src/shared/lib/code/editor/find-scan.ts +198 -0
  286. package/src/shared/lib/code/editor/find-selection.test.ts +56 -0
  287. package/src/shared/lib/code/editor/indent-guide-rule.test.ts +65 -0
  288. package/src/shared/lib/code/editor/indent-guide-rule.ts +47 -0
  289. package/src/shared/lib/code/editor/indent-rules.test.ts +219 -0
  290. package/src/shared/lib/code/editor/indent-rules.ts +198 -0
  291. package/src/shared/lib/code/editor/keymap.test.ts +23 -0
  292. package/src/shared/lib/code/editor/keymap.ts +43 -6
  293. package/src/shared/lib/code/editor/lines.test.ts +7 -55
  294. package/src/shared/lib/code/editor/lines.ts +8 -48
  295. package/src/shared/lib/code/editor/marks.test.ts +4 -5
  296. package/src/shared/lib/code/editor/marks.ts +13 -25
  297. package/src/shared/lib/code/editor/palette.test.ts +25 -2
  298. package/src/shared/lib/code/editor/palette.ts +10 -0
  299. package/src/shared/lib/code/editor/stream-modes.ts +225 -0
  300. package/src/shared/lib/code/editor/theme-loader.ts +97 -0
  301. package/src/shared/lib/code/editor/themes.generated.ts +156 -0
  302. package/src/shared/lib/code/editor/themes.test.ts +165 -0
  303. package/src/shared/lib/code/editor/themes.ts +165 -0
  304. package/src/shared/lib/code/highlight-memo.test.ts +103 -0
  305. package/src/shared/lib/code/highlight-memo.ts +149 -0
  306. package/src/shared/lib/code/highlighter-warmup.ts +112 -0
  307. package/src/shared/lib/code/highlighter.ts +3 -1
  308. package/src/shared/lib/code/idle.ts +37 -0
  309. package/src/shared/lib/code/outline/rows.test.ts +96 -0
  310. package/src/shared/lib/code/outline/rows.ts +94 -0
  311. package/src/shared/lib/code/syntax-highlight.test.ts +2 -80
  312. package/src/shared/lib/code/syntax-highlight.ts +2 -3
  313. package/src/shared/lib/fs/file-kind-classify.test.ts +25 -11
  314. package/src/shared/lib/fs/file-kind.ts +47 -13
  315. package/src/shared/lib/fs/file-tree.test.ts +41 -0
  316. package/src/shared/lib/fs/file-tree.ts +126 -34
  317. package/src/shared/lib/fs/file-url.test.ts +52 -0
  318. package/src/shared/lib/fs/file-url.ts +37 -0
  319. package/src/shared/lib/fs/folder-chain.test.ts +87 -0
  320. package/src/shared/lib/fs/folder-chain.ts +91 -0
  321. package/src/shared/lib/fs/fs-helpers.test.ts +4 -50
  322. package/src/shared/lib/fs/patch-sides.test.ts +114 -0
  323. package/src/shared/lib/fs/patch-sides.ts +139 -0
  324. package/src/shared/lib/realtime/connection.test.ts +5 -1
  325. package/src/shared/lib/realtime/connection.ts +25 -119
  326. package/src/shared/lib/realtime/frozen-recovery.test.ts +93 -0
  327. package/src/shared/lib/realtime/protocol.ts +16 -0
  328. package/src/shared/lib/realtime/resume.ts +223 -0
  329. package/src/shared/lib/realtime/state.ts +22 -0
  330. package/src/shared/lib/realtime/watchdog-wiring.test.ts +224 -0
  331. package/src/shared/lib/realtime/watchdog.test.ts +99 -0
  332. package/src/shared/lib/realtime/watchdog.ts +72 -0
  333. package/src/shared/lib/ui/toolbar-overflow.test.ts +83 -0
  334. package/src/shared/lib/ui/toolbar-overflow.ts +86 -0
  335. package/src/shared/lib/updates/shell-arch.ts +54 -0
  336. package/src/shared/lib/updates/shell-ua.ts +4 -4
  337. package/src/shared/lib/workspace/editor-view-mode.test.ts +70 -0
  338. package/src/shared/lib/workspace/editor-view-mode.ts +48 -0
  339. package/src/shared/lib/workspace/panel-widths.test.ts +30 -0
  340. package/src/shared/lib/workspace/panel-widths.ts +25 -5
  341. package/src/shared/lib/workspace/session-state/cache.ts +159 -0
  342. package/src/shared/lib/workspace/session-state/harness.test-util.ts +55 -0
  343. package/src/shared/lib/workspace/session-state/merge.ts +100 -0
  344. package/src/shared/lib/workspace/session-state/store-load.test.ts +206 -0
  345. package/src/shared/lib/workspace/session-state/store.test.ts +17 -139
  346. package/src/shared/lib/workspace/session-state/store.ts +92 -130
  347. package/src/shared/lib/workspace/split-editor.test.ts +104 -0
  348. package/src/shared/lib/workspace/split-editor.ts +129 -0
  349. package/src/shared/lib/workspace/workspace-panel-widths.ts +124 -0
  350. package/src/shared/types/realtime.ts +29 -0
  351. package/src/shared/types/settings/state.ts +10 -0
  352. package/src/test-support/file-explorer-disk.test-util.ts +30 -3
  353. package/src/test-support/loopback.ts +183 -0
  354. package/src/test-support/pristine-globals.ts +7 -0
  355. package/dist/client/static/js/index-gtbpewr7.css +0 -1
  356. package/src/client/components/common/code-editor/handler.test.ts +0 -274
  357. package/src/client/components/common/code-editor/handler.ts +0 -175
  358. package/src/client/components/common/code-editor/index.tsx +0 -314
  359. package/src/client/components/common/code-surface/measure.ts +0 -148
  360. package/src/client/components/workspace/chat-timeline/target-picker/index.test.tsx +0 -227
  361. package/src/client/components/workspace/chat-timeline/target-picker/index.tsx +0 -260
  362. package/src/client/components/workspace/diff-panel/SplitView.tsx +0 -209
  363. package/src/client/components/workspace/diff-panel/UnifiedView.tsx +0 -150
  364. package/src/client/hooks/editor/scroll-container.test.ts +0 -102
  365. package/src/client/hooks/editor/scroll-container.ts +0 -26
  366. package/src/client/hooks/editor/use-code-window.ts +0 -100
  367. package/src/client/hooks/editor/use-editor-history.test.ts +0 -216
  368. package/src/client/hooks/editor/use-editor-history.ts +0 -134
  369. package/src/client/hooks/editor/use-occurrences.test.ts +0 -236
  370. package/src/client/hooks/editor/use-occurrences.ts +0 -98
  371. package/src/shared/lib/code/editor/commands.test.ts +0 -155
  372. package/src/shared/lib/code/editor/commands.ts +0 -260
  373. package/src/shared/lib/code/editor/edits.test.ts +0 -100
  374. package/src/shared/lib/code/editor/edits.ts +0 -122
  375. package/src/shared/lib/code/editor/history.test.ts +0 -118
  376. package/src/shared/lib/code/editor/history.ts +0 -101
  377. package/src/shared/lib/code/editor/layers.test.ts +0 -77
  378. package/src/shared/lib/code/editor/layers.ts +0 -67
  379. package/src/shared/lib/code/editor/line-edits.test.ts +0 -131
  380. package/src/shared/lib/code/editor/line-edits.ts +0 -159
  381. package/src/shared/lib/code/editor/occurrences.test.ts +0 -117
  382. package/src/shared/lib/code/editor/occurrences.ts +0 -111
  383. package/src/shared/lib/code/editor/reset-css.test.ts +0 -35
  384. package/src/shared/lib/code/editor/reset-css.ts +0 -32
  385. package/src/shared/lib/code/editor/text-commands.test.ts +0 -178
  386. package/src/shared/lib/code/editor/text-commands.ts +0 -154
  387. package/src/shared/lib/code/lazy-window.test.ts +0 -161
  388. package/src/shared/lib/code/lazy-window.ts +0 -156
  389. package/src/shared/lib/code/windowed-highlight.ts +0 -114
  390. package/src/shared/lib/fs/diff-parser.test.ts +0 -116
  391. package/src/shared/lib/fs/diff-parser.ts +0 -213
@@ -0,0 +1,133 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The keymap generator.
8
+ *
9
+ * Its whole contract is negative: a command the palette lists must be BOUND, or
10
+ * the palette advertises a shortcut nothing listens for. That property is only
11
+ * checkable by comparing the generated CM6 bindings against `EDITOR_KEY_BINDINGS`
12
+ * — which is what this does, row by row, so adding a row to the table and
13
+ * forgetting the binding fails here instead of in a user's hands.
14
+ *
15
+ * The name mapping is the part that is easy to get silently wrong: CM6 matches
16
+ * on `event.keyCode` through `w3c-keyname`, so `Digit1` is `1` and `Slash` is
17
+ * `/` — a binding written `Digit1` never fires.
18
+ */
19
+
20
+ import { describe, expect, test } from 'bun:test';
21
+ import { EditorState, type TransactionSpec } from '@codemirror/state';
22
+ import type { EditorView } from '@codemirror/view';
23
+
24
+ import { EDITOR_KEY_BINDINGS } from '@/shared/lib/code/editor/keymap';
25
+ import { codeMirrorKey, runSurfaceCommand } from '@/client/components/common/code-surface/keymap';
26
+
27
+ describe('codeMirrorKey', () => {
28
+ test('maps letters, digits and function keys to the names CM6 matches', () => {
29
+ // Lower case for a bare letter: `keyName` reports the character the key
30
+ // produces, which is `d` unshifted. The shifted form (`D`) is reached
31
+ // through CM6's own base-character fallback, so the row still fires.
32
+ expect(codeMirrorKey({ code: 'KeyD', meta: true })).toBe('Meta-d');
33
+ expect(codeMirrorKey({ code: 'Digit1', meta: true, shift: true })).toBe('Shift-Meta-1');
34
+ expect(codeMirrorKey({ code: 'F3' })).toBe('F3');
35
+ });
36
+
37
+ test('maps punctuation to the character the key produces unshifted', () => {
38
+ // `Slash` is `/` on every layout that has it; `BracketRight` is `]`.
39
+ expect(codeMirrorKey({ code: 'Slash', ctrl: true })).toBe('Ctrl-/');
40
+ expect(codeMirrorKey({ code: 'BracketRight', meta: true })).toBe('Meta-]');
41
+ expect(codeMirrorKey({ code: 'BracketLeft', ctrl: true })).toBe('Ctrl-[');
42
+ });
43
+
44
+ test('names the keys that have their own spelling', () => {
45
+ expect(codeMirrorKey({ code: 'Enter', meta: true })).toBe('Meta-Enter');
46
+ expect(codeMirrorKey({ code: 'ArrowUp', alt: true })).toBe('Alt-ArrowUp');
47
+ expect(codeMirrorKey({ code: 'Home' })).toBe('Home');
48
+ });
49
+
50
+ test('orders modifiers the way CM6 canonicalises them', () => {
51
+ // CM6's `modifiers()` prepends alt, then ctrl, then meta, then shift, so
52
+ // the canonical spelling runs `Shift-Meta-Ctrl-Alt-<key>`. A different
53
+ // order here would be a binding CM6 never matches.
54
+ expect(codeMirrorKey({ code: 'ArrowDown', ctrl: true, alt: true, shift: true })).toBe('Shift-Ctrl-Alt-ArrowDown');
55
+ });
56
+
57
+ test('reports null for a key with no name, so the row is skipped', () => {
58
+ expect(codeMirrorKey({ code: 'IntlBackslash' })).toBeNull();
59
+ });
60
+ });
61
+
62
+ describe('EDITOR_KEY_BINDINGS coverage', () => {
63
+ test('every row resolves to a chord on both platforms', () => {
64
+ const missing = EDITOR_KEY_BINDINGS.filter(
65
+ (binding) => codeMirrorKey(binding.mac) === null || codeMirrorKey(binding.other) === null,
66
+ ).map((binding) => binding.command);
67
+
68
+ // A row whose chord cannot be named is a palette entry that does nothing —
69
+ // the exact failure this table exists to prevent.
70
+ expect(missing).toEqual([]);
71
+ });
72
+
73
+ test('no two rows claim the same chord on the same platform', () => {
74
+ for (const platform of ['mac', 'other'] as const) {
75
+ const seen = new Map<string, string>();
76
+ for (const binding of EDITOR_KEY_BINDINGS) {
77
+ const chord = codeMirrorKey(binding[platform]);
78
+ if (!chord) continue;
79
+ // A duplicate would make the first row win silently and leave the
80
+ // second's palette entry dead.
81
+ expect(seen.has(chord) ? `${chord} (${seen.get(chord)} and ${binding.command})` : null).toBeNull();
82
+ seen.set(chord, binding.command);
83
+ }
84
+ }
85
+ });
86
+ });
87
+
88
+ /**
89
+ * The two commands the surface implements itself, run against a state.
90
+ *
91
+ * `insertLineAbove` is the one that has no CM6 equivalent, and its indentation
92
+ * rule is the part a rewrite loses silently: a bare newline above an indented
93
+ * line puts the caret at column 0, so the next thing typed lands outside the
94
+ * block. Driven through `runSurfaceCommand` — the palette's and the handle's own
95
+ * path in — with a `{ state, dispatch }` view, which is all a `StateCommand`
96
+ * touches.
97
+ */
98
+ describe('surface commands', () => {
99
+ /**
100
+ * The two commands are `StateCommand`s: they read `state` and hand a
101
+ * transaction SPEC to `dispatch`, nothing else. A view-shaped stand-in is what
102
+ * `runSurfaceCommand` takes, so the cast states that boundary once rather than
103
+ * mounting an `EditorView` in happy-dom for a rule with no DOM in it.
104
+ */
105
+ function viewFor(doc: string, anchor: number): EditorView {
106
+ let state = EditorState.create({ doc, selection: { anchor } });
107
+ const view = {
108
+ get state() {
109
+ return state;
110
+ },
111
+ // A command dispatches a `TransactionSpec`; `EditorState.update` is the
112
+ // half of the real view's `dispatch` these commands depend on.
113
+ dispatch: (...specs: TransactionSpec[]) => {
114
+ state = state.update(...specs).state;
115
+ },
116
+ };
117
+ return view as unknown as EditorView;
118
+ }
119
+
120
+ test('insertLineAbove keeps the current line’s indentation and lands after it', () => {
121
+ const view = viewFor(' const a = 1;\nconst b = 2;\n', 4);
122
+ expect(runSurfaceCommand('insertLineAbove', view)).toBe(true);
123
+ expect(view.state.doc.toString()).toBe(' \n const a = 1;\nconst b = 2;\n');
124
+ expect(view.state.selection.main.head).toBe(2);
125
+ });
126
+
127
+ test('insertLineAbove on an unindented line inserts a bare blank line', () => {
128
+ const view = viewFor('one\ntwo\n', 5);
129
+ runSurfaceCommand('insertLineAbove', view);
130
+ expect(view.state.doc.toString()).toBe('one\n\ntwo\n');
131
+ expect(view.state.selection.main.head).toBe(4);
132
+ });
133
+ });
@@ -0,0 +1,256 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The editor's chords, generated from `EDITOR_KEY_BINDINGS`.
8
+ *
9
+ * The table in `shared/lib/code/editor/keymap.ts` is the ONE statement of what
10
+ * each chord means: the command palette lists it, the find bar's tooltips print
11
+ * it, and this module turns it into CodeMirror bindings. A hand-written CM6
12
+ * keymap would be a second statement that drifts — the palette would advertise
13
+ * `⌥⌘C` for a key CM6 never bound.
14
+ *
15
+ * Panel-owned commands (`find`, `replace`, the match steps, word wrap) are bound
16
+ * HERE too and delegate outward through `onCommand`, so a chord pressed inside
17
+ * the document reaches the same dispatcher the palette uses. Consuming the key
18
+ * is the point: `⌘F` inside the editor must open Kabmo's find bar, never the
19
+ * browser's.
20
+ */
21
+
22
+ import type { Extension, StateCommand } from '@codemirror/state';
23
+ import {
24
+ addCursorAbove,
25
+ addCursorBelow,
26
+ defaultKeymap,
27
+ copyLineDown,
28
+ copyLineUp,
29
+ cursorDocEnd,
30
+ cursorDocStart,
31
+ cursorLineEnd,
32
+ cursorLineStart,
33
+ deleteLine,
34
+ indentLess,
35
+ indentMore,
36
+ insertBlankLine,
37
+ moveLineDown,
38
+ moveLineUp,
39
+ redo,
40
+ selectDocEnd,
41
+ selectDocStart,
42
+ selectLine,
43
+ selectLineEnd,
44
+ selectLineStart,
45
+ simplifySelection,
46
+ toggleBlockComment,
47
+ toggleComment,
48
+ undo,
49
+ } from '@codemirror/commands';
50
+ import { selectNextOccurrence, selectSelectionMatches } from '@codemirror/search';
51
+ import { foldAll, foldCode, unfoldAll, unfoldCode } from '@codemirror/language';
52
+ import { keymap, type Command, type EditorView, type KeyBinding } from '@codemirror/view';
53
+
54
+ import { EDITOR_KEY_BINDINGS, type EditorCommand } from '@/shared/lib/code/editor/keymap';
55
+ import { joinLines, trimTrailingWhitespace } from '@/client/components/common/code-surface/line-commands';
56
+ import type { KeyStroke } from '@/shared/lib/ui/key-binding';
57
+
58
+ /** A command as this module invokes it: CM6's `Command` and `StateCommand` both fit. */
59
+ type SurfaceCommand = (view: EditorView) => boolean;
60
+
61
+ /**
62
+ * Insert a blank line ABOVE the caret's line, which CM6 has no command for.
63
+ *
64
+ * The new line carries the current line's own leading whitespace, and the caret
65
+ * lands after it — the rule the textarea surface applied, and the one that makes
66
+ * the key useful inside a block: a bare newline above an indented line left the
67
+ * caret at column 0 and the new line unindented, so the very next thing typed
68
+ * was in the wrong place. (CM6's `insertBlankLine`, used for "below", derives
69
+ * the same indentation from `IndentContext`; this mirrors it without needing a
70
+ * grammar.)
71
+ */
72
+ const insertLineAbove: SurfaceCommand = (view) => {
73
+ const { state } = view;
74
+ const range = state.selection.main;
75
+ const line = state.doc.lineAt(range.from);
76
+ const indent = /^[ \t]*/.exec(line.text)?.[0] ?? '';
77
+ view.dispatch({
78
+ changes: { from: line.from, insert: `${indent}\n` },
79
+ selection: { anchor: line.from + indent.length },
80
+ });
81
+ return true;
82
+ };
83
+
84
+ /**
85
+ * Collapse extra selections, keeping the main one.
86
+ *
87
+ * Escape's first job when ⌘D left several carets behind; only the next press
88
+ * hands the keyboard back to the page, which the panel's own capture handler
89
+ * does once this reports there was nothing left to collapse.
90
+ */
91
+ const collapseSelections: SurfaceCommand = (view) => {
92
+ if (view.state.selection.ranges.length <= 1) return false;
93
+ return (simplifySelection as StateCommand)(view);
94
+ };
95
+
96
+ /**
97
+ * Commands the BUFFER owns. Everything absent from this table is either the
98
+ * panel's (`find`, `replace`, `toggleWordWrap`) or a chord with no meaning in a
99
+ * CM6 surface.
100
+ */
101
+ const BUFFER_COMMANDS: Partial<Record<EditorCommand, SurfaceCommand>> = {
102
+ undo: undo as SurfaceCommand,
103
+ redo: redo as SurfaceCommand,
104
+ selectNextOccurrence: selectNextOccurrence as SurfaceCommand,
105
+ selectAllOccurrences: selectSelectionMatches as SurfaceCommand,
106
+ insertCursorBelow: addCursorBelow as SurfaceCommand,
107
+ insertCursorAbove: addCursorAbove as SurfaceCommand,
108
+ moveLineUp: moveLineUp as SurfaceCommand,
109
+ moveLineDown: moveLineDown as SurfaceCommand,
110
+ copyLineUp: copyLineUp as SurfaceCommand,
111
+ copyLineDown: copyLineDown as SurfaceCommand,
112
+ deleteLine: deleteLine as SurfaceCommand,
113
+ toggleComment: toggleComment as SurfaceCommand,
114
+ toggleBlockComment: toggleBlockComment as SurfaceCommand,
115
+ joinLines,
116
+ trimTrailingWhitespace,
117
+ insertLineBelow: insertBlankLine as SurfaceCommand,
118
+ insertLineAbove,
119
+ indent: indentMore as SurfaceCommand,
120
+ outdent: indentLess as SurfaceCommand,
121
+ fold: foldCode as SurfaceCommand,
122
+ unfold: unfoldCode as SurfaceCommand,
123
+ foldAll: foldAll as SurfaceCommand,
124
+ unfoldAll: unfoldAll as SurfaceCommand,
125
+ lineStart: cursorLineStart as SurfaceCommand,
126
+ lineEnd: cursorLineEnd as SurfaceCommand,
127
+ documentStart: cursorDocStart as SurfaceCommand,
128
+ documentEnd: cursorDocEnd as SurfaceCommand,
129
+ selectToLineStart: selectLineStart as SurfaceCommand,
130
+ selectToLineEnd: selectLineEnd as SurfaceCommand,
131
+ selectToDocumentStart: selectDocStart as SurfaceCommand,
132
+ selectToDocumentEnd: selectDocEnd as SurfaceCommand,
133
+ selectLine: selectLine as SurfaceCommand,
134
+ escape: collapseSelections,
135
+ };
136
+
137
+ /** Run a command against a live view — the palette's and the handle's path in. */
138
+ export function runSurfaceCommand(command: EditorCommand, view: EditorView, onCommand?: (command: EditorCommand) => void): boolean {
139
+ const run = BUFFER_COMMANDS[command];
140
+ if (run) return run(view);
141
+ onCommand?.(command);
142
+ return true;
143
+ }
144
+
145
+ /** Codes whose printed name is not their `KeyboardEvent.code` suffix. */
146
+ const NAMED_KEYS: Record<string, string> = {
147
+ Enter: 'Enter',
148
+ Escape: 'Escape',
149
+ Backspace: 'Backspace',
150
+ Delete: 'Delete',
151
+ Tab: 'Tab',
152
+ Space: 'Space',
153
+ ArrowUp: 'ArrowUp',
154
+ ArrowDown: 'ArrowDown',
155
+ ArrowLeft: 'ArrowLeft',
156
+ ArrowRight: 'ArrowRight',
157
+ Home: 'Home',
158
+ End: 'End',
159
+ PageUp: 'PageUp',
160
+ PageDown: 'PageDown',
161
+ };
162
+
163
+ const PUNCTUATION_KEYS: Record<string, string> = {
164
+ Slash: '/',
165
+ Backslash: '\\',
166
+ Comma: ',',
167
+ Period: '.',
168
+ Semicolon: ';',
169
+ Quote: "'",
170
+ Backquote: '`',
171
+ Minus: '-',
172
+ Equal: '=',
173
+ BracketLeft: '[',
174
+ BracketRight: ']',
175
+ };
176
+
177
+ /**
178
+ * The key CodeMirror names a physical key by, WITHOUT modifiers.
179
+ *
180
+ * CM6 matches on `event.keyCode` through `w3c-keyname`, so the base name is the
181
+ * character the key produces unshifted — `Digit1` is `1`, not `!`, and a
182
+ * `Shift` binding is written `Shift-1`. Punctuation is its US character, which
183
+ * is what that library reports for the same key code.
184
+ */
185
+ function baseKey(code: string): string | null {
186
+ if (/^Key[A-Z]$/.test(code)) return code.slice(3).toLowerCase();
187
+ if (/^Digit\d$/.test(code)) return code.slice(5);
188
+ if (/^F\d{1,2}$/.test(code)) return code;
189
+ return NAMED_KEYS[code] ?? PUNCTUATION_KEYS[code] ?? null;
190
+ }
191
+
192
+ /** `{code:'Digit1', meta:true, shift:true}` → `Meta-Shift-1` (CM6's canonical order). */
193
+ export function codeMirrorKey(stroke: KeyStroke): string | null {
194
+ const key = baseKey(stroke.code);
195
+ if (!key) return null;
196
+ let name = key;
197
+ if (stroke.alt) name = `Alt-${name}`;
198
+ if (stroke.ctrl) name = `Ctrl-${name}`;
199
+ if (stroke.meta) name = `Meta-${name}`;
200
+ if (stroke.shift) name = `Shift-${name}`;
201
+ return name;
202
+ }
203
+
204
+ /**
205
+ * One CM6 binding per keymap row, carrying each platform's own chord.
206
+ *
207
+ * `key` is the Windows/Linux chord, `mac` the macOS one, and `win` the Windows
208
+ * override for the single row that has one (`redo` is `Ctrl-Y` there and
209
+ * `Ctrl-Shift-Z` on Linux). CM6 falls back to `key` when a platform field is
210
+ * absent, so a row without an override needs nothing extra.
211
+ */
212
+ function bindingsFor(onCommand?: (command: EditorCommand) => void): KeyBinding[] {
213
+ const bindings: KeyBinding[] = [];
214
+ for (const binding of EDITOR_KEY_BINDINGS) {
215
+ const other = codeMirrorKey(binding.other);
216
+ const mac = codeMirrorKey(binding.mac);
217
+ const win = binding.windows ? codeMirrorKey(binding.windows) : null;
218
+ if (!other && !mac) continue;
219
+ const command = binding.command;
220
+ bindings.push({
221
+ key: other ?? undefined,
222
+ mac: mac ?? undefined,
223
+ win: win ?? undefined,
224
+ run: (view) => runSurfaceCommand(command, view, onCommand),
225
+ });
226
+ }
227
+ return bindings;
228
+ }
229
+
230
+ /**
231
+ * Tab indents and ⇧Tab outdents, exactly as `indentWithTab` does.
232
+ *
233
+ * Written out rather than imported so the row can be dropped when the surface is
234
+ * read-only: a viewer that still indented would edit the document. Leaving the
235
+ * editor with the keyboard is CM6's own answer — `Escape` arms a two-second tab
236
+ * focus mode, after which `Tab` moves focus instead of indenting.
237
+ */
238
+ const TAB_BINDINGS: KeyBinding[] = [
239
+ { key: 'Tab', run: indentMore as Command, shift: indentLess as Command },
240
+ ];
241
+
242
+ /**
243
+ * The editor's own keymap, as one extension.
244
+ *
245
+ * CM6's `defaultKeymap` is APPENDED rather than omitted: it is what makes the
246
+ * ordinary editing keys work — Enter, Backspace, arrows, Home/End, `Mod-End` to
247
+ * the document end, word-wise movement — and none of that is a Kabmo command.
248
+ * Appending it AFTER our rows is what keeps a chord the table claims from being
249
+ * shadowed by a CM6 default (`Mod-a` is a good example: both mean "select all",
250
+ * but only one of them also moves the selection the way the palette describes).
251
+ */
252
+ export function surfaceKeymap(onCommand?: (command: EditorCommand) => void, readOnly = false): Extension {
253
+ const rows = readOnly ? [] : TAB_BINDINGS;
254
+ return keymap.of([...bindingsFor(onCommand), ...rows, ...defaultKeymap]);
255
+ }
256
+
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Join and trim, the two line operations CodeMirror does not ship.
8
+ *
9
+ * The rules worth pinning are the ones a naive splice gets wrong: a collapsed caret
10
+ * joins its own line with the NEXT one (the chord's meaning when nothing is
11
+ * selected), a selection ending on a line boundary does not touch that line, and a
12
+ * joined method chain must not gain a space before its `.`.
13
+ */
14
+
15
+ import { describe, expect, test } from 'bun:test';
16
+ import { EditorState, type StateCommand } from '@codemirror/state';
17
+
18
+ import { joinLines, trimTrailingWhitespace } from '@/client/components/common/code-surface/line-commands';
19
+
20
+ /**
21
+ * Run a `StateCommand` against a bare `{ state, dispatch }`, the way CM6 does.
22
+ *
23
+ * A `StateCommand` receives a `Transaction` (not a spec), so the dispatch only has to
24
+ * adopt its `.state` — the shape `EditorView.dispatch` ultimately takes.
25
+ */
26
+ function run(command: StateCommand, doc: string, anchor: number, head = anchor) {
27
+ const view = {
28
+ state: EditorState.create({ doc, selection: { anchor, head } }),
29
+ dispatch(transaction: { state: EditorState }) {
30
+ view.state = transaction.state;
31
+ },
32
+ };
33
+ const handled = command(view as never);
34
+ return { handled, doc: view.state.doc.toString() };
35
+ }
36
+
37
+ describe('joinLines', () => {
38
+ test('a collapsed caret joins its line with the next one', () => {
39
+ expect(run(joinLines, 'const a = 1;\nconst b = 2;', 5)).toEqual({
40
+ handled: true,
41
+ doc: 'const a = 1; const b = 2;',
42
+ });
43
+ });
44
+
45
+ test('a method chain keeps its dot hugging the previous token', () => {
46
+ // The rule: the joined line's indentation goes, and no space is inserted before a
47
+ // character that should hug its predecessor.
48
+ expect(run(joinLines, 'foo()\n .bar()', 2).doc).toBe('foo().bar()');
49
+ expect(run(joinLines, 'foo(\n )', 4).doc).toBe('foo()');
50
+ });
51
+
52
+ test('two statements are separated by one space', () => {
53
+ expect(run(joinLines, 'let a = 1\nlet b = 2', 3).doc).toBe('let a = 1 let b = 2');
54
+ });
55
+
56
+ test('a selection joins every line it covers, and stops at a boundary', () => {
57
+ // `0..3` covers "a\nb\n", which touches lines 1 and 2 — line 3 starts at 4.
58
+ expect(run(joinLines, 'a\nb\nc', 0, 3).doc).toBe('a b\nc');
59
+ });
60
+
61
+ test('the last line has nothing to join, and the chord is inert', () => {
62
+ expect(run(joinLines, 'a\nb', 2)).toEqual({ handled: false, doc: 'a\nb' });
63
+ });
64
+
65
+ test('a single-line document is inert', () => {
66
+ expect(run(joinLines, 'only', 1).handled).toBe(false);
67
+ });
68
+
69
+ test('joining across a blank line drops the blank line', () => {
70
+ expect(run(joinLines, 'a\n\nb', 1).doc).toBe('a\nb');
71
+ });
72
+ });
73
+
74
+ describe('trimTrailingWhitespace', () => {
75
+ test('strips spaces and tabs from the lines the selection touches', () => {
76
+ expect(run(trimTrailingWhitespace, 'a \nb\t\nc', 0, 8).doc).toBe('a\nb\nc');
77
+ });
78
+
79
+ test('a collapsed caret trims only its own line', () => {
80
+ expect(run(trimTrailingWhitespace, 'a \nb ', 0).doc).toBe('a\nb ');
81
+ });
82
+
83
+ test('nothing to trim answers false, so the chord is inert rather than a no-op edit', () => {
84
+ expect(run(trimTrailingWhitespace, 'a\nb', 0, 3)).toEqual({ handled: false, doc: 'a\nb' });
85
+ });
86
+
87
+ test('indentation is never touched — only the trailing run', () => {
88
+ expect(run(trimTrailingWhitespace, ' a ', 0).doc).toBe(' a');
89
+ });
90
+ });
@@ -0,0 +1,103 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Two line operations CodeMirror does not ship: join lines and trim trailing
8
+ * whitespace.
9
+ *
10
+ * `@codemirror/commands` has `moveLineUp`, `copyLineUp`, `deleteLine` and the rest,
11
+ * but no join — so this is written rather than wired. Both commands operate on the
12
+ * SELECTION's lines (or the caret's line when nothing is selected), which is what
13
+ * every editor means by them, and both are one undoable transaction.
14
+ *
15
+ * ## Join has to decide what to do about indentation
16
+ *
17
+ * The naive `from, to` splice leaves the second line's leading whitespace in the
18
+ * middle of the result: `foo()` + ` .bar()` becomes `foo() .bar()`. That is right
19
+ * for a method chain and wrong for two statements, and a command cannot tell them
20
+ * apart. The rule taken: strip the joined line's indentation, and put ONE space
21
+ * between the halves unless the next line starts with a character that should hug its
22
+ * predecessor (`.`, `,`, `)`, `]`, `}`, `;`, `:`). That keeps a chain readable
23
+ * without inventing a second space in `foo() .bar()`.
24
+ *
25
+ * ## Trim is the one that changes a file the user did not ask about
26
+ *
27
+ * It is therefore an explicit command, never a format-on-save: trailing whitespace
28
+ * can be significant (a Markdown hard break is two spaces, and a Makefile recipe line
29
+ * ends in a tab). Documented in the command's own doc rather than left to surprise.
30
+ */
31
+
32
+ import { type ChangeSpec, type EditorState, type StateCommand } from '@codemirror/state';
33
+
34
+ /** Characters that should not be preceded by a space when two lines are joined. */
35
+ const HUGS_PREVIOUS = new Set(['.', ',', ')', ']', '}', ';', ':']);
36
+
37
+ /**
38
+ * The lines the selection touches, as `[first, last]` line numbers.
39
+ *
40
+ * A selection ENDING at the very start of a line does not touch that line — the same
41
+ * rule a cut uses, and the reason `⌘⌥J` on line 1 of a two-line file joins it with
42
+ * line 2 rather than reporting that there is nothing to join.
43
+ */
44
+ function selectedLines(state: EditorState): { first: number; last: number } {
45
+ const range = state.selection.main;
46
+ const first = state.doc.lineAt(range.from).number;
47
+ // A collapsed selection touches exactly its own line; a range that ends on a line
48
+ // boundary stops at the line BEFORE it.
49
+ const lastLine = state.doc.lineAt(range.to);
50
+ const last = range.to > range.from && lastLine.from === range.to ? lastLine.number - 1 : lastLine.number;
51
+ return { first, last: Math.max(first, last) };
52
+ }
53
+
54
+ /**
55
+ * Join every line the selection touches into one.
56
+ *
57
+ * A single-line selection with nothing to join returns `false`, so the chord is
58
+ * inert rather than eating the key.
59
+ */
60
+ export const joinLines: StateCommand = ({ state, dispatch }) => {
61
+ const range = state.selection.main;
62
+ const collapsed = range.from === range.to;
63
+ const { first, last } = selectedLines(state);
64
+ // A COLLAPSED caret joins its own line with the next one — that is what the chord
65
+ // means when nothing is selected. A RANGE joins the lines it covers.
66
+ const lastToJoin = collapsed ? first + 1 : last;
67
+ if (lastToJoin <= first || lastToJoin > state.doc.lines) return false;
68
+ const lastLine = lastToJoin;
69
+
70
+ const changes: ChangeSpec[] = [];
71
+ for (let number = first; number < lastLine; number++) {
72
+ const line = state.doc.line(number);
73
+ const next = state.doc.line(number + 1);
74
+ const trimmed = next.text.replace(/^[ \t]+/, '');
75
+ // A blank line becomes a single space, not a collapse: joining across a paragraph
76
+ // break should leave the words separated.
77
+ const separator = trimmed === '' || HUGS_PREVIOUS.has(trimmed[0]) ? '' : ' ';
78
+ changes.push({ from: line.to, to: next.from + (next.text.length - trimmed.length), insert: separator });
79
+ }
80
+ dispatch(state.update({ changes, scrollIntoView: true, userEvent: 'input.join' }));
81
+ return true;
82
+ };
83
+
84
+ /**
85
+ * Strip trailing whitespace from every line the selection touches.
86
+ *
87
+ * Returns `false` when there was nothing to trim, so a caller (and the chord) can
88
+ * tell "done" from "nothing to do".
89
+ */
90
+ export const trimTrailingWhitespace: StateCommand = ({ state, dispatch }) => {
91
+ const { first, last } = selectedLines(state);
92
+ const changes: ChangeSpec[] = [];
93
+ for (let number = first; number <= last; number++) {
94
+ const line = state.doc.line(number);
95
+ const trimmed = line.text.replace(/[ \t]+$/, '');
96
+ if (trimmed !== line.text) {
97
+ changes.push({ from: line.from + trimmed.length, to: line.to, insert: '' });
98
+ }
99
+ }
100
+ if (changes.length === 0) return false;
101
+ dispatch(state.update({ changes, userEvent: 'input.trim' }));
102
+ return true;
103
+ };
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Find matches, as a decoration field.
8
+ *
9
+ * The old surface spliced `<mark>` elements into the highlighter's own HTML, so
10
+ * every find result forced a re-tokenize of the window and the marks could not
11
+ * exist without the token layer. A field is the CM6 shape: the ranges are state,
12
+ * the decorations are derived from it, and a keystroke in the query costs one
13
+ * rebuild of the ranges instead of a second trip through Shiki.
14
+ *
15
+ * The geometry rules are inherited from that markup layer and are still
16
+ * load-bearing: `<mark>` had a background and no padding, and the current match
17
+ * is marked with horizontal edges only. A match routinely spans several tokens,
18
+ * so any decoration that draws vertical edges would paint a seam in the middle
19
+ * of it.
20
+ */
21
+
22
+ import { RangeSetBuilder, StateEffect, StateField, type Text } from '@codemirror/state';
23
+ import { Decoration, EditorView, type DecorationSet } from '@codemirror/view';
24
+
25
+ /** A document-offset range; the editor's own `TextRange` shape. */
26
+ export interface MarkRange {
27
+ start: number;
28
+ end: number;
29
+ }
30
+
31
+ export interface FindMarkState {
32
+ marks: readonly MarkRange[];
33
+ /** Index into `marks` of the match the caret is aimed at, or -1. */
34
+ current: number;
35
+ }
36
+
37
+ /** Replace the whole mark set. */
38
+ export const setFindMarks = StateEffect.define<FindMarkState>();
39
+
40
+ const MATCH = Decoration.mark({ class: 'find-match' });
41
+ const CURRENT = Decoration.mark({ class: 'find-match', attributes: { 'data-find-current': '' } });
42
+
43
+ function build(state: FindMarkState, doc: Text): DecorationSet {
44
+ if (state.marks.length === 0) return Decoration.none;
45
+ const builder = new RangeSetBuilder<Decoration>();
46
+ for (let index = 0; index < state.marks.length; index++) {
47
+ const mark = state.marks[index];
48
+ const from = Math.max(0, Math.min(mark.start, doc.length));
49
+ const to = Math.max(from, Math.min(mark.end, doc.length));
50
+ if (to === from) continue;
51
+ builder.add(from, to, index === state.current ? CURRENT : MATCH);
52
+ }
53
+ return builder.finish();
54
+ }
55
+
56
+ /**
57
+ * The mark set as editor state.
58
+ *
59
+ * A document edit MAPS the existing decorations rather than dropping them: the
60
+ * owner recomputes the list on the next render anyway (`matches` is derived from
61
+ * the live buffer), and mapping keeps the highlights roughly right for the frame
62
+ * in between instead of blinking off on every keystroke.
63
+ */
64
+ export const findMarksField = StateField.define<DecorationSet>({
65
+ create: () => Decoration.none,
66
+ update(marks, transaction) {
67
+ for (const effect of transaction.effects) {
68
+ if (effect.is(setFindMarks)) return build(effect.value, transaction.state.doc);
69
+ }
70
+ if (transaction.docChanged) return marks.map(transaction.changes);
71
+ return marks;
72
+ },
73
+ provide: (field) => EditorView.decorations.from(field),
74
+ });