@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.
- package/README.md +45 -0
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +333 -333
- package/dist/client/static/js/index-18xe7655.css +1 -0
- package/dist/client/static/js/{index-bpptfkz2.js → index-nxpe7835.js} +768 -763
- package/package.json +3 -1
- package/src/client/components/common/code-surface/bracket-guide.test.ts +249 -0
- package/src/client/components/common/code-surface/bracket-guide.ts +131 -0
- package/src/client/components/common/code-surface/documents.test.ts +5 -3
- package/src/client/components/common/code-surface/documents.ts +85 -0
- package/src/client/components/common/code-surface/extensions.test.ts +5 -0
- package/src/client/components/common/code-surface/extensions.ts +140 -7
- package/src/client/components/common/code-surface/fold-stack.test.ts +218 -0
- package/src/client/components/common/code-surface/fold.test.ts +148 -0
- package/src/client/components/common/code-surface/fold.ts +118 -0
- package/src/client/components/common/code-surface/indent-guide.ts +104 -0
- package/src/client/components/common/code-surface/indent.test.ts +239 -0
- package/src/client/components/common/code-surface/indent.ts +124 -0
- package/src/client/components/common/code-surface/index.tsx +76 -46
- package/src/client/components/common/code-surface/keymap.ts +12 -23
- package/src/client/components/common/code-surface/line-commands.test.ts +90 -0
- package/src/client/components/common/code-surface/line-commands.ts +103 -0
- package/src/client/components/common/code-surface/props.ts +87 -0
- package/src/client/components/common/code-surface/stream-language.test.ts +118 -0
- package/src/client/components/common/code-surface/stream-language.ts +123 -0
- package/src/client/components/common/code-surface/theme.test.ts +40 -5
- package/src/client/components/common/code-surface/theme.ts +113 -0
- package/src/client/components/common/code-surface/typing.test.ts +97 -0
- package/src/client/components/common/code-surface/typing.ts +111 -0
- package/src/client/components/common/toolbar-row/index.test.tsx +53 -4
- package/src/client/components/common/toolbar-row/index.tsx +101 -30
- package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +17 -0
- package/src/client/components/layout/desktop-layout/index.tsx +18 -0
- package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +35 -33
- package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +20 -1
- package/src/client/components/mobile/mobile-right-sidebar/editor-props.ts +25 -0
- package/src/client/components/settings/categories/AppearanceSettings.tsx +6 -0
- package/src/client/components/settings/categories/appearance-settings/EditorIndentSection.tsx +87 -0
- package/src/client/components/settings/categories/appearance-settings/EditorTypingSection.tsx +99 -0
- package/src/client/components/workspace/chat-timeline/chat-input/expansion.ts +9 -4
- package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +15 -11
- package/src/client/components/workspace/chat-timeline/chat-input/stretch-send.test.tsx +165 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.test.ts +17 -3
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.tsx +10 -6
- package/src/client/components/workspace/editor/Body.tsx +24 -0
- package/src/client/components/workspace/editor/CommandPalette.tsx +4 -4
- package/src/client/components/workspace/editor/ConflictStrip.tsx +77 -0
- package/src/client/components/workspace/editor/Dialogs.tsx +53 -0
- package/src/client/components/workspace/editor/EmptyEditor.tsx +23 -0
- package/src/client/components/workspace/editor/ExternalChangeStrip.tsx +61 -0
- package/src/client/components/workspace/editor/FileDocument.tsx +89 -1
- package/src/client/components/workspace/editor/FilePane.tsx +43 -0
- package/src/client/components/workspace/editor/FindWidget.test.ts +9 -5
- package/src/client/components/workspace/editor/FindWidget.tsx +29 -0
- package/src/client/components/workspace/editor/GoToLineModal.tsx +123 -0
- package/src/client/components/workspace/editor/Notices.tsx +71 -0
- package/src/client/components/workspace/editor/Pane.tsx +118 -0
- package/src/client/components/workspace/editor/PluginColumn.tsx +32 -0
- package/src/client/components/workspace/editor/QuickOpenModal.tsx +146 -0
- package/src/client/components/workspace/editor/SaveAsModal.tsx +175 -0
- package/src/client/components/workspace/editor/SplitColumn.tsx +98 -0
- package/src/client/components/workspace/editor/SplitPane.tsx +57 -0
- package/src/client/components/workspace/editor/StatusBar.tsx +106 -0
- package/src/client/components/workspace/editor/TabContextMenu.tsx +75 -0
- package/src/client/components/workspace/editor/Tabs.tsx +195 -18
- package/src/client/components/workspace/editor/Toolbar.tsx +54 -181
- package/src/client/components/workspace/editor/index.tsx +199 -189
- package/src/client/components/workspace/editor/props.ts +64 -0
- package/src/client/components/workspace/editor/toolbar-clusters.tsx +300 -0
- package/src/client/components/workspace/file-explorer/Modals.tsx +14 -1
- package/src/client/components/workspace/file-explorer/TreeItem.tsx +2 -1
- package/src/client/components/workspace/file-explorer/clipboard.test.ts +3 -1
- package/src/client/components/workspace/file-explorer/index.test.ts +61 -0
- package/src/client/components/workspace/file-explorer/index.tsx +10 -48
- package/src/client/components/workspace/file-explorer/local-edit.test.ts +8 -3
- package/src/client/components/workspace/search-panel/Body.tsx +6 -1
- package/src/client/hooks/chat/composer/file-drop.ts +22 -37
- package/src/client/hooks/chat/composer/pipeline.test.ts +76 -0
- package/src/client/hooks/chat/composer/pipeline.ts +12 -1
- package/src/client/hooks/editor/caret-position.test.ts +58 -0
- package/src/client/hooks/editor/caret-position.ts +48 -0
- package/src/client/hooks/editor/file-editor-types.ts +200 -0
- package/src/client/hooks/editor/find-history.test.ts +132 -0
- package/src/client/hooks/editor/find-history.ts +46 -0
- package/src/client/hooks/editor/find-selection-range.test.ts +100 -0
- package/src/client/hooks/editor/find-selection-range.ts +49 -0
- package/src/client/hooks/editor/find-state.ts +13 -0
- package/src/client/hooks/editor/font.test.ts +0 -93
- package/src/client/hooks/editor/indent-settings.test.ts +104 -0
- package/src/client/hooks/editor/indent-settings.ts +88 -0
- package/src/client/hooks/editor/quick-open.test.ts +141 -0
- package/src/client/hooks/editor/quick-open.ts +127 -0
- package/src/client/hooks/editor/save-as.test.ts +156 -0
- package/src/client/hooks/editor/save-as.ts +122 -0
- package/src/client/hooks/editor/save-file.test.ts +179 -0
- package/src/client/hooks/editor/save-file.ts +73 -28
- package/src/client/hooks/editor/typing-settings.test.ts +73 -0
- package/src/client/hooks/editor/typing-settings.ts +65 -0
- package/src/client/hooks/editor/use-close-guard.ts +98 -0
- package/src/client/hooks/editor/use-editor-drop-overlay.test.ts +159 -0
- package/src/client/hooks/editor/use-editor-drop.shell.test.ts +224 -0
- package/src/client/hooks/editor/use-editor-drop.ts +58 -68
- package/src/client/hooks/editor/use-editor-find.test.ts +2 -2
- package/src/client/hooks/editor/use-editor-find.ts +38 -3
- package/src/client/hooks/editor/use-editor-panel-keys.ts +152 -0
- package/src/client/hooks/editor/use-editor-tab-state.ts +71 -0
- package/src/client/hooks/editor/use-external-change-notice.ts +71 -0
- package/src/client/hooks/editor/use-external-change.test.ts +88 -0
- package/src/client/hooks/editor/use-external-change.ts +146 -0
- package/src/client/hooks/editor/use-file-editor.ts +83 -116
- package/src/client/hooks/editor/use-file-read.ts +16 -8
- package/src/client/hooks/editor/use-file-writes.ts +28 -3
- package/src/client/hooks/editor/use-mobile-editor-find.ts +69 -0
- package/src/client/hooks/editor/use-mobile-reveal.ts +53 -0
- package/src/client/hooks/editor/use-search-reveal-jump.ts +57 -0
- package/src/client/hooks/editor/use-split-editor.test.tsx +169 -0
- package/src/client/hooks/editor/use-split-editor.ts +91 -0
- package/src/client/hooks/editor/use-unload-guard.tsx +117 -0
- package/src/client/hooks/editor/use-write-conflict.test.ts +164 -0
- package/src/client/hooks/editor/use-write-conflict.ts +152 -0
- package/src/client/hooks/editor/wheel-zoom.test.ts +141 -0
- package/src/client/hooks/editor/wheel-zoom.ts +96 -0
- package/src/client/hooks/ui/drag-presence.test.ts +193 -0
- package/src/client/hooks/ui/drag-presence.ts +108 -0
- package/src/client/hooks/ui/settings-modal.test.ts +115 -0
- package/src/client/hooks/workspace/file-listing.ts +36 -24
- package/src/client/hooks/workspace/file-tabs.test.ts +90 -0
- package/src/client/hooks/workspace/file-tabs.ts +46 -0
- package/src/client/hooks/workspace/file-tree-edits.ts +103 -0
- package/src/client/lib/desktop/dropped-paths.ts +83 -0
- package/src/client/lib/editor/tab-menu.test.ts +127 -0
- package/src/client/lib/editor/tab-menu.ts +127 -0
- package/src/client/lib/editor-dirty-tabs.test.ts +102 -0
- package/src/client/lib/editor-dirty-tabs.ts +103 -0
- package/src/client/lib/native/resume.test.ts +66 -0
- package/src/client/lib/native/resume.ts +42 -0
- package/src/client/main.tsx +8 -0
- package/src/client/tailwind.css +38 -0
- package/src/server/lib/bundler/css.test.ts +31 -0
- package/src/server/lib/fs/listing.test.ts +81 -16
- package/src/server/lib/fs/listing.ts +72 -5
- package/src/server/lib/fs/write-conflict.test.ts +68 -0
- package/src/server/lib/fs/write-conflict.ts +84 -0
- package/src/server/routes/fs/action-save-as.test.ts +82 -0
- package/src/server/routes/fs/action.test.ts +82 -0
- package/src/server/routes/fs/action.ts +54 -1
- package/src/server/routes/fs/exists.ts +11 -1
- package/src/server/routes/fs/read.ts +6 -1
- package/src/shared/lib/code/editor/find-replace.test.ts +1 -1
- package/src/shared/lib/code/editor/find-scan.test.ts +1 -1
- package/src/shared/lib/code/editor/find-scan.ts +22 -1
- package/src/shared/lib/code/editor/find-selection.test.ts +56 -0
- package/src/shared/lib/code/editor/indent-guide-rule.test.ts +65 -0
- package/src/shared/lib/code/editor/indent-guide-rule.ts +47 -0
- package/src/shared/lib/code/editor/indent-rules.test.ts +219 -0
- package/src/shared/lib/code/editor/indent-rules.ts +198 -0
- package/src/shared/lib/code/editor/keymap.test.ts +23 -0
- package/src/shared/lib/code/editor/keymap.ts +40 -3
- package/src/shared/lib/code/editor/palette.test.ts +25 -2
- package/src/shared/lib/code/editor/palette.ts +10 -0
- package/src/shared/lib/code/editor/stream-modes.ts +225 -0
- package/src/shared/lib/code/syntax-highlight.test.ts +1 -1
- package/src/shared/lib/fs/file-tree.test.ts +41 -0
- package/src/shared/lib/fs/file-tree.ts +126 -34
- package/src/shared/lib/fs/folder-chain.test.ts +87 -0
- package/src/shared/lib/fs/folder-chain.ts +91 -0
- package/src/shared/lib/fs/fs-helpers.test.ts +4 -50
- package/src/shared/lib/realtime/connection.test.ts +5 -1
- package/src/shared/lib/realtime/connection.ts +25 -119
- package/src/shared/lib/realtime/frozen-recovery.test.ts +93 -0
- package/src/shared/lib/realtime/resume.ts +223 -0
- package/src/shared/lib/realtime/state.ts +22 -0
- package/src/shared/lib/realtime/watchdog-wiring.test.ts +224 -0
- package/src/shared/lib/realtime/watchdog.test.ts +99 -0
- package/src/shared/lib/realtime/watchdog.ts +72 -0
- package/src/shared/lib/workspace/split-editor.test.ts +104 -0
- package/src/shared/lib/workspace/split-editor.ts +129 -0
- package/src/test-support/file-explorer-disk.test-util.ts +30 -3
- package/src/test-support/pristine-globals.ts +7 -0
- package/dist/client/static/js/index-k315p1e8.css +0 -1
|
@@ -36,56 +36,16 @@ import { EditorView } from '@codemirror/view';
|
|
|
36
36
|
|
|
37
37
|
import { createDocumentCache, type DocumentCache } from '@/client/components/common/code-surface/documents';
|
|
38
38
|
import type { ShikiThemeNames } from '@/client/components/common/code-surface/shiki';
|
|
39
|
+
import type { CodeSurfaceProps } from '@/client/components/common/code-surface/props';
|
|
40
|
+
import { DEFAULT_EDITOR_TYPING } from '@/client/components/common/code-surface/typing';
|
|
39
41
|
import { SHIKI_THEMES } from '@/shared/lib/code/shiki-themes';
|
|
40
42
|
import { createSurfaceHandle, type CodeSurfaceHandle } from '@/client/components/common/code-surface/handle';
|
|
41
43
|
import { setFindMarks, type MarkRange } from '@/client/components/common/code-surface/marks';
|
|
42
44
|
import { syncExternal } from '@/client/components/common/code-surface/sync';
|
|
43
|
-
import
|
|
45
|
+
import { loadStreamMode, readyStreamMode } from '@/client/components/common/code-surface/stream-language';
|
|
44
46
|
|
|
45
47
|
export type { CodeSurfaceHandle } from '@/client/components/common/code-surface/handle';
|
|
46
|
-
|
|
47
|
-
export interface CodeSurfaceProps {
|
|
48
|
-
value: string;
|
|
49
|
-
onValueChange: (value: string) => void;
|
|
50
|
-
/** Shiki language id, already resolved from the file name. */
|
|
51
|
-
language: string;
|
|
52
|
-
wordWrap: boolean;
|
|
53
|
-
/** Which cached `EditorState` this document owns — a tab switch applies another. */
|
|
54
|
-
documentKey: string;
|
|
55
|
-
/** Find matches (document offsets) to paint; the current one is marked. */
|
|
56
|
-
marks?: readonly MarkRange[];
|
|
57
|
-
currentMark?: number;
|
|
58
|
-
/** Commands the editor does not own (find bar chords, word wrap, save). */
|
|
59
|
-
onCommand?: (command: EditorCommand) => void;
|
|
60
|
-
/** A viewer: both `readOnly` and `editable(false)`, so nothing can write. */
|
|
61
|
-
readOnly?: boolean;
|
|
62
|
-
/**
|
|
63
|
-
* The editor THEME, as CSS custom properties (`editorThemeVars`).
|
|
64
|
-
*
|
|
65
|
-
* Written on the host element, so the chrome repaints through CSS and a theme
|
|
66
|
-
* switch costs one attribute write. Empty/absent means `auto`: every
|
|
67
|
-
* declaration in `theme.ts` falls back to the app palette.
|
|
68
|
-
*/
|
|
69
|
-
themeVars?: Record<string, string>;
|
|
70
|
-
/** The two Shiki theme names to tokenize with (the same theme twice when the
|
|
71
|
-
* editor theme is explicit). Defaults to the app's dual pair. */
|
|
72
|
-
themes?: ShikiThemeNames;
|
|
73
|
-
/** Font family / size / line height (the zoom control and the font setting). */
|
|
74
|
-
style?: CSSProperties;
|
|
75
|
-
/** Padding around the document, in px. */
|
|
76
|
-
padding?: number;
|
|
77
|
-
/** `data-testid` for the scroller, so the e2e layer can address it. */
|
|
78
|
-
scrollTestId?: string;
|
|
79
|
-
/**
|
|
80
|
-
* The element that actually scrolls, reported on mount and cleared on unmount.
|
|
81
|
-
*
|
|
82
|
-
* It exists only once the view does, and it is CodeMirror's (`view.scrollDOM`)
|
|
83
|
-
* — a caller that needs to remember the reader's position (`useScrollRestore`)
|
|
84
|
-
* cannot reach it through a `ref`, so the surface hands it over.
|
|
85
|
-
*/
|
|
86
|
-
onScroller?: (element: HTMLElement | null) => void;
|
|
87
|
-
className?: string;
|
|
88
|
-
}
|
|
48
|
+
export type { CodeSurfaceProps } from '@/client/components/common/code-surface/props';
|
|
89
49
|
|
|
90
50
|
export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(function CodeSurface(
|
|
91
51
|
{
|
|
@@ -100,8 +60,11 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
100
60
|
readOnly = false,
|
|
101
61
|
themeVars,
|
|
102
62
|
themes,
|
|
63
|
+
indentUnitText = ' ',
|
|
64
|
+
typing = DEFAULT_EDITOR_TYPING,
|
|
103
65
|
style,
|
|
104
66
|
padding = 16,
|
|
67
|
+
onCaret,
|
|
105
68
|
scrollTestId,
|
|
106
69
|
onScroller,
|
|
107
70
|
className,
|
|
@@ -145,6 +108,8 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
145
108
|
onCommandRef.current = onCommand;
|
|
146
109
|
const onScrollerRef = useRef(onScroller);
|
|
147
110
|
onScrollerRef.current = onScroller;
|
|
111
|
+
const onCaretRef = useRef(onCaret);
|
|
112
|
+
onCaretRef.current = onCaret;
|
|
148
113
|
/** The marks as of the last render, for the re-apply a document switch owes. */
|
|
149
114
|
const marksRef = useRef<readonly MarkRange[]>(marks ?? []);
|
|
150
115
|
const currentMarkRef = useRef(currentMark ?? -1);
|
|
@@ -154,6 +119,11 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
154
119
|
const activeThemes = themes ?? SHIKI_THEMES;
|
|
155
120
|
const themesRef = useRef<ShikiThemeNames>(activeThemes);
|
|
156
121
|
themesRef.current = activeThemes;
|
|
122
|
+
/** The indentation unit, mirrored for the cache's first build. */
|
|
123
|
+
const indentRef = useRef(indentUnitText);
|
|
124
|
+
indentRef.current = indentUnitText;
|
|
125
|
+
const typingRef = useRef(typing);
|
|
126
|
+
typingRef.current = typing;
|
|
157
127
|
|
|
158
128
|
// One cache for the component's life: it owns the per-document states.
|
|
159
129
|
const cacheRef = useRef<DocumentCache | null>(null);
|
|
@@ -162,6 +132,8 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
162
132
|
wordWrap,
|
|
163
133
|
readOnly,
|
|
164
134
|
themes: themesRef.current,
|
|
135
|
+
indentUnitText: indentRef.current,
|
|
136
|
+
typing: typingRef.current,
|
|
165
137
|
onDocChange: (next) => {
|
|
166
138
|
// Recorded BEFORE the writing guard: this is what the document holds,
|
|
167
139
|
// whether or not the change came from the user.
|
|
@@ -174,6 +146,7 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
174
146
|
if (key) cacheRef.current?.remember(key, state);
|
|
175
147
|
},
|
|
176
148
|
onCommand: (command) => onCommandRef.current?.(command),
|
|
149
|
+
onCaret: (position) => onCaretRef.current?.(position),
|
|
177
150
|
});
|
|
178
151
|
}
|
|
179
152
|
|
|
@@ -193,6 +166,14 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
193
166
|
lastValueRef.current = value;
|
|
194
167
|
if (scrollTestId) view.scrollDOM.dataset.testid = scrollTestId;
|
|
195
168
|
onScrollerRef.current?.(view.scrollDOM);
|
|
169
|
+
// The caret is reported by the UPDATE listener, which only fires when something
|
|
170
|
+
// moves — so a freshly mounted document would report nothing and a status readout
|
|
171
|
+
// would print its empty state over a file that has a caret. Seeded here, once.
|
|
172
|
+
const start = view.state.doc.lineAt(view.state.selection.main.head);
|
|
173
|
+
onCaretRef.current?.({
|
|
174
|
+
line: start.number,
|
|
175
|
+
column: view.state.selection.main.head - start.from + 1,
|
|
176
|
+
});
|
|
196
177
|
|
|
197
178
|
return () => {
|
|
198
179
|
onScrollerRef.current?.(null);
|
|
@@ -219,6 +200,14 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
219
200
|
// was off comes back unwrapped and a theme chosen since comes back stale.
|
|
220
201
|
view.dispatch({ effects: cache.currentEffects(documentKey) });
|
|
221
202
|
syncExternal(view, value, writingRef, lastValueRef);
|
|
203
|
+
// The other document's caret, not the one this view was showing: a status
|
|
204
|
+
// readout that kept the previous tab's line number would be describing a file
|
|
205
|
+
// that is no longer on screen.
|
|
206
|
+
const restored = view.state.doc.lineAt(view.state.selection.main.head);
|
|
207
|
+
onCaretRef.current?.({
|
|
208
|
+
line: restored.number,
|
|
209
|
+
column: view.state.selection.main.head - restored.from + 1,
|
|
210
|
+
});
|
|
222
211
|
// The marks belonged to the previous document's offsets, and the field lives
|
|
223
212
|
// in that state. A fresh state starts empty, so this re-applies whatever the
|
|
224
213
|
// new document's owner asked for rather than leaving a stale set behind.
|
|
@@ -245,9 +234,50 @@ export const CodeSurface = forwardRef<CodeSurfaceHandle, CodeSurfaceProps>(funct
|
|
|
245
234
|
const view = viewRef.current;
|
|
246
235
|
const cache = cacheRef.current;
|
|
247
236
|
if (!view || !cache) return;
|
|
248
|
-
const effects = cache.optionsEffects(
|
|
237
|
+
const effects = cache.optionsEffects(
|
|
238
|
+
{ wordWrap, readOnly, themes: activeThemes, indentUnitText, typing },
|
|
239
|
+
docKeyRef.current,
|
|
240
|
+
);
|
|
249
241
|
if (effects.length > 0) view.dispatch({ effects });
|
|
250
|
-
}, [wordWrap, readOnly, activeThemes.light, activeThemes.dark]);
|
|
242
|
+
}, [wordWrap, readOnly, activeThemes.light, activeThemes.dark, indentUnitText, typing]);
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The stream language: fetched for this document's language, installed when it
|
|
246
|
+
* lands.
|
|
247
|
+
*
|
|
248
|
+
* The mode is a lazy import, so the document was created without a parser and
|
|
249
|
+
* `bracketMatching()` answers textually until this reconfigure runs. A mode
|
|
250
|
+
* that fails to load leaves that textual matcher, which is a worse answer
|
|
251
|
+
* rather than a broken editor — `loadStreamMode` never rejects.
|
|
252
|
+
*
|
|
253
|
+
* Re-installed on a DOCUMENT SWITCH too: the compartment is per state, and a
|
|
254
|
+
* cached tab coming back has whatever configuration it was created with.
|
|
255
|
+
*/
|
|
256
|
+
useLayoutEffect(() => {
|
|
257
|
+
const view = viewRef.current;
|
|
258
|
+
const cache = cacheRef.current;
|
|
259
|
+
if (!view || !cache) return;
|
|
260
|
+
|
|
261
|
+
let cancelled = false;
|
|
262
|
+
const install = (mode: unknown) => {
|
|
263
|
+
if (cancelled || !mode) return;
|
|
264
|
+
const live = viewRef.current;
|
|
265
|
+
if (!live) return;
|
|
266
|
+
const effects = cache.languageEffects(docKeyRef.current, mode as never);
|
|
267
|
+
if (effects.length > 0) live.dispatch({ effects });
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// Already loaded (a second tab in the same language): install immediately so
|
|
271
|
+
// the tree exists on the first paint, not one microtask later.
|
|
272
|
+
const ready = readyStreamMode(language);
|
|
273
|
+
if (ready) {
|
|
274
|
+
install(ready);
|
|
275
|
+
return () => { cancelled = true; };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
void loadStreamMode(language).then(install);
|
|
279
|
+
return () => { cancelled = true; };
|
|
280
|
+
}, [language, documentKey]);
|
|
251
281
|
|
|
252
282
|
// Find marks: a field write, never a re-tokenize.
|
|
253
283
|
useLayoutEffect(() => {
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
import type { Extension, StateCommand } from '@codemirror/state';
|
|
23
|
-
import { EditorSelection } from '@codemirror/state';
|
|
24
23
|
import {
|
|
24
|
+
addCursorAbove,
|
|
25
25
|
addCursorBelow,
|
|
26
26
|
defaultKeymap,
|
|
27
27
|
copyLineDown,
|
|
@@ -43,13 +43,16 @@ import {
|
|
|
43
43
|
selectLineEnd,
|
|
44
44
|
selectLineStart,
|
|
45
45
|
simplifySelection,
|
|
46
|
+
toggleBlockComment,
|
|
46
47
|
toggleComment,
|
|
47
48
|
undo,
|
|
48
49
|
} from '@codemirror/commands';
|
|
49
50
|
import { selectNextOccurrence, selectSelectionMatches } from '@codemirror/search';
|
|
51
|
+
import { foldAll, foldCode, unfoldAll, unfoldCode } from '@codemirror/language';
|
|
50
52
|
import { keymap, type Command, type EditorView, type KeyBinding } from '@codemirror/view';
|
|
51
53
|
|
|
52
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';
|
|
53
56
|
import type { KeyStroke } from '@/shared/lib/ui/key-binding';
|
|
54
57
|
|
|
55
58
|
/** A command as this module invokes it: CM6's `Command` and `StateCommand` both fit. */
|
|
@@ -78,27 +81,6 @@ const insertLineAbove: SurfaceCommand = (view) => {
|
|
|
78
81
|
return true;
|
|
79
82
|
};
|
|
80
83
|
|
|
81
|
-
/** Select the word under the caret, or nothing when the caret is between words. */
|
|
82
|
-
const selectWord: SurfaceCommand = (view) => {
|
|
83
|
-
const { state } = view;
|
|
84
|
-
const range = state.selection.main;
|
|
85
|
-
if (range.from !== range.to) return false;
|
|
86
|
-
const line = state.doc.lineAt(range.from);
|
|
87
|
-
const offset = range.from - line.from;
|
|
88
|
-
const isWord = (char: string | undefined) => char !== undefined && /[\w$]/.test(char);
|
|
89
|
-
let start = offset;
|
|
90
|
-
let end = offset;
|
|
91
|
-
if (!isWord(line.text[start]) && isWord(line.text[start - 1])) start--;
|
|
92
|
-
if (!isWord(line.text[start])) return false;
|
|
93
|
-
end = start;
|
|
94
|
-
while (isWord(line.text[start - 1])) start--;
|
|
95
|
-
while (isWord(line.text[end])) end++;
|
|
96
|
-
view.dispatch({
|
|
97
|
-
selection: EditorSelection.range(line.from + start, line.from + end),
|
|
98
|
-
});
|
|
99
|
-
return true;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
84
|
/**
|
|
103
85
|
* Collapse extra selections, keeping the main one.
|
|
104
86
|
*
|
|
@@ -122,16 +104,24 @@ const BUFFER_COMMANDS: Partial<Record<EditorCommand, SurfaceCommand>> = {
|
|
|
122
104
|
selectNextOccurrence: selectNextOccurrence as SurfaceCommand,
|
|
123
105
|
selectAllOccurrences: selectSelectionMatches as SurfaceCommand,
|
|
124
106
|
insertCursorBelow: addCursorBelow as SurfaceCommand,
|
|
107
|
+
insertCursorAbove: addCursorAbove as SurfaceCommand,
|
|
125
108
|
moveLineUp: moveLineUp as SurfaceCommand,
|
|
126
109
|
moveLineDown: moveLineDown as SurfaceCommand,
|
|
127
110
|
copyLineUp: copyLineUp as SurfaceCommand,
|
|
128
111
|
copyLineDown: copyLineDown as SurfaceCommand,
|
|
129
112
|
deleteLine: deleteLine as SurfaceCommand,
|
|
130
113
|
toggleComment: toggleComment as SurfaceCommand,
|
|
114
|
+
toggleBlockComment: toggleBlockComment as SurfaceCommand,
|
|
115
|
+
joinLines,
|
|
116
|
+
trimTrailingWhitespace,
|
|
131
117
|
insertLineBelow: insertBlankLine as SurfaceCommand,
|
|
132
118
|
insertLineAbove,
|
|
133
119
|
indent: indentMore as SurfaceCommand,
|
|
134
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,
|
|
135
125
|
lineStart: cursorLineStart as SurfaceCommand,
|
|
136
126
|
lineEnd: cursorLineEnd as SurfaceCommand,
|
|
137
127
|
documentStart: cursorDocStart as SurfaceCommand,
|
|
@@ -140,7 +130,6 @@ const BUFFER_COMMANDS: Partial<Record<EditorCommand, SurfaceCommand>> = {
|
|
|
140
130
|
selectToLineEnd: selectLineEnd as SurfaceCommand,
|
|
141
131
|
selectToDocumentStart: selectDocStart as SurfaceCommand,
|
|
142
132
|
selectToDocumentEnd: selectDocEnd as SurfaceCommand,
|
|
143
|
-
selectWord,
|
|
144
133
|
selectLine: selectLine as SurfaceCommand,
|
|
145
134
|
escape: collapseSelections,
|
|
146
135
|
};
|
|
@@ -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,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The code surface's prop contract.
|
|
8
|
+
*
|
|
9
|
+
* Split out of `index.tsx` for the repository's per-file ceiling, and it belongs
|
|
10
|
+
* here anyway: the diff panel, the phone's editor and every test that mounts a
|
|
11
|
+
* surface name these, and importing a prop type from the component module couples
|
|
12
|
+
* them to the mount.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { CSSProperties } from 'preact';
|
|
16
|
+
|
|
17
|
+
import type { ShikiThemeNames } from '@/client/components/common/code-surface/shiki';
|
|
18
|
+
import type { MarkRange } from '@/client/components/common/code-surface/marks';
|
|
19
|
+
import type { EditorCommand } from '@/shared/lib/code/editor/keymap';
|
|
20
|
+
import type { EditorTypingOptions } from '@/client/components/common/code-surface/typing';
|
|
21
|
+
|
|
22
|
+
export interface CodeSurfaceProps {
|
|
23
|
+
value: string;
|
|
24
|
+
onValueChange: (value: string) => void;
|
|
25
|
+
/** Shiki language id, already resolved from the file name. */
|
|
26
|
+
language: string;
|
|
27
|
+
wordWrap: boolean;
|
|
28
|
+
/** Which cached `EditorState` this document owns — a tab switch applies another. */
|
|
29
|
+
documentKey: string;
|
|
30
|
+
/** Find matches (document offsets) to paint; the current one is marked. */
|
|
31
|
+
marks?: readonly MarkRange[];
|
|
32
|
+
currentMark?: number;
|
|
33
|
+
/** Commands the editor does not own (find bar chords, word wrap, save). */
|
|
34
|
+
onCommand?: (command: EditorCommand) => void;
|
|
35
|
+
/** A viewer: both `readOnly` and `editable(false)`, so nothing can write. */
|
|
36
|
+
readOnly?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* The editor THEME, as CSS custom properties (`editorThemeVars`).
|
|
39
|
+
*
|
|
40
|
+
* Written on the host element, so the chrome repaints through CSS and a theme
|
|
41
|
+
* switch costs one attribute write. Empty/absent means `auto`: every
|
|
42
|
+
* declaration in `theme.ts` falls back to the app palette.
|
|
43
|
+
*/
|
|
44
|
+
themeVars?: Record<string, string>;
|
|
45
|
+
/** The two Shiki theme names to tokenize with (the same theme twice when the
|
|
46
|
+
* editor theme is explicit). Defaults to the app's dual pair. */
|
|
47
|
+
themes?: ShikiThemeNames;
|
|
48
|
+
/**
|
|
49
|
+
* One indentation level, as a whitespace string (`' '`, `' '`, `'\t'`).
|
|
50
|
+
*
|
|
51
|
+
* The setting reaches the editor through the settings event, not through a prop
|
|
52
|
+
* captured at boot; this is what the owner resolved from it. Defaults to two
|
|
53
|
+
* spaces, which is what the surface hardcoded before the setting existed.
|
|
54
|
+
*/
|
|
55
|
+
indentUnitText?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Auto-close brackets and the document-words completer.
|
|
58
|
+
*
|
|
59
|
+
* Both change what TYPING does, so both are settings; the surface takes the resolved
|
|
60
|
+
* value and reconfigures a compartment when it moves, which keeps an open tab's undo
|
|
61
|
+
* history.
|
|
62
|
+
*/
|
|
63
|
+
typing?: EditorTypingOptions;
|
|
64
|
+
/** Font family / size / line height (the zoom control and the font setting). */
|
|
65
|
+
style?: CSSProperties;
|
|
66
|
+
/** Padding around the document, in px. */
|
|
67
|
+
padding?: number;
|
|
68
|
+
/** `data-testid` for the scroller, so the e2e layer can address it. */
|
|
69
|
+
scrollTestId?: string;
|
|
70
|
+
/**
|
|
71
|
+
* The caret's line and column, reported whenever either moves.
|
|
72
|
+
*
|
|
73
|
+
* Separate from the state the surface caches: a status readout wants two numbers,
|
|
74
|
+
* and keeping an `EditorState` for them would pin a document snapshot in a
|
|
75
|
+
* component that prints its line number.
|
|
76
|
+
*/
|
|
77
|
+
onCaret?: (position: { line: number; column: number }) => void;
|
|
78
|
+
/**
|
|
79
|
+
* The element that actually scrolls, reported on mount and cleared on unmount.
|
|
80
|
+
*
|
|
81
|
+
* It exists only once the view does, and it is CodeMirror's (`view.scrollDOM`)
|
|
82
|
+
* — a caller that needs to remember the reader's position (`useScrollRestore`)
|
|
83
|
+
* cannot reach it through a `ref`, so the surface hands it over.
|
|
84
|
+
*/
|
|
85
|
+
onScroller?: (element: HTMLElement | null) => void;
|
|
86
|
+
className?: string;
|
|
87
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Installing a stream language into a LIVE document.
|
|
8
|
+
*
|
|
9
|
+
* The mode is a lazy import, so every document is created without one and gets
|
|
10
|
+
* it later through a compartment reconfigure. Three things must survive that
|
|
11
|
+
* reconfigure, and each is a measured way it could break:
|
|
12
|
+
*
|
|
13
|
+
* - the document itself, or the user's edits are lost;
|
|
14
|
+
* - the undo HISTORY, which is why this is a compartment rather than a new
|
|
15
|
+
* state;
|
|
16
|
+
* - the comment-token PRECEDENCE: `languageDataAt` returns providers in order
|
|
17
|
+
* and the commands take the first, so a mode installed after the manual table
|
|
18
|
+
* would let the manual entry win.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { describe, expect, test } from 'bun:test';
|
|
22
|
+
import type { EditorState } from '@codemirror/state';
|
|
23
|
+
import { toggleComment, undo } from '@codemirror/commands';
|
|
24
|
+
import { StreamLanguage, syntaxTree } from '@codemirror/language';
|
|
25
|
+
import { rust } from '@codemirror/legacy-modes/mode/rust';
|
|
26
|
+
import { shell } from '@codemirror/legacy-modes/mode/shell';
|
|
27
|
+
|
|
28
|
+
import { createDocumentCache } from '@/client/components/common/code-surface/documents';
|
|
29
|
+
import { SHIKI_THEMES } from '@/shared/lib/code/shiki-themes';
|
|
30
|
+
|
|
31
|
+
function cache() {
|
|
32
|
+
return createDocumentCache({
|
|
33
|
+
wordWrap: false,
|
|
34
|
+
readOnly: false,
|
|
35
|
+
themes: SHIKI_THEMES,
|
|
36
|
+
indentUnitText: ' ',
|
|
37
|
+
typing: { autoCloseBrackets: false, autocomplete: false },
|
|
38
|
+
onDocChange: () => {},
|
|
39
|
+
onState: () => {},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('languageEffects', () => {
|
|
44
|
+
test('installs the mode on a live document and leaves the text alone', () => {
|
|
45
|
+
const documents = cache();
|
|
46
|
+
const doc = 'fn f() {\n let s = "}";\n g();\n}\n';
|
|
47
|
+
const state = documents.stateFor('a.ts', 'rust', doc);
|
|
48
|
+
documents.remember('a.ts', state);
|
|
49
|
+
expect(syntaxTree(state).length).toBe(0);
|
|
50
|
+
|
|
51
|
+
const withMode = state.update({
|
|
52
|
+
effects: documents.languageEffects('a.ts', StreamLanguage.define(rust as never)),
|
|
53
|
+
}).state;
|
|
54
|
+
|
|
55
|
+
expect(withMode.doc.toString()).toBe(doc);
|
|
56
|
+
// The tree is what makes pairing token-aware; before the reconfigure there
|
|
57
|
+
// is none at all.
|
|
58
|
+
expect(syntaxTree(withMode).length).toBe(doc.length);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('the undo history survives the reconfigure', () => {
|
|
62
|
+
const documents = cache();
|
|
63
|
+
const state = documents.stateFor('b.ts', 'rust', 'fn f() {}\n');
|
|
64
|
+
documents.remember('b.ts', state);
|
|
65
|
+
|
|
66
|
+
let live = state.update({ changes: { from: 0, insert: '// hi\n' } }).state;
|
|
67
|
+
live = live.update({ effects: documents.languageEffects('b.ts', StreamLanguage.define(rust as never)) }).state;
|
|
68
|
+
expect(live.doc.toString()).toBe('// hi\nfn f() {}\n');
|
|
69
|
+
|
|
70
|
+
let undone = live;
|
|
71
|
+
// `undo` reads the history the state carries; the reconfigure must not have
|
|
72
|
+
// replaced it with an empty one.
|
|
73
|
+
undo({ state: live, dispatch: (transaction) => { undone = transaction.state; } });
|
|
74
|
+
expect(undone.doc.toString()).toBe('fn f() {}\n');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('a key the cache does not hold gets no effects', () => {
|
|
78
|
+
// A tab that closed while the mode was loading must not be resurrected.
|
|
79
|
+
const documents = cache();
|
|
80
|
+
documents.stateFor('open.ts', 'rust', 'x\n');
|
|
81
|
+
expect(documents.languageEffects('closed.ts', StreamLanguage.define(rust as never))).toEqual([]);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('comment tokens with a mode installed', () => {
|
|
86
|
+
/** A state from the cache, with a mode reconfigured in. */
|
|
87
|
+
function stateWithMode(language: string, mode: ReturnType<typeof StreamLanguage.define>) {
|
|
88
|
+
const documents = cache();
|
|
89
|
+
const state = documents.stateFor('k.ts', language, 'let a = 1;\n');
|
|
90
|
+
documents.remember('k.ts', state);
|
|
91
|
+
return state.update({ effects: documents.languageEffects('k.ts', mode) }).state;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function toggled(state: EditorState): string {
|
|
95
|
+
let next = state;
|
|
96
|
+
toggleComment({ state, dispatch: (transaction) => { next = transaction.state; } });
|
|
97
|
+
return next.doc.toString();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
test('the mode’s own tokens win, because the compartment is installed first', () => {
|
|
101
|
+
// The manual table says `//` for a `typescript` path; the Rust mode says
|
|
102
|
+
// `//` too, so the assertion is on the DATA order rather than the output.
|
|
103
|
+
const state = stateWithMode('typescript', StreamLanguage.define(rust as never));
|
|
104
|
+
const tokens = state.languageDataAt('commentTokens', 0);
|
|
105
|
+
// Two providers, and the mode's is first — which is the ordering the
|
|
106
|
+
// commands rely on.
|
|
107
|
+
expect(tokens.length).toBe(2);
|
|
108
|
+
expect(tokens[0]).toEqual({ line: '//', block: { open: '/*', close: '*/' } });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('a shell mode comments with # rather than the path’s //', () => {
|
|
112
|
+
// `bash` maps to the shell mode, whose token is `#`. Before the mode is
|
|
113
|
+
// installed the manual table already says `#` for bash, so this pins that
|
|
114
|
+
// the two agree rather than which one won.
|
|
115
|
+
const state = stateWithMode('bash', StreamLanguage.define(shell as never));
|
|
116
|
+
expect(toggled(state)).toBe('# let a = 1;\n');
|
|
117
|
+
});
|
|
118
|
+
});
|