@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,56 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * "In selection" and the query history.
8
+ *
9
+ * The range is applied INSIDE the scan rather than by slicing the text, which is the
10
+ * whole point: every consumer (the widget's count, the painted marks, replace-one,
11
+ * replace-all) works in DOCUMENT offsets. A slice would hand them a second coordinate
12
+ * system and replace would write at the wrong place.
13
+ */
14
+
15
+ import { describe, expect, test } from 'bun:test';
16
+
17
+ import { findMatches, type FindOptions } from '@/shared/lib/code/editor/find-scan';
18
+
19
+ const BASE: FindOptions = { matchCase: false, wholeWord: false, isRegex: false, inSelection: false };
20
+
21
+ describe('findMatches with a range', () => {
22
+ const text = 'alpha beta alpha gamma alpha';
23
+
24
+ test('without the flag the range is ignored, so a caller cannot restrict by accident', () => {
25
+ const all = findMatches(text, 'alpha', BASE, { start: 0, end: 10 });
26
+ expect(all.matches).toHaveLength(3);
27
+ });
28
+
29
+ test('with the flag only matches fully inside the range survive', () => {
30
+ const inSelection = findMatches(text, 'alpha', { ...BASE, inSelection: true }, { start: 0, end: 10 });
31
+ expect(inSelection.matches).toEqual([{ start: 0, end: 5 }]);
32
+ });
33
+
34
+ test('the offsets are DOCUMENT offsets, not offsets within the range', () => {
35
+ // The second `alpha` starts at 11; a slice would have reported 1.
36
+ const result = findMatches(text, 'alpha', { ...BASE, inSelection: true }, { start: 11, end: 22 });
37
+ expect(result.matches).toEqual([{ start: 11, end: 16 }]);
38
+ });
39
+
40
+ test('a match that STRADDLES the boundary is excluded — it is not wholly in the selection', () => {
41
+ const result = findMatches(text, 'alpha', { ...BASE, inSelection: true }, { start: 3, end: 20 });
42
+ expect(result.matches).toEqual([{ start: 11, end: 16 }]);
43
+ });
44
+
45
+ test('a null range with the flag set behaves like no restriction', () => {
46
+ // The widget disarms the flag when nothing is selected, so this is the defensive
47
+ // branch rather than the normal path.
48
+ const result = findMatches(text, 'alpha', { ...BASE, inSelection: true }, null);
49
+ expect(result.matches).toHaveLength(3);
50
+ });
51
+
52
+ test('an empty range finds nothing, rather than everything', () => {
53
+ const result = findMatches(text, 'alpha', { ...BASE, inSelection: true }, { start: 4, end: 4 });
54
+ expect(result.matches).toEqual([]);
55
+ });
56
+ });
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * What an indent guide means.
8
+ *
9
+ * Three rules, and each is a case a naive version gets wrong: one guide per indented
10
+ * line (not one per column), none on a blank line (its whitespace is trailing noise),
11
+ * and none past the column cap (a minified file is one huge line).
12
+ */
13
+
14
+ import { describe, expect, test } from 'bun:test';
15
+
16
+ import { MAX_GUIDE_COLUMNS, guideColumn, guideColumns } from '@/shared/lib/code/editor/indent-guide-rule';
17
+
18
+ describe('guideColumn', () => {
19
+ test('an indented line gets one guide, at the first level', () => {
20
+ expect(guideColumn(' const a = 1;', 2)).toBe(2);
21
+ expect(guideColumn(' const a = 1;', 2)).toBe(2);
22
+ expect(guideColumn(' const a = 1;', 4)).toBe(4);
23
+ });
24
+
25
+ test('a line at column 0 gets none', () => {
26
+ expect(guideColumn('const a = 1;', 2)).toBeNull();
27
+ expect(guideColumn(' const a = 1;', 2)).toBeNull();
28
+ });
29
+
30
+ test('a blank line gets none, whatever its whitespace', () => {
31
+ // Its indentation is trailing whitespace, not structure — a guide there would
32
+ // draw a rule through the gap that makes a block readable.
33
+ expect(guideColumn('', 2)).toBeNull();
34
+ expect(guideColumn(' ', 2)).toBeNull();
35
+ expect(guideColumn('\t', 2)).toBeNull();
36
+ });
37
+
38
+ test('a tab counts as one column of indent', () => {
39
+ expect(guideColumn('\tconst a = 1;', 1)).toBe(1);
40
+ expect(guideColumn('\tconst a = 1;', 2)).toBeNull();
41
+ });
42
+
43
+ test('a line past the cap gets none, because counting it is work nobody can see', () => {
44
+ const huge = ' '.repeat(MAX_GUIDE_COLUMNS + 1) + 'x';
45
+ expect(guideColumn(huge, 2)).toBeNull();
46
+ });
47
+
48
+ test('a unit of zero or less answers null rather than dividing by nothing', () => {
49
+ expect(guideColumn(' x', 0)).toBeNull();
50
+ expect(guideColumn(' x', -2)).toBeNull();
51
+ });
52
+ });
53
+
54
+ describe('guideColumns', () => {
55
+ test('reports the index and column for each line that gets one', () => {
56
+ expect(guideColumns(['a', ' b', '', ' c'], 2)).toEqual([
57
+ { index: 1, column: 2 },
58
+ { index: 3, column: 2 },
59
+ ]);
60
+ });
61
+
62
+ test('a document with no indentation reports nothing', () => {
63
+ expect(guideColumns(['a', 'b'], 2)).toEqual([]);
64
+ });
65
+ });
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The indent guide's decisions, as a pure function.
8
+ *
9
+ * The plugin (`indent-guide.ts`) is a view layer; what a guide MEANS is decided here,
10
+ * so it can be tested without a mounted editor. Three rules, and each is a case the
11
+ * naive version gets wrong:
12
+ *
13
+ * - a line indented at least one level gets exactly ONE guide (at the first level),
14
+ * not one per column;
15
+ * - a blank line's whitespace is trailing noise, not structure, so it gets none;
16
+ * - a line past `MAX_GUIDE_COLUMNS` gets none, because a minified file is one huge
17
+ * line and counting its indentation per frame is work nobody can see.
18
+ */
19
+
20
+ /** Columns past which a line gets no guide. */
21
+ export const MAX_GUIDE_COLUMNS = 200;
22
+
23
+ /** The first level's column, in `ch`, or null when this line gets no guide. */
24
+ export function guideColumn(text: string, unit: number): number | null {
25
+ if (unit <= 0) return null;
26
+ // A blank line is a gap between blocks; its own indentation says nothing.
27
+ if (!text.trim()) return null;
28
+ const indent = /^[ \t]*/.exec(text)?.[0].length ?? 0;
29
+ if (indent < unit) return null;
30
+ if (indent > MAX_GUIDE_COLUMNS) return null;
31
+ return unit;
32
+ }
33
+
34
+ /**
35
+ * The guides for a run of lines, as `[lineIndex, column]` pairs.
36
+ *
37
+ * `lineIndex` is 0-based within `texts`, which is what a caller mapping back to
38
+ * document positions already has.
39
+ */
40
+ export function guideColumns(texts: readonly string[], unit: number): Array<{ index: number; column: number }> {
41
+ const guides: Array<{ index: number; column: number }> = [];
42
+ texts.forEach((text, index) => {
43
+ const column = guideColumn(text, unit);
44
+ if (column !== null) guides.push({ index, column });
45
+ });
46
+ return guides;
47
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The indent table, and the GATE that keeps it honest.
8
+ *
9
+ * The gate is the load-bearing half. Measured: an `indentService` that returns a
10
+ * NUMBER overrides a stream mode's own answer (JavaScript's 11 became 0), while
11
+ * returning `undefined` falls through. So every language whose installed mode
12
+ * answers must be ABSENT from `INDENT_RULES`, and the first test below
13
+ * re-derives that set from the installed `@codemirror/legacy-modes` — a mode that
14
+ * later grows an `indent()` fails here instead of silently degrading 34 working
15
+ * languages.
16
+ */
17
+
18
+ import { describe, expect, test } from 'bun:test';
19
+
20
+ import { StreamLanguage, indentUnit, getIndentation } from '@codemirror/language';
21
+ import { EditorState } from '@codemirror/state';
22
+
23
+ import {
24
+ INDENT_RULES,
25
+ indentAfter,
26
+ indentForCloser,
27
+ indentOnInputPattern,
28
+ leadingWidth,
29
+ } from '@/shared/lib/code/editor/indent-rules';
30
+ import { STREAM_MODE_LOADERS, STREAM_MODE_EXPORTS, TEXTUAL_ONLY_LANGS } from '@/shared/lib/code/editor/stream-modes';
31
+
32
+ /** One "a block just opened" snippet per language, as the survey used. */
33
+ const OPENER: Record<string, string> = {
34
+ typescript: 'if (x) {', javascript: 'if (x) {', tsx: 'const a = (', jsx: 'const a = (',
35
+ c: 'void f() {', cpp: 'void f() {', csharp: 'void F() {', java: 'void f() {',
36
+ go: 'func f() {', rust: 'fn f() {', kotlin: 'fun f() {', swift: 'func f() {',
37
+ dart: 'void f() {', scala: 'def f = {', groovy: 'def f() {', 'objective-c': 'void f() {',
38
+ css: 'a {', scss: 'a {', less: 'a {', stylus: 'a', sass: 'a', json: '{', json5: '{',
39
+ xml: '<a>', julia: 'function f()', lua: 'if x then', r: 'if (TRUE) {',
40
+ clojure: '(defn f [x]', racket: '(define (f x)', scheme: '(define (f x)',
41
+ elixir: 'defmodule M do', erlang: 'f() ->', python: 'def f():', ruby: 'def f',
42
+ perl: 'if ($x) {', powershell: 'if ($x) {', yaml: 'a:', toml: 'a = {', hcl: 'a {',
43
+ ini: '[a', protobuf: 'message M {', dockerfile: 'RUN a \\', bash: 'if true; then',
44
+ haskell: 'f x =', ocaml: 'let f x =', makefile: 'all:', properties: 'a=1', sql: 'SELECT',
45
+ };
46
+
47
+ /** Languages whose installed mode answers `getIndentation` for its opener. */
48
+ async function languagesTheModeAnswers(): Promise<Set<string>> {
49
+ const answering = new Set<string>();
50
+ for (const [language, opener] of Object.entries(OPENER)) {
51
+ const loader = STREAM_MODE_LOADERS[language];
52
+ if (!loader) continue;
53
+ const module = (await loader()) as Record<string, unknown>;
54
+ const mode = module[STREAM_MODE_EXPORTS[language]];
55
+ const state = EditorState.create({
56
+ doc: opener,
57
+ extensions: [indentUnit.of(' '), StreamLanguage.define(mode as never)],
58
+ });
59
+ if (getIndentation(state, opener.length) !== null) answering.add(language);
60
+ }
61
+ return answering;
62
+ }
63
+
64
+ describe('INDENT_RULES is exactly the set of languages whose parser cannot answer', () => {
65
+ test('a language whose mode answers must NOT have an `opens` rule, or it overrides the mode', async () => {
66
+ const answering = await languagesTheModeAnswers();
67
+ // Measured on 6.5.5: 34 ids answer. Asserting the count is what makes an
68
+ // upstream change to one mode show up as a failure rather than a silent
69
+ // regression in a language nobody re-checked.
70
+ expect(answering.size).toBe(34);
71
+
72
+ const overridden = Object.keys(INDENT_RULES).filter(
73
+ (language) => answering.has(language) && INDENT_RULES[language]?.opens !== undefined,
74
+ );
75
+ expect(overridden).toEqual([]);
76
+ });
77
+
78
+ test('every mode-less language has an `opens` rule, so Enter is answered', async () => {
79
+ const answering = await languagesTheModeAnswers();
80
+ const needsOpens = [
81
+ ...Object.keys(STREAM_MODE_LOADERS).filter((language) => !answering.has(language)),
82
+ ...TEXTUAL_ONLY_LANGS,
83
+ ];
84
+ const missing = needsOpens.filter((language) => INDENT_RULES[language]?.opens === undefined);
85
+ expect(missing).toEqual([]);
86
+ });
87
+
88
+ test('a closer rule may be added to a language whose mode answers Enter', () => {
89
+ // `go`'s own regexp demands exactly one space, so ` }` never dedents there.
90
+ // These entries add the closer half ONLY — an `opens` would shadow the mode.
91
+ const answeringWithCloser = ['go', 'tsx', 'jsx'];
92
+ for (const language of answeringWithCloser) {
93
+ expect(INDENT_RULES[language]?.closes).toBeDefined();
94
+ expect(INDENT_RULES[language]?.opens).toBeUndefined();
95
+ }
96
+ });
97
+ });
98
+
99
+ describe('indentAfter — what Enter asks of the line above', () => {
100
+ test('a YAML key opens a block', () => {
101
+ expect(indentAfter('yaml', 'a:')).toBe(2);
102
+ expect(indentAfter('yaml', 'a:\n b: 1'.split('\n')[1])).toBe(2);
103
+ });
104
+
105
+ test('a YAML list item does NOT open one — its continuation is at its own column', () => {
106
+ expect(indentAfter('yaml', '- item')).toBe(0);
107
+ expect(indentAfter('yaml', ' - item')).toBe(2);
108
+ });
109
+
110
+ test('a brace opens a block in the brace families', () => {
111
+ expect(indentAfter('toml', 'a = {')).toBe(2);
112
+ expect(indentAfter('hcl', 'resource "a" "b" {')).toBe(2);
113
+ expect(indentAfter('graphql', 'query Q {')).toBe(2);
114
+ expect(indentAfter('php', 'function f() {')).toBe(2);
115
+ });
116
+
117
+ test('a keyword opens a block in bash, and a trailing backslash continues in Dockerfile', () => {
118
+ expect(indentAfter('bash', 'if true; then')).toBe(2);
119
+ expect(indentAfter('bash', 'for x in a; do')).toBe(2);
120
+ expect(indentAfter('bash', 'echo hi')).toBe(0);
121
+ expect(indentAfter('dockerfile', 'RUN apt-get install \\')).toBe(2);
122
+ expect(indentAfter('dockerfile', 'FROM node')).toBe(0);
123
+ });
124
+
125
+ test('an expression body opens in the layout languages', () => {
126
+ expect(indentAfter('haskell', 'f x =')).toBe(2);
127
+ expect(indentAfter('ocaml', 'let f x =')).toBe(2);
128
+ });
129
+
130
+ test('an unclosed HTML tag opens, a closing tag does not', () => {
131
+ expect(indentAfter('html', '<div>')).toBe(2);
132
+ expect(indentAfter('html', '</div>')).toBe(0);
133
+ expect(indentAfter('html', '<br/>')).toBe(0);
134
+ });
135
+
136
+ test('the flat languages never move the indent', () => {
137
+ for (const language of ['sql', 'makefile', 'properties', 'markdown', 'diff', 'text']) {
138
+ expect(indentAfter(language, 'anything {')).toBe(0);
139
+ expect(indentAfter(language, ' already indented')).toBe(2);
140
+ }
141
+ });
142
+
143
+ test('a language with no rule answers null, so the parser keeps its own answer', () => {
144
+ expect(indentAfter('typescript', 'if (x) {')).toBeNull();
145
+ expect(indentAfter('python', 'def f():')).toBeNull();
146
+ });
147
+
148
+ test('the unit is the caller’s, so a tab-size setting propagates', () => {
149
+ expect(indentAfter('yaml', 'a:', 4)).toBe(4);
150
+ expect(indentAfter('yaml', ' a:', 4)).toBe(8);
151
+ });
152
+ });
153
+
154
+ describe('indentForCloser — what indentOnInput asks of the line that now starts with one', () => {
155
+ test('a closer dedents one level in the bracket families', () => {
156
+ expect(indentForCloser('toml', ' }')).toBe(0);
157
+ expect(indentForCloser('hcl', ' }')).toBe(2);
158
+ expect(indentForCloser('graphql', ' ]')).toBe(0);
159
+ });
160
+
161
+ test('a closer in a flat language is content and is left alone', () => {
162
+ expect(indentForCloser('markdown', ' ]')).toBeNull();
163
+ expect(indentForCloser('yaml', ' }')).toBeNull();
164
+ expect(indentForCloser('sql', ' )')).toBeNull();
165
+ });
166
+
167
+ test('a line that is not a closer is not touched', () => {
168
+ expect(indentForCloser('toml', ' a = 1')).toBeNull();
169
+ // Anchored at both ends: `}x` is content, not a dedent.
170
+ expect(indentForCloser('toml', ' }x')).toBeNull();
171
+ });
172
+
173
+ test('never goes below column zero', () => {
174
+ expect(indentForCloser('toml', '}')).toBe(0);
175
+ });
176
+ });
177
+
178
+ describe('indentOnInputPattern', () => {
179
+ test('a bracket family gets its own closer pattern', () => {
180
+ const pattern = indentOnInputPattern('toml');
181
+ expect(pattern).not.toBeNull();
182
+ expect(pattern!.test('}')).toBe(true);
183
+ expect(pattern!.test(' }')).toBe(true);
184
+ expect(pattern!.test('\t}')).toBe(true);
185
+ expect(pattern!.test(' ]')).toBe(true);
186
+ expect(pattern!.test('}x')).toBe(false);
187
+ expect(pattern!.test('a')).toBe(false);
188
+ });
189
+
190
+ test('bash accepts its own keywords as closers', () => {
191
+ const pattern = indentOnInputPattern('bash');
192
+ expect(pattern!.test('fi')).toBe(true);
193
+ expect(pattern!.test('done')).toBe(true);
194
+ expect(pattern!.test('esac')).toBe(true);
195
+ expect(pattern!.test('echo')).toBe(false);
196
+ });
197
+
198
+ test('a language with no closers gets NO pattern — the generic bracket rule is wrong there', () => {
199
+ // Measured before this gate existed: the generic pattern turned `a:\n }`
200
+ // into `a:\n}`, moving YAML content the user did not ask to move.
201
+ for (const language of ['yaml', 'markdown', 'sql', 'makefile', 'properties', 'diff', 'text', 'haskell', 'ocaml', 'dockerfile']) {
202
+ expect(indentOnInputPattern(language)).toBeNull();
203
+ }
204
+ });
205
+
206
+ test('a language with no rule at all gets no pattern', () => {
207
+ expect(indentOnInputPattern('typescript')).toBeNull();
208
+ expect(indentOnInputPattern('python')).toBeNull();
209
+ });
210
+ });
211
+
212
+ describe('leadingWidth', () => {
213
+ test('counts the leading whitespace only', () => {
214
+ expect(leadingWidth('abc')).toBe(0);
215
+ expect(leadingWidth(' abc')).toBe(2);
216
+ expect(leadingWidth('\tabc')).toBe(1);
217
+ expect(leadingWidth(' ')).toBe(3);
218
+ });
219
+ });
@@ -0,0 +1,198 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * How each language indents a new line, for the questions its parser cannot
8
+ * answer.
9
+ *
10
+ * The code surface parses with a `StreamLanguage` (`stream-modes.ts`), and a
11
+ * legacy stream mode may ship its own `indent()`. Measured against
12
+ * `@codemirror/legacy-modes` 6.5.5: **34 of the 48 mode-backed ids answer
13
+ * `getIndentation`** for a "block just opened" snippet, 14 answer nothing, and 12
14
+ * ids have no mode at all.
15
+ *
16
+ * ## Two independent halves, because the two failures are independent
17
+ *
18
+ * `opens` answers "what indent should the NEXT line have" (Enter). `closes`
19
+ * answers "this line IS a closer, dedent it" (`indentOnInput`). They are separate
20
+ * fields because a mode can do one and not the other — measured on `go`, whose
21
+ * own regexp is `/^\s([{}]|case |default\s*:)$/`: it indents correctly, and it
22
+ * refuses to dedent ` }` because the pattern demands EXACTLY one space. A single
23
+ * combined entry would have had to override the working half to fix the broken
24
+ * one.
25
+ *
26
+ * ## The gate: an `opens` must never shadow a mode that answers
27
+ *
28
+ * Measured: an `indentService` returning a NUMBER overrides a mode's own answer
29
+ * (JavaScript's 11 became 0); returning `undefined` falls through. So a language
30
+ * whose mode answers has NO `opens`, and `indent-rules.test.ts` re-derives that
31
+ * set from the installed modes — a mode that later grows an `indent()` fails
32
+ * there instead of silently degrading 34 working languages. `closes` carries no
33
+ * such constraint: it only adds the cases a mode declines.
34
+ *
35
+ * ## The rule is a TABLE, not a bracket counter
36
+ *
37
+ * A single "depth of `{[(` on the line" rule was measured and rejected: it gives
38
+ * `yaml a:` and `markdown - item` nothing (no bracket), and over-indents
39
+ * `const s = "{"` (a brace inside a string). Each family states its own opener in
40
+ * one line a reader can check, and a language with no reliable textual rule
41
+ * (`sql`, `makefile`, `properties`, `markdown`) states nothing — absence is what
42
+ * makes `indentAfter` return `null` and leave the language alone.
43
+ *
44
+ * ## What this gets wrong, stated rather than hidden
45
+ *
46
+ * No grammar, so the rules are textual: `yaml` cannot tell a `:` inside a quoted
47
+ * scalar from a key, `bash` cannot tell `then` inside a comment from a keyword,
48
+ * and a `{` inside a string still opens a level. Each costs one wrong INITIAL
49
+ * indent the user fixes with one keystroke, against no indent at all.
50
+ */
51
+
52
+ /** Column width assumed when the caller has no `indentUnit` (the surface's default). */
53
+ export const DEFAULT_INDENT_WIDTH = 2;
54
+
55
+ /** One language's textual indent rules. Both halves are optional and independent. */
56
+ export interface IndentRule {
57
+ /**
58
+ * Whether this line opens a level for the lines that follow it.
59
+ *
60
+ * ABSENT for a language whose parser answers — presence would override it.
61
+ */
62
+ opens?: (text: string) => boolean;
63
+ /**
64
+ * A line that IS a closer — anchored at both ends, so `}x` is content rather
65
+ * than a dedent.
66
+ *
67
+ * One pattern serves both `indentForCloser` and the `indentOnInput` language
68
+ * data, which is what stops the two from disagreeing about which lines move. A
69
+ * keyword closer (`fi`, `done`) cannot be expressed as a character set, which
70
+ * is why this is a pattern and not a string of characters.
71
+ */
72
+ closes?: RegExp;
73
+ }
74
+
75
+ /** The line's leading whitespace, in columns (a tab counts as one). */
76
+ export function leadingWidth(text: string): number {
77
+ return /^[ \t]*/.exec(text)?.[0].length ?? 0;
78
+ }
79
+
80
+ /** Ends with a bracket — the C-family shape. */
81
+ const endsWithBracket = (text: string): boolean => /[{[(\[]$/.test(text.trimEnd());
82
+
83
+ /** A single `}`, `]` or `)` on its own line. */
84
+ const BRACKET_CLOSER = /^[ \t]*[}\])]$/;
85
+
86
+ /**
87
+ * The rules, keyed by Shiki id — the key `getLanguageFromPath` answers and the
88
+ * surface holds.
89
+ */
90
+ export const INDENT_RULES: Record<string, IndentRule> = {
91
+ // ── Indentation-significant: a `key:` opens a block ──────────────────────
92
+ // A `- item` deliberately does NOT open one: a list item's continuation sits
93
+ // at the item's own column, not deeper. A comment opens nothing either.
94
+ yaml: {
95
+ opens: (text) => /:\s*$/.test(text.trimEnd()) && !/^[ \t]*#/.test(text),
96
+ },
97
+
98
+ // ── Brace families whose mode answers nothing ────────────────────────────
99
+ toml: { opens: endsWithBracket, closes: BRACKET_CLOSER },
100
+ ini: { opens: endsWithBracket, closes: BRACKET_CLOSER },
101
+ hcl: { opens: endsWithBracket, closes: BRACKET_CLOSER },
102
+ protobuf: { opens: endsWithBracket, closes: BRACKET_CLOSER },
103
+ graphql: { opens: endsWithBracket, closes: BRACKET_CLOSER },
104
+ nix: { opens: endsWithBracket, closes: BRACKET_CLOSER },
105
+ svelte: { opens: endsWithBracket, closes: BRACKET_CLOSER },
106
+ astro: { opens: endsWithBracket, closes: BRACKET_CLOSER },
107
+ vue: { opens: endsWithBracket, closes: BRACKET_CLOSER },
108
+ zig: { opens: endsWithBracket, closes: BRACKET_CLOSER },
109
+ solidity: { opens: endsWithBracket, closes: BRACKET_CLOSER },
110
+ php: { opens: endsWithBracket, closes: BRACKET_CLOSER },
111
+ powershell: { opens: endsWithBracket, closes: BRACKET_CLOSER },
112
+ perl: { opens: endsWithBracket, closes: BRACKET_CLOSER },
113
+
114
+ // ── Keyword-delimited shell ──────────────────────────────────────────────
115
+ bash: {
116
+ opens: (text) => /\b(then|do|else)\s*$/.test(text.trimEnd()),
117
+ closes: /^[ \t]*(fi|done|esac|\})$/,
118
+ },
119
+
120
+ // ── Expression languages whose blocks are layout, not brackets ────────────
121
+ haskell: { opens: (text) => /(=|\bwhere|\bdo|\bof|\bthen|\belse)\s*$/.test(text.trimEnd()) },
122
+ ocaml: { opens: (text) => /(=|\bdo|\bthen|\belse|\bin|\bwith)\s*$/.test(text.trimEnd()) },
123
+
124
+ // ── Continuation by trailing backslash ───────────────────────────────────
125
+ dockerfile: { opens: (text) => /\\$/.test(text.trimEnd()) },
126
+
127
+ // ── Markup: an unclosed, non-self-closing tag opens a level ───────────────
128
+ html: {
129
+ opens: (text) => {
130
+ const line = text.trimEnd();
131
+ return /<[a-zA-Z][^<>]*>$/.test(line) && !/\/>$/.test(line);
132
+ },
133
+ closes: /^[ \t]*<\//,
134
+ },
135
+
136
+ // ── Deliberately flat ────────────────────────────────────────────────────
137
+ // `sql` (clauses are keywords with no terminator), `makefile` (a recipe is a
138
+ // TAB, not a level), `properties` (flat), `markdown` (a continuation sits at
139
+ // the item's column), `diff` (hunk bodies are flat), `text` (nothing).
140
+ sql: { opens: () => false },
141
+ makefile: { opens: () => false },
142
+ properties: { opens: () => false },
143
+ markdown: { opens: () => false },
144
+ diff: { opens: () => false },
145
+ text: { opens: () => false },
146
+
147
+ // ── Mode answers Enter, but its own closer pattern is too strict ──────────
148
+ // `go`'s mode regexp is `/^\s([{}]|case |default\s*:)$/` — it demands EXACTLY
149
+ // one space of indentation, so ` }` never matches and never dedents. These
150
+ // entries add the CLOSER half only; their `opens` stays absent on purpose.
151
+ go: { closes: BRACKET_CLOSER },
152
+ tsx: { closes: BRACKET_CLOSER },
153
+ jsx: { closes: BRACKET_CLOSER },
154
+ };
155
+
156
+ /** Whether this language has any rule at all. */
157
+ export function hasIndentRule(language: string): boolean {
158
+ const rule = INDENT_RULES[language];
159
+ return rule !== undefined && (rule.opens !== undefined || rule.closes !== undefined);
160
+ }
161
+
162
+ /**
163
+ * The indent for the line AFTER `previousText` — what Enter asks.
164
+ *
165
+ * `null` when the language has no `opens` rule, so the caller can fall through to
166
+ * the parser (or to no indent at all).
167
+ */
168
+ export function indentAfter(language: string, previousText: string, unit = DEFAULT_INDENT_WIDTH): number | null {
169
+ const opens = INDENT_RULES[language]?.opens;
170
+ if (!opens) return null;
171
+ const own = leadingWidth(previousText);
172
+ return opens(previousText) ? own + unit : own;
173
+ }
174
+
175
+ /**
176
+ * The indent a line that IS a closer should have — what `indentOnInput` asks.
177
+ *
178
+ * `null` when the language has no closer rule, or when this line is not one: a
179
+ * `}` at the start of a YAML line is text, and moving it would edit the user's
180
+ * content.
181
+ */
182
+ export function indentForCloser(language: string, lineText: string, unit = DEFAULT_INDENT_WIDTH): number | null {
183
+ const closes = INDENT_RULES[language]?.closes;
184
+ if (!closes?.test(lineText)) return null;
185
+ return Math.max(0, leadingWidth(lineText) - unit);
186
+ }
187
+
188
+ /**
189
+ * The `indentOnInput` pattern for a language, or `null`.
190
+ *
191
+ * It is the rule's OWN `closes`, not a derived character class: measured, a
192
+ * generic bracket pattern turned `a:\n }` into `a:\n}` in YAML, moving content
193
+ * the user never asked to move. And a keyword closer cannot be a character set at
194
+ * all, so the two halves share one pattern instead of approximating it.
195
+ */
196
+ export function indentOnInputPattern(language: string): RegExp | null {
197
+ return INDENT_RULES[language]?.closes ?? null;
198
+ }
@@ -86,6 +86,29 @@ describe('editor keymap', () => {
86
86
  expect(isFindBarCommand('toggleComment')).toBe(false);
87
87
  expect(isFindBarCommand('selectNextOccurrence')).toBe(false);
88
88
  });
89
+
90
+ test('folding is bound on both platforms, on chords the app already owns', () => {
91
+ // These are CM6's own `foldKeymap` chords. The reason this test exists is
92
+ // that the palette and the tooltips read the SAME table: a fold command
93
+ // listed with a chord nothing binds is the bug the table prevents.
94
+ expect(commandFor(key('BracketLeft', '[', { ctrlKey: true, shiftKey: true }), false)).toBe('fold');
95
+ expect(commandFor(key('BracketRight', ']', { ctrlKey: true, shiftKey: true }), false)).toBe('unfold');
96
+ expect(commandFor(key('BracketLeft', '[', { metaKey: true, altKey: true }), true)).toBe('fold');
97
+ expect(commandFor(key('BracketRight', ']', { metaKey: true, altKey: true }), true)).toBe('unfold');
98
+ // Ctrl-Alt on both: CM6 binds it without a platform override, and it is the
99
+ // one fold chord that cannot collide with macOS's ⌥⌘ pair.
100
+ expect(commandFor(key('BracketLeft', '[', { ctrlKey: true, altKey: true }), false)).toBe('foldAll');
101
+ expect(commandFor(key('BracketRight', ']', { ctrlKey: true, altKey: true }), false)).toBe('unfoldAll');
102
+ });
103
+
104
+ test('the fold chords do not collide with indent or outdent', () => {
105
+ // `⌘]` indents and `⌘⌥]` unfolds: one modifier apart, which is exactly the
106
+ // pair a hand-written table gets wrong.
107
+ expect(editorStrokeFor('indent', true, false)).toEqual({ code: 'BracketRight', meta: true });
108
+ expect(editorStrokeFor('unfold', true, false)).toEqual({ code: 'BracketRight', meta: true, alt: true });
109
+ expect(editorStrokeFor('indent', false, false)).toEqual({ code: 'BracketRight', ctrl: true });
110
+ expect(editorStrokeFor('unfold', false, false)).toEqual({ code: 'BracketRight', ctrl: true, shift: true });
111
+ });
89
112
  });
90
113
 
91
114
  describe('binding labels', () => {