@monaco-neovim-wasm/lib 0.1.8 → 0.1.10

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.
@@ -1,137 +1,137 @@
1
- import * as m from "monaco-editor";
2
- class W {
1
+ import * as g from "monaco-editor";
2
+ class U {
3
3
  buffer;
4
4
  ctrl;
5
5
  data;
6
6
  capacity;
7
- constructor(t = I) {
7
+ constructor(t = R) {
8
8
  this.capacity = Math.max(8 * 1024, t | 0), this.buffer = new SharedArrayBuffer(8 + this.capacity), this.ctrl = new Int32Array(this.buffer, 0, 2), this.data = new Uint8Array(this.buffer, 8), Atomics.store(this.ctrl, 0, 0), Atomics.store(this.ctrl, 1, 0);
9
9
  }
10
10
  push(t) {
11
11
  let e;
12
12
  if (t instanceof Uint8Array ? e = t : t instanceof ArrayBuffer || t instanceof SharedArrayBuffer ? e = new Uint8Array(t) : e = new Uint8Array(t), !e.byteLength) return !0;
13
- const i = Atomics.load(this.ctrl, 0), s = Atomics.load(this.ctrl, 1), n = s >= i ? s - i : this.capacity - (i - s), o = this.capacity - n - 1;
14
- if (e.byteLength > o) return !1;
13
+ const i = Atomics.load(this.ctrl, 0), s = Atomics.load(this.ctrl, 1), n = s >= i ? s - i : this.capacity - (i - s), r = this.capacity - n - 1;
14
+ if (e.byteLength > r) return !1;
15
15
  const l = this.capacity - s;
16
16
  return e.byteLength <= l ? (this.data.set(e, s), Atomics.store(this.ctrl, 1, (s + e.byteLength) % this.capacity)) : (this.data.set(e.subarray(0, l), s), this.data.set(e.subarray(l), 0), Atomics.store(this.ctrl, 1, e.byteLength - l)), Atomics.notify(this.ctrl, 1), !0;
17
17
  }
18
18
  }
19
- const I = 262144;
20
- function z(r = I) {
21
- return new W(r);
19
+ const R = 262144;
20
+ function V(o = R) {
21
+ return new U(o);
22
22
  }
23
- function N(r) {
23
+ function A(o) {
24
24
  const t = [];
25
- return x(r, t), new Uint8Array(t);
25
+ return T(o, t), new Uint8Array(t);
26
26
  }
27
- function x(r, t) {
28
- if (r == null) {
27
+ function T(o, t) {
28
+ if (o == null) {
29
29
  t.push(192);
30
30
  return;
31
31
  }
32
- if (typeof r == "boolean") {
33
- t.push(r ? 195 : 194);
32
+ if (typeof o == "boolean") {
33
+ t.push(o ? 195 : 194);
34
34
  return;
35
35
  }
36
- if (typeof r == "number") {
37
- O(r, t);
36
+ if (typeof o == "number") {
37
+ Q(o, t);
38
38
  return;
39
39
  }
40
- if (typeof r == "string") {
41
- $(r, t);
40
+ if (typeof o == "string") {
41
+ q(o, t);
42
42
  return;
43
43
  }
44
- if (typeof r == "bigint") {
45
- H(r, t);
44
+ if (typeof o == "bigint") {
45
+ O(o, t);
46
46
  return;
47
47
  }
48
- if (Array.isArray(r)) {
49
- V(r, t);
48
+ if (Array.isArray(o)) {
49
+ G(o, t);
50
50
  return;
51
51
  }
52
- if (r instanceof Uint8Array) {
53
- U(r, t);
52
+ if (o instanceof Uint8Array) {
53
+ j(o, t);
54
54
  return;
55
55
  }
56
- if (j(r)) {
57
- q(r, t);
56
+ if (Z(o)) {
57
+ Y(o, t);
58
58
  return;
59
59
  }
60
- if (typeof r == "object") {
61
- Q(r, t);
60
+ if (typeof o == "object") {
61
+ J(o, t);
62
62
  return;
63
63
  }
64
64
  throw new Error("Unsupported type in msgpack encode");
65
65
  }
66
- function O(r, t) {
67
- if (!Number.isFinite(r)) throw new Error("Cannot encode non-finite number");
68
- if (Number.isInteger(r)) {
69
- if (r >= 0 && r <= 127) {
70
- t.push(r);
66
+ function Q(o, t) {
67
+ if (!Number.isFinite(o)) throw new Error("Cannot encode non-finite number");
68
+ if (Number.isInteger(o)) {
69
+ if (o >= 0 && o <= 127) {
70
+ t.push(o);
71
71
  return;
72
72
  }
73
- if (r < 0 && r >= -32) {
74
- t.push(224 | r + 32);
73
+ if (o < 0 && o >= -32) {
74
+ t.push(224 | o + 32);
75
75
  return;
76
76
  }
77
- if (r >= -128 && r <= 127) {
78
- t.push(208, r + 256 & 255);
77
+ if (o >= -128 && o <= 127) {
78
+ t.push(208, o + 256 & 255);
79
79
  return;
80
80
  }
81
- if (r >= -32768 && r <= 32767) {
82
- t.push(209, r >> 8 & 255, r & 255);
81
+ if (o >= -32768 && o <= 32767) {
82
+ t.push(209, o >> 8 & 255, o & 255);
83
83
  return;
84
84
  }
85
- if (r >= -2147483648 && r <= 2147483647) {
86
- t.push(210, r >> 24 & 255, r >> 16 & 255, r >> 8 & 255, r & 255);
85
+ if (o >= -2147483648 && o <= 2147483647) {
86
+ t.push(210, o >> 24 & 255, o >> 16 & 255, o >> 8 & 255, o & 255);
87
87
  return;
88
88
  }
89
- H(BigInt(r), t);
89
+ O(BigInt(o), t);
90
90
  return;
91
91
  }
92
92
  const e = new ArrayBuffer(8);
93
- new DataView(e).setFloat64(0, r), t.push(203, ...new Uint8Array(e));
93
+ new DataView(e).setFloat64(0, o), t.push(203, ...new Uint8Array(e));
94
94
  }
95
- function H(r, t) {
95
+ function O(o, t) {
96
96
  const e = new ArrayBuffer(8);
97
- new DataView(e).setBigInt64(0, BigInt(r)), t.push(211, ...new Uint8Array(e));
97
+ new DataView(e).setBigInt64(0, BigInt(o)), t.push(211, ...new Uint8Array(e));
98
98
  }
99
- function $(r, t) {
100
- const i = new TextEncoder().encode(r), s = i.length;
99
+ function q(o, t) {
100
+ const i = new TextEncoder().encode(o), s = i.length;
101
101
  s <= 31 ? t.push(160 | s) : s <= 255 ? t.push(217, s) : s <= 65535 ? t.push(218, s >> 8 & 255, s & 255) : t.push(219, s >> 24 & 255, s >> 16 & 255, s >> 8 & 255, s & 255), t.push(...i);
102
102
  }
103
- function U(r, t) {
104
- const e = r.length;
105
- e <= 255 ? t.push(196, e) : e <= 65535 ? t.push(197, e >> 8 & 255, e & 255) : t.push(198, e >> 24 & 255, e >> 16 & 255, e >> 8 & 255, e & 255), t.push(...r);
103
+ function j(o, t) {
104
+ const e = o.length;
105
+ e <= 255 ? t.push(196, e) : e <= 65535 ? t.push(197, e >> 8 & 255, e & 255) : t.push(198, e >> 24 & 255, e >> 16 & 255, e >> 8 & 255, e & 255), t.push(...o);
106
106
  }
107
- function V(r, t) {
108
- const e = r.length;
107
+ function G(o, t) {
108
+ const e = o.length;
109
109
  e <= 15 ? t.push(144 | e) : e <= 65535 ? t.push(220, e >> 8 & 255, e & 255) : t.push(221, e >> 24 & 255, e >> 16 & 255, e >> 8 & 255, e & 255);
110
- for (const i of r)
111
- x(i, t);
110
+ for (const i of o)
111
+ T(i, t);
112
112
  }
113
- function Q(r, t) {
114
- const e = Object.keys(r), i = e.length;
113
+ function J(o, t) {
114
+ const e = Object.keys(o), i = e.length;
115
115
  i <= 15 ? t.push(128 | i) : i <= 65535 ? t.push(222, i >> 8 & 255, i & 255) : t.push(223, i >> 24 & 255, i >> 16 & 255, i >> 8 & 255, i & 255);
116
116
  for (const s of e)
117
- x(s, t), x(r[s], t);
117
+ T(s, t), T(o[s], t);
118
118
  }
119
- function q(r, t) {
120
- const e = r.data.length;
121
- e === 1 ? t.push(212, r.type) : e === 2 ? t.push(213, r.type) : e === 4 ? t.push(214, r.type) : e === 8 ? t.push(215, r.type) : e === 16 ? t.push(216, r.type) : t.push(199, e, r.type);
119
+ function Y(o, t) {
120
+ const e = o.data.length;
121
+ e === 1 ? t.push(212, o.type) : e === 2 ? t.push(213, o.type) : e === 4 ? t.push(214, o.type) : e === 8 ? t.push(215, o.type) : e === 16 ? t.push(216, o.type) : t.push(199, e, o.type);
122
122
  for (let i = 0; i < e; i += 1)
123
- t.push(r.data[i]);
123
+ t.push(o.data[i]);
124
124
  }
125
- function j(r) {
126
- return !!(r && typeof r == "object" && typeof r.type == "number" && r.data instanceof Uint8Array);
125
+ function Z(o) {
126
+ return !!(o && typeof o == "object" && typeof o.type == "number" && o.data instanceof Uint8Array);
127
127
  }
128
- function G() {
128
+ function X() {
129
129
  return typeof SharedArrayBuffer < "u" && typeof crossOriginIsolated < "u" && crossOriginIsolated;
130
130
  }
131
- function Z(r) {
132
- return r.byteOffset === 0 && r.byteLength === r.buffer.byteLength ? r : r.slice();
131
+ function ee(o) {
132
+ return o.byteOffset === 0 && o.byteLength === o.buffer.byteLength ? o : o.slice();
133
133
  }
134
- class Y {
134
+ class te {
135
135
  init;
136
136
  worker = null;
137
137
  sharedInput = null;
@@ -149,7 +149,7 @@ class Y {
149
149
  worker: t.worker ?? null,
150
150
  workerUrl: t.workerUrl ?? null,
151
151
  createWorker: t.createWorker ?? null,
152
- sharedInputBytes: t.sharedInputBytes ?? I,
152
+ sharedInputBytes: t.sharedInputBytes ?? R,
153
153
  inputMode: t.inputMode ?? "shared",
154
154
  rpcTimeoutMs: t.rpcTimeoutMs ?? 8e3,
155
155
  maxQueuedBytes: t.maxQueuedBytes ?? 4 * 1024 * 1024,
@@ -167,26 +167,26 @@ class Y {
167
167
  return this.workerExitCode;
168
168
  }
169
169
  async start(t) {
170
- const { cols: e, rows: i, wasmPath: s, runtimePath: n, env: o, files: l } = t;
171
- if (this.inputMode = t.inputMode ?? this.init.inputMode, this.inputMode === "shared" && !G())
170
+ const { cols: e, rows: i, wasmPath: s, runtimePath: n, env: r, files: l } = t;
171
+ if (this.inputMode = t.inputMode ?? this.init.inputMode, this.inputMode === "shared" && !X())
172
172
  throw new Error('SharedArrayBuffer is required for inputMode="shared"; serve with COOP/COEP so crossOriginIsolated is true, or use inputMode="message" with the asyncify worker.');
173
- const a = this.worker;
174
- if (this.stop({ terminate: !this.init.reuseWorker, silent: !0 }), this.sharedInput = this.inputMode === "shared" ? z(this.init.sharedInputBytes) : null, this.workerExited = !1, this.workerExitCode = null, this.reqId = 1, this.pending.clear(), this.init.worker ? this.worker = this.init.worker : this.init.createWorker ? this.worker = this.init.createWorker() : this.init.reuseWorker && a ? this.worker = a : this.worker = this.init.workerUrl ? new Worker(this.init.workerUrl, { type: "module" }) : null, !this.worker) throw new Error("worker/workerUrl is required");
175
- this.worker.onmessage = (h) => this.handleWorkerMessage(h.data);
176
- const u = {
173
+ const c = this.worker;
174
+ if (this.stop({ terminate: !this.init.reuseWorker, silent: !0 }), this.sharedInput = this.inputMode === "shared" ? V(this.init.sharedInputBytes) : null, this.workerExited = !1, this.workerExitCode = null, this.reqId = 1, this.pending.clear(), this.init.worker ? this.worker = this.init.worker : this.init.createWorker ? this.worker = this.init.createWorker() : this.init.reuseWorker && c ? this.worker = c : this.worker = this.init.workerUrl ? new Worker(this.init.workerUrl, { type: "module" }) : null, !this.worker) throw new Error("worker/workerUrl is required");
175
+ this.worker.onmessage = (f) => this.handleWorkerMessage(f.data);
176
+ const h = {
177
177
  type: "start",
178
178
  cols: Number(e) || 80,
179
179
  rows: Number(i) || 24,
180
180
  wasmPath: String(s ?? ""),
181
181
  runtimePath: String(n ?? ""),
182
182
  inputBuffer: this.sharedInput?.buffer ?? null,
183
- env: o ?? void 0,
183
+ env: r ?? void 0,
184
184
  files: l ?? void 0
185
185
  };
186
186
  try {
187
- this.worker.postMessage(u, []);
187
+ this.worker.postMessage(h, []);
188
188
  } catch {
189
- this.worker.postMessage(u);
189
+ this.worker.postMessage(h);
190
190
  }
191
191
  }
192
192
  stop(t = {}) {
@@ -211,7 +211,7 @@ class Y {
211
211
  }
212
212
  notify(t, e = []) {
213
213
  if (!this.worker || this.workerExited) return;
214
- const i = N([2, t, e]);
214
+ const i = A([2, t, e]);
215
215
  this.postInput(i);
216
216
  }
217
217
  call(t, e = []) {
@@ -227,8 +227,8 @@ class Y {
227
227
  }
228
228
  const n = this.reqId++;
229
229
  this.pending.set(n, { resolve: i, reject: s, ts: Date.now(), method: t });
230
- const o = N([0, n, t, e]);
231
- this.postInput(o), setTimeout(() => {
230
+ const r = A([0, n, t, e]);
231
+ this.postInput(r), setTimeout(() => {
232
232
  this.pending.has(n) && (this.pending.delete(n), s(new Error(this.workerExited ? this.workerExitCode != null ? `nvim exited (${this.workerExitCode})` : "nvim exited" : `rpc timeout: ${t}`)));
233
233
  }, this.init.rpcTimeoutMs);
234
234
  });
@@ -240,21 +240,21 @@ class Y {
240
240
  await this.call("nvim_get_api_info", []);
241
241
  return;
242
242
  } catch {
243
- await new Promise((o) => setTimeout(o, 300));
243
+ await new Promise((r) => setTimeout(r, 300));
244
244
  }
245
245
  throw new Error("nvim_get_api_info timed out");
246
246
  }
247
247
  respond(t, e, i) {
248
248
  if (!this.worker || this.workerExited) return;
249
- const s = N([1, t, e, i]);
249
+ const s = A([1, t, e, i]);
250
250
  this.postInput(s);
251
251
  }
252
252
  handleWorkerMessage(t) {
253
253
  const e = t?.type;
254
254
  if (e === "rpc-response") {
255
- const { msgid: i, error: s, result: n } = t, o = this.pending.get(i);
256
- if (!o) return;
257
- this.pending.delete(i), s ? o.reject(new Error(String(s))) : o.resolve(n);
255
+ const { msgid: i, error: s, result: n } = t, r = this.pending.get(i);
256
+ if (!r) return;
257
+ this.pending.delete(i), s ? r.reject(new Error(String(s))) : r.resolve(n);
258
258
  return;
259
259
  }
260
260
  if (e === "rpc-notify") {
@@ -308,8 +308,8 @@ class Y {
308
308
  if (e === "exit") {
309
309
  const i = t, s = i.code, n = i.lastStderr;
310
310
  if (this.workerExited = !0, this.workerExitCode = s, this.pending.size) {
311
- const o = n ? `: ${n.trim()}` : "", l = new Error(`nvim exited (${s})${o}`);
312
- this.pending.forEach((a) => a.reject(l)), this.pending.clear();
311
+ const r = n ? `: ${n.trim()}` : "", l = new Error(`nvim exited (${s})${r}`);
312
+ this.pending.forEach((c) => c.reject(l)), this.pending.clear();
313
313
  }
314
314
  try {
315
315
  this.init.handlers.onExit?.(s, n);
@@ -319,7 +319,7 @@ class Y {
319
319
  }
320
320
  postInput(t) {
321
321
  if (!t || !t.buffer) return;
322
- const e = Z(t);
322
+ const e = ee(t);
323
323
  if (!(!this.worker || this.workerExited)) {
324
324
  if (this.inputMode === "shared") {
325
325
  if (!this.sharedInput || this.inputQueueHead >= this.inputQueue.length && this.sharedInput.push(e))
@@ -359,8 +359,8 @@ class Y {
359
359
  }
360
360
  } else {
361
361
  let e = 0, i = 0;
362
- for (let o = this.inputQueueHead; o < this.inputQueue.length; o += 1) {
363
- const l = this.inputQueue[o];
362
+ for (let r = this.inputQueueHead; r < this.inputQueue.length; r += 1) {
363
+ const l = this.inputQueue[r];
364
364
  if (l?.byteLength) {
365
365
  if (i > 0 && e + l.byteLength > 262144) break;
366
366
  e += l.byteLength, i += 1;
@@ -369,8 +369,8 @@ class Y {
369
369
  if (i <= 0 || e <= 0) return;
370
370
  const s = new Uint8Array(e);
371
371
  let n = 0;
372
- for (let o = 0; o < i; o += 1) {
373
- const l = this.inputQueue[this.inputQueueHead + o];
372
+ for (let r = 0; r < i; r += 1) {
373
+ const l = this.inputQueue[this.inputQueueHead + r];
374
374
  s.set(l, n), n += l.byteLength;
375
375
  }
376
376
  this.inputQueueHead += i, this.inputQueuedBytes -= e, this.postStdin(s);
@@ -389,7 +389,7 @@ class Y {
389
389
  }
390
390
  }
391
391
  }
392
- const J = `if !exists('g:monaco_neovim_wasm_chan')
392
+ const ie = `if !exists('g:monaco_neovim_wasm_chan')
393
393
  finish
394
394
  endif
395
395
 
@@ -452,7 +452,7 @@ onoremap <silent> gk <Cmd>call <SID>up_down('up')<CR>
452
452
  nnoremap <silent> gj <Cmd>call <SID>up_down('down')<CR>
453
453
  xnoremap <silent> gj <Cmd>call <SID>up_down('down')<CR>
454
454
  onoremap <silent> gj <Cmd>call <SID>up_down('down')<CR>
455
- `, X = `if !exists('g:monaco_neovim_wasm_chan')
455
+ `, ne = `if !exists('g:monaco_neovim_wasm_chan')
456
456
  finish
457
457
  endif
458
458
 
@@ -553,7 +553,7 @@ onoremap <silent> <C-f> <Cmd>call <SID>scroll('down', 'page')<CR>
553
553
  nnoremap <silent> <C-b> <Cmd>call <SID>scroll('up', 'page')<CR>
554
554
  xnoremap <silent> <C-b> <Cmd>call <SID>scroll('up', 'page')<CR>
555
555
  onoremap <silent> <C-b> <Cmd>call <SID>scroll('up', 'page')<CR>
556
- `, ee = `if !exists('g:monaco_neovim_wasm_chan')
556
+ `, se = `if !exists('g:monaco_neovim_wasm_chan')
557
557
  finish
558
558
  endif
559
559
 
@@ -604,7 +604,7 @@ cnoreabbrev <expr> q <SID>abbr('q', 'MonacoQuit')
604
604
  cnoreabbrev <expr> quit <SID>abbr('quit', 'MonacoQuit')
605
605
  cnoreabbrev <expr> wq <SID>abbr('wq', 'MonacoWq')
606
606
  cnoreabbrev <expr> x <SID>abbr('x', 'MonacoWq')
607
- `, te = [
607
+ `, re = [
608
608
  "-- Monaco + Neovim (WASM)",
609
609
  "-- Click into the editor, press i, and start typing.",
610
610
  "",
@@ -613,7 +613,7 @@ cnoreabbrev <expr> x <SID>abbr('x', 'MonacoWq')
613
613
  "end",
614
614
  "",
615
615
  "print(greet('monaco'))"
616
- ], ie = `
616
+ ], oe = `
617
617
  local api, fn = vim.api, vim.fn
618
618
 
619
619
  local function virtcol2byte(winid, lnum, virtcol)
@@ -697,7 +697,7 @@ local function get_selections(win)
697
697
  end
698
698
 
699
699
  return get_selections(...)
700
- `, ne = `
700
+ `, le = `
701
701
  local api, fn = vim.api, vim.fn
702
702
 
703
703
  local function get_visible_range(s, e)
@@ -786,7 +786,7 @@ end
786
786
 
787
787
  return run(...)
788
788
  `;
789
- class se {
789
+ class ae {
790
790
  editor;
791
791
  opts;
792
792
  session = null;
@@ -826,6 +826,8 @@ class se {
826
826
  searchRefreshPending = !1;
827
827
  execLuaAvailable = null;
828
828
  visualSelectionRefreshTimer = null;
829
+ selectionSyncTimer = null;
830
+ pendingSelection = null;
829
831
  lastCursorStyle = null;
830
832
  lastCursorBlink = null;
831
833
  lastCursorWidth = null;
@@ -834,6 +836,10 @@ class se {
834
836
  nextSeedLines = null;
835
837
  cmdlineEl = null;
836
838
  cmdlineVisible = !1;
839
+ cmdlineTextRaw = null;
840
+ cmdlineCursorByte = null;
841
+ cmdlineCursorOffsetBytes = 0;
842
+ cmdlineCursorContentBytes = 0;
837
843
  messageEl = null;
838
844
  messageTimer = null;
839
845
  popupEl = null;
@@ -844,7 +850,18 @@ class se {
844
850
  compositionActive = !1;
845
851
  pendingResyncAfterComposition = !1;
846
852
  ignoreNextInputEvent = !1;
853
+ ignoreNvimBufLinesUntil = 0;
854
+ exitingInsertMode = !1;
855
+ pendingKeysAfterExit = "";
856
+ exitInsertTimer = null;
857
+ dotRepeatKeys = "";
858
+ dotRepeatBackspaces = 0;
847
859
  ignoreTextKeydownUntil = 0;
860
+ lastImeCommitAt = 0;
861
+ lastImeCommitText = "";
862
+ recordingRegister = "";
863
+ recordingRefreshArmed = !1;
864
+ recordingRefreshTimer = null;
848
865
  optimisticCursorUntil = 0;
849
866
  optimisticCursorPos = null;
850
867
  optimisticCursorPrevPos = null;
@@ -856,6 +873,33 @@ class se {
856
873
  modelContentDisposable = null;
857
874
  originalOptions = null;
858
875
  resyncTimer = null;
876
+ debugLog(t) {
877
+ if (this.opts.debug)
878
+ try {
879
+ const e = `[monaco-neovim] ${t}`;
880
+ this.opts.debugLog ? this.opts.debugLog(e) : typeof console < "u" && console.debug && console.debug(e);
881
+ } catch {
882
+ }
883
+ }
884
+ scheduleRecordingRefresh() {
885
+ this.recordingRefreshTimer || (this.recordingRefreshTimer = window.setTimeout(() => {
886
+ this.recordingRefreshTimer = null, this.refreshRecordingState();
887
+ }, 0));
888
+ }
889
+ async refreshRecordingState() {
890
+ if (!(!this.session || !this.session.isRunning()))
891
+ try {
892
+ const t = await this.rpcCall("nvim_call_function", ["reg_recording", []]);
893
+ this.recordingRegister = typeof t == "string" ? t : String(t ?? "");
894
+ } catch {
895
+ }
896
+ }
897
+ sendCmdlineImeText(t) {
898
+ const e = String(t ?? "");
899
+ if (!e) return;
900
+ const i = typeof performance < "u" && performance.now ? performance.now() : Date.now();
901
+ e.length > 1 && e === this.lastImeCommitText && i - this.lastImeCommitAt < 60 || (this.lastImeCommitText = e, this.lastImeCommitAt = i, this.sendInput(L(e, !0)), this.ignoreTextKeydownUntil = i + 60);
902
+ }
859
903
  constructor(t, e = {}) {
860
904
  this.editor = t, this.opts = {
861
905
  worker: e.worker ?? null,
@@ -866,7 +910,7 @@ class se {
866
910
  inputMode: e.inputMode ?? "shared",
867
911
  env: e.env,
868
912
  files: e.files,
869
- sharedInputBytes: e.sharedInputBytes ?? I,
913
+ sharedInputBytes: e.sharedInputBytes ?? R,
870
914
  cols: e.cols ?? 120,
871
915
  rows: e.rows ?? 40,
872
916
  minCols: e.minCols ?? 20,
@@ -892,7 +936,7 @@ class se {
892
936
  onHostCommand: e.onHostCommand,
893
937
  status: e.status ?? (() => {
894
938
  }),
895
- seedLines: e.seedLines ?? te,
939
+ seedLines: e.seedLines ?? re,
896
940
  seedName: e.seedName ?? "monaco-demo.lua",
897
941
  seedFiletype: e.seedFiletype ?? "lua",
898
942
  uiAttach: e.uiAttach ?? !0,
@@ -921,8 +965,11 @@ class se {
921
965
  onMessage: e.onMessage,
922
966
  onPopupmenu: e.onPopupmenu,
923
967
  cmdlineContainer: e.cmdlineContainer,
968
+ insertSyncDebounceMs: Number.isFinite(e.insertSyncDebounceMs) ? Math.max(0, Number(e.insertSyncDebounceMs)) : 20,
969
+ debug: e.debug ?? !1,
970
+ debugLog: e.debugLog,
924
971
  shouldHandleKey: e.shouldHandleKey ?? (() => !0),
925
- translateKey: e.translateKey ?? oe
972
+ translateKey: e.translateKey ?? ce
926
973
  }, this.ctrlKeysNormal = this.opts.ctrlKeysForNormalMode ? new Set(this.opts.ctrlKeysForNormalMode.map((i) => String(i).toLowerCase())) : null, this.ctrlKeysInsert = this.opts.ctrlKeysForInsertMode ? new Set(this.opts.ctrlKeysForInsertMode.map((i) => String(i).toLowerCase())) : null, this.altKeysNormal = this.opts.altKeysForNormalMode ? new Set(this.opts.altKeysForNormalMode.map((i) => String(i).toLowerCase())) : null, this.altKeysInsert = this.opts.altKeysForInsertMode ? new Set(this.opts.altKeysForInsertMode.map((i) => String(i).toLowerCase())) : null, this.metaKeysNormal = this.opts.metaKeysForNormalMode ? new Set(this.opts.metaKeysForNormalMode.map((i) => String(i).toLowerCase())) : null, this.metaKeysInsert = this.opts.metaKeysForInsertMode ? new Set(this.opts.metaKeysForInsertMode.map((i) => String(i).toLowerCase())) : null;
927
974
  }
928
975
  async start(t) {
@@ -935,8 +982,8 @@ class se {
935
982
  this.notifyChain = this.notifyChain.then(() => this.handleNotify(s, n)).catch(() => {
936
983
  });
937
984
  },
938
- onRequest: (s, n, o) => {
939
- this.handleRequest(s, n, o);
985
+ onRequest: (s, n, r) => {
986
+ this.handleRequest(s, n, r);
940
987
  },
941
988
  onClipboardCopy: (s, n) => {
942
989
  this.handleClipboardCopy(s);
@@ -959,8 +1006,8 @@ class se {
959
1006
  this.opts.status(`start failed: ${s ?? "unknown"}`, !0);
960
1007
  },
961
1008
  onExit: (s, n) => {
962
- const o = n ? `: ${n.trim()}` : "";
963
- this.opts.status(`nvim exited (${s})${o}`, s !== 0);
1009
+ const r = n ? `: ${n.trim()}` : "";
1010
+ this.opts.status(`nvim exited (${s})${r}`, s !== 0);
964
1011
  try {
965
1012
  this.opts.onExit?.(s, n);
966
1013
  } catch {
@@ -974,7 +1021,7 @@ class se {
974
1021
  this.opts.status(s, !0);
975
1022
  }
976
1023
  };
977
- if (!this.session || !this.opts.reuseWorker ? this.session = new Y({
1024
+ if (!this.session || !this.opts.reuseWorker ? this.session = new te({
978
1025
  worker: this.opts.worker,
979
1026
  workerUrl: this.opts.workerUrl,
980
1027
  inputMode: this.opts.inputMode,
@@ -991,12 +1038,12 @@ class se {
991
1038
  runtimePath: this.opts.runtimePath,
992
1039
  inputMode: this.opts.inputMode,
993
1040
  env: this.opts.env,
994
- files: ce(ue(
1041
+ files: fe(me(
995
1042
  this.opts.files,
996
1043
  this.opts.wrappedLineMotions || this.opts.scrollMotions || this.opts.hostCommands ? [
997
- { path: "home/.config/nvim/monaco-neovim-wasm/motion.vim", data: J },
998
- { path: "home/.config/nvim/monaco-neovim-wasm/scrolling.vim", data: X },
999
- { path: "home/.config/nvim/monaco-neovim-wasm/host-commands.vim", data: ee }
1044
+ { path: "home/.config/nvim/monaco-neovim-wasm/motion.vim", data: ie },
1045
+ { path: "home/.config/nvim/monaco-neovim-wasm/scrolling.vim", data: ne },
1046
+ { path: "home/.config/nvim/monaco-neovim-wasm/host-commands.vim", data: se }
1000
1047
  ] : []
1001
1048
  ))
1002
1049
  }), this.opts.status("starting..."), this.primeSent = !1, setTimeout(() => {
@@ -1008,7 +1055,7 @@ class se {
1008
1055
  }
1009
1056
  }
1010
1057
  stop(t = !1) {
1011
- this.session && (this.opts.reuseWorker ? this.session.stop({ terminate: !1, silent: !0 }) : (this.session.dispose(), this.session = null)), this.bufHandle = null, this.primeSent = !1, this.visualSelectionActive = !1, this.delegateInsertToMonaco = !1, this.applyingFromNvim = !1, this.clearBufferStates(), this.cursorSyncTimer && (clearTimeout(this.cursorSyncTimer), this.cursorSyncTimer = null), this.nvimChannelId = null, this.hostAutocmdInstalled = !1, this.notifyChain = Promise.resolve(), this.visualSelectionRefreshTimer && (clearTimeout(this.visualSelectionRefreshTimer), this.visualSelectionRefreshTimer = null), this.cursorRefreshTimer && (clearTimeout(this.cursorRefreshTimer), this.cursorRefreshTimer = null), this.cursorRefreshPending = !1, this.cursorRefreshInFlight = !1, this.setCmdline(null), this.setMessage(null), this.setPopupmenu(null, -1), this.resyncTimer && (clearTimeout(this.resyncTimer), this.resyncTimer = null), this.resizeTimer && (clearTimeout(this.resizeTimer), this.resizeTimer = null), t || this.opts.status("stopped", !0), this.disposeEditorListeners();
1058
+ this.session && (this.opts.reuseWorker ? this.session.stop({ terminate: !1, silent: !0 }) : (this.session.dispose(), this.session = null)), this.bufHandle = null, this.primeSent = !1, this.visualSelectionActive = !1, this.delegateInsertToMonaco = !1, this.recordingRegister = "", this.recordingRefreshArmed = !1, this.recordingRefreshTimer && (clearTimeout(this.recordingRefreshTimer), this.recordingRefreshTimer = null), this.exitingInsertMode = !1, this.pendingKeysAfterExit = "", this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.applyingFromNvim = !1, this.clearBufferStates(), this.cursorSyncTimer && (clearTimeout(this.cursorSyncTimer), this.cursorSyncTimer = null), this.nvimChannelId = null, this.hostAutocmdInstalled = !1, this.notifyChain = Promise.resolve(), this.visualSelectionRefreshTimer && (clearTimeout(this.visualSelectionRefreshTimer), this.visualSelectionRefreshTimer = null), this.cursorRefreshTimer && (clearTimeout(this.cursorRefreshTimer), this.cursorRefreshTimer = null), this.selectionSyncTimer && (clearTimeout(this.selectionSyncTimer), this.selectionSyncTimer = null), this.pendingSelection = null, this.cursorRefreshPending = !1, this.cursorRefreshInFlight = !1, this.setCmdline(null), this.setMessage(null), this.setPopupmenu(null, -1), this.resyncTimer && (clearTimeout(this.resyncTimer), this.resyncTimer = null), this.resizeTimer && (clearTimeout(this.resizeTimer), this.resizeTimer = null), t || this.opts.status("stopped", !0), this.disposeEditorListeners();
1012
1059
  }
1013
1060
  dispose() {
1014
1061
  this.session && (this.session.dispose(), this.session = null), this.stop(!0);
@@ -1025,6 +1072,10 @@ class se {
1025
1072
  input(t) {
1026
1073
  this.sendNotify("nvim_input", [String(t ?? "")]);
1027
1074
  }
1075
+ type(t, e = !0) {
1076
+ const i = L(String(t ?? ""), e);
1077
+ i && this.sendNotify("nvim_input", [i]);
1078
+ }
1028
1079
  paste(t) {
1029
1080
  this.pasteText(t);
1030
1081
  }
@@ -1032,14 +1083,14 @@ class se {
1032
1083
  const i = String(t ?? ""), s = Array.isArray(e) ? e : [];
1033
1084
  if (this.execLuaAvailable !== !1)
1034
1085
  try {
1035
- const o = await this.rpcCall("nvim_exec_lua", [i, s]);
1036
- return this.execLuaAvailable = !0, o;
1037
- } catch (o) {
1038
- const l = o?.message || String(o);
1086
+ const r = await this.rpcCall("nvim_exec_lua", [i, s]);
1087
+ return this.execLuaAvailable = !0, r;
1088
+ } catch (r) {
1089
+ const l = r?.message || String(r);
1039
1090
  if (l.includes("Invalid method") && l.includes("nvim_exec_lua"))
1040
1091
  this.execLuaAvailable = !1;
1041
1092
  else
1042
- throw o;
1093
+ throw r;
1043
1094
  }
1044
1095
  const n = `(function(...)
1045
1096
  ${i}
@@ -1060,7 +1111,7 @@ end)(unpack(_A))`;
1060
1111
  }
1061
1112
  attachEditorListeners() {
1062
1113
  this.disposeEditorListeners();
1063
- const t = m.editor.EditorOption;
1114
+ const t = g.editor.EditorOption;
1064
1115
  try {
1065
1116
  const e = this.editor.getOption(t.fontInfo);
1066
1117
  this.initialCursorWidth = this.editor.getOption(t.cursorWidth) || 0, this.typicalFullWidth = e?.typicalFullwidthCharacterWidth || 2;
@@ -1096,6 +1147,7 @@ end)(unpack(_A))`;
1096
1147
  this.editor.onDidChangeModel(() => this.attachActiveModelListener()),
1097
1148
  this.editor.onKeyDown((e) => this.handleKey(e)),
1098
1149
  this.editor.onMouseDown((e) => this.handleMouse(e)),
1150
+ this.editor.onDidChangeCursorSelection((e) => this.handleSelection(e)),
1099
1151
  this.editor.onDidChangeCursorPosition((e) => {
1100
1152
  const i = this.editor.getPosition();
1101
1153
  if (i && (this.lastCursorPos = i), this.delegateInsertToMonaco) {
@@ -1124,8 +1176,8 @@ end)(unpack(_A))`;
1124
1176
  }
1125
1177
  computeGridSize() {
1126
1178
  try {
1127
- const t = this.editor.getLayoutInfo(), e = Math.max(0, Number(t?.contentWidth ?? t?.width ?? 0) || 0), i = Math.max(0, Number(t?.contentHeight ?? t?.height ?? 0) || 0), s = this.editor.getOption(m.editor.EditorOption.fontInfo), n = Math.max(1, Number(s?.typicalHalfwidthCharacterWidth ?? s?.maxDigitWidth ?? 0) || 0), o = Math.max(1, Number(s?.lineHeight ?? 0) || 0), l = Math.max(this.opts.minCols, Math.floor(e / n)), a = Math.max(this.opts.minRows, Math.floor(i / o));
1128
- if (Number.isFinite(l) && Number.isFinite(a) && l > 0 && a > 0) return { cols: l, rows: a };
1179
+ const t = this.editor.getLayoutInfo(), e = Math.max(0, Number(t?.contentWidth ?? t?.width ?? 0) || 0), i = Math.max(0, Number(t?.contentHeight ?? t?.height ?? 0) || 0), s = this.editor.getOption(g.editor.EditorOption.fontInfo), n = Math.max(1, Number(s?.typicalHalfwidthCharacterWidth ?? s?.maxDigitWidth ?? 0) || 0), r = Math.max(1, Number(s?.lineHeight ?? 0) || 0), l = Math.max(this.opts.minCols, Math.floor(e / n)), c = Math.max(this.opts.minRows, Math.floor(i / r));
1180
+ if (Number.isFinite(l) && Number.isFinite(c) && l > 0 && c > 0) return { cols: l, rows: c };
1129
1181
  } catch {
1130
1182
  }
1131
1183
  return { cols: this.opts.cols, rows: this.opts.rows };
@@ -1148,7 +1200,7 @@ end)(unpack(_A))`;
1148
1200
  const e = document.createElement("div");
1149
1201
  e.style.position = "absolute", e.style.display = "none", e.style.pointerEvents = "none", e.style.zIndex = "40", e.style.whiteSpace = "pre", e.style.padding = "0 1px", e.style.background = "rgba(0, 0, 0, 0.15)", e.style.borderBottom = "1px solid rgba(255, 255, 255, 0.6)", e.style.borderRadius = "2px";
1150
1202
  try {
1151
- const i = this.editor.getOption(m.editor.EditorOption.fontInfo);
1203
+ const i = this.editor.getOption(g.editor.EditorOption.fontInfo);
1152
1204
  i?.fontFamily && (e.style.fontFamily = i.fontFamily), i?.fontSize && (e.style.fontSize = `${i.fontSize}px`), i?.lineHeight && (e.style.lineHeight = `${i.lineHeight}px`);
1153
1205
  } catch {
1154
1206
  }
@@ -1167,11 +1219,11 @@ end)(unpack(_A))`;
1167
1219
  if (!t || this.cmdlineVisible) return;
1168
1220
  const e = this.editor.getModel();
1169
1221
  if (!e) return;
1170
- const i = this.editor.getSelection(), n = (i && !i.isEmpty() ? i.getStartPosition() : null) ?? this.editor.getPosition() ?? this.lastCursorPos ?? new m.Position(1, 1), o = String(t).split(/\r?\n/);
1171
- let l = n.lineNumber, a = n.column;
1172
- o.length <= 1 ? a += Array.from(o[0] ?? "").length : (l += o.length - 1, a = 1 + Array.from(o[o.length - 1] ?? "").length);
1173
- const u = e.validatePosition(new m.Position(l, a)), h = this.editor.getPosition() ?? this.lastCursorPos ?? n;
1174
- this.optimisticCursorPrevPos = h, this.optimisticCursorPos = u, this.optimisticCursorUntil = (typeof performance < "u" && performance.now ? performance.now() : Date.now()) + 120, this.lastCursorPos = u, !this.compositionActive && (this.suppressCursorSync = !0, this.editor.setPosition(u), this.suppressCursorSync = !1);
1222
+ const i = this.editor.getSelection(), n = (i && !i.isEmpty() ? i.getStartPosition() : null) ?? this.editor.getPosition() ?? this.lastCursorPos ?? new g.Position(1, 1), r = String(t).split(/\r?\n/);
1223
+ let l = n.lineNumber, c = n.column;
1224
+ r.length <= 1 ? c += Array.from(r[0] ?? "").length : (l += r.length - 1, c = 1 + Array.from(r[r.length - 1] ?? "").length);
1225
+ const h = e.validatePosition(new g.Position(l, c)), f = this.editor.getPosition() ?? this.lastCursorPos ?? n;
1226
+ this.optimisticCursorPrevPos = f, this.optimisticCursorPos = h, this.optimisticCursorUntil = (typeof performance < "u" && performance.now ? performance.now() : Date.now()) + 120, this.lastCursorPos = h, !this.compositionActive && (this.suppressCursorSync = !0, this.editor.setPosition(h), this.suppressCursorSync = !1);
1175
1227
  }
1176
1228
  positionPreedit() {
1177
1229
  if (!this.preeditEl || !this.preeditVisible) return;
@@ -1290,14 +1342,14 @@ end)(unpack(_A))`;
1290
1342
  await this.rpcCall("nvim_ui_attach", [this.uiCols || this.opts.cols, this.uiRows || this.opts.rows, this.opts.uiAttachOptions]);
1291
1343
  } catch {
1292
1344
  }
1293
- for (const o of this.opts.startupCommands)
1294
- o && this.sendNotify("nvim_command", [o]);
1345
+ for (const r of this.opts.startupCommands)
1346
+ r && this.sendNotify("nvim_command", [r]);
1295
1347
  if (this.opts.startupLua)
1296
1348
  try {
1297
1349
  await this.execLua(this.opts.startupLua, []);
1298
1350
  } catch {
1299
1351
  }
1300
- const t = await this.rpcCall("nvim_get_current_buf", []), e = _(t) ?? 1;
1352
+ const t = await this.rpcCall("nvim_get_current_buf", []), e = x(t) ?? 1;
1301
1353
  if (this.bufHandle = e, await this.rpcCall("nvim_buf_attach", [e, !0, {}]) !== !0) throw new Error("nvim_buf_attach failed");
1302
1354
  this.ensureActiveState(), this.opts.syncTabstop && this.syncTabstopFromMonaco();
1303
1355
  const s = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
@@ -1305,13 +1357,13 @@ end)(unpack(_A))`;
1305
1357
  const n = await this.seedBuffer(e, this.nextSeedLines);
1306
1358
  this.nextSeedLines = null, n && n.length && this.applyBuffer(n);
1307
1359
  try {
1308
- const o = this.ensureActiveState();
1309
- if (o) {
1360
+ const r = this.ensureActiveState();
1361
+ if (r) {
1310
1362
  const l = await this.rpcCall("nvim_buf_get_name", [e]);
1311
- o.name = typeof l == "string" ? l : "", o.name && this.buffersByName.set(o.name, e);
1363
+ r.name = typeof l == "string" ? l : "", r.name && this.buffersByName.set(r.name, e);
1312
1364
  try {
1313
- const a = await this.rpcCall("nvim_buf_get_option", [e, "filetype"]);
1314
- o.filetype = typeof a == "string" ? a : "";
1365
+ const c = await this.rpcCall("nvim_buf_get_option", [e, "filetype"]);
1366
+ r.filetype = typeof c == "string" ? c : "";
1315
1367
  } catch {
1316
1368
  }
1317
1369
  }
@@ -1381,13 +1433,13 @@ end)(unpack(_A))`;
1381
1433
  return;
1382
1434
  }
1383
1435
  if (t === "monaco_cursor") {
1384
- const [i, s] = e, n = L(this.editor, Number(i), Number(s));
1436
+ const [i, s] = e, n = B(this.editor, Number(i), Number(s));
1385
1437
  if (this.delegateInsertToMonaco) {
1386
- const o = this.editor.getModel(), l = o ? o.validatePosition(new m.Position(n.line, n.col)) : new m.Position(n.line, n.col);
1438
+ const r = this.editor.getModel(), l = r ? r.validatePosition(new g.Position(n.line, n.col)) : new g.Position(n.line, n.col);
1387
1439
  this.lastCursorPos = l;
1388
1440
  return;
1389
1441
  }
1390
- this.updateCursor(n.line, n.col), E(this.lastMode) && this.scheduleVisualSelectionRefresh();
1442
+ this.updateCursor(n.line, n.col), v(this.lastMode) && this.scheduleVisualSelectionRefresh();
1391
1443
  return;
1392
1444
  }
1393
1445
  if (t === "monaco_mode") {
@@ -1395,30 +1447,52 @@ end)(unpack(_A))`;
1395
1447
  this.applyNvimMode(i);
1396
1448
  return;
1397
1449
  }
1450
+ if (t === "monaco_recording") {
1451
+ const i = typeof e?.[0] == "string" ? String(e[0]) : "";
1452
+ this.recordingRegister = i;
1453
+ return;
1454
+ }
1398
1455
  if (t === "nvim_buf_lines_event") {
1399
- const [i, s, n, o, l] = e, a = _(i);
1400
- if (!a || this.delegateInsertToMonaco && this.bufHandle != null && a === this.bufHandle) return;
1401
- const u = this.bufHandle != null && a === this.bufHandle ? this.ensureActiveState() : this.buffers.get(a) ?? null;
1402
- if (!u) return;
1403
- if (this.bufHandle != null && a === this.bufHandle && this.compositionActive) {
1456
+ const [i, s, n, r, l] = e, c = x(i);
1457
+ if (!c) return;
1458
+ const h = this.bufHandle != null && c === this.bufHandle ? this.ensureActiveState() : this.buffers.get(c) ?? null;
1459
+ if (!h) return;
1460
+ if (this.bufHandle != null && c === this.bufHandle && this.compositionActive) {
1404
1461
  this.pendingResyncAfterComposition = !0;
1405
1462
  return;
1406
1463
  }
1407
- const h = u.model, f = Number(n), c = Number(o), d = Array.isArray(l) ? l.map((g) => String(g ?? "")) : null;
1408
- if (h && Number.isInteger(f) && Number.isInteger(c) && f >= 0 && c >= f && d)
1464
+ const f = h.model, a = Number(n), u = Number(r), d = Array.isArray(l) ? l.map((p) => String(p ?? "")) : null;
1465
+ if (f && Number.isInteger(a) && Number.isInteger(u) && a >= 0 && u >= a && d)
1409
1466
  try {
1410
- this.bufHandle != null && a === this.bufHandle && this.editor.getModel() === h ? this.applyLinePatch(h, f, c, d) : this.applyLinePatchToModel(h, f, c, d);
1467
+ const p = this.bufHandle != null && c === this.bufHandle && this.editor.getModel() === f;
1468
+ if (p && this.delegateInsertToMonaco) {
1469
+ const y = this.computeLinePatch(f, a, u, d);
1470
+ let b = !1;
1471
+ try {
1472
+ b = f.getValueInRange(y.range) === y.text;
1473
+ } catch {
1474
+ }
1475
+ if (!b) {
1476
+ const S = typeof performance < "u" && performance.now ? performance.now() : Date.now();
1477
+ if (h.pendingFullSync || h.pendingBufEdits.length || S < this.ignoreNvimBufLinesUntil) return;
1478
+ this.applyLinePatchToModel(f, a, u, d);
1479
+ try {
1480
+ h.shadowLines = f.getLinesContent();
1481
+ } catch {
1482
+ }
1483
+ }
1484
+ } else p ? this.applyLinePatch(f, a, u, d) : this.applyLinePatchToModel(f, a, u, d);
1411
1485
  } catch {
1412
1486
  }
1413
1487
  else
1414
1488
  try {
1415
- const g = await this.rpcCall("nvim_buf_get_lines", [a, 0, -1, !1]), y = Array.isArray(g) ? g : [""];
1416
- this.bufHandle != null && a === this.bufHandle && this.editor.getModel() === h ? this.applyBuffer(y) : this.setModelText(h, y);
1489
+ const p = await this.rpcCall("nvim_buf_get_lines", [c, 0, -1, !1]), y = Array.isArray(p) ? p : [""];
1490
+ this.bufHandle != null && c === this.bufHandle && this.editor.getModel() === f ? this.applyBuffer(y) : this.setModelText(f, y);
1417
1491
  } catch {
1418
1492
  }
1419
- this.bufHandle != null && a === this.bufHandle && E(this.lastMode) && this.scheduleVisualSelectionRefresh();
1493
+ this.bufHandle != null && c === this.bufHandle && v(this.lastMode) && this.scheduleVisualSelectionRefresh();
1420
1494
  } else if (t === "nvim_buf_detach_event") {
1421
- const i = _(e?.[0]);
1495
+ const i = x(e?.[0]);
1422
1496
  if (i && this.buffers.has(i)) {
1423
1497
  const s = this.buffers.get(i);
1424
1498
  if (s.name && this.buffersByName.delete(s.name), s.createdModel)
@@ -1431,42 +1505,38 @@ end)(unpack(_A))`;
1431
1505
  i && this.bufHandle === i && (this.bufHandle = null);
1432
1506
  } else t === "redraw" && this.handleRedraw(e);
1433
1507
  }
1434
- applyLinePatch(t, e, i, s) {
1435
- const n = t.getLineCount(), o = Math.min(e, n), l = Math.min(i, n), a = this.lastCursorPos ?? this.editor.getPosition() ?? new m.Position(1, 1), u = new m.Position(n, t.getLineMaxColumn(n)), h = o < n ? new m.Position(o + 1, 1) : u, f = l < n ? new m.Position(l + 1, 1) : u;
1436
- let c = s.join(`
1437
- `);
1438
- e >= n && i >= n && s.length > 0 && (c = `
1439
- ${c}`), i < n && s.length > 0 && (c += `
1508
+ computeLinePatch(t, e, i, s) {
1509
+ const n = t.getLineCount(), r = Math.min(e, n), l = Math.min(i, n), c = new g.Position(n, t.getLineMaxColumn(n)), f = l >= n && s.length === 0 && r > 0 ? new g.Position(r, t.getLineMaxColumn(r)) : r < n ? new g.Position(r + 1, 1) : c, a = l < n ? new g.Position(l + 1, 1) : c;
1510
+ let u = s.join(`
1440
1511
  `);
1441
- const g = new m.Range(h.lineNumber, h.column, f.lineNumber, f.column);
1512
+ return e >= n && i >= n && s.length > 0 && (u = `
1513
+ ${u}`), i < n && s.length > 0 && (u += `
1514
+ `), { range: new g.Range(f.lineNumber, f.column, a.lineNumber, a.column), text: u };
1515
+ }
1516
+ applyLinePatch(t, e, i, s) {
1517
+ const n = this.lastCursorPos ?? this.editor.getPosition() ?? new g.Position(1, 1), r = this.computeLinePatch(t, e, i, s);
1442
1518
  try {
1443
- if (t.getValueInRange(g) === c) return;
1519
+ if (t.getValueInRange(r.range) === r.text) return;
1444
1520
  } catch {
1445
1521
  }
1446
- if (this.suppressCursorSync = !0, this.applyingFromNvim = !0, t.applyEdits([{ range: g, text: c }]), this.applyingFromNvim = !1, a && this.editor.setPosition(a), this.suppressCursorSync = !1, this.delegateInsertToMonaco) {
1447
- const y = this.getActiveState();
1448
- if (y && y.model === t)
1522
+ if (this.suppressCursorSync = !0, this.applyingFromNvim = !0, t.applyEdits([{ range: r.range, text: r.text }]), this.applyingFromNvim = !1, n && this.editor.setPosition(n), this.suppressCursorSync = !1, this.delegateInsertToMonaco) {
1523
+ const l = this.getActiveState();
1524
+ if (l && l.model === t)
1449
1525
  try {
1450
- y.shadowLines = t.getLinesContent();
1526
+ l.shadowLines = t.getLinesContent();
1451
1527
  } catch {
1452
1528
  }
1453
1529
  }
1454
1530
  }
1455
1531
  applyLinePatchToModel(t, e, i, s) {
1456
- const n = t.getLineCount(), o = Math.min(e, n), l = Math.min(i, n), a = new m.Position(n, t.getLineMaxColumn(n)), u = o < n ? new m.Position(o + 1, 1) : a, h = l < n ? new m.Position(l + 1, 1) : a;
1457
- let f = s.join(`
1458
- `);
1459
- e >= n && i >= n && s.length > 0 && (f = `
1460
- ${f}`), i < n && s.length > 0 && (f += `
1461
- `);
1462
- const p = new m.Range(u.lineNumber, u.column, h.lineNumber, h.column);
1532
+ const n = this.computeLinePatch(t, e, i, s);
1463
1533
  try {
1464
- if (t.getValueInRange(p) === f) return;
1534
+ if (t.getValueInRange(n.range) === n.text) return;
1465
1535
  } catch {
1466
1536
  }
1467
1537
  this.applyingFromNvim = !0;
1468
1538
  try {
1469
- t.applyEdits([{ range: p, text: f }]);
1539
+ t.applyEdits([{ range: n.range, text: n.text }]);
1470
1540
  } catch {
1471
1541
  }
1472
1542
  this.applyingFromNvim = !1;
@@ -1508,7 +1578,7 @@ ${f}`), i < n && s.length > 0 && (f += `
1508
1578
  }
1509
1579
  }
1510
1580
  setCmdline(t) {
1511
- if (this.opts.onCmdline)
1581
+ if (this.cmdlineTextRaw = t, this.cmdlineCursorByte = null, this.cmdlineCursorOffsetBytes = 0, this.cmdlineCursorContentBytes = 0, this.opts.onCmdline)
1512
1582
  try {
1513
1583
  this.opts.onCmdline(t);
1514
1584
  } catch {
@@ -1521,6 +1591,15 @@ ${f}`), i < n && s.length > 0 && (f += `
1521
1591
  this.cmdlineEl.textContent = t, this.cmdlineEl.style.display = "block", this.cmdlineVisible = !0;
1522
1592
  }
1523
1593
  }
1594
+ setCmdlineCursor(t) {
1595
+ if (!this.cmdlineEl || !this.cmdlineVisible || !this.cmdlineTextRaw) return;
1596
+ if (t == null || !Number.isFinite(t)) {
1597
+ this.cmdlineEl.textContent = this.cmdlineTextRaw, this.cmdlineCursorByte = null;
1598
+ return;
1599
+ }
1600
+ const e = this.cmdlineTextRaw, i = Math.max(0, Math.min(Number(t) || 0, k(e))), s = Math.max(0, Math.min(e.length, w(e, i)));
1601
+ this.cmdlineCursorByte = i, this.cmdlineEl.textContent = `${e.slice(0, s)}▏${e.slice(s)}`;
1602
+ }
1524
1603
  setMessage(t) {
1525
1604
  if (this.opts.onMessage)
1526
1605
  try {
@@ -1552,10 +1631,10 @@ ${f}`), i < n && s.length > 0 && (f += `
1552
1631
  for (let i = 0; i < t.length; i += 1) {
1553
1632
  const s = t[i], n = document.createElement("div");
1554
1633
  n.style.padding = "2px 10px", n.style.display = "flex", n.style.gap = "10px", n.style.justifyContent = "space-between", n.style.background = i === e ? "rgba(255,255,255,0.12)" : "transparent";
1555
- const o = document.createElement("span");
1556
- o.textContent = s.word ?? "";
1634
+ const r = document.createElement("span");
1635
+ r.textContent = s.word ?? "";
1557
1636
  const l = document.createElement("span");
1558
- l.style.opacity = "0.7", l.textContent = s.menu ?? s.kind ?? "", n.appendChild(o), n.appendChild(l), this.popupEl.appendChild(n);
1637
+ l.style.opacity = "0.7", l.textContent = s.menu ?? s.kind ?? "", n.appendChild(r), n.appendChild(l), this.popupEl.appendChild(n);
1559
1638
  }
1560
1639
  this.popupEl.style.display = "block";
1561
1640
  }
@@ -1584,7 +1663,7 @@ ${f}`), i < n && s.length > 0 && (f += `
1584
1663
  }
1585
1664
  if (i === "wrappedLine" && (e === "down" || e === "up")) {
1586
1665
  const l = e === "down" ? "cursorDown" : "cursorUp";
1587
- for (let a = 0; a < s; a += 1)
1666
+ for (let c = 0; c < s; c += 1)
1588
1667
  try {
1589
1668
  this.editor.trigger("monaco-neovim-wasm", l, null);
1590
1669
  } catch {
@@ -1594,8 +1673,8 @@ ${f}`), i < n && s.length > 0 && (f += `
1594
1673
  return !1;
1595
1674
  };
1596
1675
  this.suppressCursorSync = !0, n();
1597
- const o = this.editor.getPosition();
1598
- o && (this.lastCursorPos = o, this.applyScrolloff(o)), this.suppressCursorSync = !1, this.scheduleSearchHighlightRefresh();
1676
+ const r = this.editor.getPosition();
1677
+ r && (this.lastCursorPos = r, this.applyScrolloff(r)), this.suppressCursorSync = !1, this.scheduleSearchHighlightRefresh();
1599
1678
  }
1600
1679
  getScrolloffLines() {
1601
1680
  return this.opts.scrolloff != null ? Math.max(0, Math.floor(this.opts.scrolloff)) : this.opts.syncScrolloff ? Math.max(0, Math.floor(this.nvimScrolloff)) : 0;
@@ -1606,89 +1685,89 @@ ${f}`), i < n && s.length > 0 && (f += `
1606
1685
  if (e <= 0) return !1;
1607
1686
  const i = t ?? this.editor.getPosition();
1608
1687
  if (!i) return !1;
1609
- const s = Math.max(1, Number(this.editor.getOption(m.editor.EditorOption.lineHeight)) || 0), n = e * s;
1688
+ const s = Math.max(1, Number(this.editor.getOption(g.editor.EditorOption.lineHeight)) || 0), n = e * s;
1610
1689
  if (n <= 0) return !1;
1611
- let o = 0;
1690
+ let r = 0;
1612
1691
  try {
1613
- const p = this.editor.getLayoutInfo();
1614
- o = Math.max(0, Number(p?.contentHeight ?? p?.height ?? 0) || 0);
1692
+ const m = this.editor.getLayoutInfo();
1693
+ r = Math.max(0, Number(m?.contentHeight ?? m?.height ?? 0) || 0);
1615
1694
  } catch {
1616
1695
  }
1617
- if (o <= 0) return !1;
1696
+ if (r <= 0) return !1;
1618
1697
  const l = () => {
1619
1698
  try {
1620
1699
  this.editor.revealPositionInCenterIfOutsideViewport(i);
1621
1700
  } catch {
1622
1701
  }
1623
1702
  };
1624
- let a = null;
1703
+ let c = null;
1625
1704
  try {
1626
- a = this.editor.getScrolledVisiblePosition(i);
1705
+ c = this.editor.getScrolledVisiblePosition(i);
1627
1706
  } catch {
1628
1707
  }
1629
- if (!a) {
1708
+ if (!c) {
1630
1709
  l();
1631
1710
  try {
1632
- a = this.editor.getScrolledVisiblePosition(i);
1711
+ c = this.editor.getScrolledVisiblePosition(i);
1633
1712
  } catch {
1634
1713
  }
1635
1714
  }
1636
- if (!a || !Number.isFinite(a.top) || !Number.isFinite(a.height)) return !1;
1637
- const u = this.editor.getScrollTop(), h = a.top, f = a.top + a.height, c = Math.max(0, o - n);
1715
+ if (!c || !Number.isFinite(c.top) || !Number.isFinite(c.height)) return !1;
1716
+ const h = this.editor.getScrollTop(), f = c.top, a = c.top + c.height, u = Math.max(0, r - n);
1638
1717
  let d = 0;
1639
- if (h < n ? d = h - n : f > c && (d = f - c), !d) return !1;
1718
+ if (f < n ? d = f - n : a > u && (d = a - u), !d) return !1;
1640
1719
  try {
1641
- this.editor.setScrollTop(Math.max(0, u + d));
1720
+ this.editor.setScrollTop(Math.max(0, h + d));
1642
1721
  } catch {
1643
1722
  }
1644
1723
  return !0;
1645
1724
  }
1646
1725
  applyMonacoScroll(t) {
1647
- const e = typeof t.by == "string" ? t.by : "", i = typeof t.direction == "string" ? t.direction : "", s = Math.max(1, Number(t.value ?? 1) || 1), n = !!t.moveCursor, o = typeof t.cursorBy == "string" ? t.cursorBy : "wrappedLine", l = Math.max(3, this.uiRows || this.opts.rows), a = this.editor.getOption(m.editor.EditorOption.fontInfo), u = Math.max(1, Number(a?.lineHeight ?? 0) || 0), h = Math.max(1, l - 2), f = Math.max(1, Math.floor(h / 2));
1648
- let c = s;
1649
- if (e === "page") c = h * s;
1650
- else if (e === "halfPage") c = f * s;
1651
- else if (e === "line") c = s;
1726
+ const e = typeof t.by == "string" ? t.by : "", i = typeof t.direction == "string" ? t.direction : "", s = Math.max(1, Number(t.value ?? 1) || 1), n = !!t.moveCursor, r = typeof t.cursorBy == "string" ? t.cursorBy : "wrappedLine", l = Math.max(3, this.uiRows || this.opts.rows), c = this.editor.getOption(g.editor.EditorOption.fontInfo), h = Math.max(1, Number(c?.lineHeight ?? 0) || 0), f = Math.max(1, l - 2), a = Math.max(1, Math.floor(f / 2));
1727
+ let u = s;
1728
+ if (e === "page") u = f * s;
1729
+ else if (e === "halfPage") u = a * s;
1730
+ else if (e === "line") u = s;
1652
1731
  else return !1;
1653
1732
  const d = i === "up" ? -1 : i === "down" ? 1 : 0;
1654
1733
  if (!d) return !1;
1655
1734
  this.suppressCursorSync = !0;
1656
1735
  try {
1657
- const g = this.editor.getScrollTop(), y = Math.max(0, g + d * c * u);
1736
+ const p = this.editor.getScrollTop(), y = Math.max(0, p + d * u * h);
1658
1737
  this.editor.setScrollTop(y);
1659
1738
  } catch {
1660
1739
  }
1661
- let p = !1;
1740
+ let m = !1;
1662
1741
  if (n)
1663
1742
  try {
1664
1743
  this.applyMonacoCursorMove({
1665
1744
  to: d > 0 ? "down" : "up",
1666
- by: o,
1667
- value: c
1668
- }), p = !0;
1745
+ by: r,
1746
+ value: u
1747
+ }), m = !0;
1669
1748
  } catch {
1670
1749
  }
1671
- return this.suppressCursorSync = !1, p;
1750
+ return this.suppressCursorSync = !1, m;
1672
1751
  }
1673
1752
  applyMonacoReveal(t) {
1674
1753
  const e = typeof t.direction == "string" ? t.direction : "", i = !!t.resetCursor, s = this.editor.getPosition();
1675
1754
  if (!s) return !1;
1676
- const n = s.lineNumber, o = Math.max(1, this.uiRows || this.opts.rows), l = this.editor.getOption(m.editor.EditorOption.fontInfo), a = Math.max(1, Number(l?.lineHeight ?? 0) || 0);
1755
+ const n = s.lineNumber, r = Math.max(1, this.uiRows || this.opts.rows), l = this.editor.getOption(g.editor.EditorOption.fontInfo), c = Math.max(1, Number(l?.lineHeight ?? 0) || 0);
1677
1756
  this.suppressCursorSync = !0;
1678
1757
  try {
1679
1758
  if (e === "top") {
1680
- const u = this.editor.getTopForLineNumber(n);
1681
- this.editor.setScrollTop(u);
1759
+ const h = this.editor.getTopForLineNumber(n);
1760
+ this.editor.setScrollTop(h);
1682
1761
  } else if (e === "center")
1683
1762
  this.editor.revealLineInCenter(n);
1684
1763
  else if (e === "bottom") {
1685
- const u = this.editor.getTopForLineNumber(n), h = Math.max(0, u - (o - 1) * a);
1686
- this.editor.setScrollTop(h);
1764
+ const h = this.editor.getTopForLineNumber(n), f = Math.max(0, h - (r - 1) * c);
1765
+ this.editor.setScrollTop(f);
1687
1766
  }
1688
1767
  if (i) {
1689
- const u = this.editor.getModel();
1690
- if (u) {
1691
- const h = u.getLineContent(n) ?? "", f = /\S/.exec(h), c = f ? f.index + 1 : 1, d = u.validatePosition(new m.Position(n, c));
1768
+ const h = this.editor.getModel();
1769
+ if (h) {
1770
+ const f = h.getLineContent(n) ?? "", a = /\S/.exec(f), u = a ? a.index + 1 : 1, d = h.validatePosition(new g.Position(n, u));
1692
1771
  this.editor.setPosition(d), this.lastCursorPos = d;
1693
1772
  }
1694
1773
  }
@@ -1702,30 +1781,30 @@ ${f}`), i < n && s.length > 0 && (f += `
1702
1781
  const i = this.editor.getModel();
1703
1782
  if (!i) return;
1704
1783
  const s = Math.max(1, Number(t.value ?? 1) || 1);
1705
- let n = 1, o = i.getLineCount();
1784
+ let n = 1, r = i.getLineCount();
1706
1785
  try {
1707
- const c = this.editor.getVisibleRanges();
1708
- c && c.length && (n = Math.min(...c.map((d) => d.startLineNumber)), o = Math.max(...c.map((d) => d.endLineNumber)));
1786
+ const u = this.editor.getVisibleRanges();
1787
+ u && u.length && (n = Math.min(...u.map((d) => d.startLineNumber)), r = Math.max(...u.map((d) => d.endLineNumber)));
1709
1788
  } catch {
1710
1789
  }
1711
- n = Math.max(1, Math.min(n, i.getLineCount())), o = Math.max(1, Math.min(o, i.getLineCount())), o < n && (o = n);
1790
+ n = Math.max(1, Math.min(n, i.getLineCount())), r = Math.max(1, Math.min(r, i.getLineCount())), r < n && (r = n);
1712
1791
  let l = n;
1713
1792
  if (e === "top")
1714
1793
  l = n + (s - 1);
1715
1794
  else if (e === "middle")
1716
- l = Math.floor((n + o) / 2);
1795
+ l = Math.floor((n + r) / 2);
1717
1796
  else if (e === "bottom")
1718
- l = o - (s - 1);
1797
+ l = r - (s - 1);
1719
1798
  else
1720
1799
  return;
1721
- l = Math.max(n, Math.min(o, l));
1722
- const a = i.getLineContent(l) ?? "", u = /\S/.exec(a), h = u ? u.index + 1 : 1, f = i.validatePosition(new m.Position(l, h));
1800
+ l = Math.max(n, Math.min(r, l));
1801
+ const c = i.getLineContent(l) ?? "", h = /\S/.exec(c), f = h ? h.index + 1 : 1, a = i.validatePosition(new g.Position(l, f));
1723
1802
  this.suppressCursorSync = !0;
1724
1803
  try {
1725
- this.editor.setPosition(f);
1804
+ this.editor.setPosition(a);
1726
1805
  } catch {
1727
1806
  }
1728
- this.lastCursorPos = f, this.suppressCursorSync = !1;
1807
+ this.lastCursorPos = a, this.suppressCursorSync = !1;
1729
1808
  }
1730
1809
  syncTabstopFromMonaco() {
1731
1810
  const t = this.editor.getModel();
@@ -1759,12 +1838,17 @@ ${f}`), i < n && s.length > 0 && (f += `
1759
1838
  continue;
1760
1839
  }
1761
1840
  if (i === "cmdline_show") {
1762
- const o = n[0], l = typeof n[2] == "string" ? n[2] : "", a = typeof n[3] == "string" ? n[3] : "", u = Math.max(0, Number(n[4] ?? 0) || 0), h = a || l || "", f = `${" ".repeat(u)}${h}${M(o)}`;
1763
- this.setCmdline(f), this.scheduleSearchHighlightRefresh();
1841
+ const r = n[0], l = Math.max(0, Number(n[1] ?? 0) || 0), c = typeof n[2] == "string" ? n[2] : "", h = typeof n[3] == "string" ? n[3] : "", f = Math.max(0, Number(n[4] ?? 0) || 0), a = h || c || "", u = " ".repeat(f), d = N(r), m = `${u}${a}${d}`;
1842
+ this.setCmdline(m);
1843
+ const p = k(`${u}${a}`), y = k(d);
1844
+ this.cmdlineCursorOffsetBytes = p, this.cmdlineCursorContentBytes = y;
1845
+ const b = l <= y ? this.cmdlineCursorOffsetBytes + l : l;
1846
+ this.setCmdlineCursor(b), this.scheduleSearchHighlightRefresh();
1764
1847
  continue;
1765
1848
  }
1766
1849
  if (i === "cmdline_pos") {
1767
- this.scheduleSearchHighlightRefresh();
1850
+ const r = Math.max(0, Number(n[0] ?? 0) || 0), l = this.cmdlineCursorOffsetBytes > 0 && r <= this.cmdlineCursorContentBytes ? this.cmdlineCursorOffsetBytes + r : r;
1851
+ this.setCmdlineCursor(l), this.scheduleSearchHighlightRefresh();
1768
1852
  continue;
1769
1853
  }
1770
1854
  if (i === "msg_clear") {
@@ -1772,25 +1856,25 @@ ${f}`), i < n && s.length > 0 && (f += `
1772
1856
  continue;
1773
1857
  }
1774
1858
  if (i === "msg_show") {
1775
- const o = typeof n[0] == "string" ? n[0] : "", l = n[1], a = !!n[2], u = !!n[4], h = M(l);
1776
- if (o === "empty" && !h) {
1859
+ const r = typeof n[0] == "string" ? n[0] : "", l = n[1], c = !!n[2], h = !!n[4], f = N(l);
1860
+ if (r === "empty" && !f) {
1777
1861
  this.setMessage(null);
1778
1862
  continue;
1779
1863
  }
1780
- if (!h) continue;
1781
- if (u && this.messageEl?.textContent) {
1782
- this.setMessage(`${this.messageEl.textContent}${h}`);
1864
+ if (!f) continue;
1865
+ if (h && this.messageEl?.textContent) {
1866
+ this.setMessage(`${this.messageEl.textContent}${f}`);
1783
1867
  continue;
1784
1868
  }
1785
- if (a) {
1786
- this.setMessage(h);
1869
+ if (c) {
1870
+ this.setMessage(f);
1787
1871
  continue;
1788
1872
  }
1789
- this.setMessage(h);
1873
+ this.setMessage(f);
1790
1874
  continue;
1791
1875
  }
1792
1876
  if (i === "msg_showmode" || i === "msg_showcmd" || i === "msg_ruler") {
1793
- const o = n[0], l = M(o);
1877
+ const r = n[0], l = N(r);
1794
1878
  this.setMessage(l || null);
1795
1879
  continue;
1796
1880
  }
@@ -1799,18 +1883,18 @@ ${f}`), i < n && s.length > 0 && (f += `
1799
1883
  continue;
1800
1884
  }
1801
1885
  if (i === "popupmenu_show") {
1802
- const o = n[0], l = Number(n[1] ?? -1), a = me(o);
1803
- this.setPopupmenu(a, Number.isFinite(l) ? l : -1);
1886
+ const r = n[0], l = Number(n[1] ?? -1), c = be(r);
1887
+ this.setPopupmenu(c, Number.isFinite(l) ? l : -1);
1804
1888
  continue;
1805
1889
  }
1806
1890
  if (i === "popupmenu_select") {
1807
- const o = Number(n[0] ?? -1);
1808
- this.updatePopupmenuSelection(Number.isFinite(o) ? o : -1);
1891
+ const r = Number(n[0] ?? -1);
1892
+ this.updatePopupmenuSelection(Number.isFinite(r) ? r : -1);
1809
1893
  continue;
1810
1894
  }
1811
1895
  if (i === "mode_change") {
1812
- const o = typeof n[0] == "string" ? n[0] : "";
1813
- this.applyNvimMode(o);
1896
+ const r = typeof n[0] == "string" ? n[0] : "";
1897
+ this.applyNvimMode(r);
1814
1898
  continue;
1815
1899
  }
1816
1900
  }
@@ -1818,108 +1902,131 @@ ${f}`), i < n && s.length > 0 && (f += `
1818
1902
  initTextInputListeners() {
1819
1903
  const t = this.editor.getDomNode();
1820
1904
  if (!t) return;
1821
- const e = Array.from(t.querySelectorAll("textarea.inputarea"));
1822
- if (!e.length) return;
1823
- const i = (c) => {
1905
+ const e = (a) => {
1906
+ try {
1907
+ a.stopImmediatePropagation?.();
1908
+ } catch {
1909
+ }
1824
1910
  try {
1825
- c.stopImmediatePropagation?.();
1911
+ a.stopPropagation();
1826
1912
  } catch {
1827
1913
  }
1914
+ }, i = (a) => {
1828
1915
  try {
1829
- c.stopPropagation();
1916
+ const u = a;
1917
+ return !u || typeof u.tagName != "string" || u.tagName !== "TEXTAREA" || !u.classList?.contains?.("inputarea") ? null : u;
1830
1918
  } catch {
1919
+ return null;
1831
1920
  }
1832
- }, s = (c) => {
1833
- if (this.compositionActive || c.isComposing || !c.ctrlKey && !c.altKey && !c.metaKey) return;
1834
- const d = this.modifiedKeyName(c);
1835
- if (!d || !this.opts.shouldHandleKey(c)) return;
1836
- const p = this.delegateInsertToMonaco;
1837
- if (this.hasExplicitModAllowlist(p)) {
1838
- if (!this.shouldForwardModifiedKeys(c, p)) return;
1839
- } else if (p || !c.ctrlKey || c.altKey || c.metaKey || d !== "f" && d !== "b" && d !== "d" && d !== "u" && d !== "e" && d !== "y") return;
1840
- const g = this.opts.translateKey(c);
1841
- if (g) {
1842
- i(c);
1921
+ }, s = (a) => {
1922
+ if (this.compositionActive || a.isComposing || a.getModifierState?.("AltGraph")) return;
1923
+ const u = this.delegateInsertToMonaco && !this.exitingInsertMode;
1924
+ if (!u && (a.key === "Backspace" || a.key === "Delete" || a.key === "Escape")) {
1925
+ if (!this.opts.shouldHandleKey(a)) return;
1926
+ const p = this.opts.translateKey(a);
1927
+ if (!p) return;
1928
+ e(a);
1843
1929
  try {
1844
- c.preventDefault();
1930
+ a.preventDefault();
1845
1931
  } catch {
1846
1932
  }
1847
- p && this.flushPendingMonacoSync(), this.sendInput(g);
1933
+ this.exitingInsertMode ? this.pendingKeysAfterExit += p : this.sendInput(p);
1934
+ return;
1935
+ }
1936
+ if (M(this.lastMode) && !this.delegateInsertToMonaco && !this.exitingInsertMode && !a.ctrlKey && !a.metaKey && typeof a.key == "string" && a.key.length === 1) {
1937
+ const p = /^[\x20-\x7E]$/.test(a.key);
1938
+ if (!!!(a.altKey && p)) {
1939
+ if (!this.opts.shouldHandleKey(a)) return;
1940
+ e(a);
1941
+ return;
1942
+ }
1943
+ }
1944
+ if (!a.ctrlKey && !a.altKey && !a.metaKey) return;
1945
+ const d = this.modifiedKeyName(a);
1946
+ if (!d || !this.opts.shouldHandleKey(a)) return;
1947
+ if (this.hasExplicitModAllowlist(u)) {
1948
+ if (!this.shouldForwardModifiedKeys(a, u)) return;
1949
+ } else if (u || !a.ctrlKey || a.altKey || a.metaKey || d !== "f" && d !== "b" && d !== "d" && d !== "u" && d !== "e" && d !== "y" && d !== "v") return;
1950
+ const m = this.opts.translateKey(a);
1951
+ if (m) {
1952
+ e(a);
1953
+ try {
1954
+ a.preventDefault();
1955
+ } catch {
1956
+ }
1957
+ this.exitingInsertMode ? (this.debugLog(`keydown(capture) buffer: key=${JSON.stringify(a.key)} code=${JSON.stringify(a.code)} mods=${a.ctrlKey ? "C" : ""}${a.altKey ? "A" : ""}${a.metaKey ? "D" : ""}${a.shiftKey ? "S" : ""} -> ${m}`), this.pendingKeysAfterExit += m) : (u && this.flushPendingMonacoSync(), this.debugLog(`keydown(capture) send: key=${JSON.stringify(a.key)} code=${JSON.stringify(a.code)} mods=${a.ctrlKey ? "C" : ""}${a.altKey ? "A" : ""}${a.metaKey ? "D" : ""}${a.shiftKey ? "S" : ""} -> ${m}`), this.sendInput(m));
1848
1958
  }
1849
1959
  }, n = () => {
1850
- if (this.compositionActive = !0, this.delegateInsertToMonaco) {
1960
+ if (this.compositionActive = !0, this.debugLog(`compositionstart delegateInsert=${this.delegateInsertToMonaco} mode=${JSON.stringify(this.lastMode)}`), this.delegateInsertToMonaco) {
1851
1961
  this.setPreedit(null);
1852
1962
  return;
1853
1963
  }
1854
1964
  this.setPreedit("");
1855
- }, o = () => {
1856
- this.compositionActive = !1, this.setPreedit(null);
1857
- }, l = (c) => {
1858
- if (this.compositionActive = !1, this.setPreedit(null), this.delegateInsertToMonaco) {
1965
+ }, r = (a) => {
1966
+ if (this.compositionActive = !1, this.setPreedit(null), this.debugLog(`compositionend delegateInsert=${this.delegateInsertToMonaco} mode=${JSON.stringify(this.lastMode)} data=${JSON.stringify(a.data ?? "")}`), this.delegateInsertToMonaco) {
1859
1967
  this.scheduleCursorSyncToNvim();
1860
1968
  return;
1861
1969
  }
1862
- const d = c.target;
1970
+ const u = a.target;
1971
+ if (M(this.lastMode)) {
1972
+ const d = typeof a.data == "string" ? a.data : "", m = !d && u?.value ? String(u.value) : "", p = d || m;
1973
+ p && this.sendCmdlineImeText(p);
1974
+ }
1863
1975
  try {
1976
+ const d = i(u);
1864
1977
  d && (d.value = "");
1865
1978
  } catch {
1866
1979
  }
1867
1980
  this.pendingResyncAfterComposition && (this.pendingResyncAfterComposition = !1, this.scheduleResync());
1868
- }, a = (c) => {
1981
+ }, l = (a) => {
1869
1982
  if (this.compositionActive || (this.compositionActive = !0), this.delegateInsertToMonaco) return;
1870
- const d = c.target, p = typeof c.data == "string" ? c.data : d?.value ? String(d.value) : "";
1871
- this.setPreedit(p || "");
1872
- }, u = (c) => {
1873
- this.delegateInsertToMonaco || i(c);
1874
- }, h = (c) => {
1875
- if (this.delegateInsertToMonaco) return;
1876
- i(c);
1877
- const p = c.target;
1983
+ const u = i(a.target), d = typeof a.data == "string" ? a.data : u?.value ? String(u.value) : "";
1984
+ this.setPreedit(d || "");
1985
+ }, c = (a) => {
1986
+ i(a.target) && (this.delegateInsertToMonaco && !this.exitingInsertMode || e(a));
1987
+ }, h = (a) => {
1988
+ const u = i(a.target);
1989
+ if (!u || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
1990
+ e(a);
1991
+ const d = a;
1878
1992
  if (this.ignoreNextInputEvent) {
1879
1993
  this.ignoreNextInputEvent = !1;
1880
1994
  try {
1881
- p && (p.value = "");
1995
+ u && (u.value = "");
1882
1996
  } catch {
1883
1997
  }
1884
1998
  return;
1885
1999
  }
1886
- if (!this.compositionActive)
2000
+ if (!this.compositionActive) {
2001
+ if (M(this.lastMode)) {
2002
+ const m = typeof d.data == "string" ? d.data : "", p = !m && u?.value ? String(u.value) : "", y = m || p;
2003
+ y && this.sendCmdlineImeText(y);
2004
+ }
1887
2005
  try {
1888
- p && (p.value = "");
2006
+ u && (u.value = "");
1889
2007
  } catch {
1890
2008
  }
1891
- }, f = (c) => {
1892
- if (this.delegateInsertToMonaco) return;
1893
- i(c);
1894
- const d = c.clipboardData?.getData("text/plain") ?? "";
1895
- d && (c.preventDefault(), this.pasteText(d));
2009
+ }
2010
+ }, f = (a) => {
2011
+ if (!i(a.target) || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2012
+ e(a);
2013
+ const u = a.clipboardData?.getData("text/plain") ?? "";
2014
+ u && (a.preventDefault(), this.ignoreNextInputEvent = !0, this.pasteText(u));
1896
2015
  try {
1897
- c.target?.value && (c.target.value = "");
2016
+ a.target?.value && (a.target.value = "");
1898
2017
  } catch {
1899
2018
  }
1900
2019
  };
1901
2020
  this.disposables.push(
1902
2021
  // Capture phase to ensure we see events even if Monaco stops propagation.
1903
- b(t, "keydown", s, !0),
1904
- b(t, "compositionstart", n, !0),
1905
- b(t, "compositionupdate", a, !0),
1906
- b(t, "compositionend", o, !0)
2022
+ _(t, "keydown", s, !0),
2023
+ _(t, "beforeinput", c, !0),
2024
+ _(t, "input", h, !0),
2025
+ _(t, "paste", f, !0),
2026
+ _(t, "compositionstart", n, !0),
2027
+ _(t, "compositionupdate", l, !0),
2028
+ _(t, "compositionend", r, !0)
1907
2029
  );
1908
- for (const c of e)
1909
- this.disposables.push(
1910
- b(c, "beforeinput", u, !0),
1911
- b(c, "input", h, !0),
1912
- b(c, "compositionstart", (d) => {
1913
- n();
1914
- }, !0),
1915
- b(c, "compositionupdate", (d) => {
1916
- a(d);
1917
- }, !0),
1918
- b(c, "compositionend", (d) => {
1919
- l(d);
1920
- }, !0),
1921
- b(c, "paste", f, !0)
1922
- );
1923
2030
  }
1924
2031
  scheduleResync() {
1925
2032
  this.resyncTimer || (this.resyncTimer = window.setTimeout(() => {
@@ -1940,7 +2047,7 @@ ${f}`), i < n && s.length > 0 && (f += `
1940
2047
  const i = (t && t.length ? t : [""]).join(`
1941
2048
  `);
1942
2049
  if (e.getValue() === i) return;
1943
- const s = this.lastCursorPos ?? this.editor.getPosition() ?? new m.Position(1, 1);
2050
+ const s = this.lastCursorPos ?? this.editor.getPosition() ?? new g.Position(1, 1);
1944
2051
  if (this.suppressCursorSync = !0, this.applyingFromNvim = !0, e.setValue(i), this.applyingFromNvim = !1, s && this.editor.setPosition(s), this.suppressCursorSync = !1, this.delegateInsertToMonaco) {
1945
2052
  const n = this.getActiveState();
1946
2053
  if (n && n.model === e)
@@ -2021,11 +2128,11 @@ ${f}`), i < n && s.length > 0 && (f += `
2021
2128
  let e = 1, i = t.getLineCount();
2022
2129
  try {
2023
2130
  const n = this.editor.getVisibleRanges();
2024
- n && n.length && (e = Math.min(...n.map((o) => o.startLineNumber)), i = Math.max(...n.map((o) => o.endLineNumber)));
2131
+ n && n.length && (e = Math.min(...n.map((r) => r.startLineNumber)), i = Math.max(...n.map((r) => r.endLineNumber)));
2025
2132
  } catch {
2026
2133
  }
2027
2134
  e = Math.max(1, Math.min(e, t.getLineCount())), i = Math.max(1, Math.min(i, t.getLineCount()));
2028
- const s = await this.execLua(ne, [e, i]);
2135
+ const s = await this.execLua(le, [e, i]);
2029
2136
  this.applySearchHighlights(s, t);
2030
2137
  } catch {
2031
2138
  }
@@ -2042,49 +2149,49 @@ ${f}`), i < n && s.length > 0 && (f += `
2042
2149
  this.clearSearchHighlights();
2043
2150
  return;
2044
2151
  }
2045
- const n = Array.isArray(i.matches) ? i.matches : [], o = i.current && typeof i.current == "object" ? i.current : null, l = o && Number.isFinite(Number(o.l)) ? `${Number(o.l)}:${Number(o.s)}:${Number(o.e)}` : null, a = [], u = 2e3, h = /* @__PURE__ */ new Map(), f = (c) => {
2046
- if (h.has(c)) return h.get(c);
2047
- const d = e.getLineContent(c) ?? "";
2048
- return h.set(c, d), d;
2152
+ const n = Array.isArray(i.matches) ? i.matches : [], r = i.current && typeof i.current == "object" ? i.current : null, l = r && Number.isFinite(Number(r.l)) ? `${Number(r.l)}:${Number(r.s)}:${Number(r.e)}` : null, c = [], h = 2e3, f = /* @__PURE__ */ new Map(), a = (u) => {
2153
+ if (f.has(u)) return f.get(u);
2154
+ const d = e.getLineContent(u) ?? "";
2155
+ return f.set(u, d), d;
2049
2156
  };
2050
- for (let c = 0; c < n.length && a.length < u; c += 1) {
2051
- const d = n[c], p = Number(d?.l), g = Number(d?.s), y = Number(d?.e);
2052
- if (!Number.isFinite(p) || !Number.isFinite(g) || !Number.isFinite(y)) continue;
2053
- const C = p + 1;
2054
- if (C < 1 || C > e.getLineCount()) continue;
2055
- const T = f(C), A = v(T, Math.max(0, g)) + 1, P = v(T, Math.max(0, y)) + 1;
2056
- if (P <= A) continue;
2057
- const B = `${p}:${g}:${y}`, K = l && B === l ? "monaco-neovim-search-current" : "monaco-neovim-search-match";
2058
- a.push({
2059
- range: new m.Range(C, A, C, P),
2060
- options: { inlineClassName: K }
2157
+ for (let u = 0; u < n.length && c.length < h; u += 1) {
2158
+ const d = n[u], m = Number(d?.l), p = Number(d?.s), y = Number(d?.e);
2159
+ if (!Number.isFinite(m) || !Number.isFinite(p) || !Number.isFinite(y)) continue;
2160
+ const b = m + 1;
2161
+ if (b < 1 || b > e.getLineCount()) continue;
2162
+ const S = a(b), E = w(S, Math.max(0, p)) + 1, D = w(S, Math.max(0, y)) + 1;
2163
+ if (D <= E) continue;
2164
+ const W = `${m}:${p}:${y}`, z = l && W === l ? "monaco-neovim-search-current" : "monaco-neovim-search-match";
2165
+ c.push({
2166
+ range: new g.Range(b, E, b, D),
2167
+ options: { inlineClassName: z }
2061
2168
  });
2062
2169
  }
2063
2170
  this.ensureSearchStyle();
2064
2171
  try {
2065
- this.searchDecorationIds = this.editor.deltaDecorations(this.searchDecorationIds, a);
2172
+ this.searchDecorationIds = this.editor.deltaDecorations(this.searchDecorationIds, c);
2066
2173
  } catch {
2067
2174
  this.searchDecorationIds = [];
2068
2175
  }
2069
2176
  }
2070
2177
  applyVisualDecorations(t, e) {
2071
2178
  this.ensureVisualStyle();
2072
- const s = fe(e) === "V", n = [];
2179
+ const s = ge(e) === "V", n = [];
2073
2180
  if (s) {
2074
- let o = 1 / 0, l = -1 / 0;
2075
- for (const a of t) {
2076
- const u = a.getStartPosition(), h = a.getEndPosition();
2077
- o = Math.min(o, u.lineNumber, h.lineNumber), l = Math.max(l, u.lineNumber, h.lineNumber);
2181
+ let r = 1 / 0, l = -1 / 0;
2182
+ for (const c of t) {
2183
+ const h = c.getStartPosition(), f = c.getEndPosition();
2184
+ r = Math.min(r, h.lineNumber, f.lineNumber), l = Math.max(l, h.lineNumber, f.lineNumber);
2078
2185
  }
2079
- Number.isFinite(o) && Number.isFinite(l) && l >= o && n.push({
2080
- range: new m.Range(o, 1, l, 1),
2186
+ Number.isFinite(r) && Number.isFinite(l) && l >= r && n.push({
2187
+ range: new g.Range(r, 1, l, 1),
2081
2188
  options: { isWholeLine: !0, className: "monaco-neovim-visual-line" }
2082
2189
  });
2083
2190
  } else
2084
- for (const o of t) {
2085
- const l = o.getStartPosition(), a = o.getEndPosition();
2191
+ for (const r of t) {
2192
+ const l = r.getStartPosition(), c = r.getEndPosition();
2086
2193
  n.push({
2087
- range: m.Range.fromPositions(l, a),
2194
+ range: g.Range.fromPositions(l, c),
2088
2195
  options: { inlineClassName: "monaco-neovim-visual-inline" }
2089
2196
  });
2090
2197
  }
@@ -2097,26 +2204,97 @@ ${f}`), i < n && s.length > 0 && (f += `
2097
2204
  applyNvimMode(t) {
2098
2205
  const e = typeof t == "string" ? t : "";
2099
2206
  if (!e || e === this.lastMode) return;
2100
- this.lastMode = e;
2101
- const i = de(e);
2102
- if (i !== this.delegateInsertToMonaco) {
2103
- this.delegateInsertToMonaco = i;
2104
- const s = this.ensureActiveState();
2105
- i ? (this.setPreedit(null), s && (s.shadowLines = this.editor.getModel()?.getLinesContent() ?? null, s.pendingBufEdits = [], s.pendingFullSync = !1, s.pendingCursorSync = !1)) : s && ((s.pendingBufEdits.length || s.pendingCursorSync || s.pendingFullSync) && this.flushPendingMonacoSync(), s.shadowLines = null, s.pendingBufEdits = [], s.pendingFullSync = !1, s.pendingCursorSync = !1);
2106
- }
2107
- this.applyCursorStyle(e), this.opts.onModeChange && this.opts.onModeChange(e), this.updateVisualSelection(e), this.scheduleSearchHighlightRefresh();
2207
+ const i = this.lastMode;
2208
+ this.lastMode = e, M(e) && (this.ignoreNextInputEvent = !1);
2209
+ const s = $(e) && !this.recordingRegister;
2210
+ if (s !== this.delegateInsertToMonaco) {
2211
+ this.delegateInsertToMonaco = s;
2212
+ const n = this.ensureActiveState();
2213
+ s ? (this.setPreedit(null), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, n && (n.shadowLines = this.editor.getModel()?.getLinesContent() ?? null, n.pendingBufEdits = [], n.pendingFullSync = !1, n.pendingCursorSync = !1)) : (n && ((n.pendingBufEdits.length || n.pendingCursorSync || n.pendingFullSync) && this.flushPendingMonacoSync(), n.shadowLines = null, n.pendingBufEdits = [], n.pendingFullSync = !1, n.pendingCursorSync = !1), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0);
2214
+ }
2215
+ if (this.exitingInsertMode && !$(e)) {
2216
+ this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.exitingInsertMode = !1;
2217
+ const n = this.pendingKeysAfterExit;
2218
+ this.pendingKeysAfterExit = "", n && this.sendInput(n);
2219
+ }
2220
+ if (this.applyCursorStyle(e), this.opts.onModeChange && this.opts.onModeChange(e), this.updateVisualSelection(e), v(i) && !v(e))
2221
+ try {
2222
+ const n = this.editor.getPosition() ?? this.lastCursorPos;
2223
+ n && !this.compositionActive && (this.suppressCursorSync = !0, this.editor.setSelection(new g.Selection(n.lineNumber, n.column, n.lineNumber, n.column)), this.suppressCursorSync = !1);
2224
+ } catch {
2225
+ this.suppressCursorSync = !1;
2226
+ }
2227
+ this.scheduleSearchHighlightRefresh();
2228
+ }
2229
+ armInsertExit() {
2230
+ this.exitingInsertMode = !0, this.pendingKeysAfterExit = "", this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.exitInsertTimer = window.setTimeout(() => {
2231
+ if (this.exitInsertTimer = null, !this.exitingInsertMode) return;
2232
+ this.exitingInsertMode = !1;
2233
+ const t = this.pendingKeysAfterExit;
2234
+ this.pendingKeysAfterExit = "", t && this.sendInput(t);
2235
+ }, 200);
2236
+ }
2237
+ syncDotRepeatToNvim() {
2238
+ if (!this.session || !this.session.isRunning()) return;
2239
+ const t = this.dotRepeatKeys, e = this.dotRepeatBackspaces;
2240
+ if (this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, !t) return;
2241
+ this.execLua(`
2242
+ local api = vim.api
2243
+ local keys, bs = ...
2244
+ keys = type(keys) == "string" and keys or ""
2245
+ if keys == "" then
2246
+ return
2247
+ end
2248
+ local mode = (api.nvim_get_mode() or {}).mode or ""
2249
+ if not mode:match("^[iR]") then
2250
+ return
2251
+ end
2252
+ local ei = vim.opt.ei:get()
2253
+ vim.opt.ei = "all"
2254
+ local curr_win = api.nvim_get_current_win()
2255
+ local temp_buf = api.nvim_create_buf(false, true)
2256
+ local temp_win = api.nvim_open_win(temp_buf, true, { external = true, width = 1, height = 1 })
2257
+ bs = tonumber(bs) or 0
2258
+ if bs > 0 then
2259
+ pcall(api.nvim_buf_set_lines, temp_buf, 0, -1, false, { ("x"):rep(bs) })
2260
+ pcall(api.nvim_win_set_cursor, temp_win, { 1, bs })
2261
+ end
2262
+ local tc = api.nvim_replace_termcodes(keys, true, true, true)
2263
+ api.nvim_feedkeys(tc, "n", true)
2264
+ pcall(api.nvim_set_current_win, curr_win)
2265
+ pcall(api.nvim_win_close, temp_win, true)
2266
+ pcall(api.nvim_buf_delete, temp_buf, { force = true })
2267
+ vim.opt.ei = ei
2268
+ `, [t, e]).catch(() => {
2269
+ });
2108
2270
  }
2109
2271
  handleKey(t) {
2110
2272
  const e = t.browserEvent;
2111
- if (e.defaultPrevented && e.key !== "Escape") return;
2273
+ if (e.defaultPrevented && e.key !== "Escape" && e.key !== "Enter") return;
2274
+ if (this.exitingInsertMode) {
2275
+ if (e.key === "Escape") {
2276
+ t.preventDefault();
2277
+ return;
2278
+ }
2279
+ if (e.key === "Process" || e.keyCode === 229) {
2280
+ this.compositionActive = !0;
2281
+ return;
2282
+ }
2283
+ if (this.compositionActive || e.isComposing || !this.opts.shouldHandleKey(e) || this.hasExplicitModAllowlist(!1) && !this.shouldForwardModifiedKeys(e, !1))
2284
+ return;
2285
+ const s = this.opts.translateKey(e);
2286
+ if (!s) return;
2287
+ this.ignoreNextInputEvent = !0, t.preventDefault(), this.pendingKeysAfterExit += s;
2288
+ return;
2289
+ }
2112
2290
  if (this.delegateInsertToMonaco) {
2113
2291
  if (e.key === "Escape") {
2114
- t.preventDefault(), this.flushPendingMonacoSync(), this.sendInput("<Esc>");
2292
+ t.preventDefault(), this.armInsertExit(), this.flushPendingMonacoSync(), this.syncDotRepeatToNvim(), this.sendInput("<Esc>");
2115
2293
  return;
2116
2294
  }
2117
2295
  if (!this.opts.shouldHandleKey(e) || this.hasExplicitModAllowlist(!0) && !this.shouldForwardModifiedKeys(e, !0))
2118
2296
  return;
2119
- if (e.ctrlKey || e.altKey || e.metaKey) {
2297
+ if ((e.ctrlKey || e.altKey || e.metaKey) && !e.getModifierState?.("AltGraph")) {
2120
2298
  const s = this.opts.translateKey(e);
2121
2299
  if (!s) return;
2122
2300
  t.preventDefault(), this.flushPendingMonacoSync(), this.sendInput(s);
@@ -2124,6 +2302,11 @@ ${f}`), i < n && s.length > 0 && (f += `
2124
2302
  }
2125
2303
  return;
2126
2304
  }
2305
+ if (M(this.lastMode) && !this.compositionActive && !e.isComposing && !e.getModifierState?.("AltGraph") && !e.ctrlKey && !e.metaKey && typeof e.key == "string" && e.key.length === 1) {
2306
+ const s = /^[\x20-\x7E]$/.test(e.key);
2307
+ if (!!!(e.altKey && s))
2308
+ return this.opts.shouldHandleKey(e), void 0;
2309
+ }
2127
2310
  if (this.ignoreTextKeydownUntil > 0 && typeof e.key == "string" && e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey && (typeof performance < "u" && performance.now ? performance.now() : Date.now()) < this.ignoreTextKeydownUntil)
2128
2311
  return;
2129
2312
  if (e.key === "Process" || e.keyCode === 229) {
@@ -2133,13 +2316,53 @@ ${f}`), i < n && s.length > 0 && (f += `
2133
2316
  if (this.compositionActive || e.isComposing || !this.opts.shouldHandleKey(e) || this.hasExplicitModAllowlist(!1) && !this.shouldForwardModifiedKeys(e, !1))
2134
2317
  return;
2135
2318
  const i = this.opts.translateKey(e);
2136
- i && (this.ignoreNextInputEvent = !0, t.preventDefault(), this.sendInput(i));
2319
+ i && (this.ignoreNextInputEvent = !0, t.preventDefault(), this.lastMode.startsWith("n") && (i === "q" ? this.recordingRegister ? (this.recordingRegister = "", this.scheduleRecordingRefresh()) : this.recordingRefreshArmed = !0 : this.recordingRefreshArmed && typeof i == "string" && i.length === 1 && (this.recordingRefreshArmed = !1, this.recordingRegister = i, this.scheduleRecordingRefresh())), this.sendInput(i));
2137
2320
  }
2138
2321
  handleMouse(t) {
2139
2322
  if (this.delegateInsertToMonaco || !this.bufHandle || !t.target?.position) return;
2140
- const { lineNumber: e, column: i } = t.target.position, s = this.editor.getModel(), n = Math.max(0, i - 1), o = s ? s.getLineContent(e) ?? "" : "", l = s ? S(o, n) : n;
2323
+ const { lineNumber: e, column: i } = t.target.position, s = this.editor.getModel(), n = Math.max(0, i - 1), r = s ? s.getLineContent(e) ?? "" : "", l = s ? C(r, n) : n;
2141
2324
  this.sendNotify("nvim_win_set_cursor", [0, [e, l]]);
2142
2325
  }
2326
+ handleSelection(t) {
2327
+ if (this.delegateInsertToMonaco || !this.session || !this.session.isRunning() || !this.bufHandle || this.suppressCursorSync || this.compositionActive || t?.isComposing) return;
2328
+ const e = t.selection;
2329
+ if (e && t.source === "mouse") {
2330
+ if (e.isEmpty()) {
2331
+ v(this.lastMode) && this.sendInput("<Esc>");
2332
+ return;
2333
+ }
2334
+ this.pendingSelection = new g.Selection(
2335
+ e.selectionStartLineNumber,
2336
+ e.selectionStartColumn,
2337
+ e.positionLineNumber,
2338
+ e.positionColumn
2339
+ ), !this.selectionSyncTimer && (this.selectionSyncTimer = window.setTimeout(() => {
2340
+ this.selectionSyncTimer = null;
2341
+ const i = this.pendingSelection;
2342
+ this.pendingSelection = null, i && this.syncVisualSelectionToNvim(i).catch(() => {
2343
+ });
2344
+ }, 80));
2345
+ }
2346
+ }
2347
+ async syncVisualSelectionToNvim(t) {
2348
+ if (this.delegateInsertToMonaco || !this.session || !this.session.isRunning() || !this.bufHandle) return;
2349
+ const e = this.editor.getModel();
2350
+ if (!e) return;
2351
+ const i = t.selectionStartLineNumber, s = Math.max(0, t.selectionStartColumn - 1), n = t.positionLineNumber, r = Math.max(0, t.positionColumn - 1), l = e.getLineContent(i) ?? "", c = e.getLineContent(n) ?? "", h = C(l, s), f = C(c, r);
2352
+ await this.execLua(`
2353
+ local api = vim.api
2354
+ local fn = vim.fn
2355
+ local a_line, a_col0, b_line, b_col0 = ...
2356
+ local mode = api.nvim_get_mode().mode or ""
2357
+ if mode:match("[vV\\022]") then
2358
+ local esc = api.nvim_replace_termcodes("<Esc>", true, true, true)
2359
+ api.nvim_feedkeys(esc, "n", false)
2360
+ end
2361
+ api.nvim_win_set_cursor(0, { a_line, a_col0 })
2362
+ api.nvim_feedkeys("v", "n", false)
2363
+ api.nvim_win_set_cursor(0, { b_line, b_col0 })
2364
+ `, [i, h, n, f]);
2365
+ }
2143
2366
  sendInput(t) {
2144
2367
  this.sendNotify("nvim_input", [t]);
2145
2368
  }
@@ -2193,7 +2416,12 @@ ${f}`), i < n && s.length > 0 && (f += `
2193
2416
  this.sendNotify("nvim_paste", [e, i, -1]);
2194
2417
  }
2195
2418
  handleMonacoModelChange(t) {
2196
- if (!this.delegateInsertToMonaco || this.applyingFromNvim || !this.session || !this.session.isRunning() || !this.bufHandle) return;
2419
+ if (!this.delegateInsertToMonaco) {
2420
+ if (this.applyingFromNvim || !this.session || !this.session.isRunning() || !this.bufHandle || this.compositionActive || t?.isComposing) return;
2421
+ t?.changes?.length && this.scheduleResync();
2422
+ return;
2423
+ }
2424
+ if (this.applyingFromNvim || !this.session || !this.session.isRunning() || !this.bufHandle) return;
2197
2425
  const e = this.editor.getModel();
2198
2426
  if (!e) return;
2199
2427
  const i = this.ensureActiveState();
@@ -2206,14 +2434,22 @@ ${f}`), i < n && s.length > 0 && (f += `
2206
2434
  i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.scheduleFlushPendingMonacoSync();
2207
2435
  return;
2208
2436
  }
2209
- const s = t.changes[0], n = s.range, o = n.startLineNumber - 1, l = n.endLineNumber - 1;
2210
- if (o < 0 || l < 0) return;
2211
- if (i.shadowLines[o] == null || i.shadowLines[l] == null) {
2437
+ const s = t.changes[0], n = s.range, r = n.startLineNumber - 1, l = n.endLineNumber - 1;
2438
+ if (r < 0 || l < 0) return;
2439
+ if (i.shadowLines[r] == null || i.shadowLines[l] == null) {
2212
2440
  i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.scheduleFlushPendingMonacoSync();
2213
2441
  return;
2214
2442
  }
2215
- const a = i.shadowLines[o] ?? "", u = i.shadowLines[l] ?? "", h = Math.max(0, n.startColumn - 1), f = Math.max(0, n.endColumn - 1), c = S(a, h), d = S(u, f), p = String(s.text ?? ""), g = p.length ? p.split(/\r?\n/) : [];
2216
- i.pendingBufEdits.push({ startRow: o, startColByte: c, endRow: l, endColByte: d, lines: g }), pe(i.shadowLines, o, h, l, f, p), i.pendingCursorSync = !0, this.scheduleFlushPendingMonacoSync();
2443
+ const c = i.shadowLines[r] ?? "", h = i.shadowLines[l] ?? "", f = Math.max(0, n.startColumn - 1), a = Math.max(0, n.endColumn - 1), u = C(c, f), d = C(h, a), m = String(s.text ?? ""), p = m.length ? m.split(/\r?\n/) : [];
2444
+ try {
2445
+ const b = Math.max(0, Number(s.rangeLength ?? 0) || 0);
2446
+ r === l && !m.includes(`
2447
+ `) ? (b > 0 && (this.dotRepeatBackspaces += b, this.dotRepeatKeys += "<BS>".repeat(b)), m && (this.dotRepeatKeys += L(m, !0)), this.dotRepeatKeys.length > 2e4 && (this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0)) : (this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0);
2448
+ } catch {
2449
+ }
2450
+ i.pendingBufEdits.push({ startRow: r, startColByte: u, endRow: l, endColByte: d, lines: p }), ye(i.shadowLines, r, f, l, a, m), i.pendingCursorSync = !0;
2451
+ const y = typeof performance < "u" && performance.now ? performance.now() : Date.now();
2452
+ this.ignoreNvimBufLinesUntil = y + Math.max(80, (this.opts.insertSyncDebounceMs || 20) * 4), this.scheduleFlushPendingMonacoSync();
2217
2453
  }
2218
2454
  scheduleCursorSyncToNvim() {
2219
2455
  if (!this.delegateInsertToMonaco) return;
@@ -2221,9 +2457,11 @@ ${f}`), i < n && s.length > 0 && (f += `
2221
2457
  t && (t.pendingCursorSync = !0, this.scheduleFlushPendingMonacoSync());
2222
2458
  }
2223
2459
  scheduleFlushPendingMonacoSync() {
2224
- this.cursorSyncTimer || (this.cursorSyncTimer = window.setTimeout(() => {
2460
+ if (this.cursorSyncTimer) return;
2461
+ const t = Math.max(0, Number(this.opts.insertSyncDebounceMs) || 0);
2462
+ this.cursorSyncTimer = window.setTimeout(() => {
2225
2463
  this.cursorSyncTimer = null, this.flushPendingMonacoSync();
2226
- }, 0));
2464
+ }, t);
2227
2465
  }
2228
2466
  flushPendingMonacoSync() {
2229
2467
  if (!this.session || !this.session.isRunning() || !this.bufHandle) return;
@@ -2231,7 +2469,7 @@ ${f}`), i < n && s.length > 0 && (f += `
2231
2469
  if (!t) return;
2232
2470
  const e = this.editor.getModel();
2233
2471
  if (e && t.model === e) {
2234
- if (t.pendingFullSync) {
2472
+ if (!t.pendingFullSync && t.pendingBufEdits.length > 1 && (t.pendingFullSync = !0), t.pendingFullSync) {
2235
2473
  const i = e.getLinesContent();
2236
2474
  this.sendNotify("nvim_buf_set_lines", [this.bufHandle, 0, -1, !1, i]), t.pendingFullSync = !1, t.pendingBufEdits = [], t.shadowLines = i.slice();
2237
2475
  } else if (t.pendingBufEdits.length) {
@@ -2246,7 +2484,7 @@ ${f}`), i < n && s.length > 0 && (f += `
2246
2484
  if (!this.session || !this.session.isRunning() || !this.bufHandle) return;
2247
2485
  const t = this.editor.getModel(), e = this.editor.getPosition();
2248
2486
  if (!t || !e) return;
2249
- const i = e.lineNumber, s = t.getLineContent(i) ?? "", n = S(s, Math.max(0, e.column - 1));
2487
+ const i = e.lineNumber, s = t.getLineContent(i) ?? "", n = C(s, Math.max(0, e.column - 1));
2250
2488
  this.sendNotify("nvim_win_set_cursor", [0, [i, n]]);
2251
2489
  }
2252
2490
  scheduleVisualSelectionRefresh() {
@@ -2260,7 +2498,7 @@ ${f}`), i < n && s.length > 0 && (f += `
2260
2498
  const t = await this.rpcCall("nvim_get_api_info", []), e = Array.isArray(t) ? Number(t[0]) : NaN;
2261
2499
  if (!Number.isFinite(e) || e <= 0) return;
2262
2500
  this.nvimChannelId = e;
2263
- const i = this.opts.wrappedLineMotions ? "true" : "false", s = this.opts.scrollMotions ? "true" : "false", n = this.opts.syncScrolloff && this.opts.scrolloff == null ? "true" : "false", o = this.opts.hostCommands ? "true" : "false", l = `
2501
+ const i = this.opts.wrappedLineMotions ? "true" : "false", s = this.opts.scrollMotions ? "true" : "false", n = this.opts.syncScrolloff && this.opts.scrolloff == null ? "true" : "false", r = this.opts.hostCommands ? "true" : "false", l = `
2264
2502
  local chan = ...
2265
2503
  local api = vim.api
2266
2504
  vim.g.monaco_neovim_wasm_chan = chan
@@ -2280,6 +2518,11 @@ local function send_scrolloff()
2280
2518
  vim.rpcnotify(chan, "monaco_scrolloff", so)
2281
2519
  end
2282
2520
 
2521
+ local function send_recording()
2522
+ local r = vim.fn.reg_recording() or ""
2523
+ vim.rpcnotify(chan, "monaco_recording", r)
2524
+ end
2525
+
2283
2526
  if ${i} then
2284
2527
  pcall(vim.cmd, "silent! source $HOME/.config/nvim/monaco-neovim-wasm/motion.vim")
2285
2528
  end
@@ -2287,7 +2530,7 @@ end
2287
2530
  if ${s} then
2288
2531
  pcall(vim.cmd, "silent! source $HOME/.config/nvim/monaco-neovim-wasm/scrolling.vim")
2289
2532
  end
2290
- if ${o} then
2533
+ if ${r} then
2291
2534
  pcall(vim.cmd, "silent! source $HOME/.config/nvim/monaco-neovim-wasm/host-commands.vim")
2292
2535
  end
2293
2536
 
@@ -2304,6 +2547,10 @@ api.nvim_create_autocmd({ "VisualEnter", "VisualLeave" }, {
2304
2547
  group = group,
2305
2548
  callback = function() send_mode(); send_cursor() end,
2306
2549
  })
2550
+ api.nvim_create_autocmd({ "RecordingEnter", "RecordingLeave" }, {
2551
+ group = group,
2552
+ callback = function() send_recording() end,
2553
+ })
2307
2554
  if ${n} then
2308
2555
  api.nvim_create_autocmd({ "OptionSet" }, {
2309
2556
  group = group,
@@ -2332,6 +2579,7 @@ api.nvim_create_autocmd({ "BufDelete" }, {
2332
2579
  send_mode()
2333
2580
  send_cursor()
2334
2581
  send_scrolloff()
2582
+ send_recording()
2335
2583
  vim.rpcnotify(chan, "monaco_buf_enter", {
2336
2584
  buf = api.nvim_get_current_buf(),
2337
2585
  name = api.nvim_buf_get_name(api.nvim_get_current_buf()) or "",
@@ -2369,22 +2617,22 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2369
2617
  return;
2370
2618
  }
2371
2619
  try {
2372
- const o = await s.readFile(n);
2373
- if (o == null) return;
2374
- const l = o instanceof Uint8Array ? new TextDecoder().decode(o) : String(o);
2620
+ const r = await s.readFile(n);
2621
+ if (r == null) return;
2622
+ const l = r instanceof Uint8Array ? new TextDecoder().decode(r) : String(r);
2375
2623
  await this.openText({ path: n, text: l }), this.opts.status(`opened: ${n}`);
2376
- } catch (o) {
2377
- this.opts.status(`edit failed: ${o?.message ?? o}`, !0);
2624
+ } catch (r) {
2625
+ this.opts.status(`edit failed: ${r?.message ?? r}`, !0);
2378
2626
  }
2379
2627
  return;
2380
2628
  }
2381
2629
  if (e === "write" || e === "wq") {
2382
- const n = this.editor.getModel(), o = n ? n.getValue() : "";
2630
+ const n = this.editor.getModel(), r = n ? n.getValue() : "";
2383
2631
  let l = typeof i.path == "string" ? String(i.path) : "";
2384
2632
  if (!l)
2385
2633
  try {
2386
- const a = await this.rpcCall("nvim_buf_get_name", [0]);
2387
- typeof a == "string" && (l = a);
2634
+ const c = await this.rpcCall("nvim_buf_get_name", [0]);
2635
+ typeof c == "string" && (l = c);
2388
2636
  } catch {
2389
2637
  }
2390
2638
  if (l || (l = this.opts.seedName), !s.writeFile) {
@@ -2392,7 +2640,7 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2392
2640
  return;
2393
2641
  }
2394
2642
  try {
2395
- await s.writeFile(l, o);
2643
+ await s.writeFile(l, r);
2396
2644
  try {
2397
2645
  await this.rpcCall("nvim_buf_set_name", [0, l]);
2398
2646
  } catch {
@@ -2401,10 +2649,10 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2401
2649
  await this.rpcCall("nvim_buf_set_option", [0, "modified", !1]);
2402
2650
  } catch {
2403
2651
  }
2404
- const a = this.getActiveState();
2405
- a && (a.name && a.name !== l && this.buffersByName.delete(a.name), a.name = l, this.buffersByName.set(l, a.id)), this.opts.status(`written: ${l}`), e === "wq" && this.opts.status("wq requested (provide onHostCommand to close the editor)");
2406
- } catch (a) {
2407
- this.opts.status(`write failed: ${a?.message ?? a}`, !0);
2652
+ const c = this.getActiveState();
2653
+ c && (c.name && c.name !== l && this.buffersByName.delete(c.name), c.name = l, this.buffersByName.set(l, c.id)), this.opts.status(`written: ${l}`), e === "wq" && this.opts.status("wq requested (provide onHostCommand to close the editor)");
2654
+ } catch (c) {
2655
+ this.opts.status(`write failed: ${c?.message ?? c}`, !0);
2408
2656
  }
2409
2657
  return;
2410
2658
  }
@@ -2427,17 +2675,17 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2427
2675
  }
2428
2676
  let l = [""];
2429
2677
  try {
2430
- const c = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
2431
- Array.isArray(c) && (l = c.map((d) => String(d ?? "")));
2678
+ const u = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
2679
+ Array.isArray(u) && (l = u.map((d) => String(d ?? "")));
2432
2680
  } catch {
2433
2681
  }
2434
- const a = m.Uri.from({ scheme: "nvim", authority: "buf", path: `/${e}` }), u = l.join(`
2435
- `), h = s && m.languages.getLanguages().some((c) => c.id === s), f = m.editor.createModel(u, h ? s : void 0, a);
2682
+ const c = g.Uri.from({ scheme: "nvim", authority: "buf", path: `/${e}` }), h = l.join(`
2683
+ `), f = s && g.languages.getLanguages().some((u) => u.id === s), a = g.editor.createModel(h, f ? s : void 0, c);
2436
2684
  n = {
2437
2685
  id: e,
2438
2686
  name: i || "",
2439
2687
  filetype: s || "",
2440
- model: f,
2688
+ model: a,
2441
2689
  createdModel: !0,
2442
2690
  shadowLines: null,
2443
2691
  pendingBufEdits: [],
@@ -2480,8 +2728,8 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2480
2728
  let n = this.buffersByName.get(s) ?? null;
2481
2729
  if (!n)
2482
2730
  try {
2483
- const o = await this.rpcCall("nvim_create_buf", [!0, !1]);
2484
- n = _(o) ?? Number(o);
2731
+ const r = await this.rpcCall("nvim_create_buf", [!0, !1]);
2732
+ n = x(r) ?? Number(r);
2485
2733
  } catch {
2486
2734
  n = null;
2487
2735
  }
@@ -2537,10 +2785,10 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2537
2785
  navigator.clipboard.readText().then((s) => e(s || "")).catch(() => e(""));
2538
2786
  }
2539
2787
  updateCursor(t, e) {
2540
- const i = this.editor.getModel(), s = Math.max(1, Number(t) || 1), n = Math.max(1, Number(e) || 1), o = i ? i.validatePosition(new m.Position(s, n)) : new m.Position(s, n);
2541
- if (this.lastCursorPos = o, this.compositionActive) return;
2788
+ const i = this.editor.getModel(), s = Math.max(1, Number(t) || 1), n = Math.max(1, Number(e) || 1), r = i ? i.validatePosition(new g.Position(s, n)) : new g.Position(s, n);
2789
+ if (this.lastCursorPos = r, this.compositionActive) return;
2542
2790
  const l = this.editor.getPosition();
2543
- l && l.lineNumber === o.lineNumber && l.column === o.column || (this.suppressCursorSync = !0, this.editor.setPosition(this.lastCursorPos), this.visualSelectionActive && this.editor.setSelection(new m.Selection(o.lineNumber, o.column, o.lineNumber, o.column)), this.applyScrolloff(this.lastCursorPos) || this.editor.revealPositionInCenterIfOutsideViewport(this.lastCursorPos), this.suppressCursorSync = !1), this.scheduleSearchHighlightRefresh();
2791
+ l && l.lineNumber === r.lineNumber && l.column === r.column || (this.suppressCursorSync = !0, this.editor.setPosition(this.lastCursorPos), this.visualSelectionActive && this.editor.setSelection(new g.Selection(r.lineNumber, r.column, r.lineNumber, r.column)), this.applyScrolloff(this.lastCursorPos) || this.editor.revealPositionInCenterIfOutsideViewport(this.lastCursorPos), this.suppressCursorSync = !1), this.scheduleSearchHighlightRefresh();
2544
2792
  }
2545
2793
  scheduleCursorRefresh() {
2546
2794
  if (this.compositionActive || this.cursorRefreshTimer) return;
@@ -2564,12 +2812,12 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2564
2812
  try {
2565
2813
  const e = await this.rpcCall("nvim_win_get_cursor", [0]);
2566
2814
  if (Array.isArray(e) && e.length >= 2) {
2567
- const i = Number(e[0]), s = Number(e[1]), n = L(this.editor, i, s), o = new m.Position(n.line, n.col), l = typeof performance < "u" && performance.now ? performance.now() : Date.now();
2815
+ const i = Number(e[0]), s = Number(e[1]), n = B(this.editor, i, s), r = new g.Position(n.line, n.col), l = typeof performance < "u" && performance.now ? performance.now() : Date.now();
2568
2816
  if (this.optimisticCursorPos && this.optimisticCursorUntil > l) {
2569
- const a = this.optimisticCursorPrevPos;
2570
- a && o.lineNumber === a.lineNumber && o.column === a.column || o.lineNumber < this.optimisticCursorPos.lineNumber || o.lineNumber === this.optimisticCursorPos.lineNumber && o.column < this.optimisticCursorPos.column || (this.optimisticCursorPos = null, this.optimisticCursorPrevPos = null, this.optimisticCursorUntil = 0, this.updateCursor(o.lineNumber, o.column));
2817
+ const c = this.optimisticCursorPrevPos;
2818
+ c && r.lineNumber === c.lineNumber && r.column === c.column || r.lineNumber < this.optimisticCursorPos.lineNumber || r.lineNumber === this.optimisticCursorPos.lineNumber && r.column < this.optimisticCursorPos.column || (this.optimisticCursorPos = null, this.optimisticCursorPrevPos = null, this.optimisticCursorUntil = 0, this.updateCursor(r.lineNumber, r.column));
2571
2819
  } else
2572
- this.optimisticCursorPos = null, this.optimisticCursorPrevPos = null, this.optimisticCursorUntil = 0, this.updateCursor(o.lineNumber, o.column);
2820
+ this.optimisticCursorPos = null, this.optimisticCursorPrevPos = null, this.optimisticCursorUntil = 0, this.updateCursor(r.lineNumber, r.column);
2573
2821
  }
2574
2822
  if (!this.lastMode)
2575
2823
  try {
@@ -2587,11 +2835,11 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2587
2835
  }
2588
2836
  }
2589
2837
  applyCursorStyle(t) {
2590
- const e = typeof t == "string" ? t : "", i = e.startsWith("i") || e.startsWith("R"), s = i ? "line" : "block", n = i ? "blink" : "solid", o = i ? this.initialCursorWidth || 1 : this.typicalFullWidth;
2591
- s === this.lastCursorStyle && n === this.lastCursorBlink && o === this.lastCursorWidth || (this.editor.updateOptions({ cursorStyle: s, cursorBlinking: n, cursorWidth: o }), this.lastCursorStyle = s, this.lastCursorBlink = n, this.lastCursorWidth = o);
2838
+ const e = typeof t == "string" ? t : "", i = e.startsWith("i") || e.startsWith("R"), s = i ? "line" : "block", n = i ? "blink" : "solid", r = i ? this.initialCursorWidth || 1 : this.typicalFullWidth;
2839
+ s === this.lastCursorStyle && n === this.lastCursorBlink && r === this.lastCursorWidth || (this.editor.updateOptions({ cursorStyle: s, cursorBlinking: n, cursorWidth: r }), this.lastCursorStyle = s, this.lastCursorBlink = n, this.lastCursorWidth = r);
2592
2840
  }
2593
2841
  async updateVisualSelection(t) {
2594
- const e = E(t), i = ++this.visualSelectionToken;
2842
+ const e = v(t), i = ++this.visualSelectionToken;
2595
2843
  if (!e) {
2596
2844
  this.clearVisualDecorations();
2597
2845
  return;
@@ -2604,13 +2852,13 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2604
2852
  }
2605
2853
  }
2606
2854
  async fetchVisualRanges() {
2607
- const t = await this.execLua(ie, []);
2608
- return Array.isArray(t) ? t.map(ae(this.editor)).filter((i) => !!i) : [];
2855
+ const t = await this.execLua(oe, []);
2856
+ return Array.isArray(t) ? t.map(de(this.editor)).filter((i) => !!i) : [];
2609
2857
  }
2610
2858
  async syncVisualSelectionColor() {
2611
2859
  try {
2612
- const e = await this.fetchVisualBg() || "#3e4451", i = D(e, 0.45), s = D(e, 0.3);
2613
- m.editor.defineTheme(this.opts.visualThemeName, {
2860
+ const e = await this.fetchVisualBg() || "#3e4451", i = H(e, 0.45), s = H(e, 0.3);
2861
+ g.editor.defineTheme(this.opts.visualThemeName, {
2614
2862
  base: "vs-dark",
2615
2863
  inherit: !0,
2616
2864
  rules: [],
@@ -2624,19 +2872,19 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2624
2872
  }
2625
2873
  async fetchVisualBg() {
2626
2874
  try {
2627
- const t = await this.rpcCall("nvim_get_hl", [0, { name: "Visual", link: !1 }]), e = F(t);
2875
+ const t = await this.rpcCall("nvim_get_hl", [0, { name: "Visual", link: !1 }]), e = K(t);
2628
2876
  if (e) return e;
2629
2877
  } catch {
2630
2878
  }
2631
2879
  try {
2632
- const t = await this.rpcCall("nvim_get_hl_by_name", ["Visual", !0]), e = F(t);
2880
+ const t = await this.rpcCall("nvim_get_hl_by_name", ["Visual", !0]), e = K(t);
2633
2881
  if (e) return e;
2634
2882
  } catch {
2635
2883
  }
2636
2884
  return null;
2637
2885
  }
2638
2886
  async seedBuffer(t, e) {
2639
- const i = _(t);
2887
+ const i = x(t);
2640
2888
  if (!i || i <= 0) return null;
2641
2889
  const s = e ?? this.opts.seedLines;
2642
2890
  if (!s || !s.length) return null;
@@ -2647,71 +2895,105 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2647
2895
  }
2648
2896
  }
2649
2897
  }
2650
- function ge(r, t = {}) {
2651
- return new se(r, t);
2898
+ function _e(o, t = {}) {
2899
+ return new ae(o, t);
2652
2900
  }
2653
- function w(r, t, e) {
2654
- return Math.min(Math.max(r, t), e);
2901
+ function I(o, t, e) {
2902
+ return Math.min(Math.max(o, t), e);
2655
2903
  }
2656
- function L(r, t, e) {
2657
- const i = Math.max(1, Number(t) || 1), s = Math.max(1, (Number(e) || 0) + 1), n = r.getModel();
2904
+ function k(o) {
2905
+ try {
2906
+ return new TextEncoder().encode(String(o ?? "")).length;
2907
+ } catch {
2908
+ return String(o ?? "").length;
2909
+ }
2910
+ }
2911
+ function B(o, t, e) {
2912
+ const i = Math.max(1, Number(t) || 1), s = Math.max(1, (Number(e) || 0) + 1), n = o.getModel();
2658
2913
  if (!n)
2659
2914
  return { line: i, col: s };
2660
- const o = n.getLineCount(), l = w(i, 1, o), a = n.getLineContent(l) ?? "", u = n.getLineMaxColumn(l), h = Math.max(0, Number(e) || 0), f = v(a, h), c = w(f + 1, 1, u);
2661
- return { line: l, col: c };
2915
+ const r = n.getLineCount(), l = I(i, 1, r), c = n.getLineContent(l) ?? "", h = n.getLineMaxColumn(l), f = Math.max(0, Number(e) || 0), a = w(c, f), u = I(a + 1, 1, h);
2916
+ return { line: l, col: u };
2917
+ }
2918
+ function L(o, t = !0) {
2919
+ const e = String(o ?? "");
2920
+ if (!e) return "";
2921
+ const i = e.replace(/\r\n/g, `
2922
+ `).replace(/\r/g, `
2923
+ `).replace(/</g, "<lt>");
2924
+ return t ? i.replace(/\n/g, "<CR>") : i;
2662
2925
  }
2663
- function oe(r) {
2664
- const t = r.key;
2926
+ function ce(o) {
2927
+ const t = o.key;
2665
2928
  if (!t || t === "Dead" || t === "Unidentified") return null;
2666
- const e = r.ctrlKey, i = r.altKey, s = r.metaKey, n = r.shiftKey, o = (l, a = !1) => {
2667
- const u = [];
2668
- return e && u.push("C-"), a && n && u.push("S-"), i && u.push("A-"), s && u.push("D-"), u.length ? `<${u.join("")}${l}>` : `<${l}>`;
2929
+ const e = !!o.getModifierState?.("AltGraph"), i = o.ctrlKey && !e, s = o.altKey && !e, n = o.metaKey && !e, r = o.shiftKey, l = (a) => a === "<" ? "lt" : a, c = (a) => a === "<" ? "<lt>" : a, h = (a, u = !1) => {
2930
+ const d = [];
2931
+ i && d.push("C-"), u && r && d.push("S-"), s && d.push("A-"), n && d.push("D-");
2932
+ const m = l(a);
2933
+ return d.length ? `<${d.join("")}${m}>` : `<${m}>`;
2669
2934
  };
2935
+ if (typeof o.code == "string" && o.code.startsWith("Numpad") || typeof KeyboardEvent?.DOM_KEY_LOCATION_NUMPAD == "number" && o.location === KeyboardEvent.DOM_KEY_LOCATION_NUMPAD) {
2936
+ switch (o.code) {
2937
+ case "NumpadEnter":
2938
+ return h("kEnter", !0);
2939
+ case "NumpadAdd":
2940
+ return h("kPlus", !0);
2941
+ case "NumpadSubtract":
2942
+ return h("kMinus", !0);
2943
+ case "NumpadMultiply":
2944
+ return h("kMultiply", !0);
2945
+ case "NumpadDivide":
2946
+ return h("kDivide", !0);
2947
+ case "NumpadDecimal":
2948
+ return h("kPoint", !0);
2949
+ }
2950
+ if (/^\d$/.test(t)) return h(`k${t}`, !0);
2951
+ }
2670
2952
  switch (t) {
2671
2953
  case "Backspace":
2672
- return o("BS", !0);
2954
+ return h("BS", !0);
2673
2955
  case "Enter":
2674
- return o("CR", !0);
2956
+ return h("CR", !0);
2675
2957
  case "Escape":
2676
- return o("Esc", !0);
2958
+ return h("Esc", !0);
2677
2959
  case "Tab":
2678
- return n && !e && !i && !s ? "<S-Tab>" : o("Tab", !0);
2960
+ return r && !i && !s && !n ? "<S-Tab>" : h("Tab", !0);
2679
2961
  case "ArrowUp":
2680
- return o("Up", !0);
2962
+ return h("Up", !0);
2681
2963
  case "ArrowDown":
2682
- return o("Down", !0);
2964
+ return h("Down", !0);
2683
2965
  case "ArrowLeft":
2684
- return o("Left", !0);
2966
+ return h("Left", !0);
2685
2967
  case "ArrowRight":
2686
- return o("Right", !0);
2968
+ return h("Right", !0);
2687
2969
  case "Delete":
2688
- return o("Del", !0);
2970
+ return h("Del", !0);
2689
2971
  case "Home":
2690
- return o("Home", !0);
2972
+ return h("Home", !0);
2691
2973
  case "End":
2692
- return o("End", !0);
2974
+ return h("End", !0);
2693
2975
  case "PageUp":
2694
- return o("PageUp", !0);
2976
+ return h("PageUp", !0);
2695
2977
  case "PageDown":
2696
- return o("PageDown", !0);
2978
+ return h("PageDown", !0);
2697
2979
  case "Insert":
2698
- return o("Insert", !0);
2980
+ return h("Insert", !0);
2699
2981
  }
2700
- if (/^F\d{1,2}$/.test(t)) return o(t, !0);
2982
+ if (/^F\d{1,2}$/.test(t)) return h(t, !0);
2701
2983
  if (t.length === 1) {
2702
- if (!e && !i && !s) return t;
2703
- if (t === " " && e && !i && !s) return "<Nul>";
2704
- const l = /^[A-Za-z]$/.test(t) ? t.toLowerCase() : t;
2705
- return `<${(e ? "C-" : "") + (i ? "A-" : "") + (s ? "D-" : "")}${l}>`;
2984
+ if (!i && !s && !n) return c(t);
2985
+ if (t === " " && i && !s && !n) return "<Nul>";
2986
+ const a = /^[A-Za-z]$/.test(t) ? t.toLowerCase() : t, u = l(a);
2987
+ return `<${(i ? "C-" : "") + (s ? "A-" : "") + (n ? "D-" : "")}${u}>`;
2706
2988
  }
2707
2989
  return null;
2708
2990
  }
2709
- function re(r) {
2710
- if (!r || r.length === 0) return null;
2711
- const t = r[0];
2712
- if (r.length === 1)
2991
+ function ue(o) {
2992
+ if (!o || o.length === 0) return null;
2993
+ const t = o[0];
2994
+ if (o.length === 1)
2713
2995
  return t <= 127 ? t : t - 256;
2714
- const e = new DataView(r.buffer, r.byteOffset, r.byteLength);
2996
+ const e = new DataView(o.buffer, o.byteOffset, o.byteLength);
2715
2997
  switch (t) {
2716
2998
  case 204:
2717
2999
  return e.getUint8(1);
@@ -2733,77 +3015,77 @@ function re(r) {
2733
3015
  return t;
2734
3016
  }
2735
3017
  }
2736
- function le(r) {
2737
- return r instanceof Uint8Array ? r : r instanceof ArrayBuffer ? new Uint8Array(r) : Array.isArray(r) ? new Uint8Array(r) : Number.isInteger(r) ? new Uint8Array([r & 255]) : r && r.type === "Buffer" && Array.isArray(r.data) ? new Uint8Array(r.data) : null;
3018
+ function he(o) {
3019
+ return o instanceof Uint8Array ? o : o instanceof ArrayBuffer ? new Uint8Array(o) : Array.isArray(o) ? new Uint8Array(o) : Number.isInteger(o) ? new Uint8Array([o & 255]) : o && o.type === "Buffer" && Array.isArray(o.data) ? new Uint8Array(o.data) : null;
2738
3020
  }
2739
- function _(r) {
2740
- if (r && typeof r == "object" && typeof r.type == "number") {
2741
- const e = le(r.data);
3021
+ function x(o) {
3022
+ if (o && typeof o == "object" && typeof o.type == "number") {
3023
+ const e = he(o.data);
2742
3024
  if (e) {
2743
- const i = re(e);
3025
+ const i = ue(e);
2744
3026
  if (i != null && i > 0) return i;
2745
3027
  }
2746
3028
  }
2747
- const t = Number(r);
3029
+ const t = Number(o);
2748
3030
  return Number.isInteger(t) && t > 0 ? t : null;
2749
3031
  }
2750
- function v(r, t) {
3032
+ function w(o, t) {
2751
3033
  let e = 0, i = 0;
2752
3034
  const s = Math.max(0, Number(t) || 0);
2753
3035
  for (; e < s; ) {
2754
- if (i >= r.length)
3036
+ if (i >= o.length)
2755
3037
  return i + (s - e);
2756
- const n = r.codePointAt(i), o = R(n ?? 0);
2757
- e += o, i += o === 4 ? 2 : 1;
3038
+ const n = o.codePointAt(i), r = P(n ?? 0);
3039
+ e += r, i += r === 4 ? 2 : 1;
2758
3040
  }
2759
3041
  return i;
2760
3042
  }
2761
- function S(r, t) {
3043
+ function C(o, t) {
2762
3044
  const e = Math.max(0, Number(t) || 0);
2763
3045
  let i = 0, s = 0;
2764
- for (; s < e && s < r.length; ) {
2765
- const n = r.codePointAt(s), o = R(n ?? 0);
2766
- i += o, s += o === 4 ? 2 : 1;
3046
+ for (; s < e && s < o.length; ) {
3047
+ const n = o.codePointAt(s), r = P(n ?? 0);
3048
+ i += r, s += r === 4 ? 2 : 1;
2767
3049
  }
2768
3050
  return i;
2769
3051
  }
2770
- function R(r) {
2771
- return r == null ? 0 : r <= 127 ? 1 : r <= 2047 ? 2 : r >= 55296 && r <= 57343 ? 4 : r < 65535 ? 3 : 4;
3052
+ function P(o) {
3053
+ return o == null ? 0 : o <= 127 ? 1 : o <= 2047 ? 2 : o >= 55296 && o <= 57343 ? 4 : o < 65535 ? 3 : 4;
2772
3054
  }
2773
- function ae(r) {
3055
+ function de(o) {
2774
3056
  return (t) => {
2775
3057
  if (!t || !t.start || !t.end) return null;
2776
- const e = k(r, t.start, !1), i = k(r, t.end, !!t.inclusive);
2777
- return !e || !i ? null : new m.Selection(e.lineNumber, e.column, i.lineNumber, i.column);
3058
+ const e = F(o, t.start, !1), i = F(o, t.end, !!t.inclusive);
3059
+ return !e || !i ? null : new g.Selection(e.lineNumber, e.column, i.lineNumber, i.column);
2778
3060
  };
2779
3061
  }
2780
- function k(r, t, e) {
3062
+ function F(o, t, e) {
2781
3063
  if (!t || typeof t.line != "number" || typeof t.col != "number") return null;
2782
- const i = r.getModel(), s = w(t.line + 1, 1, i?.getLineCount() || 1 / 0);
3064
+ const i = o.getModel(), s = I(t.line + 1, 1, i?.getLineCount() || 1 / 0);
2783
3065
  if (!i) {
2784
- const h = Math.max(1, t.col + 1);
2785
- return { lineNumber: s, column: h + (e ? 1 : 0) };
3066
+ const f = Math.max(1, t.col + 1);
3067
+ return { lineNumber: s, column: f + (e ? 1 : 0) };
2786
3068
  }
2787
- const n = i.getLineContent(s) ?? "", o = Math.max(0, Number(t.col) || 0);
2788
- let l = o;
3069
+ const n = i.getLineContent(s) ?? "", r = Math.max(0, Number(t.col) || 0);
3070
+ let l = r;
2789
3071
  if (e) {
2790
- const h = v(n, o);
2791
- if (h < n.length) {
2792
- const f = n.codePointAt(h);
2793
- l = o + R(f ?? 0);
3072
+ const f = w(n, r);
3073
+ if (f < n.length) {
3074
+ const a = n.codePointAt(f);
3075
+ l = r + P(a ?? 0);
2794
3076
  }
2795
3077
  }
2796
- const a = v(n, l) + 1, u = i.getLineMaxColumn(s);
2797
- return { lineNumber: s, column: w(a, 1, u) };
3078
+ const c = w(n, l) + 1, h = i.getLineMaxColumn(s);
3079
+ return { lineNumber: s, column: I(c, 1, h) };
2798
3080
  }
2799
- function E(r) {
2800
- const t = typeof r == "string" ? r : "";
3081
+ function v(o) {
3082
+ const t = typeof o == "string" ? o : "";
2801
3083
  return t.includes("v") || t.includes("V") || t.includes("") || t.includes("s") || t.includes("S") || t.includes("");
2802
3084
  }
2803
- function ce(r) {
2804
- if (!r || !Array.isArray(r) || r.length === 0) return;
3085
+ function fe(o) {
3086
+ if (!o || !Array.isArray(o) || o.length === 0) return;
2805
3087
  const t = new TextEncoder(), e = [];
2806
- for (const i of r) {
3088
+ for (const i of o) {
2807
3089
  if (!i) continue;
2808
3090
  const s = String(i.path ?? "");
2809
3091
  if (!s) continue;
@@ -2812,68 +3094,72 @@ function ce(r) {
2812
3094
  }
2813
3095
  return e.length ? e : void 0;
2814
3096
  }
2815
- function ue(r, t) {
2816
- const e = Array.isArray(t) ? t.filter(Boolean) : [], i = Array.isArray(r) ? r.filter(Boolean) : [], s = [...e, ...i];
3097
+ function me(o, t) {
3098
+ const e = Array.isArray(t) ? t.filter(Boolean) : [], i = Array.isArray(o) ? o.filter(Boolean) : [], s = [...e, ...i];
2817
3099
  return s.length ? s : void 0;
2818
3100
  }
2819
- function D(r, t) {
2820
- const e = r.replace("#", "");
2821
- if (e.length !== 6) return r;
2822
- const i = Math.round(w(t, 0, 1) * 255);
3101
+ function H(o, t) {
3102
+ const e = o.replace("#", "");
3103
+ if (e.length !== 6) return o;
3104
+ const i = Math.round(I(t, 0, 1) * 255);
2823
3105
  return `#${e}${i.toString(16).padStart(2, "0")}`;
2824
3106
  }
2825
- function F(r) {
2826
- if (!r || typeof r != "object") return null;
2827
- const t = r, e = t.background ?? t.bg;
2828
- return typeof e == "number" && e >= 0 ? he(e) : typeof t.background == "string" && t.background.startsWith("#") ? t.background : typeof t.bg == "string" && t.bg.startsWith("#") ? t.bg : null;
3107
+ function K(o) {
3108
+ if (!o || typeof o != "object") return null;
3109
+ const t = o, e = t.background ?? t.bg;
3110
+ return typeof e == "number" && e >= 0 ? pe(e) : typeof t.background == "string" && t.background.startsWith("#") ? t.background : typeof t.bg == "string" && t.bg.startsWith("#") ? t.bg : null;
2829
3111
  }
2830
- function he(r) {
2831
- return `#${Number(r >>> 0).toString(16).padStart(6, "0").slice(-6)}`;
3112
+ function pe(o) {
3113
+ return `#${Number(o >>> 0).toString(16).padStart(6, "0").slice(-6)}`;
2832
3114
  }
2833
- function b(r, t, e, i) {
2834
- return r.addEventListener(t, e, i), { dispose: () => r.removeEventListener(t, e, i) };
3115
+ function _(o, t, e, i) {
3116
+ return o.addEventListener(t, e, i), { dispose: () => o.removeEventListener(t, e, i) };
2835
3117
  }
2836
- function de(r) {
2837
- const t = typeof r == "string" ? r : "";
3118
+ function $(o) {
3119
+ const t = typeof o == "string" ? o : "";
2838
3120
  return t.startsWith("i") || t.startsWith("R");
2839
3121
  }
2840
- function fe(r) {
2841
- const t = typeof r == "string" ? r : "";
3122
+ function ge(o) {
3123
+ const t = typeof o == "string" ? o : "";
2842
3124
  return t.length ? t[t.length - 1] : "";
2843
3125
  }
2844
- function pe(r, t, e, i, s, n) {
2845
- const o = r[t] ?? "", l = r[i] ?? "", a = o.slice(0, Math.max(0, e)), u = l.slice(Math.max(0, s)), f = String(n ?? "").split(/\r?\n/);
2846
- let c;
2847
- if (f.length <= 1)
2848
- c = [`${a}${f[0] ?? ""}${u}`];
3126
+ function M(o) {
3127
+ const t = typeof o == "string" ? o : "";
3128
+ return t === "c" || t.startsWith("c");
3129
+ }
3130
+ function ye(o, t, e, i, s, n) {
3131
+ const r = o[t] ?? "", l = o[i] ?? "", c = r.slice(0, Math.max(0, e)), h = l.slice(Math.max(0, s)), a = String(n ?? "").split(/\r?\n/);
3132
+ let u;
3133
+ if (a.length <= 1)
3134
+ u = [`${c}${a[0] ?? ""}${h}`];
2849
3135
  else {
2850
- const d = `${a}${f[0] ?? ""}`, p = `${f[f.length - 1] ?? ""}${u}`, g = f.slice(1, -1);
2851
- c = [d, ...g, p];
3136
+ const d = `${c}${a[0] ?? ""}`, m = `${a[a.length - 1] ?? ""}${h}`, p = a.slice(1, -1);
3137
+ u = [d, ...p, m];
2852
3138
  }
2853
- r.splice(t, Math.max(0, i - t + 1), ...c);
3139
+ o.splice(t, Math.max(0, i - t + 1), ...u);
2854
3140
  }
2855
- function M(r) {
2856
- if (!r) return "";
2857
- if (typeof r == "string") return r;
2858
- if (!Array.isArray(r)) return "";
3141
+ function N(o) {
3142
+ if (!o) return "";
3143
+ if (typeof o == "string") return o;
3144
+ if (!Array.isArray(o)) return "";
2859
3145
  let t = "";
2860
- for (const e of r) {
3146
+ for (const e of o) {
2861
3147
  if (typeof e == "string") {
2862
3148
  t += e;
2863
3149
  continue;
2864
3150
  }
2865
3151
  if (Array.isArray(e)) {
2866
- typeof e[1] == "string" ? t += e[1] : typeof e[0] == "string" ? t += e[0] : t += M(e);
3152
+ typeof e[1] == "string" ? t += e[1] : typeof e[0] == "string" ? t += e[0] : t += N(e);
2867
3153
  continue;
2868
3154
  }
2869
3155
  e && typeof e == "object" && typeof e.text == "string" && (t += e.text);
2870
3156
  }
2871
3157
  return t;
2872
3158
  }
2873
- function me(r) {
2874
- if (!Array.isArray(r)) return [];
3159
+ function be(o) {
3160
+ if (!Array.isArray(o)) return [];
2875
3161
  const t = [];
2876
- for (const e of r) {
3162
+ for (const e of o) {
2877
3163
  if (Array.isArray(e)) {
2878
3164
  t.push({
2879
3165
  word: String(e[0] ?? ""),
@@ -2890,12 +3176,12 @@ function me(r) {
2890
3176
  }
2891
3177
  return t.filter((e) => e.word.length > 0);
2892
3178
  }
2893
- const ye = new URL("./nvimWorker.js", import.meta.url), be = new URL("./nvimWorkerAsyncify.js", import.meta.url);
3179
+ const Ce = new URL("./nvimWorker.js", import.meta.url), ve = new URL("./nvimWorkerAsyncify.js", import.meta.url);
2894
3180
  export {
2895
- se as MonacoNeovimClient,
2896
- Y as NeovimWasmSession,
2897
- ge as createMonacoNeovim,
2898
- ye as defaultWorkerUrl,
2899
- be as defaultWorkerUrlAsyncify,
2900
- G as isSharedArrayBufferAvailable
3181
+ ae as MonacoNeovimClient,
3182
+ te as NeovimWasmSession,
3183
+ _e as createMonacoNeovim,
3184
+ Ce as defaultWorkerUrl,
3185
+ ve as defaultWorkerUrlAsyncify,
3186
+ X as isSharedArrayBufferAvailable
2901
3187
  };