Line data Source code
1 : import { Prec } from '@codemirror/state';
2 : import { EditorView, keymap, KeyBinding } from '@codemirror/view';
3 :
4 : import { DashQLDiffAcceptEffect, DashQLDiffRejectEffect, DashQLProcessorPlugin } from './dashql_processor.js';
5 :
6 : /// Accept the pending diff: keep the current (new) text and clear the overlay.
7 1 : export function acceptPendingDiff(view: EditorView): boolean {
8 1 : const processor = view.state.field(DashQLProcessorPlugin);
9 1 : if (processor.scriptPendingDiff == null) {
10 1 : return false;
11 : }
12 1 : view.dispatch({ effects: DashQLDiffAcceptEffect.of(null) });
13 1 : return true;
14 1 : }
15 :
16 : /// Reject the pending diff: restore the prior text verbatim and clear the overlay.
17 1 : export function rejectPendingDiff(view: EditorView): boolean {
18 1 : const processor = view.state.field(DashQLProcessorPlugin);
19 1 : const pending = processor.scriptPendingDiff;
20 1 : if (pending == null) {
21 1 : return false;
22 : }
23 : // Restore the prior text in the same transaction that clears the overlay. The processor's
24 : // docChanged branch mirrors the restore to the WASM rope; `updateDiff` sees the reject effect
25 : // and clears the pending diff before the auto-accept check can fire.
26 1 : view.dispatch({
27 1 : changes: { from: 0, to: view.state.doc.length, insert: pending.priorText },
28 1 : effects: DashQLDiffRejectEffect.of(null),
29 1 : });
30 1 : return true;
31 1 : }
32 :
33 : /// Enter accepts, Escape rejects. Both no-op (return false, letting the key fall through) when no
34 : /// diff is pending, and defer to an active completion via lower precedence than the completion
35 : /// keymap (which is registered at Prec.highest).
36 : ///
37 : /// The Accept/Reject *buttons* live in the card body, not in a panel inside the editor. This keymap
38 : /// keeps the Enter/Escape shortcuts working while the diff editor holds focus.
39 1 : const KEYBINDINGS: KeyBinding[] = [
40 1 : { key: 'Enter', run: acceptPendingDiff },
41 1 : { key: 'Escape', run: rejectPendingDiff },
42 1 : ];
43 :
44 1 : export const DashQLDiffHintPlugin = [
45 1 : Prec.high(keymap.of(KEYBINDINGS)),
46 1 : ];
|