@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
@@ -0,0 +1,100 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The range an "in selection" search is restricted to.
8
+ *
9
+ * The rule is capture-on-arm: a range the user then MOVES must not change what is being
10
+ * searched, because the toggle says "the text I had selected". Arming with nothing selected
11
+ * answers false, so the caller can refuse to turn the toggle on rather than promise a
12
+ * restriction it cannot apply.
13
+ */
14
+
15
+ import { afterAll, describe, expect, test } from 'bun:test';
16
+ import { Window } from 'happy-dom';
17
+ import { h, render } from 'preact';
18
+ import { act } from 'preact/test-utils';
19
+
20
+ import { useSelectionRange, type FindSelectionRange } from '@/client/hooks/editor/find-selection-range';
21
+
22
+ const DOM_GLOBALS = ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'Event'] as const;
23
+ const native: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
24
+ const win = new Window({ url: 'http://localhost' });
25
+ for (const key of DOM_GLOBALS) {
26
+ const target = globalThis as unknown as Record<string, unknown>;
27
+ native[key] = target[key];
28
+ target[key] = (win as unknown as Record<string, unknown>)[key];
29
+ }
30
+
31
+ afterAll(() => {
32
+ const target = globalThis as unknown as Record<string, unknown>;
33
+ for (const key of DOM_GLOBALS) target[key] = native[key];
34
+ });
35
+
36
+ function mount(): { api: () => FindSelectionRange; unmount: () => void } {
37
+ let slot: FindSelectionRange | null = null;
38
+ function Probe() {
39
+ slot = useSelectionRange();
40
+ return null;
41
+ }
42
+ const host = document.createElement('div');
43
+ render(h(Probe, {}), host);
44
+ return {
45
+ api: () => {
46
+ if (!slot) throw new Error('not mounted');
47
+ return slot;
48
+ },
49
+ unmount: () => render(null, host),
50
+ };
51
+ }
52
+
53
+ describe('useSelectionRange', () => {
54
+ test('starts with no range', () => {
55
+ const { api, unmount } = mount();
56
+ expect(api().range).toBeNull();
57
+ unmount();
58
+ });
59
+
60
+ test('arming captures the selection it was given', () => {
61
+ const { api, unmount } = mount();
62
+ let armed = false;
63
+ act(() => { armed = api().arm(() => ({ start: 4, end: 9 })); });
64
+ expect(armed).toBe(true);
65
+ expect(api().range).toEqual({ start: 4, end: 9 });
66
+ unmount();
67
+ });
68
+
69
+ test('a range that MOVES afterwards does not change the captured one', () => {
70
+ const { api, unmount } = mount();
71
+ let selection = { start: 4, end: 9 };
72
+ act(() => { api().arm(() => selection); });
73
+ // The user drags a new selection; the armed range must not follow it.
74
+ selection = { start: 20, end: 30 };
75
+ expect(api().range).toEqual({ start: 4, end: 9 });
76
+ unmount();
77
+ });
78
+
79
+ test('arming with nothing selected answers false and leaves no range', () => {
80
+ const { api, unmount } = mount();
81
+ let armed = true;
82
+ act(() => { armed = api().arm(() => ({ start: 5, end: 5 })); });
83
+ expect(armed).toBe(false);
84
+ expect(api().range).toBeNull();
85
+ act(() => { armed = api().arm(() => null); });
86
+ expect(armed).toBe(false);
87
+ expect(api().range).toBeNull();
88
+ unmount();
89
+ });
90
+
91
+ test('disarming drops the range, so a later arm takes the THEN-current selection', () => {
92
+ const { api, unmount } = mount();
93
+ act(() => { api().arm(() => ({ start: 1, end: 3 })); });
94
+ act(() => api().disarm());
95
+ expect(api().range).toBeNull();
96
+ act(() => { api().arm(() => ({ start: 7, end: 9 })); });
97
+ expect(api().range).toEqual({ start: 7, end: 9 });
98
+ unmount();
99
+ });
100
+ });
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The range an "in selection" search is restricted to.
8
+ *
9
+ * Captured when the toggle is ARMED, never read live: a range the user then moves would
10
+ * silently change what is searched, which is the opposite of what the toggle says it
11
+ * does. Disarming drops it, so a later arm takes whatever is selected THEN rather than
12
+ * a stale range.
13
+ *
14
+ * `arm` answers false when nothing is selected — a toggle that promised "the selection"
15
+ * and searched the whole document instead would be a lie about the count.
16
+ */
17
+
18
+ import { useCallback, useState } from 'preact/hooks';
19
+
20
+ export interface SelectionRange {
21
+ start: number;
22
+ end: number;
23
+ }
24
+
25
+ export interface FindSelectionRange {
26
+ /** The captured range, or null while the toggle is off. */
27
+ range: SelectionRange | null;
28
+ /** Capture `readSelection()`; false when there is nothing selected to capture. */
29
+ arm: (readSelection: () => SelectionRange | null) => boolean;
30
+ disarm: () => void;
31
+ }
32
+
33
+ export function useSelectionRange(): FindSelectionRange {
34
+ const [range, setRange] = useState<SelectionRange | null>(null);
35
+
36
+ const arm = useCallback((readSelection: () => SelectionRange | null) => {
37
+ const selection = readSelection();
38
+ if (!selection || selection.start === selection.end) {
39
+ setRange(null);
40
+ return false;
41
+ }
42
+ setRange(selection);
43
+ return true;
44
+ }, []);
45
+
46
+ const disarm = useCallback(() => setRange(null), []);
47
+
48
+ return { range, arm, disarm };
49
+ }
@@ -61,7 +61,20 @@ export interface EditorFindState {
61
61
  * lines it is showing.
62
62
  */
63
63
  revealMatch: (request: { query: string; options: FindOptions; line: number }) => void;
64
+ /**
65
+ * Flip one option.
66
+ *
67
+ * `inSelection` is armed from the CURRENT selection at the moment it is switched on,
68
+ * and the range is remembered: a range that the user then moves must not silently
69
+ * change what is being searched.
70
+ */
64
71
  toggleOption: (key: keyof FindOptions) => void;
72
+ /** The range an "in selection" search is restricted to, or null when it is off. */
73
+ selectionRange: { start: number; end: number } | null;
74
+ /** The last `FIND_HISTORY_LIMIT` queries, newest first. */
75
+ history: readonly string[];
76
+ /** Re-run a query from the history and step to its first match. */
77
+ useHistoryEntry: (query: string) => void;
65
78
  step: (direction: 1 | -1) => void;
66
79
  replaceCurrent: () => void;
67
80
  replaceAll: () => void;
@@ -32,7 +32,6 @@ import {
32
32
  useEditorFontStack,
33
33
  } from '@/client/hooks/editor/font';
34
34
  import { copyImageToClipboard, downloadUrl, saveBlob } from '@/client/hooks/editor/file-actions';
35
- import { retainKeys, saveEditorFile } from '@/client/hooks/editor/save-file';
36
35
  import { EDITOR_DEFAULT_FONT_FAMILY, EDITOR_FONT_FAMILY } from '@/shared/lib/code/editor/typography';
37
36
  import { primeKabmoSettings } from '@/shared/lib/settings/client';
38
37
 
@@ -250,95 +249,3 @@ describe('copyImageToClipboard', () => {
250
249
  await expect(copyImageToClipboard('/api/fs/raw?path=a.png')).rejects.toThrow('Image clipboard unavailable');
251
250
  });
252
251
  });
253
-
254
- // ── save-file.ts ────────────────────────────────────────────────────────────
255
-
256
- interface Write {
257
- url: string;
258
- method: string;
259
- form: FormData;
260
- }
261
-
262
- /** Captures the write request and answers it with `answer`. */
263
- function stubWrite(answer: { ok: boolean; body?: unknown; throwOnJson?: boolean }): Write[] {
264
- const writes: Write[] = [];
265
- globalThis.fetch = (async (input: unknown, init?: RequestInit) => {
266
- writes.push({ url: String(input), method: init?.method ?? 'GET', form: init?.body as FormData });
267
- return {
268
- ok: answer.ok,
269
- json: async () => {
270
- if (answer.throwOnJson) throw new Error('not json');
271
- return answer.body ?? null;
272
- },
273
- };
274
- }) as unknown as typeof fetch;
275
- return writes;
276
- }
277
-
278
- describe('saveEditorFile', () => {
279
- test('posts the buffer with the path and the ending as its own field', async () => {
280
- const writes = stubWrite({ ok: true, body: { success: true } });
281
- const ok = await saveEditorFile({ path: 'src/a.ts', root: '/repo' }, 'const a = 1;\n', 'lf');
282
-
283
- expect(ok).toBe('written');
284
- expect(writes.length).toBe(1);
285
- expect(writes[0].url).toBe('/api/fs/action');
286
- expect(writes[0].method).toBe('POST');
287
- // A multipart body normalizes bare LF, so the ending cannot travel in it.
288
- expect(writes[0].form.get('actionType')).toBe('save');
289
- expect(writes[0].form.get('path')).toBe('src/a.ts');
290
- expect(writes[0].form.get('content')).toBe('const a = 1;\n');
291
- expect(writes[0].form.get('eol')).toBe('lf');
292
- expect(writes[0].form.get('root')).toBe('/repo');
293
- });
294
-
295
- test('omits the root when the file has none and the repo when it is the cwd marker', async () => {
296
- const writes = stubWrite({ ok: true, body: { success: true } });
297
- await saveEditorFile({ path: 'a.ts', repo: '.' }, 'x', 'crlf');
298
- expect(writes[0].form.get('root')).toBeNull();
299
- expect(writes[0].form.get('repo')).toBeNull();
300
- });
301
-
302
- test('a named repo travels with the write', async () => {
303
- const writes = stubWrite({ ok: true, body: { success: true } });
304
- await saveEditorFile({ path: 'a.ts', repo: 'other' }, 'x', 'crlf');
305
- expect(writes[0].form.get('repo')).toBe('other');
306
- });
307
-
308
- test('a file with no path is refused without touching the network', async () => {
309
- const writes = stubWrite({ ok: true, body: { success: true } });
310
- expect(await saveEditorFile({}, 'x', 'lf')).toBe('failed');
311
- expect(writes).toEqual([]);
312
- });
313
-
314
- test('every refusal reports failed: non-2xx, an error body, a non-JSON body, a dropped connection', async () => {
315
- stubWrite({ ok: false, body: { success: true } });
316
- expect(await saveEditorFile({ path: 'a.ts' }, 'x', 'lf')).toBe('failed');
317
-
318
- stubWrite({ ok: true, body: { success: false } });
319
- expect(await saveEditorFile({ path: 'a.ts' }, 'x', 'lf')).toBe('failed');
320
-
321
- stubWrite({ ok: true, body: null });
322
- expect(await saveEditorFile({ path: 'a.ts' }, 'x', 'lf')).toBe('failed');
323
-
324
- stubWrite({ ok: true, throwOnJson: true });
325
- expect(await saveEditorFile({ path: 'a.ts' }, 'x', 'lf')).toBe('failed');
326
-
327
- globalThis.fetch = (async () => { throw new Error('offline'); }) as unknown as typeof fetch;
328
- expect(await saveEditorFile({ path: 'a.ts' }, 'x', 'lf')).toBe('failed');
329
- });
330
- });
331
-
332
- describe('retainKeys', () => {
333
- test('keeps the same object when nothing was dropped, so children keep their identity', () => {
334
- const record = { a: 1, b: 2 };
335
- expect(retainKeys(record, new Set(['a', 'b']))).toBe(record);
336
- });
337
-
338
- test('drops the keys that left the tab set and keeps the rest', () => {
339
- const record = { a: 1, b: 2, c: 3 };
340
- expect(retainKeys(record, new Set(['a', 'c']))).toEqual({ a: 1, c: 3 });
341
- expect(retainKeys(record, new Set())).toEqual({});
342
- });
343
- });
344
-
@@ -0,0 +1,104 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The indentation setting.
8
+ *
9
+ * Two things are pinned here. The stored value is GUARDED on read — a width outside
10
+ * the offered set is a downgrade or a hand-edited row, and configuring the surface
11
+ * with a value the picker cannot show is a setting the user cannot change back. And
12
+ * a tab-indented unit is `'\t'`, the only spelling `indentUnit` accepts for a tab: the
13
+ * facet is documented as "a string consisting entirely of the same whitespace
14
+ * character", so a mixture would be a facet that silently does nothing.
15
+ */
16
+
17
+ import { afterAll, afterEach, describe, expect, test } from 'bun:test';
18
+ import { Window } from 'happy-dom';
19
+
20
+ import {
21
+ DEFAULT_EDITOR_INDENT,
22
+ EDITOR_INDENT_CHANGED_EVENT,
23
+ currentEditorIndent,
24
+ indentUnitFor,
25
+ } from '@/client/hooks/editor/indent-settings';
26
+ import { primeKabmoSettings, writeKabmoSettings } from '@/shared/lib/settings/client';
27
+
28
+ const DOM_GLOBALS = ['window', 'document', 'navigator', 'Event', 'CustomEvent'] as const;
29
+ const native: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
30
+ const win = new Window({ url: 'http://localhost' });
31
+ for (const key of DOM_GLOBALS) {
32
+ const target = globalThis as unknown as Record<string, unknown>;
33
+ native[key] = target[key];
34
+ target[key] = (win as unknown as Record<string, unknown>)[key];
35
+ }
36
+
37
+ afterAll(() => {
38
+ const target = globalThis as unknown as Record<string, unknown>;
39
+ for (const key of DOM_GLOBALS) target[key] = native[key];
40
+ });
41
+
42
+ afterEach(() => {
43
+ // The blob is module-level in the settings client, so every case primes it.
44
+ primeKabmoSettings({});
45
+ });
46
+
47
+ describe('currentEditorIndent', () => {
48
+ test('an unset value reads as the shipped default', () => {
49
+ expect(currentEditorIndent()).toEqual(DEFAULT_EDITOR_INDENT);
50
+ expect(DEFAULT_EDITOR_INDENT).toEqual({ width: 2, insertSpaces: true });
51
+ });
52
+
53
+ test('a stored width and spaces flag are adopted', () => {
54
+ primeKabmoSettings({ kabmo_settings: { editorIndentWidth: 4, editorInsertSpaces: true } });
55
+ expect(currentEditorIndent()).toEqual({ width: 4, insertSpaces: true });
56
+ });
57
+
58
+ test('a width outside the offered set falls back, so the picker can still show it', () => {
59
+ // A downgrade or a hand-edited row must not configure the surface with a value
60
+ // the picker cannot represent — that would be a setting with no way back.
61
+ primeKabmoSettings({ kabmo_settings: { editorIndentWidth: 3 } });
62
+ expect(currentEditorIndent().width).toBe(2);
63
+ primeKabmoSettings({ kabmo_settings: { editorIndentWidth: '4' } });
64
+ expect(currentEditorIndent().width).toBe(2);
65
+ });
66
+
67
+ test('a non-boolean spaces flag falls back', () => {
68
+ primeKabmoSettings({ kabmo_settings: { editorInsertSpaces: 'no' } });
69
+ expect(currentEditorIndent().insertSpaces).toBe(true);
70
+ });
71
+
72
+ test('a tab-indented setting is preserved', () => {
73
+ primeKabmoSettings({ kabmo_settings: { editorIndentWidth: 4, editorInsertSpaces: false } });
74
+ expect(currentEditorIndent()).toEqual({ width: 4, insertSpaces: false });
75
+ });
76
+ });
77
+
78
+ describe('indentUnitFor', () => {
79
+ test('spaces become a run of that many spaces', () => {
80
+ expect(indentUnitFor({ width: 2, insertSpaces: true })).toBe(' ');
81
+ expect(indentUnitFor({ width: 4, insertSpaces: true })).toBe(' ');
82
+ expect(indentUnitFor({ width: 8, insertSpaces: true })).toBe(' ');
83
+ });
84
+
85
+ test('a tab-indented setting is ONE tab, whatever the width says', () => {
86
+ // `indentUnit` requires a string of one repeated whitespace character, so a
87
+ // width is meaningless here — mixing them would be a facet that does nothing.
88
+ expect(indentUnitFor({ width: 4, insertSpaces: false })).toBe('\t');
89
+ expect(indentUnitFor({ width: 2, insertSpaces: false })).toBe('\t');
90
+ });
91
+ });
92
+
93
+ describe('the live event', () => {
94
+ test('the event name is the one the editor subscribes to', () => {
95
+ // Both ends state this string, so a rename on one side would silently stop
96
+ // reaching an open editor.
97
+ expect(EDITOR_INDENT_CHANGED_EVENT).toBe('omp:editor-indent-changed');
98
+ });
99
+
100
+ test('a write lands in the blob the readers resolve', () => {
101
+ writeKabmoSettings({ editorIndentWidth: 8 });
102
+ expect(currentEditorIndent().width).toBe(8);
103
+ });
104
+ });
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The editor's indentation SETTING: how wide a level is, and what a Tab inserts.
8
+ *
9
+ * The surface has always hardcoded two spaces (`extensions.ts`' `INDENT`), which is
10
+ * wrong for every 4-space repository — and now that the auto-indent rules emit
11
+ * `indentUnit`, the setting propagates through them for free (the service reads
12
+ * `ctx.unit`, which is `getIndentUnit(state)`).
13
+ *
14
+ * ## The setting reaches an open editor through an EVENT
15
+ *
16
+ * Same reason the font and the theme do: `appSettings` is a boot-time prop, so a
17
+ * value read once would not reach the editor already on screen. The picker writes
18
+ * the setting AND announces it; the editor subscribes. The event fires only when the
19
+ * value actually moved, or every unrelated settings toggle would churn every
20
+ * editor's configuration — and a reconfigure re-creates the editor's plugins,
21
+ * including the tokenizer.
22
+ *
23
+ * ## Two facets, because they answer different questions
24
+ *
25
+ * `EditorState.tabSize` is what a literal TAB character counts as. `indentUnit` is
26
+ * what one indentation LEVEL is, and it is what `indentMore`/`indentLess` and the
27
+ * indent service emit. A tab-indented repository wants both to be 1 tab; a
28
+ * space-indented one wants a width and `insertSpaces`.
29
+ */
30
+
31
+ import { useEffect, useState } from 'preact/hooks';
32
+ import { readKabmoSetting } from '@/shared/lib/settings/client';
33
+
34
+ /** Broadcast on every indentation-setting write. Detail is the new value. */
35
+ export const EDITOR_INDENT_CHANGED_EVENT = 'omp:editor-indent-changed';
36
+
37
+ /** The widths the picker offers. */
38
+ export const EDITOR_INDENT_WIDTHS: readonly number[] = [2, 4, 8];
39
+
40
+ /** What the editor's indentation resolves to. */
41
+ export interface EditorIndentSettings {
42
+ /** Columns in one level. */
43
+ width: number;
44
+ /** `false` means a level is one TAB character. */
45
+ insertSpaces: boolean;
46
+ }
47
+
48
+ export const DEFAULT_EDITOR_INDENT: EditorIndentSettings = { width: 2, insertSpaces: true };
49
+
50
+ /** The stored indentation, falling back to the shipped default. */
51
+ export function currentEditorIndent(): EditorIndentSettings {
52
+ if (typeof window === 'undefined') return DEFAULT_EDITOR_INDENT;
53
+ const width = readKabmoSetting<number>('editorIndentWidth');
54
+ const spaces = readKabmoSetting<boolean>('editorInsertSpaces');
55
+ return {
56
+ // A stored width outside the offered set is a downgrade or a hand-edited row;
57
+ // the surface must not be configured with a value the picker cannot show.
58
+ width: typeof width === 'number' && EDITOR_INDENT_WIDTHS.includes(width) ? width : DEFAULT_EDITOR_INDENT.width,
59
+ insertSpaces: typeof spaces === 'boolean' ? spaces : DEFAULT_EDITOR_INDENT.insertSpaces,
60
+ };
61
+ }
62
+
63
+ /**
64
+ * The indentation unit as a string — what `indentUnit.of` takes.
65
+ *
66
+ * `'\t'` for a tab-indented setting, which is the ONE way `indentUnit` accepts a
67
+ * tab: the facet is documented as "a string consisting entirely of the same
68
+ * whitespace character".
69
+ */
70
+ export function indentUnitFor(settings: EditorIndentSettings): string {
71
+ return settings.insertSpaces ? ' '.repeat(settings.width) : '\t';
72
+ }
73
+
74
+ /** The stored indentation, kept in step with writes from the settings modal. */
75
+ export function useEditorIndent(): EditorIndentSettings {
76
+ const [settings, setSettings] = useState<EditorIndentSettings>(currentEditorIndent);
77
+
78
+ useEffect(() => {
79
+ const onChange = (event: Event) => {
80
+ const detail = (event as CustomEvent<EditorIndentSettings>).detail;
81
+ setSettings(detail ?? currentEditorIndent());
82
+ };
83
+ window.addEventListener(EDITOR_INDENT_CHANGED_EVENT, onChange);
84
+ return () => window.removeEventListener(EDITOR_INDENT_CHANGED_EVENT, onChange);
85
+ }, []);
86
+
87
+ return settings;
88
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The quick-open ranking, and the index reader.
8
+ *
9
+ * The ranking is the feature: a picker that lists every file whose path contains the
10
+ * query is not a picker, it is a filtered tree. The cases below are the ones that
11
+ * distinguish a useful order from an alphabetical one.
12
+ */
13
+
14
+ import { afterEach, describe, expect, test } from 'bun:test';
15
+
16
+ import {
17
+ fetchQuickOpenIndex,
18
+ isSubsequence,
19
+ quickOpenEntry,
20
+ rankQuickOpen,
21
+ scoreQuickOpen,
22
+ type QuickOpenEntry,
23
+ } from '@/client/hooks/editor/quick-open';
24
+
25
+ const nativeFetch = Bun.fetch;
26
+
27
+ afterEach(() => {
28
+ globalThis.fetch = nativeFetch;
29
+ });
30
+
31
+ const ENTRIES: QuickOpenEntry[] = [
32
+ 'src/client/hooks/editor/use-file-tabs.ts',
33
+ 'src/client/components/workspace/editor/Tabs.tsx',
34
+ 'src/shared/lib/code/editor/keymap.ts',
35
+ 'src/client/hooks/editor/quick-open.ts',
36
+ 'README.md',
37
+ ].map(quickOpenEntry);
38
+
39
+ describe('quickOpenEntry', () => {
40
+ test('splits the path into the name and the directory a row prints', () => {
41
+ expect(quickOpenEntry('src/a/b.ts')).toEqual({ path: 'src/a/b.ts', name: 'b.ts', directory: 'src/a' });
42
+ });
43
+
44
+ test('a root-level file has no directory', () => {
45
+ expect(quickOpenEntry('README.md')).toEqual({ path: 'README.md', name: 'README.md', directory: '' });
46
+ });
47
+ });
48
+
49
+ describe('scoreQuickOpen', () => {
50
+ test('an exact basename beats a prefix, which beats a substring', () => {
51
+ const exact = scoreQuickOpen(quickOpenEntry('a/quick-open.ts'), 'quick-open.ts')!;
52
+ const prefix = scoreQuickOpen(quickOpenEntry('a/quick-open.tsx'), 'quick-open')!;
53
+ const inside = scoreQuickOpen(quickOpenEntry('a/xquick-open.ts'), 'quick-open')!;
54
+ expect(exact).toBeGreaterThan(prefix);
55
+ expect(prefix).toBeGreaterThan(inside);
56
+ });
57
+
58
+ test('a basename hit always beats a directory-only hit', () => {
59
+ // Typing a file's name must find the FILE, not every sibling in a folder named
60
+ // after it — which is the failure a plain `path.includes` produces.
61
+ const nameHit = scoreQuickOpen(quickOpenEntry('deep/nested/editor.ts'), 'editor')!;
62
+ const dirHit = scoreQuickOpen(quickOpenEntry('editor/other.ts'), 'editor')!;
63
+ expect(nameHit).toBeGreaterThan(dirHit);
64
+ });
65
+
66
+ test('shorter paths win ties, because the file you want is usually nearer the root', () => {
67
+ const near = scoreQuickOpen(quickOpenEntry('a/keymap.ts'), 'keymap')!;
68
+ const far = scoreQuickOpen(quickOpenEntry('a/b/c/d/e/keymap.ts'), 'keymap')!;
69
+ expect(near).toBeGreaterThan(far);
70
+ });
71
+
72
+ test('an empty query matches everything with the same score', () => {
73
+ expect(scoreQuickOpen(quickOpenEntry('a.ts'), '')).toBe(0);
74
+ });
75
+
76
+ test('a non-match is null, and a subsequence is the last resort', () => {
77
+ expect(scoreQuickOpen(quickOpenEntry('src/a.ts'), 'zzz')).toBeNull();
78
+ // `uft` is a subsequence of `use-file-tabs`, and it must still be found — that
79
+ // is how people type an acronym for a long name.
80
+ expect(scoreQuickOpen(quickOpenEntry('a/use-file-tabs.ts'), 'uft')).not.toBeNull();
81
+ });
82
+ });
83
+
84
+ describe('isSubsequence', () => {
85
+ test('matches characters in order, not contiguously', () => {
86
+ expect(isSubsequence('uft', 'use-file-tabs')).toBe(true);
87
+ expect(isSubsequence('tabs', 'use-file-tabs')).toBe(true);
88
+ expect(isSubsequence('ftu', 'use-file-tabs')).toBe(false);
89
+ });
90
+
91
+ test('an empty needle matches', () => {
92
+ expect(isSubsequence('', 'anything')).toBe(true);
93
+ });
94
+ });
95
+
96
+ describe('rankQuickOpen', () => {
97
+ test('returns the best matches first and respects the limit', () => {
98
+ const ranked = rankQuickOpen(ENTRIES, 'tabs');
99
+ expect(ranked[0].name).toBe('Tabs.tsx');
100
+ expect(ranked.map((entry) => entry.path)).toContain('src/client/hooks/editor/use-file-tabs.ts');
101
+ expect(rankQuickOpen(ENTRIES, 'tabs', 1)).toHaveLength(1);
102
+ });
103
+
104
+ test('an empty query lists the index in path order', () => {
105
+ const ranked = rankQuickOpen(ENTRIES, '');
106
+ expect(ranked).toHaveLength(ENTRIES.length);
107
+ expect(ranked.map((entry) => entry.path)).toEqual([...ranked.map((entry) => entry.path)].sort());
108
+ });
109
+
110
+ test('a query that matches nothing answers an empty list', () => {
111
+ expect(rankQuickOpen(ENTRIES, 'zzzz')).toEqual([]);
112
+ });
113
+ });
114
+
115
+ describe('fetchQuickOpenIndex', () => {
116
+ test('reads the flat index and shapes every row', async () => {
117
+ globalThis.fetch = (async () => ({
118
+ ok: true,
119
+ json: async () => ({ files: [{ path: 'src/a.ts' }, { path: 'b/c.ts' }] }),
120
+ })) as unknown as typeof fetch;
121
+ expect(await fetchQuickOpenIndex('/repo')).toEqual([
122
+ { path: 'src/a.ts', name: 'a.ts', directory: 'src' },
123
+ { path: 'b/c.ts', name: 'c.ts', directory: 'b' },
124
+ ]);
125
+ });
126
+
127
+ test('a refusal or a malformed row answers an empty list, never a throw', async () => {
128
+ globalThis.fetch = (async () => ({ ok: false, json: async () => ({}) })) as unknown as typeof fetch;
129
+ expect(await fetchQuickOpenIndex('/repo')).toEqual([]);
130
+
131
+ globalThis.fetch = (async () => ({
132
+ ok: true,
133
+ json: async () => ({ files: [{ path: 42 }, null, { path: 'ok.ts' }] }),
134
+ })) as unknown as typeof fetch;
135
+ // A row without a string path is dropped rather than rendered as `undefined`.
136
+ expect((await fetchQuickOpenIndex('/repo')).map((entry) => entry.path)).toEqual(['ok.ts']);
137
+
138
+ globalThis.fetch = (async () => { throw new Error('offline'); }) as unknown as typeof fetch;
139
+ expect(await fetchQuickOpenIndex('/repo')).toEqual([]);
140
+ });
141
+ });