@monaco-neovim-wasm/lib 0.1.11 → 0.1.12

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,10 +1,10 @@
1
- import * as g from "monaco-editor";
2
- class V {
1
+ import * as y from "monaco-editor";
2
+ class U {
3
3
  buffer;
4
4
  ctrl;
5
5
  data;
6
6
  capacity;
7
- constructor(t = N) {
7
+ constructor(t = L) {
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) {
@@ -12,19 +12,19 @@ class V {
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
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
14
  if (e.byteLength > r) return !1;
15
- const a = this.capacity - s;
16
- return e.byteLength <= a ? (this.data.set(e, s), Atomics.store(this.ctrl, 1, (s + e.byteLength) % this.capacity)) : (this.data.set(e.subarray(0, a), s), this.data.set(e.subarray(a), 0), Atomics.store(this.ctrl, 1, e.byteLength - a)), Atomics.notify(this.ctrl, 1), !0;
15
+ const l = this.capacity - s;
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 N = 262144;
20
- function U(o = N) {
21
- return new V(o);
19
+ const L = 262144;
20
+ function Q(o = L) {
21
+ return new U(o);
22
22
  }
23
- function R(o) {
23
+ function D(o) {
24
24
  const t = [];
25
- return E(o, t), new Uint8Array(t);
25
+ return P(o, t), new Uint8Array(t);
26
26
  }
27
- function E(o, t) {
27
+ function P(o, t) {
28
28
  if (o == null) {
29
29
  t.push(192);
30
30
  return;
@@ -34,15 +34,15 @@ function E(o, t) {
34
34
  return;
35
35
  }
36
36
  if (typeof o == "number") {
37
- Q(o, t);
37
+ q(o, t);
38
38
  return;
39
39
  }
40
40
  if (typeof o == "string") {
41
- q(o, t);
41
+ j(o, t);
42
42
  return;
43
43
  }
44
44
  if (typeof o == "bigint") {
45
- O(o, t);
45
+ z(o, t);
46
46
  return;
47
47
  }
48
48
  if (Array.isArray(o)) {
@@ -50,11 +50,11 @@ function E(o, t) {
50
50
  return;
51
51
  }
52
52
  if (o instanceof Uint8Array) {
53
- j(o, t);
53
+ J(o, t);
54
54
  return;
55
55
  }
56
- if (Y(o)) {
57
- J(o, t);
56
+ if (Z(o)) {
57
+ Y(o, t);
58
58
  return;
59
59
  }
60
60
  if (typeof o == "object") {
@@ -63,7 +63,7 @@ function E(o, t) {
63
63
  }
64
64
  throw new Error("Unsupported type in msgpack encode");
65
65
  }
66
- function Q(o, t) {
66
+ function q(o, t) {
67
67
  if (!Number.isFinite(o)) throw new Error("Cannot encode non-finite number");
68
68
  if (Number.isInteger(o)) {
69
69
  if (o >= 0 && o <= 127) {
@@ -86,21 +86,21 @@ function Q(o, t) {
86
86
  t.push(210, o >> 24 & 255, o >> 16 & 255, o >> 8 & 255, o & 255);
87
87
  return;
88
88
  }
89
- O(BigInt(o), t);
89
+ z(BigInt(o), t);
90
90
  return;
91
91
  }
92
92
  const e = new ArrayBuffer(8);
93
93
  new DataView(e).setFloat64(0, o), t.push(203, ...new Uint8Array(e));
94
94
  }
95
- function O(o, t) {
95
+ function z(o, t) {
96
96
  const e = new ArrayBuffer(8);
97
97
  new DataView(e).setBigInt64(0, BigInt(o)), t.push(211, ...new Uint8Array(e));
98
98
  }
99
- function q(o, t) {
99
+ function j(o, t) {
100
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 j(o, t) {
103
+ function J(o, t) {
104
104
  const e = o.length;
105
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
  }
@@ -108,30 +108,30 @@ function G(o, t) {
108
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
110
  for (const i of o)
111
- E(i, t);
111
+ P(i, t);
112
112
  }
113
113
  function X(o, t) {
114
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
- E(s, t), E(o[s], t);
117
+ P(s, t), P(o[s], t);
118
118
  }
119
- function J(o, t) {
119
+ function Y(o, t) {
120
120
  const e = o.data.length;
121
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
123
  t.push(o.data[i]);
124
124
  }
125
- function Y(o) {
125
+ function Z(o) {
126
126
  return !!(o && typeof o == "object" && typeof o.type == "number" && o.data instanceof Uint8Array);
127
127
  }
128
- function Z() {
128
+ function ee() {
129
129
  return typeof SharedArrayBuffer < "u" && typeof crossOriginIsolated < "u" && crossOriginIsolated;
130
130
  }
131
- function ee(o) {
131
+ function te(o) {
132
132
  return o.byteOffset === 0 && o.byteLength === o.buffer.byteLength ? o : o.slice();
133
133
  }
134
- class te {
134
+ class ie {
135
135
  init;
136
136
  worker = null;
137
137
  sharedInput = null;
@@ -150,7 +150,7 @@ class te {
150
150
  worker: t.worker ?? null,
151
151
  workerUrl: t.workerUrl ?? null,
152
152
  createWorker: t.createWorker ?? null,
153
- sharedInputBytes: t.sharedInputBytes ?? N,
153
+ sharedInputBytes: t.sharedInputBytes ?? L,
154
154
  inputMode: t.inputMode ?? "shared",
155
155
  rpcTimeoutMs: t.rpcTimeoutMs ?? 8e3,
156
156
  maxQueuedBytes: t.maxQueuedBytes ?? 4 * 1024 * 1024,
@@ -168,32 +168,32 @@ class te {
168
168
  return this.workerExitCode;
169
169
  }
170
170
  async start(t) {
171
- const { cols: e, rows: i, wasmPath: s, runtimePath: n, env: r, files: a } = t;
172
- if (this.inputMode = t.inputMode ?? this.init.inputMode, this.inputMode === "shared" && !Z())
171
+ const { cols: e, rows: i, wasmPath: s, runtimePath: n, env: r, files: l } = t;
172
+ if (this.inputMode = t.inputMode ?? this.init.inputMode, this.inputMode === "shared" && !ee())
173
173
  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.');
174
- const u = this.worker;
175
- if (this.stop({ terminate: !this.init.reuseWorker, silent: !0 }), this.sharedInput = this.inputMode === "shared" ? U(this.init.sharedInputBytes) : null, this.workerExited = !1, this.workerExitCode = null, this.workerFatalError = 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 && u ? this.worker = u : this.worker = this.init.workerUrl ? new Worker(this.init.workerUrl, { type: "module" }) : null, !this.worker) throw new Error("worker/workerUrl is required");
176
- this.worker.onmessage = (l) => this.handleWorkerMessage(l.data);
177
- const d = (l) => {
174
+ const c = this.worker;
175
+ if (this.stop({ terminate: !this.init.reuseWorker, silent: !0 }), this.sharedInput = this.inputMode === "shared" ? Q(this.init.sharedInputBytes) : null, this.workerExited = !1, this.workerExitCode = null, this.workerFatalError = 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");
176
+ this.worker.onmessage = (f) => this.handleWorkerMessage(f.data);
177
+ const u = (f) => {
178
178
  if (!this.workerExited) {
179
- if (this.workerFatalError = l, this.workerExited = !0, this.workerExitCode = 1, this.pending.size) {
180
- const c = new Error(l);
181
- this.pending.forEach((h) => h.reject(c)), this.pending.clear();
179
+ if (this.workerFatalError = f, this.workerExited = !0, this.workerExitCode = 1, this.pending.size) {
180
+ const p = new Error(f);
181
+ this.pending.forEach((m) => m.reject(p)), this.pending.clear();
182
182
  }
183
183
  try {
184
- this.init.handlers.onStartError?.(l);
184
+ this.init.handlers.onStartError?.(f);
185
185
  } catch {
186
186
  }
187
187
  }
188
188
  };
189
- this.worker.onerror = (l) => {
190
- const c = l?.message ? `worker error: ${l.message}` : "worker error", h = l?.error?.stack;
191
- d(h ? `${c}
192
- ${h}` : c);
189
+ this.worker.onerror = (f) => {
190
+ const p = f?.message ? `worker error: ${f.message}` : "worker error", m = f?.error?.stack;
191
+ u(m ? `${p}
192
+ ${m}` : p);
193
193
  }, this.worker.onmessageerror = () => {
194
- d("worker messageerror");
194
+ u("worker messageerror");
195
195
  };
196
- const f = {
196
+ const d = {
197
197
  type: "start",
198
198
  cols: Number(e) || 80,
199
199
  rows: Number(i) || 24,
@@ -201,12 +201,12 @@ ${h}` : c);
201
201
  runtimePath: String(n ?? ""),
202
202
  inputBuffer: this.sharedInput?.buffer ?? null,
203
203
  env: r ?? void 0,
204
- files: a ?? void 0
204
+ files: l ?? void 0
205
205
  };
206
206
  try {
207
- this.worker.postMessage(f, []);
207
+ this.worker.postMessage(d, []);
208
208
  } catch {
209
- this.worker.postMessage(f);
209
+ this.worker.postMessage(d);
210
210
  }
211
211
  }
212
212
  stop(t = {}) {
@@ -231,7 +231,7 @@ ${h}` : c);
231
231
  }
232
232
  notify(t, e = []) {
233
233
  if (!this.worker || this.workerExited) return;
234
- const i = R([2, t, e]);
234
+ const i = D([2, t, e]);
235
235
  this.postInput(i);
236
236
  }
237
237
  call(t, e = []) {
@@ -241,13 +241,13 @@ ${h}` : c);
241
241
  return;
242
242
  }
243
243
  if (this.workerExited) {
244
- const a = this.workerExitCode, u = this.workerFatalError ? `: ${this.workerFatalError}` : "";
245
- s(new Error(a != null ? `nvim exited (${a})${u}` : `nvim exited${u}`));
244
+ const l = this.workerExitCode, c = this.workerFatalError ? `: ${this.workerFatalError}` : "";
245
+ s(new Error(l != null ? `nvim exited (${l})${c}` : `nvim exited${c}`));
246
246
  return;
247
247
  }
248
248
  const n = this.reqId++;
249
249
  this.pending.set(n, { resolve: i, reject: s, ts: Date.now(), method: t });
250
- const r = R([0, n, t, e]);
250
+ const r = D([0, n, t, e]);
251
251
  this.postInput(r), setTimeout(() => {
252
252
  this.pending.has(n) && (this.pending.delete(n), s(new Error(this.workerExited ? this.workerExitCode != null ? `nvim exited (${this.workerExitCode})${this.workerFatalError ? `: ${this.workerFatalError}` : ""}` : `nvim exited${this.workerFatalError ? `: ${this.workerFatalError}` : ""}` : `rpc timeout: ${t}`)));
253
253
  }, this.init.rpcTimeoutMs);
@@ -275,7 +275,7 @@ ${h}` : c);
275
275
  }
276
276
  respond(t, e, i) {
277
277
  if (!this.worker || this.workerExited) return;
278
- const s = R([1, t, e, i]);
278
+ const s = D([1, t, e, i]);
279
279
  this.postInput(s);
280
280
  }
281
281
  handleWorkerMessage(t) {
@@ -337,8 +337,8 @@ ${h}` : c);
337
337
  if (e === "exit") {
338
338
  const i = t, s = i.code, n = i.lastStderr;
339
339
  if (this.workerExited = !0, this.workerExitCode = s, this.pending.size) {
340
- const r = n ? `: ${n.trim()}` : "", a = new Error(`nvim exited (${s})${r}`);
341
- this.pending.forEach((u) => u.reject(a)), this.pending.clear();
340
+ const r = n ? `: ${n.trim()}` : "", l = new Error(`nvim exited (${s})${r}`);
341
+ this.pending.forEach((c) => c.reject(l)), this.pending.clear();
342
342
  }
343
343
  try {
344
344
  this.init.handlers.onExit?.(s, n);
@@ -348,7 +348,7 @@ ${h}` : c);
348
348
  }
349
349
  postInput(t) {
350
350
  if (!t || !t.buffer) return;
351
- const e = ee(t);
351
+ const e = te(t);
352
352
  if (!(!this.worker || this.workerExited)) {
353
353
  if (this.inputMode === "shared") {
354
354
  if (!this.sharedInput || this.inputQueueHead >= this.inputQueue.length && this.sharedInput.push(e))
@@ -389,18 +389,18 @@ ${h}` : c);
389
389
  } else {
390
390
  let e = 0, i = 0;
391
391
  for (let r = this.inputQueueHead; r < this.inputQueue.length; r += 1) {
392
- const a = this.inputQueue[r];
393
- if (a?.byteLength) {
394
- if (i > 0 && e + a.byteLength > 262144) break;
395
- e += a.byteLength, i += 1;
392
+ const l = this.inputQueue[r];
393
+ if (l?.byteLength) {
394
+ if (i > 0 && e + l.byteLength > 262144) break;
395
+ e += l.byteLength, i += 1;
396
396
  }
397
397
  }
398
398
  if (i <= 0 || e <= 0) return;
399
399
  const s = new Uint8Array(e);
400
400
  let n = 0;
401
401
  for (let r = 0; r < i; r += 1) {
402
- const a = this.inputQueue[this.inputQueueHead + r];
403
- s.set(a, n), n += a.byteLength;
402
+ const l = this.inputQueue[this.inputQueueHead + r];
403
+ s.set(l, n), n += l.byteLength;
404
404
  }
405
405
  this.inputQueueHead += i, this.inputQueuedBytes -= e, this.postStdin(s);
406
406
  }
@@ -418,7 +418,7 @@ ${h}` : c);
418
418
  }
419
419
  }
420
420
  }
421
- const ie = `if !exists('g:monaco_neovim_wasm_chan')
421
+ const ne = `if !exists('g:monaco_neovim_wasm_chan')
422
422
  finish
423
423
  endif
424
424
 
@@ -481,7 +481,7 @@ onoremap <silent> gk <Cmd>call <SID>up_down('up')<CR>
481
481
  nnoremap <silent> gj <Cmd>call <SID>up_down('down')<CR>
482
482
  xnoremap <silent> gj <Cmd>call <SID>up_down('down')<CR>
483
483
  onoremap <silent> gj <Cmd>call <SID>up_down('down')<CR>
484
- `, ne = `if !exists('g:monaco_neovim_wasm_chan')
484
+ `, se = `if !exists('g:monaco_neovim_wasm_chan')
485
485
  finish
486
486
  endif
487
487
 
@@ -582,7 +582,7 @@ onoremap <silent> <C-f> <Cmd>call <SID>scroll('down', 'page')<CR>
582
582
  nnoremap <silent> <C-b> <Cmd>call <SID>scroll('up', 'page')<CR>
583
583
  xnoremap <silent> <C-b> <Cmd>call <SID>scroll('up', 'page')<CR>
584
584
  onoremap <silent> <C-b> <Cmd>call <SID>scroll('up', 'page')<CR>
585
- `, se = `if !exists('g:monaco_neovim_wasm_chan')
585
+ `, re = `if !exists('g:monaco_neovim_wasm_chan')
586
586
  finish
587
587
  endif
588
588
 
@@ -633,7 +633,7 @@ cnoreabbrev <expr> q <SID>abbr('q', 'MonacoQuit')
633
633
  cnoreabbrev <expr> quit <SID>abbr('quit', 'MonacoQuit')
634
634
  cnoreabbrev <expr> wq <SID>abbr('wq', 'MonacoWq')
635
635
  cnoreabbrev <expr> x <SID>abbr('x', 'MonacoWq')
636
- `, re = [
636
+ `, oe = [
637
637
  "-- Monaco + Neovim (WASM)",
638
638
  "-- Click into the editor, press i, and start typing.",
639
639
  "",
@@ -642,20 +642,56 @@ cnoreabbrev <expr> x <SID>abbr('x', 'MonacoWq')
642
642
  "end",
643
643
  "",
644
644
  "print(greet('monaco'))"
645
- ], oe = `
646
- local api, fn = vim.api, vim.fn
645
+ ];
646
+ function A(o) {
647
+ try {
648
+ if (typeof document > "u") return !1;
649
+ const t = document.body;
650
+ if (!t) return !1;
651
+ const e = document.createElement("textarea");
652
+ e.value = String(o ?? ""), e.setAttribute("readonly", ""), e.style.position = "fixed", e.style.left = "0", e.style.top = "0", e.style.width = "1px", e.style.height = "1px", e.style.opacity = "0", e.style.pointerEvents = "none", t.appendChild(e), e.focus(), e.select(), e.setSelectionRange(0, e.value.length);
653
+ const i = !!document.execCommand?.("copy");
654
+ return t.removeChild(e), i;
655
+ } catch {
656
+ return !1;
657
+ }
658
+ }
659
+ const le = `
660
+ local api, fn = vim.api, vim.fn
647
661
 
648
- local function virtcol2byte(winid, lnum, virtcol)
649
- local byte_idx = fn.virtcol2col(winid, lnum, virtcol) - 1
650
- if fn.has("nvim-0.10.0") == 0 then
651
- return byte_idx
652
- end
653
- local buf = api.nvim_win_get_buf(winid)
654
- local line = api.nvim_buf_get_lines(buf, lnum - 1, lnum, false)[1] or ""
655
- local char_idx = fn.charidx(line, byte_idx)
656
- local prefix = fn.strcharpart(line, 0, char_idx + 1)
657
- return #prefix
658
- end
662
+ -- virtcol2col() returns a column (1-indexed) for the given virtual column.
663
+ -- Neovim <= 0.9 returns the *last* byte of a multibyte character, while Neovim >= 0.10 returns the *first* byte.
664
+ -- For our byte<->Monaco conversion we always want the 0-indexed *first* byte of the character.
665
+ local function virtcol2byte0(winid, lnum, virtcol)
666
+ local byte0 = fn.virtcol2col(winid, lnum, virtcol) - 1
667
+ if fn.has("nvim-0.10.0") == 1 then
668
+ return byte0
669
+ end
670
+ local buf = api.nvim_win_get_buf(winid)
671
+ local line = api.nvim_buf_get_lines(buf, lnum - 1, lnum, false)[1] or ""
672
+ local char_idx = fn.charidx(line, byte0)
673
+ local prefix = fn.strcharpart(line, 0, char_idx)
674
+ return #prefix
675
+ end
676
+
677
+ local function char_width_at_byte0(line, byte0)
678
+ if not line or line == "" then return 1, "" end
679
+ local b = math.max(0, math.min(#line, byte0 or 0))
680
+ local char_idx = fn.charidx(line, b)
681
+ local ch = fn.strcharpart(line, char_idx, 1)
682
+ if ch == "\\t" then return 1, ch end
683
+ local w = fn.strdisplaywidth(ch)
684
+ if not w or w < 1 then w = 1 end
685
+ return w, ch
686
+ end
687
+
688
+ local function start_vcol_at_byte0(line, byte0)
689
+ if not line or line == "" then return 1 end
690
+ local b = math.max(0, math.min(#line, byte0 or 0))
691
+ local char_idx = fn.charidx(line, b)
692
+ local prefix = fn.strcharpart(line, 0, char_idx)
693
+ return fn.strdisplaywidth(prefix) + 1
694
+ end
659
695
 
660
696
  local function get_selections(win)
661
697
  win = win or api.nvim_get_current_win()
@@ -699,21 +735,59 @@ local function get_selections(win)
699
735
  return { range }
700
736
  end
701
737
 
702
- local ranges = {}
703
- local start_vcol, end_vcol = fn.virtcol("v"), fn.virtcol(".")
704
- local top, bot = math.min(sline, eline), math.max(sline, eline)
705
- for lnum = top, bot do
706
- local line = api.nvim_buf_get_lines(buf, lnum, lnum + 1, false)[1] or ""
707
- local disp = fn.strdisplaywidth(line)
708
- if start_vcol > disp and end_vcol > disp then
709
- local chars = ({ vim.str_utfindex(line) })[2]
710
- table.insert(ranges, { start = { line = lnum, col = chars }, ["end"] = { line = lnum, col = chars }, inclusive = true })
711
- else
712
- local col_a = virtcol2byte(win, lnum + 1, math.min(start_vcol, end_vcol))
713
- local col_b = virtcol2byte(win, lnum + 1, math.max(start_vcol, end_vcol))
714
- table.insert(ranges, { start = { line = lnum, col = col_a }, ["end"] = { line = lnum, col = col_b }, inclusive = true })
715
- end
716
- end
738
+ local ranges = {}
739
+ local inclusive = (vim.o.selection or "inclusive") == "inclusive"
740
+ local start_vcol, end_vcol = fn.virtcol("v"), fn.virtcol(".")
741
+ if not inclusive then
742
+ -- 'selection=exclusive' excludes the cursor column from Visual selections.
743
+ if end_vcol >= start_vcol then
744
+ end_vcol = end_vcol - 1
745
+ else
746
+ end_vcol = end_vcol + 1
747
+ end
748
+ if end_vcol < 1 then end_vcol = 1 end
749
+ end
750
+ local left_vcol, right_vcol = math.min(start_vcol, end_vcol), math.max(start_vcol, end_vcol)
751
+ local top, bot = math.min(sline, eline), math.max(sline, eline)
752
+ for lnum = top, bot do
753
+ local line = api.nvim_buf_get_lines(buf, lnum, lnum + 1, false)[1] or ""
754
+ local disp = fn.strdisplaywidth(line)
755
+ local line_bytes = #line
756
+ local col_a = left_vcol > disp and line_bytes or virtcol2byte0(win, lnum + 1, left_vcol)
757
+ local col_b = right_vcol > disp and line_bytes or virtcol2byte0(win, lnum + 1, right_vcol)
758
+ if col_a > line_bytes then col_a = line_bytes end
759
+ if col_b > line_bytes then col_b = line_bytes end
760
+
761
+ local eff_left_vcol = left_vcol
762
+ if left_vcol <= disp then
763
+ local start_vcol_a = start_vcol_at_byte0(line, col_a)
764
+ local w_a, ch_a = char_width_at_byte0(line, col_a)
765
+ if ch_a ~= "\\t" and w_a > 1 and eff_left_vcol > start_vcol_a then
766
+ eff_left_vcol = start_vcol_a
767
+ end
768
+ end
769
+
770
+ local eff_right_vcol = right_vcol
771
+ if right_vcol <= disp then
772
+ local start_vcol_b = start_vcol_at_byte0(line, col_b)
773
+ local w_b, ch_b = char_width_at_byte0(line, col_b)
774
+ if ch_b ~= "\\t" and w_b > 1 then
775
+ eff_right_vcol = start_vcol_b + (w_b - 1)
776
+ end
777
+ end
778
+
779
+ if eff_right_vcol < eff_left_vcol then
780
+ eff_left_vcol, eff_right_vcol = eff_right_vcol, eff_left_vcol
781
+ end
782
+ table.insert(ranges, {
783
+ start = { line = lnum, col = col_a },
784
+ ["end"] = { line = lnum, col = col_b },
785
+ inclusive = inclusive,
786
+ start_vcol = eff_left_vcol,
787
+ end_vcol = eff_right_vcol,
788
+ disp = disp,
789
+ })
790
+ end
717
791
 
718
792
  if #ranges == 0 then
719
793
  local cur = api.nvim_win_get_cursor(win)
@@ -725,8 +799,9 @@ local function get_selections(win)
725
799
  return ranges
726
800
  end
727
801
 
728
- return get_selections(...)
729
- `, ae = `
802
+ local tail = (api.nvim_get_mode().mode or ""):sub(-1)
803
+ return { tail = tail, ranges = get_selections(...) }
804
+ `, ae = `
730
805
  local api, fn = vim.api, vim.fn
731
806
 
732
807
  local function get_visible_range(s, e)
@@ -815,7 +890,7 @@ end
815
890
 
816
891
  return run(...)
817
892
  `;
818
- class le {
893
+ class ce {
819
894
  editor;
820
895
  opts;
821
896
  session = null;
@@ -833,7 +908,14 @@ class le {
833
908
  visualSelectionActive = !1;
834
909
  visualDecorationIds = [];
835
910
  visualStyleEl = null;
911
+ visualVirtualOverlayEl = null;
912
+ visualVirtualRawRanges = [];
913
+ visualVirtualActive = !1;
836
914
  visualBgCss = "rgba(62, 68, 81, 0.45)";
915
+ monacoPrevOccurrencesHighlight = null;
916
+ monacoPrevSelectionHighlight = null;
917
+ monacoPrevSelectionHighlightMultiline = null;
918
+ monacoHighlightsSuppressed = !1;
837
919
  cursorRefreshTimer = null;
838
920
  cursorRefreshInFlight = !1;
839
921
  cursorRefreshPending = !1;
@@ -879,11 +961,18 @@ class le {
879
961
  compositionActive = !1;
880
962
  pendingResyncAfterComposition = !1;
881
963
  ignoreNextInputEvent = !1;
964
+ pendingEscAfterComposition = !1;
882
965
  exitingInsertMode = !1;
883
966
  pendingKeysAfterExit = "";
884
967
  exitInsertTimer = null;
885
968
  dotRepeatKeys = "";
886
969
  dotRepeatBackspaces = 0;
970
+ delegatedInsertReplayPossible = !1;
971
+ recentNormalKeys = "";
972
+ lastDelegatedInsertPrefix = null;
973
+ lastDelegatedDotRepeat = null;
974
+ ignoreInsertExitCursor = null;
975
+ ignoreMonacoCursorSyncToNvimUntil = 0;
887
976
  ignoreTextKeydownUntil = 0;
888
977
  lastImeCommitAt = 0;
889
978
  lastImeCommitText = "";
@@ -894,21 +983,27 @@ class le {
894
983
  optimisticCursorPos = null;
895
984
  optimisticCursorPrevPos = null;
896
985
  delegateInsertToMonaco = !1;
986
+ editorReadOnly = null;
897
987
  applyingFromNvim = !1;
898
988
  buffers = /* @__PURE__ */ new Map();
899
989
  buffersByName = /* @__PURE__ */ new Map();
900
990
  cursorSyncTimer = null;
991
+ ignoreSelectionSyncUntil = 0;
992
+ ignoreActiveBufLinesEventsUntil = 0;
901
993
  modelContentDisposable = null;
902
994
  originalOptions = null;
903
995
  resyncTimer = null;
904
996
  debugLog(t) {
905
997
  if (this.opts.debug)
906
998
  try {
907
- const e = `[monaco-neovim] ${t}`;
908
- this.opts.debugLog ? this.opts.debugLog(e) : typeof console < "u" && console.debug && console.debug(e);
999
+ const e = `[monaco-neovim-wasm] ${t}`;
1000
+ this.opts.debugLog ? this.opts.debugLog(e) : typeof console < "u" && console.log(e);
909
1001
  } catch {
910
1002
  }
911
1003
  }
1004
+ nowMs() {
1005
+ return typeof performance < "u" && performance.now ? performance.now() : Date.now();
1006
+ }
912
1007
  scheduleRecordingRefresh() {
913
1008
  this.recordingRefreshTimer || (this.recordingRefreshTimer = window.setTimeout(() => {
914
1009
  this.recordingRefreshTimer = null, this.refreshRecordingState();
@@ -926,10 +1021,26 @@ class le {
926
1021
  const e = String(t ?? "");
927
1022
  if (!e) return;
928
1023
  const i = typeof performance < "u" && performance.now ? performance.now() : Date.now();
929
- e.length > 1 && e === this.lastImeCommitText && i - this.lastImeCommitAt < 60 || (this.lastImeCommitText = e, this.lastImeCommitAt = i, this.sendInput(k(e, !0)));
1024
+ e.length > 1 && e === this.lastImeCommitText && i - this.lastImeCommitAt < 60 || (this.lastImeCommitText = e, this.lastImeCommitAt = i, this.sendInput(F(e, !0)));
930
1025
  }
931
1026
  constructor(t, e = {}) {
932
- this.editor = t, this.opts = {
1027
+ this.editor = t;
1028
+ const i = (() => {
1029
+ try {
1030
+ return !!globalThis?.__MONACO_NEOVIM_WASM_DEBUG__;
1031
+ } catch {
1032
+ return !1;
1033
+ }
1034
+ })(), s = (() => {
1035
+ try {
1036
+ if (typeof location > "u" || !location?.search) return !1;
1037
+ const l = new URLSearchParams(location.search);
1038
+ return l.has("monaco-neovim-wasm-debug") || l.has("mnw_debug");
1039
+ } catch {
1040
+ return !1;
1041
+ }
1042
+ })(), r = i || s ? !0 : !!e.debug;
1043
+ this.opts = {
933
1044
  worker: e.worker ?? null,
934
1045
  workerUrl: e.workerUrl ?? new URL("./nvimWorker.js", import.meta.url),
935
1046
  reuseWorker: e.reuseWorker ?? !1,
@@ -938,7 +1049,7 @@ class le {
938
1049
  inputMode: e.inputMode ?? "shared",
939
1050
  env: e.env,
940
1051
  files: e.files,
941
- sharedInputBytes: e.sharedInputBytes ?? N,
1052
+ sharedInputBytes: e.sharedInputBytes ?? L,
942
1053
  cols: e.cols ?? 120,
943
1054
  rows: e.rows ?? 40,
944
1055
  minCols: e.minCols ?? 20,
@@ -964,7 +1075,7 @@ class le {
964
1075
  onHostCommand: e.onHostCommand,
965
1076
  status: e.status ?? (() => {
966
1077
  }),
967
- seedLines: e.seedLines ?? re,
1078
+ seedLines: e.seedLines ?? oe,
968
1079
  seedName: e.seedName ?? "monaco-demo.lua",
969
1080
  seedFiletype: e.seedFiletype ?? "lua",
970
1081
  uiAttach: e.uiAttach ?? !0,
@@ -978,7 +1089,7 @@ class le {
978
1089
  "set noswapfile signcolumn=no number norelativenumber",
979
1090
  "set nowrap laststatus=0 cmdheight=1",
980
1091
  "set shortmess+=F",
981
- "set clipboard=unnamedplus"
1092
+ ...e.clipboard === null ? [] : ["set clipboard=unnamedplus"]
982
1093
  ],
983
1094
  startupLua: e.startupLua ?? "",
984
1095
  visualThemeName: e.visualThemeName ?? "nvim-visual",
@@ -994,11 +1105,11 @@ class le {
994
1105
  onPopupmenu: e.onPopupmenu,
995
1106
  cmdlineContainer: e.cmdlineContainer,
996
1107
  insertSyncDebounceMs: Number.isFinite(e.insertSyncDebounceMs) ? Math.max(0, Number(e.insertSyncDebounceMs)) : 20,
997
- debug: e.debug ?? !1,
1108
+ debug: r,
998
1109
  debugLog: e.debugLog,
999
1110
  shouldHandleKey: e.shouldHandleKey ?? (() => !0),
1000
- translateKey: e.translateKey ?? ce
1001
- }, 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;
1111
+ translateKey: e.translateKey ?? ue
1112
+ }, this.ctrlKeysNormal = this.opts.ctrlKeysForNormalMode ? new Set(this.opts.ctrlKeysForNormalMode.map((l) => String(l).toLowerCase())) : null, this.ctrlKeysInsert = this.opts.ctrlKeysForInsertMode ? new Set(this.opts.ctrlKeysForInsertMode.map((l) => String(l).toLowerCase())) : null, this.altKeysNormal = this.opts.altKeysForNormalMode ? new Set(this.opts.altKeysForNormalMode.map((l) => String(l).toLowerCase())) : null, this.altKeysInsert = this.opts.altKeysForInsertMode ? new Set(this.opts.altKeysForInsertMode.map((l) => String(l).toLowerCase())) : null, this.metaKeysNormal = this.opts.metaKeysForNormalMode ? new Set(this.opts.metaKeysForNormalMode.map((l) => String(l).toLowerCase())) : null, this.metaKeysInsert = this.opts.metaKeysForInsertMode ? new Set(this.opts.metaKeysForInsertMode.map((l) => String(l).toLowerCase())) : null, this.opts.debug && (this.opts.status?.("[monaco-neovim-wasm] debug enabled"), this.debugLog("debug enabled"));
1002
1113
  }
1003
1114
  async start(t) {
1004
1115
  this.stop(!0), this.nextSeedLines = t ?? null;
@@ -1049,7 +1160,7 @@ class le {
1049
1160
  this.opts.status(s, !0);
1050
1161
  }
1051
1162
  };
1052
- if (!this.session || !this.opts.reuseWorker ? this.session = new te({
1163
+ if (!this.session || !this.opts.reuseWorker ? this.session = new ie({
1053
1164
  worker: this.opts.worker,
1054
1165
  workerUrl: this.opts.workerUrl,
1055
1166
  inputMode: this.opts.inputMode,
@@ -1066,12 +1177,12 @@ class le {
1066
1177
  runtimePath: this.opts.runtimePath,
1067
1178
  inputMode: this.opts.inputMode,
1068
1179
  env: this.opts.env,
1069
- files: fe(me(
1180
+ files: pe(me(
1070
1181
  this.opts.files,
1071
1182
  this.opts.wrappedLineMotions || this.opts.scrollMotions || this.opts.hostCommands ? [
1072
- { path: "home/.config/nvim/monaco-neovim-wasm/motion.vim", data: ie },
1073
- { path: "home/.config/nvim/monaco-neovim-wasm/scrolling.vim", data: ne },
1074
- { path: "home/.config/nvim/monaco-neovim-wasm/host-commands.vim", data: se }
1183
+ { path: "home/.config/nvim/monaco-neovim-wasm/motion.vim", data: ne },
1184
+ { path: "home/.config/nvim/monaco-neovim-wasm/scrolling.vim", data: se },
1185
+ { path: "home/.config/nvim/monaco-neovim-wasm/host-commands.vim", data: re }
1075
1186
  ] : []
1076
1187
  ))
1077
1188
  }), this.opts.status("starting..."), this.primeSent = !1, setTimeout(() => {
@@ -1101,7 +1212,7 @@ class le {
1101
1212
  this.sendNotify("nvim_input", [String(t ?? "")]);
1102
1213
  }
1103
1214
  type(t, e = !0) {
1104
- const i = k(String(t ?? ""), e);
1215
+ const i = F(String(t ?? ""), e);
1105
1216
  i && this.sendNotify("nvim_input", [i]);
1106
1217
  }
1107
1218
  paste(t) {
@@ -1114,8 +1225,8 @@ class le {
1114
1225
  const r = await this.rpcCall("nvim_exec_lua", [i, s]);
1115
1226
  return this.execLuaAvailable = !0, r;
1116
1227
  } catch (r) {
1117
- const a = r?.message || String(r);
1118
- if (a.includes("Invalid method") && a.includes("nvim_exec_lua"))
1228
+ const l = r?.message || String(r);
1229
+ if (l.includes("Invalid method") && l.includes("nvim_exec_lua"))
1119
1230
  this.execLuaAvailable = !1;
1120
1231
  else
1121
1232
  throw r;
@@ -1139,7 +1250,7 @@ end)(unpack(_A))`;
1139
1250
  }
1140
1251
  attachEditorListeners() {
1141
1252
  this.disposeEditorListeners();
1142
- const t = g.editor.EditorOption;
1253
+ const t = y.editor.EditorOption;
1143
1254
  try {
1144
1255
  const e = this.editor.getOption(t.fontInfo);
1145
1256
  this.initialCursorWidth = this.editor.getOption(t.cursorWidth) || 0, this.typicalFullWidth = e?.typicalFullwidthCharacterWidth || 2;
@@ -1167,12 +1278,12 @@ end)(unpack(_A))`;
1167
1278
  } catch {
1168
1279
  this.originalOptions = null;
1169
1280
  }
1170
- if (this.editor.updateOptions({ readOnly: !1, domReadOnly: !1 }), this.opts.syncWrap) {
1281
+ if (this.setEditorReadOnly(!this.delegateInsertToMonaco), this.editor.updateOptions({ domReadOnly: !1 }), this.opts.syncWrap) {
1171
1282
  const e = this.uiCols || this.opts.cols;
1172
1283
  e > 0 && this.applyMonacoWrap(e);
1173
1284
  }
1174
1285
  this.attachActiveModelListener(), this.disposables.push(
1175
- this.editor.onDidChangeModel(() => this.attachActiveModelListener()),
1286
+ this.editor.onDidChangeModel(() => this.handleActiveModelChanged()),
1176
1287
  this.editor.onKeyDown((e) => this.handleKey(e)),
1177
1288
  this.editor.onMouseDown((e) => this.handleMouse(e)),
1178
1289
  this.editor.onDidChangeCursorSelection((e) => this.handleSelection(e)),
@@ -1182,11 +1293,23 @@ end)(unpack(_A))`;
1182
1293
  this.compositionActive && this.positionPreedit(), this.scheduleCursorSyncToNvim();
1183
1294
  return;
1184
1295
  }
1185
- if (this.compositionActive) {
1186
- this.positionPreedit();
1187
- return;
1296
+ if (!(this.nowMs() < this.ignoreMonacoCursorSyncToNvimUntil && e.source !== "mouse")) {
1297
+ if (this.compositionActive) {
1298
+ this.positionPreedit();
1299
+ return;
1300
+ }
1301
+ if (!(this.suppressCursorSync || !this.lastCursorPos)) {
1302
+ if (e.source === "mouse") {
1303
+ const s = this.editor.getSelection();
1304
+ s && s.isEmpty() && this.syncCursorToNvimNow(!0);
1305
+ return;
1306
+ }
1307
+ if (e.source === "keyboard") {
1308
+ this.suppressCursorSync = !0, this.editor.setPosition(this.lastCursorPos), this.suppressCursorSync = !1;
1309
+ return;
1310
+ }
1311
+ }
1188
1312
  }
1189
- this.suppressCursorSync || !this.lastCursorPos || e.source === "keyboard" && (this.suppressCursorSync = !0, this.editor.setPosition(this.lastCursorPos), this.suppressCursorSync = !1);
1190
1313
  }),
1191
1314
  this.editor.onDidScrollChange(() => {
1192
1315
  this.compositionActive && this.positionPreedit();
@@ -1195,6 +1318,20 @@ end)(unpack(_A))`;
1195
1318
  this.editor.onDidScrollChange(() => {
1196
1319
  this.opts.searchHighlights && (this.compositionActive || this.scheduleSearchHighlightRefresh());
1197
1320
  })
1321
+ ), this.disposables.push(
1322
+ this.editor.onDidScrollChange(() => {
1323
+ this.visualVirtualActive && this.renderVisualVirtualOverlay();
1324
+ }),
1325
+ this.editor.onDidLayoutChange(() => {
1326
+ this.visualVirtualActive && this.renderVisualVirtualOverlay();
1327
+ }),
1328
+ this.editor.onDidChangeConfiguration((e) => {
1329
+ this.visualVirtualActive && (e.hasChanged(t.fontInfo) || e.hasChanged(t.lineHeight) || e.hasChanged(t.fontSize) || e.hasChanged(t.fontFamily)) && this.renderVisualVirtualOverlay();
1330
+ })
1331
+ ), this.disposables.push(
1332
+ this.editor.onDidChangeConfiguration((e) => {
1333
+ e.hasChanged(t.readOnly) && !this.delegateInsertToMonaco && this.setEditorReadOnly(!0);
1334
+ })
1198
1335
  ), this.opts.autoResize && this.disposables.push(
1199
1336
  this.editor.onDidLayoutChange(() => this.scheduleResizeToEditor()),
1200
1337
  this.editor.onDidChangeConfiguration((e) => {
@@ -1202,10 +1339,32 @@ end)(unpack(_A))`;
1202
1339
  })
1203
1340
  ), this.initCmdlineUi(), this.initTextInputListeners();
1204
1341
  }
1342
+ handleActiveModelChanged() {
1343
+ if (this.attachActiveModelListener(), !this.session || !this.session.isRunning() || !this.bufHandle) return;
1344
+ const t = this.editor.getModel();
1345
+ if (!t) return;
1346
+ try {
1347
+ const i = t.uri, s = i?.scheme ?? "", n = i?.authority ?? "", r = i?.path ?? "";
1348
+ if (s === "nvim" && n === "buf") {
1349
+ const l = /^\/(\d+)$/.exec(r), c = l ? Number(l[1]) : NaN;
1350
+ Number.isFinite(c) && c > 0 && (this.bufHandle = c);
1351
+ }
1352
+ } catch {
1353
+ }
1354
+ const e = this.ensureActiveState();
1355
+ if (e && e.model !== t) {
1356
+ this.debugLog(`active model changed: rebind buf=${this.bufHandle}`), e.model = t, e.createdModel = !1, e.pendingBufEdits = [], e.pendingFullSync = !1, e.pendingCursorSync = !1, e.shadowLines = this.delegateInsertToMonaco ? t.getLinesContent?.() ?? null : null;
1357
+ try {
1358
+ const i = t.getLinesContent?.() ?? t.getValue().split(/\r?\n/);
1359
+ this.sendNotify("nvim_buf_set_lines", [this.bufHandle, 0, -1, !1, i]), this.syncCursorToNvimNow(!0);
1360
+ } catch {
1361
+ }
1362
+ }
1363
+ }
1205
1364
  computeGridSize() {
1206
1365
  try {
1207
- 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), a = Math.max(this.opts.minCols, Math.floor(e / n)), u = Math.max(this.opts.minRows, Math.floor(i / r));
1208
- if (Number.isFinite(a) && Number.isFinite(u) && a > 0 && u > 0) return { cols: a, rows: u };
1366
+ 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(y.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));
1367
+ if (Number.isFinite(l) && Number.isFinite(c) && l > 0 && c > 0) return { cols: l, rows: c };
1209
1368
  } catch {
1210
1369
  }
1211
1370
  return { cols: this.opts.cols, rows: this.opts.rows };
@@ -1228,7 +1387,7 @@ end)(unpack(_A))`;
1228
1387
  const e = document.createElement("div");
1229
1388
  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";
1230
1389
  try {
1231
- const i = this.editor.getOption(g.editor.EditorOption.fontInfo);
1390
+ const i = this.editor.getOption(y.editor.EditorOption.fontInfo);
1232
1391
  i?.fontFamily && (e.style.fontFamily = i.fontFamily), i?.fontSize && (e.style.fontSize = `${i.fontSize}px`), i?.lineHeight && (e.style.lineHeight = `${i.lineHeight}px`);
1233
1392
  } catch {
1234
1393
  }
@@ -1247,11 +1406,11 @@ end)(unpack(_A))`;
1247
1406
  if (!t || this.cmdlineVisible) return;
1248
1407
  const e = this.editor.getModel();
1249
1408
  if (!e) return;
1250
- 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/);
1251
- let a = n.lineNumber, u = n.column;
1252
- r.length <= 1 ? u += Array.from(r[0] ?? "").length : (a += r.length - 1, u = 1 + Array.from(r[r.length - 1] ?? "").length);
1253
- const d = e.validatePosition(new g.Position(a, u)), f = this.editor.getPosition() ?? this.lastCursorPos ?? n;
1254
- this.optimisticCursorPrevPos = f, this.optimisticCursorPos = d, this.optimisticCursorUntil = (typeof performance < "u" && performance.now ? performance.now() : Date.now()) + 120, this.lastCursorPos = d, !this.compositionActive && (this.suppressCursorSync = !0, this.editor.setPosition(d), this.suppressCursorSync = !1);
1409
+ const i = this.editor.getSelection(), n = (i && !i.isEmpty() ? i.getStartPosition() : null) ?? this.editor.getPosition() ?? this.lastCursorPos ?? new y.Position(1, 1), r = String(t).split(/\r?\n/);
1410
+ let l = n.lineNumber, c = n.column;
1411
+ r.length <= 1 ? c += Array.from(r[0] ?? "").length : (l += r.length - 1, c = 1 + Array.from(r[r.length - 1] ?? "").length);
1412
+ const u = e.validatePosition(new y.Position(l, c)), d = this.editor.getPosition() ?? this.lastCursorPos ?? n;
1413
+ this.optimisticCursorPrevPos = d, 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);
1255
1414
  }
1256
1415
  positionPreedit() {
1257
1416
  if (!this.preeditEl || !this.preeditVisible) return;
@@ -1268,7 +1427,7 @@ end)(unpack(_A))`;
1268
1427
  }
1269
1428
  this.modelContentDisposable = null;
1270
1429
  }
1271
- if (this.clearVisualDecorations(), this.clearSearchHighlights(), this.searchStyleEl) {
1430
+ if (this.clearVisualDecorations(), this.clearSearchHighlights(), this.setMonacoHighlightsSuppressed(!1), this.searchStyleEl) {
1272
1431
  try {
1273
1432
  this.searchStyleEl.remove();
1274
1433
  } catch {
@@ -1282,7 +1441,14 @@ end)(unpack(_A))`;
1282
1441
  }
1283
1442
  this.visualStyleEl = null;
1284
1443
  }
1285
- if (this.cmdlineEl) {
1444
+ if (this.visualVirtualOverlayEl) {
1445
+ try {
1446
+ this.visualVirtualOverlayEl.remove();
1447
+ } catch {
1448
+ }
1449
+ this.visualVirtualOverlayEl = null;
1450
+ }
1451
+ if (this.visualVirtualRawRanges = [], this.visualVirtualActive = !1, this.cmdlineEl) {
1286
1452
  try {
1287
1453
  this.cmdlineEl.remove();
1288
1454
  } catch {
@@ -1317,6 +1483,7 @@ end)(unpack(_A))`;
1317
1483
  }
1318
1484
  this.originalOptions = null;
1319
1485
  }
1486
+ this.editorReadOnly = null;
1320
1487
  }
1321
1488
  attachActiveModelListener() {
1322
1489
  if (this.modelContentDisposable) {
@@ -1377,7 +1544,7 @@ end)(unpack(_A))`;
1377
1544
  await this.execLua(this.opts.startupLua, []);
1378
1545
  } catch {
1379
1546
  }
1380
- const t = await this.rpcCall("nvim_get_current_buf", []), e = x(t) ?? 1;
1547
+ const t = await this.rpcCall("nvim_get_current_buf", []), e = N(t) ?? 1;
1381
1548
  if (this.bufHandle = e, await this.rpcCall("nvim_buf_attach", [e, !0, {}]) !== !0) throw new Error("nvim_buf_attach failed");
1382
1549
  this.ensureActiveState(), this.opts.syncTabstop && this.syncTabstopFromMonaco();
1383
1550
  const s = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
@@ -1387,11 +1554,11 @@ end)(unpack(_A))`;
1387
1554
  try {
1388
1555
  const r = this.ensureActiveState();
1389
1556
  if (r) {
1390
- const a = await this.rpcCall("nvim_buf_get_name", [e]);
1391
- r.name = typeof a == "string" ? a : "", r.name && this.buffersByName.set(r.name, e);
1557
+ const l = await this.rpcCall("nvim_buf_get_name", [e]);
1558
+ r.name = typeof l == "string" ? l : "", r.name && this.buffersByName.set(r.name, e);
1392
1559
  try {
1393
- const u = await this.rpcCall("nvim_buf_get_option", [e, "filetype"]);
1394
- r.filetype = typeof u == "string" ? u : "";
1560
+ const c = await this.rpcCall("nvim_buf_get_option", [e, "filetype"]);
1561
+ r.filetype = typeof c == "string" ? c : "";
1395
1562
  } catch {
1396
1563
  }
1397
1564
  }
@@ -1409,11 +1576,19 @@ end)(unpack(_A))`;
1409
1576
  if (i !== null) {
1410
1577
  if (i?.writeText) {
1411
1578
  i.writeText(e).catch(() => {
1579
+ navigator.clipboard?.writeText ? navigator.clipboard.writeText(e).catch(() => {
1580
+ A(e);
1581
+ }) : A(e);
1412
1582
  });
1413
1583
  return;
1414
1584
  }
1415
- navigator.clipboard?.writeText && navigator.clipboard.writeText(e).catch(() => {
1416
- });
1585
+ if (navigator.clipboard?.writeText) {
1586
+ navigator.clipboard.writeText(e).catch(() => {
1587
+ A(e);
1588
+ });
1589
+ return;
1590
+ }
1591
+ A(e);
1417
1592
  }
1418
1593
  }
1419
1594
  handleRequest(t, e, i) {
@@ -1422,22 +1597,22 @@ end)(unpack(_A))`;
1422
1597
  async handleNotify(t, e) {
1423
1598
  if (t === "monaco_cursorMove") {
1424
1599
  const i = e?.[0];
1425
- i && typeof i == "object" && (this.applyMonacoCursorMove(i), this.syncCursorToNvimNow());
1600
+ i && typeof i == "object" && (this.applyMonacoCursorMove(i), this.exitingInsertMode || this.syncCursorToNvimNow(!0));
1426
1601
  return;
1427
1602
  }
1428
1603
  if (t === "monaco_scroll") {
1429
1604
  const i = e?.[0];
1430
- i && typeof i == "object" && this.applyMonacoScroll(i) && this.syncCursorToNvimNow();
1605
+ i && typeof i == "object" && this.applyMonacoScroll(i) && !this.exitingInsertMode && this.syncCursorToNvimNow(!0);
1431
1606
  return;
1432
1607
  }
1433
1608
  if (t === "monaco_reveal") {
1434
1609
  const i = e?.[0];
1435
- i && typeof i == "object" && this.applyMonacoReveal(i) && this.syncCursorToNvimNow();
1610
+ i && typeof i == "object" && this.applyMonacoReveal(i) && !this.exitingInsertMode && this.syncCursorToNvimNow(!0);
1436
1611
  return;
1437
1612
  }
1438
1613
  if (t === "monaco_moveCursor") {
1439
1614
  const i = e?.[0];
1440
- i && typeof i == "object" && (this.applyMonacoMoveCursor(i), this.syncCursorToNvimNow());
1615
+ i && typeof i == "object" && (this.applyMonacoMoveCursor(i), this.exitingInsertMode || this.syncCursorToNvimNow(!0));
1441
1616
  return;
1442
1617
  }
1443
1618
  if (t === "monaco_scrolloff") {
@@ -1461,18 +1636,28 @@ end)(unpack(_A))`;
1461
1636
  return;
1462
1637
  }
1463
1638
  if (t === "monaco_cursor") {
1464
- const [i, s] = e, n = B(this.editor, Number(i), Number(s));
1465
- if (this.delegateInsertToMonaco) {
1466
- const r = this.editor.getModel(), a = r ? r.validatePosition(new g.Position(n.line, n.col)) : new g.Position(n.line, n.col);
1467
- this.lastCursorPos = a;
1639
+ const [i, s] = e;
1640
+ try {
1641
+ const r = this.ignoreInsertExitCursor, l = this.nowMs();
1642
+ if (r && l < r.untilMs && !this.delegateInsertToMonaco && Number(i) === r.line && Number(s) === r.col0 && r.col0 > 0) {
1643
+ this.debugLog(`nvim->monaco cursor: ignore stale post-exit insert cursor ln=${Number(i)} col0=${Number(s)}`);
1644
+ return;
1645
+ }
1646
+ r && l >= r.untilMs && (this.ignoreInsertExitCursor = null);
1647
+ } catch {
1648
+ }
1649
+ const n = K(this.editor, Number(i), Number(s));
1650
+ if (this.debugLog(`nvim->monaco cursor: ln=${Number(i)} col0=${Number(s)} -> line=${n.line} col=${n.col} delegateInsert=${this.delegateInsertToMonaco} exitingInsert=${this.exitingInsertMode} mode=${JSON.stringify(this.lastMode)}`), this.delegateInsertToMonaco && !this.exitingInsertMode) {
1651
+ const r = this.editor.getModel(), l = r ? r.validatePosition(new y.Position(n.line, n.col)) : new y.Position(n.line, n.col);
1652
+ this.lastCursorPos = l;
1468
1653
  return;
1469
1654
  }
1470
- this.updateCursor(n.line, n.col), w(this.lastMode) && this.scheduleVisualSelectionRefresh();
1655
+ this.updateCursor(n.line, n.col), S(this.lastMode) && this.scheduleVisualSelectionRefresh();
1471
1656
  return;
1472
1657
  }
1473
1658
  if (t === "monaco_mode") {
1474
1659
  const i = typeof e?.[0] == "string" ? String(e[0]) : "";
1475
- this.applyNvimMode(i);
1660
+ this.hostAutocmdInstalled || (this.hostAutocmdInstalled = !0), this.debugLog(`nvim->monaco mode: ${JSON.stringify(this.lastMode)} -> ${JSON.stringify(i)}`), this.applyNvimMode(i);
1476
1661
  return;
1477
1662
  }
1478
1663
  if (t === "monaco_recording") {
@@ -1481,45 +1666,49 @@ end)(unpack(_A))`;
1481
1666
  return;
1482
1667
  }
1483
1668
  if (t === "nvim_buf_lines_event") {
1484
- const [i, s, n, r, a] = e, u = x(i);
1485
- if (!u) return;
1486
- const d = this.bufHandle != null && u === this.bufHandle ? this.ensureActiveState() : this.buffers.get(u) ?? null;
1487
- if (!d) return;
1488
- if (this.bufHandle != null && u === this.bufHandle && this.compositionActive) {
1669
+ const [i, s, n, r, l] = e, c = N(i);
1670
+ if (!c) return;
1671
+ const u = this.bufHandle != null && c === this.bufHandle ? this.ensureActiveState() : this.buffers.get(c) ?? null;
1672
+ if (!u || this.bufHandle != null && c === this.bufHandle && this.delegateInsertToMonaco && !this.exitingInsertMode && this.nowMs() < this.ignoreActiveBufLinesEventsUntil)
1673
+ return;
1674
+ if (this.bufHandle != null && c === this.bufHandle && this.compositionActive) {
1489
1675
  this.pendingResyncAfterComposition = !0;
1490
1676
  return;
1491
1677
  }
1492
- const f = d.model, l = Number(n), c = Number(r), h = Array.isArray(a) ? a.map((p) => String(p ?? "")) : null;
1493
- if (f && Number.isInteger(l) && Number.isInteger(c) && l >= 0 && c >= l && h)
1678
+ const d = u.model, f = Number(n), p = Number(r), m = Array.isArray(l) ? l.map((h) => String(h ?? "")) : null;
1679
+ if (d && Number.isInteger(f) && Number.isInteger(p) && f >= 0 && p >= f && m)
1494
1680
  try {
1495
- const p = this.bufHandle != null && u === this.bufHandle && this.editor.getModel() === f;
1496
- if (p && this.delegateInsertToMonaco) {
1497
- const y = this.computeLinePatch(f, l, c, h);
1681
+ const h = this.bufHandle != null && c === this.bufHandle && this.editor.getModel() === d;
1682
+ if (h && this.delegateInsertToMonaco) {
1683
+ const g = this.computeLinePatch(d, f, p, m);
1498
1684
  let b = !1;
1499
1685
  try {
1500
- b = f.getValueInRange(y.range) === y.text;
1686
+ b = d.getValueInRange(g.range) === g.text;
1501
1687
  } catch {
1502
1688
  }
1503
1689
  if (!b) {
1504
- if (d.pendingFullSync || d.pendingBufEdits.length) return;
1505
- this.applyLinePatchToModel(f, l, c, h);
1690
+ if (u.pendingFullSync || u.pendingBufEdits.length) {
1691
+ this.scheduleResync();
1692
+ return;
1693
+ }
1694
+ this.applyLinePatchToModel(d, f, p, m);
1506
1695
  try {
1507
- d.shadowLines = f.getLinesContent();
1696
+ u.shadowLines = d.getLinesContent();
1508
1697
  } catch {
1509
1698
  }
1510
1699
  }
1511
- } else p ? this.applyLinePatch(f, l, c, h) : this.applyLinePatchToModel(f, l, c, h);
1700
+ } else h ? this.applyLinePatch(d, f, p, m) : this.applyLinePatchToModel(d, f, p, m);
1512
1701
  } catch {
1513
1702
  }
1514
1703
  else
1515
1704
  try {
1516
- const p = await this.rpcCall("nvim_buf_get_lines", [u, 0, -1, !1]), y = Array.isArray(p) ? p : [""];
1517
- this.bufHandle != null && u === this.bufHandle && this.editor.getModel() === f ? this.applyBuffer(y) : this.setModelText(f, y);
1705
+ const h = await this.rpcCall("nvim_buf_get_lines", [c, 0, -1, !1]), g = Array.isArray(h) ? h : [""];
1706
+ this.bufHandle != null && c === this.bufHandle && this.editor.getModel() === d ? this.applyBuffer(g) : this.setModelText(d, g);
1518
1707
  } catch {
1519
1708
  }
1520
- this.bufHandle != null && u === this.bufHandle && w(this.lastMode) && this.scheduleVisualSelectionRefresh();
1709
+ this.bufHandle != null && c === this.bufHandle && S(this.lastMode) && this.scheduleVisualSelectionRefresh();
1521
1710
  } else if (t === "nvim_buf_detach_event") {
1522
- const i = x(e?.[0]);
1711
+ const i = N(e?.[0]);
1523
1712
  if (i && this.buffers.has(i)) {
1524
1713
  const s = this.buffers.get(i);
1525
1714
  if (s.name && this.buffersByName.delete(s.name), s.createdModel)
@@ -1533,24 +1722,24 @@ end)(unpack(_A))`;
1533
1722
  } else t === "redraw" && this.handleRedraw(e);
1534
1723
  }
1535
1724
  computeLinePatch(t, e, i, s) {
1536
- const n = t.getLineCount(), r = Math.min(e, n), a = Math.min(i, n), u = new g.Position(n, t.getLineMaxColumn(n)), f = a >= n && s.length === 0 && r > 0 ? new g.Position(r, t.getLineMaxColumn(r)) : r < n ? new g.Position(r + 1, 1) : u, l = a < n ? new g.Position(a + 1, 1) : u;
1537
- let c = s.join(`
1725
+ const n = t.getLineCount(), r = Math.min(e, n), l = Math.min(i, n), c = new y.Position(n, t.getLineMaxColumn(n)), d = l >= n && s.length === 0 && r > 0 ? new y.Position(r, t.getLineMaxColumn(r)) : r < n ? new y.Position(r + 1, 1) : c, f = l < n ? new y.Position(l + 1, 1) : c;
1726
+ let p = s.join(`
1538
1727
  `);
1539
- return e >= n && i >= n && s.length > 0 && (c = `
1540
- ${c}`), i < n && s.length > 0 && (c += `
1541
- `), { range: new g.Range(f.lineNumber, f.column, l.lineNumber, l.column), text: c };
1728
+ return e >= n && i >= n && s.length > 0 && (p = `
1729
+ ${p}`), i < n && s.length > 0 && (p += `
1730
+ `), { range: new y.Range(d.lineNumber, d.column, f.lineNumber, f.column), text: p };
1542
1731
  }
1543
1732
  applyLinePatch(t, e, i, s) {
1544
- const n = this.lastCursorPos ?? this.editor.getPosition() ?? new g.Position(1, 1), r = this.computeLinePatch(t, e, i, s);
1733
+ const n = this.lastCursorPos ?? this.editor.getPosition() ?? new y.Position(1, 1), r = this.computeLinePatch(t, e, i, s);
1545
1734
  try {
1546
1735
  if (t.getValueInRange(r.range) === r.text) return;
1547
1736
  } catch {
1548
1737
  }
1549
1738
  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) {
1550
- const a = this.getActiveState();
1551
- if (a && a.model === t)
1739
+ const l = this.getActiveState();
1740
+ if (l && l.model === t)
1552
1741
  try {
1553
- a.shadowLines = t.getLinesContent();
1742
+ l.shadowLines = t.getLinesContent();
1554
1743
  } catch {
1555
1744
  }
1556
1745
  }
@@ -1624,7 +1813,7 @@ ${c}`), i < n && s.length > 0 && (c += `
1624
1813
  this.cmdlineEl.textContent = this.cmdlineTextRaw, this.cmdlineCursorByte = null;
1625
1814
  return;
1626
1815
  }
1627
- const e = this.cmdlineTextRaw, i = Math.max(0, Math.min(Number(t) || 0, A(e))), s = Math.max(0, Math.min(e.length, S(e, i)));
1816
+ const e = this.cmdlineTextRaw, i = Math.max(0, Math.min(Number(t) || 0, B(e))), s = Math.max(0, Math.min(e.length, I(e, i)));
1628
1817
  this.cmdlineCursorByte = i, this.cmdlineEl.textContent = `${e.slice(0, s)}▏${e.slice(s)}`;
1629
1818
  }
1630
1819
  setMessage(t) {
@@ -1660,8 +1849,8 @@ ${c}`), i < n && s.length > 0 && (c += `
1660
1849
  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";
1661
1850
  const r = document.createElement("span");
1662
1851
  r.textContent = s.word ?? "";
1663
- const a = document.createElement("span");
1664
- a.style.opacity = "0.7", a.textContent = s.menu ?? s.kind ?? "", n.appendChild(r), n.appendChild(a), this.popupEl.appendChild(n);
1852
+ const l = document.createElement("span");
1853
+ l.style.opacity = "0.7", l.textContent = s.menu ?? s.kind ?? "", n.appendChild(r), n.appendChild(l), this.popupEl.appendChild(n);
1665
1854
  }
1666
1855
  this.popupEl.style.display = "block";
1667
1856
  }
@@ -1689,10 +1878,10 @@ ${c}`), i < n && s.length > 0 && (c += `
1689
1878
  } catch {
1690
1879
  }
1691
1880
  if (i === "wrappedLine" && (e === "down" || e === "up")) {
1692
- const a = e === "down" ? "cursorDown" : "cursorUp";
1693
- for (let u = 0; u < s; u += 1)
1881
+ const l = e === "down" ? "cursorDown" : "cursorUp";
1882
+ for (let c = 0; c < s; c += 1)
1694
1883
  try {
1695
- this.editor.trigger("monaco-neovim-wasm", a, null);
1884
+ this.editor.trigger("monaco-neovim-wasm", l, null);
1696
1885
  } catch {
1697
1886
  }
1698
1887
  return !0;
@@ -1712,90 +1901,90 @@ ${c}`), i < n && s.length > 0 && (c += `
1712
1901
  if (e <= 0) return !1;
1713
1902
  const i = t ?? this.editor.getPosition();
1714
1903
  if (!i) return !1;
1715
- const s = Math.max(1, Number(this.editor.getOption(g.editor.EditorOption.lineHeight)) || 0), n = e * s;
1904
+ const s = Math.max(1, Number(this.editor.getOption(y.editor.EditorOption.lineHeight)) || 0), n = e * s;
1716
1905
  if (n <= 0) return !1;
1717
1906
  let r = 0;
1718
1907
  try {
1719
- const m = this.editor.getLayoutInfo();
1720
- r = Math.max(0, Number(m?.contentHeight ?? m?.height ?? 0) || 0);
1908
+ const a = this.editor.getLayoutInfo();
1909
+ r = Math.max(0, Number(a?.contentHeight ?? a?.height ?? 0) || 0);
1721
1910
  } catch {
1722
1911
  }
1723
1912
  if (r <= 0) return !1;
1724
- const a = () => {
1913
+ const l = () => {
1725
1914
  try {
1726
1915
  this.editor.revealPositionInCenterIfOutsideViewport(i);
1727
1916
  } catch {
1728
1917
  }
1729
1918
  };
1730
- let u = null;
1919
+ let c = null;
1731
1920
  try {
1732
- u = this.editor.getScrolledVisiblePosition(i);
1921
+ c = this.editor.getScrolledVisiblePosition(i);
1733
1922
  } catch {
1734
1923
  }
1735
- if (!u) {
1736
- a();
1924
+ if (!c) {
1925
+ l();
1737
1926
  try {
1738
- u = this.editor.getScrolledVisiblePosition(i);
1927
+ c = this.editor.getScrolledVisiblePosition(i);
1739
1928
  } catch {
1740
1929
  }
1741
1930
  }
1742
- if (!u || !Number.isFinite(u.top) || !Number.isFinite(u.height)) return !1;
1743
- const d = this.editor.getScrollTop(), f = u.top, l = u.top + u.height, c = Math.max(0, r - n);
1744
- let h = 0;
1745
- if (f < n ? h = f - n : l > c && (h = l - c), !h) return !1;
1931
+ if (!c || !Number.isFinite(c.top) || !Number.isFinite(c.height)) return !1;
1932
+ const u = this.editor.getScrollTop(), d = c.top, f = c.top + c.height, p = Math.max(0, r - n);
1933
+ let m = 0;
1934
+ if (d < n ? m = d - n : f > p && (m = f - p), !m) return !1;
1746
1935
  try {
1747
- this.editor.setScrollTop(Math.max(0, d + h));
1936
+ this.editor.setScrollTop(Math.max(0, u + m));
1748
1937
  } catch {
1749
1938
  }
1750
1939
  return !0;
1751
1940
  }
1752
1941
  applyMonacoScroll(t) {
1753
- 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", a = Math.max(3, this.uiRows || this.opts.rows), u = this.editor.getOption(g.editor.EditorOption.fontInfo), d = Math.max(1, Number(u?.lineHeight ?? 0) || 0), f = Math.max(1, a - 2), l = Math.max(1, Math.floor(f / 2));
1754
- let c = s;
1755
- if (e === "page") c = f * s;
1756
- else if (e === "halfPage") c = l * s;
1757
- else if (e === "line") c = s;
1942
+ 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(y.editor.EditorOption.fontInfo), u = Math.max(1, Number(c?.lineHeight ?? 0) || 0), d = Math.max(1, l - 2), f = Math.max(1, Math.floor(d / 2));
1943
+ let p = s;
1944
+ if (e === "page") p = d * s;
1945
+ else if (e === "halfPage") p = f * s;
1946
+ else if (e === "line") p = s;
1758
1947
  else return !1;
1759
- const h = i === "up" ? -1 : i === "down" ? 1 : 0;
1760
- if (!h) return !1;
1948
+ const m = i === "up" ? -1 : i === "down" ? 1 : 0;
1949
+ if (!m) return !1;
1761
1950
  this.suppressCursorSync = !0;
1762
1951
  try {
1763
- const p = this.editor.getScrollTop(), y = Math.max(0, p + h * c * d);
1764
- this.editor.setScrollTop(y);
1952
+ const h = this.editor.getScrollTop(), g = Math.max(0, h + m * p * u);
1953
+ this.editor.setScrollTop(g);
1765
1954
  } catch {
1766
1955
  }
1767
- let m = !1;
1956
+ let a = !1;
1768
1957
  if (n)
1769
1958
  try {
1770
1959
  this.applyMonacoCursorMove({
1771
- to: h > 0 ? "down" : "up",
1960
+ to: m > 0 ? "down" : "up",
1772
1961
  by: r,
1773
- value: c
1774
- }), m = !0;
1962
+ value: p
1963
+ }), a = !0;
1775
1964
  } catch {
1776
1965
  }
1777
- return this.suppressCursorSync = !1, m;
1966
+ return this.suppressCursorSync = !1, a;
1778
1967
  }
1779
1968
  applyMonacoReveal(t) {
1780
1969
  const e = typeof t.direction == "string" ? t.direction : "", i = !!t.resetCursor, s = this.editor.getPosition();
1781
1970
  if (!s) return !1;
1782
- const n = s.lineNumber, r = Math.max(1, this.uiRows || this.opts.rows), a = this.editor.getOption(g.editor.EditorOption.fontInfo), u = Math.max(1, Number(a?.lineHeight ?? 0) || 0);
1971
+ const n = s.lineNumber, r = Math.max(1, this.uiRows || this.opts.rows), l = this.editor.getOption(y.editor.EditorOption.fontInfo), c = Math.max(1, Number(l?.lineHeight ?? 0) || 0);
1783
1972
  this.suppressCursorSync = !0;
1784
1973
  try {
1785
1974
  if (e === "top") {
1786
- const d = this.editor.getTopForLineNumber(n);
1787
- this.editor.setScrollTop(d);
1975
+ const u = this.editor.getTopForLineNumber(n);
1976
+ this.editor.setScrollTop(u);
1788
1977
  } else if (e === "center")
1789
1978
  this.editor.revealLineInCenter(n);
1790
1979
  else if (e === "bottom") {
1791
- const d = this.editor.getTopForLineNumber(n), f = Math.max(0, d - (r - 1) * u);
1792
- this.editor.setScrollTop(f);
1980
+ const u = this.editor.getTopForLineNumber(n), d = Math.max(0, u - (r - 1) * c);
1981
+ this.editor.setScrollTop(d);
1793
1982
  }
1794
1983
  if (i) {
1795
- const d = this.editor.getModel();
1796
- if (d) {
1797
- const f = d.getLineContent(n) ?? "", l = /\S/.exec(f), c = l ? l.index + 1 : 1, h = d.validatePosition(new g.Position(n, c));
1798
- this.editor.setPosition(h), this.lastCursorPos = h;
1984
+ const u = this.editor.getModel();
1985
+ if (u) {
1986
+ const d = u.getLineContent(n) ?? "", f = /\S/.exec(d), p = f ? f.index + 1 : 1, m = u.validatePosition(new y.Position(n, p));
1987
+ this.editor.setPosition(m), this.lastCursorPos = m;
1799
1988
  }
1800
1989
  }
1801
1990
  } catch {
@@ -1810,28 +1999,28 @@ ${c}`), i < n && s.length > 0 && (c += `
1810
1999
  const s = Math.max(1, Number(t.value ?? 1) || 1);
1811
2000
  let n = 1, r = i.getLineCount();
1812
2001
  try {
1813
- const c = this.editor.getVisibleRanges();
1814
- c && c.length && (n = Math.min(...c.map((h) => h.startLineNumber)), r = Math.max(...c.map((h) => h.endLineNumber)));
2002
+ const p = this.editor.getVisibleRanges();
2003
+ p && p.length && (n = Math.min(...p.map((m) => m.startLineNumber)), r = Math.max(...p.map((m) => m.endLineNumber)));
1815
2004
  } catch {
1816
2005
  }
1817
2006
  n = Math.max(1, Math.min(n, i.getLineCount())), r = Math.max(1, Math.min(r, i.getLineCount())), r < n && (r = n);
1818
- let a = n;
2007
+ let l = n;
1819
2008
  if (e === "top")
1820
- a = n + (s - 1);
2009
+ l = n + (s - 1);
1821
2010
  else if (e === "middle")
1822
- a = Math.floor((n + r) / 2);
2011
+ l = Math.floor((n + r) / 2);
1823
2012
  else if (e === "bottom")
1824
- a = r - (s - 1);
2013
+ l = r - (s - 1);
1825
2014
  else
1826
2015
  return;
1827
- a = Math.max(n, Math.min(r, a));
1828
- const u = i.getLineContent(a) ?? "", d = /\S/.exec(u), f = d ? d.index + 1 : 1, l = i.validatePosition(new g.Position(a, f));
2016
+ l = Math.max(n, Math.min(r, l));
2017
+ const c = i.getLineContent(l) ?? "", u = /\S/.exec(c), d = u ? u.index + 1 : 1, f = i.validatePosition(new y.Position(l, d));
1829
2018
  this.suppressCursorSync = !0;
1830
2019
  try {
1831
- this.editor.setPosition(l);
2020
+ this.editor.setPosition(f);
1832
2021
  } catch {
1833
2022
  }
1834
- this.lastCursorPos = l, this.suppressCursorSync = !1;
2023
+ this.lastCursorPos = f, this.suppressCursorSync = !1;
1835
2024
  }
1836
2025
  syncTabstopFromMonaco() {
1837
2026
  const t = this.editor.getModel();
@@ -1865,17 +2054,17 @@ ${c}`), i < n && s.length > 0 && (c += `
1865
2054
  continue;
1866
2055
  }
1867
2056
  if (i === "cmdline_show") {
1868
- const r = n[0], a = Math.max(0, Number(n[1] ?? 0) || 0), u = typeof n[2] == "string" ? n[2] : "", d = typeof n[3] == "string" ? n[3] : "", f = Math.max(0, Number(n[4] ?? 0) || 0), l = d || u || "", c = " ".repeat(f), h = T(r), m = `${c}${l}${h}`;
1869
- this.setCmdline(m);
1870
- const p = A(`${c}${l}`), y = A(h);
1871
- this.cmdlineCursorOffsetBytes = p, this.cmdlineCursorContentBytes = y;
1872
- const b = a <= y ? this.cmdlineCursorOffsetBytes + a : a;
2057
+ const r = n[0], l = Math.max(0, Number(n[1] ?? 0) || 0), c = typeof n[2] == "string" ? n[2] : "", u = typeof n[3] == "string" ? n[3] : "", d = Math.max(0, Number(n[4] ?? 0) || 0), f = u || c || "", p = " ".repeat(d), m = k(r), a = `${p}${f}${m}`;
2058
+ this.setCmdline(a);
2059
+ const h = B(`${p}${f}`), g = B(m);
2060
+ this.cmdlineCursorOffsetBytes = h, this.cmdlineCursorContentBytes = g;
2061
+ const b = l <= g ? this.cmdlineCursorOffsetBytes + l : l;
1873
2062
  this.setCmdlineCursor(b), this.scheduleSearchHighlightRefresh();
1874
2063
  continue;
1875
2064
  }
1876
2065
  if (i === "cmdline_pos") {
1877
- const r = Math.max(0, Number(n[0] ?? 0) || 0), a = this.cmdlineCursorOffsetBytes > 0 && r <= this.cmdlineCursorContentBytes ? this.cmdlineCursorOffsetBytes + r : r;
1878
- this.setCmdlineCursor(a), this.scheduleSearchHighlightRefresh();
2066
+ const r = Math.max(0, Number(n[0] ?? 0) || 0), l = this.cmdlineCursorOffsetBytes > 0 && r <= this.cmdlineCursorContentBytes ? this.cmdlineCursorOffsetBytes + r : r;
2067
+ this.setCmdlineCursor(l), this.scheduleSearchHighlightRefresh();
1879
2068
  continue;
1880
2069
  }
1881
2070
  if (i === "msg_clear") {
@@ -1883,26 +2072,26 @@ ${c}`), i < n && s.length > 0 && (c += `
1883
2072
  continue;
1884
2073
  }
1885
2074
  if (i === "msg_show") {
1886
- const r = typeof n[0] == "string" ? n[0] : "", a = n[1], u = !!n[2], d = !!n[4], f = T(a);
1887
- if (r === "empty" && !f) {
2075
+ const r = typeof n[0] == "string" ? n[0] : "", l = n[1], c = !!n[2], u = !!n[4], d = k(l);
2076
+ if (r === "empty" && !d) {
1888
2077
  this.setMessage(null);
1889
2078
  continue;
1890
2079
  }
1891
- if (!f) continue;
1892
- if (d && this.messageEl?.textContent) {
1893
- this.setMessage(`${this.messageEl.textContent}${f}`);
2080
+ if (!d) continue;
2081
+ if (u && this.messageEl?.textContent) {
2082
+ this.setMessage(`${this.messageEl.textContent}${d}`);
1894
2083
  continue;
1895
2084
  }
1896
- if (u) {
1897
- this.setMessage(f);
2085
+ if (c) {
2086
+ this.setMessage(d);
1898
2087
  continue;
1899
2088
  }
1900
- this.setMessage(f);
2089
+ this.setMessage(d);
1901
2090
  continue;
1902
2091
  }
1903
2092
  if (i === "msg_showmode" || i === "msg_showcmd" || i === "msg_ruler") {
1904
- const r = n[0], a = T(r);
1905
- this.setMessage(a || null);
2093
+ const r = n[0], l = k(r);
2094
+ this.setMessage(l || null);
1906
2095
  continue;
1907
2096
  }
1908
2097
  if (i === "popupmenu_hide") {
@@ -1910,8 +2099,8 @@ ${c}`), i < n && s.length > 0 && (c += `
1910
2099
  continue;
1911
2100
  }
1912
2101
  if (i === "popupmenu_show") {
1913
- const r = n[0], a = Number(n[1] ?? -1), u = be(r);
1914
- this.setPopupmenu(u, Number.isFinite(a) ? a : -1);
2102
+ const r = n[0], l = Number(n[1] ?? -1), c = _e(r);
2103
+ this.setPopupmenu(c, Number.isFinite(l) ? l : -1);
1915
2104
  continue;
1916
2105
  }
1917
2106
  if (i === "popupmenu_select") {
@@ -1921,7 +2110,7 @@ ${c}`), i < n && s.length > 0 && (c += `
1921
2110
  }
1922
2111
  if (i === "mode_change") {
1923
2112
  const r = typeof n[0] == "string" ? n[0] : "";
1924
- this.applyNvimMode(r);
2113
+ this.hostAutocmdInstalled || this.applyNvimMode(r);
1925
2114
  continue;
1926
2115
  }
1927
2116
  }
@@ -1929,142 +2118,176 @@ ${c}`), i < n && s.length > 0 && (c += `
1929
2118
  initTextInputListeners() {
1930
2119
  const t = this.editor.getDomNode();
1931
2120
  if (!t) return;
1932
- const e = (l) => {
2121
+ const e = t.ownerDocument || document, i = e.defaultView || window, s = (a) => {
2122
+ try {
2123
+ a.stopImmediatePropagation?.();
2124
+ } catch {
2125
+ }
1933
2126
  try {
1934
- l.stopImmediatePropagation?.();
2127
+ a.stopPropagation();
1935
2128
  } catch {
1936
2129
  }
2130
+ }, n = (a) => {
1937
2131
  try {
1938
- l.stopPropagation();
2132
+ if (typeof this.editor.hasTextFocus == "function" ? !!this.editor.hasTextFocus() : !1) return !0;
2133
+ const g = a.target;
2134
+ if (g && t.contains(g)) return !0;
2135
+ const b = e.activeElement;
2136
+ return !!(b && t.contains(b));
1939
2137
  } catch {
2138
+ return !1;
1940
2139
  }
1941
- }, i = (l) => {
2140
+ }, r = (a) => {
1942
2141
  try {
1943
- const c = l;
1944
- if (!c || typeof c.tagName != "string") return null;
1945
- const h = c.tagName;
1946
- return h === "TEXTAREA" ? c.classList?.contains?.("inputarea") || c.classList?.contains?.("ime-text-area") ? c : null : h === "DIV" && c.classList?.contains?.("native-edit-context") ? c : null;
2142
+ const h = a;
2143
+ return !h || typeof h.tagName != "string" ? null : h;
1947
2144
  } catch {
1948
2145
  return null;
1949
2146
  }
1950
- }, s = (l) => {
1951
- if (this.compositionActive || l.isComposing || l.getModifierState?.("AltGraph")) return;
1952
- const c = this.delegateInsertToMonaco && !this.exitingInsertMode;
1953
- if (!c && (l.key === "Backspace" || l.key === "Delete" || l.key === "Escape")) {
1954
- if (!this.opts.shouldHandleKey(l)) return;
1955
- const p = this.opts.translateKey(l);
1956
- if (!p) return;
1957
- e(l);
2147
+ }, l = (a) => {
2148
+ if (!n(a) || this.compositionActive || a.isComposing || a.getModifierState?.("AltGraph")) return;
2149
+ const h = this.delegateInsertToMonaco && !this.exitingInsertMode;
2150
+ if (h) {
2151
+ if (a.key === "Escape") {
2152
+ if (!this.opts.shouldHandleKey(a)) return;
2153
+ s(a);
2154
+ try {
2155
+ a.preventDefault();
2156
+ } catch {
2157
+ }
2158
+ if (this.compositionActive || a.isComposing) {
2159
+ this.pendingEscAfterComposition = !0;
2160
+ return;
2161
+ }
2162
+ this.exitDelegatedInsertMode("<Esc>");
2163
+ return;
2164
+ }
2165
+ const _ = a.key;
2166
+ (_ === "ArrowLeft" || _ === "ArrowRight" || _ === "ArrowUp" || _ === "ArrowDown" || _ === "Home" || _ === "End" || _ === "PageUp" || _ === "PageDown" || _ === "Tab" || _ === "Enter") && (this.delegatedInsertReplayPossible = !1);
2167
+ }
2168
+ if (!h && (a.key === "Backspace" || a.key === "Delete" || a.key === "Escape")) {
2169
+ if (!this.opts.shouldHandleKey(a)) return;
2170
+ const _ = this.opts.translateKey(a);
2171
+ if (!_) return;
2172
+ s(a);
1958
2173
  try {
1959
- l.preventDefault();
2174
+ a.preventDefault();
1960
2175
  } catch {
1961
2176
  }
1962
- this.exitingInsertMode ? this.pendingKeysAfterExit += p : this.sendInput(p);
2177
+ if (this.exitingInsertMode && _ === "<Esc>") return;
2178
+ this.exitingInsertMode ? this.pendingKeysAfterExit += _ : this.sendInput(_);
1963
2179
  return;
1964
2180
  }
1965
- if (v(this.lastMode) && !this.delegateInsertToMonaco && !this.exitingInsertMode && !l.ctrlKey && !l.metaKey && typeof l.key == "string" && l.key.length === 1) {
1966
- const p = /^[\x20-\x7E]$/.test(l.key);
1967
- if (!!!(l.altKey && p)) {
1968
- if (!this.opts.shouldHandleKey(l)) return;
1969
- e(l);
2181
+ if (M(this.lastMode) && !this.delegateInsertToMonaco && !this.exitingInsertMode && !a.ctrlKey && !a.metaKey && typeof a.key == "string" && a.key.length === 1) {
2182
+ const _ = /^[\x20-\x7E]$/.test(a.key);
2183
+ if (!!!(a.altKey && _)) {
2184
+ if (!this.opts.shouldHandleKey(a)) return;
2185
+ s(a);
1970
2186
  return;
1971
2187
  }
1972
2188
  }
1973
- if (!l.ctrlKey && !l.altKey && !l.metaKey) return;
1974
- const h = this.modifiedKeyName(l);
1975
- if (!h || !this.opts.shouldHandleKey(l)) return;
1976
- if (this.hasExplicitModAllowlist(c)) {
1977
- if (!this.shouldForwardModifiedKeys(l, c)) return;
1978
- } else if (c || !l.ctrlKey || l.altKey || l.metaKey || h !== "f" && h !== "b" && h !== "d" && h !== "u" && h !== "e" && h !== "y" && h !== "v") return;
1979
- const m = this.opts.translateKey(l);
1980
- if (m) {
1981
- e(l);
2189
+ if (!a.ctrlKey && !a.altKey && !a.metaKey) return;
2190
+ const g = this.modifiedKeyName(a);
2191
+ if (!g || !this.opts.shouldHandleKey(a)) return;
2192
+ if (this.hasExplicitModAllowlist(h)) {
2193
+ if (!this.shouldForwardModifiedKeys(a, h)) return;
2194
+ } else if (h || !a.ctrlKey || a.altKey || a.metaKey || g !== "f" && g !== "b" && g !== "d" && g !== "u" && g !== "e" && g !== "y" && g !== "v") return;
2195
+ const b = this.opts.translateKey(a);
2196
+ if (b) {
2197
+ s(a);
1982
2198
  try {
1983
- l.preventDefault();
2199
+ a.preventDefault();
1984
2200
  } catch {
1985
2201
  }
1986
- this.exitingInsertMode ? (this.debugLog(`keydown(capture) buffer: key=${JSON.stringify(l.key)} code=${JSON.stringify(l.code)} mods=${l.ctrlKey ? "C" : ""}${l.altKey ? "A" : ""}${l.metaKey ? "D" : ""}${l.shiftKey ? "S" : ""} -> ${m}`), this.pendingKeysAfterExit += m) : (c && this.flushPendingMonacoSync(), this.debugLog(`keydown(capture) send: key=${JSON.stringify(l.key)} code=${JSON.stringify(l.code)} mods=${l.ctrlKey ? "C" : ""}${l.altKey ? "A" : ""}${l.metaKey ? "D" : ""}${l.shiftKey ? "S" : ""} -> ${m}`), this.sendInput(m));
2202
+ 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" : ""} -> ${b}`), this.pendingKeysAfterExit += b) : h && (b === "<C-[>" || b === "<C-c>") ? (this.debugLog(`keydown(capture) exit insert: key=${JSON.stringify(a.key)} code=${JSON.stringify(a.code)} mods=${a.ctrlKey ? "C" : ""}${a.altKey ? "A" : ""}${a.metaKey ? "D" : ""}${a.shiftKey ? "S" : ""} -> ${b}`), this.exitDelegatedInsertMode(b)) : (h && 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" : ""} -> ${b}`), this.sendInput(b));
1987
2203
  }
1988
- }, n = () => {
1989
- if (this.compositionActive = !0, this.debugLog(`compositionstart delegateInsert=${this.delegateInsertToMonaco} mode=${JSON.stringify(this.lastMode)}`), this.delegateInsertToMonaco) {
1990
- this.setPreedit(null);
1991
- return;
2204
+ }, c = (a) => {
2205
+ if (n(a)) {
2206
+ if (this.compositionActive = !0, this.debugLog(`compositionstart delegateInsert=${this.delegateInsertToMonaco} mode=${JSON.stringify(this.lastMode)}`), this.delegateInsertToMonaco) {
2207
+ this.setPreedit(null);
2208
+ return;
2209
+ }
2210
+ this.setPreedit("");
1992
2211
  }
1993
- this.setPreedit("");
1994
- }, r = (l) => {
1995
- if (this.compositionActive = !1, this.setPreedit(null), this.debugLog(`compositionend delegateInsert=${this.delegateInsertToMonaco} mode=${JSON.stringify(this.lastMode)} data=${JSON.stringify(l.data ?? "")}`), this.delegateInsertToMonaco) {
1996
- this.scheduleCursorSyncToNvim();
2212
+ }, u = (a) => {
2213
+ if (!n(a)) return;
2214
+ 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) {
2215
+ if (this.pendingEscAfterComposition) {
2216
+ this.pendingEscAfterComposition = !1, this.exitDelegatedInsertMode("<Esc>");
2217
+ return;
2218
+ }
2219
+ this.exitingInsertMode || this.scheduleCursorSyncToNvim();
1997
2220
  return;
1998
2221
  }
1999
- const c = i(l.target);
2000
- if (v(this.lastMode)) {
2001
- const h = typeof l.data == "string" ? l.data : "", m = !h && c && c.tagName === "TEXTAREA" && c.value ? String(c.value) : !h && c?.textContent ? String(c.textContent) : "", p = h || m;
2002
- p && this.sendCmdlineImeText(p);
2222
+ const h = r(a.target);
2223
+ if (M(this.lastMode)) {
2224
+ const g = typeof a.data == "string" ? a.data : "", b = !g && h && h.tagName === "TEXTAREA" && h.value ? String(h.value) : !g && h?.textContent ? String(h.textContent) : "", _ = g || b;
2225
+ _ && this.sendCmdlineImeText(_);
2003
2226
  }
2004
2227
  try {
2005
- c && c.tagName === "TEXTAREA" ? c.value = "" : c && (c.textContent = "");
2228
+ h && h.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2006
2229
  } catch {
2007
2230
  }
2008
2231
  this.pendingResyncAfterComposition && (this.pendingResyncAfterComposition = !1, this.scheduleResync());
2009
- }, a = (l) => {
2010
- if (this.compositionActive || (this.compositionActive = !0), this.delegateInsertToMonaco) return;
2011
- const c = i(l.target), h = typeof l.data == "string" ? l.data : c && c.tagName === "TEXTAREA" && c.value ? String(c.value) : c?.textContent ? String(c.textContent) : "";
2012
- this.setPreedit(h || "");
2013
- }, u = (l) => {
2014
- if (i(l.target) && !(this.delegateInsertToMonaco && !this.exitingInsertMode)) {
2015
- e(l);
2232
+ }, d = (a) => {
2233
+ if (!n(a) || (this.compositionActive || (this.compositionActive = !0), this.delegateInsertToMonaco)) return;
2234
+ const h = r(a.target), g = typeof a.data == "string" ? a.data : h && h.tagName === "TEXTAREA" && h.value ? String(h.value) : h?.textContent ? String(h.textContent) : "";
2235
+ this.setPreedit(g || "");
2236
+ }, f = (a) => {
2237
+ if (n(a) && !(this.delegateInsertToMonaco && !this.exitingInsertMode)) {
2238
+ r(a.target), s(a);
2016
2239
  try {
2017
- l.preventDefault?.();
2240
+ a.preventDefault?.();
2018
2241
  } catch {
2019
2242
  }
2020
- if (v(this.lastMode) && !this.compositionActive) {
2021
- const h = l, m = typeof h.data == "string" ? h.data : "", p = typeof h.inputType == "string" ? String(h.inputType) : "";
2022
- !!!h.isComposing && !p.includes("Composition") && m && this.sendCmdlineImeText(m);
2243
+ if (M(this.lastMode) && !this.compositionActive) {
2244
+ const h = a, g = typeof h.data == "string" ? h.data : "", b = typeof h.inputType == "string" ? String(h.inputType) : "";
2245
+ !!!h.isComposing && !b.includes("Composition") && g && this.sendCmdlineImeText(g);
2023
2246
  }
2024
2247
  }
2025
- }, d = (l) => {
2026
- const c = i(l.target);
2027
- if (!c || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2028
- e(l);
2029
- const h = l;
2248
+ }, p = (a) => {
2249
+ if (!n(a) || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2250
+ const h = r(a.target);
2251
+ s(a);
2252
+ const g = a;
2030
2253
  if (this.ignoreNextInputEvent) {
2031
2254
  this.ignoreNextInputEvent = !1;
2032
2255
  try {
2033
- c.tagName === "TEXTAREA" ? c.value = "" : c.textContent = "";
2256
+ h?.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2034
2257
  } catch {
2035
2258
  }
2036
2259
  return;
2037
2260
  }
2038
2261
  if (!this.compositionActive) {
2039
- if (v(this.lastMode)) {
2040
- const m = typeof h.data == "string" ? h.data : "", p = !m && c.tagName === "TEXTAREA" && c.value ? String(c.value) : !m && c?.textContent ? String(c.textContent) : "", y = m || p;
2041
- y && this.sendCmdlineImeText(y);
2262
+ if (M(this.lastMode)) {
2263
+ const b = typeof g.data == "string" ? g.data : "", _ = !b && h?.tagName === "TEXTAREA" && h.value ? String(h.value) : !b && h?.textContent ? String(h.textContent) : "", v = b || _;
2264
+ v && this.sendCmdlineImeText(v);
2042
2265
  }
2043
2266
  try {
2044
- c.tagName === "TEXTAREA" ? c.value = "" : c.textContent = "";
2267
+ h?.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2045
2268
  } catch {
2046
2269
  }
2047
2270
  }
2048
- }, f = (l) => {
2049
- const c = i(l.target);
2050
- if (!c || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2051
- e(l);
2052
- const h = l.clipboardData?.getData("text/plain") ?? "";
2053
- h && (l.preventDefault(), this.ignoreNextInputEvent = !0, this.pasteText(h));
2271
+ }, m = (a) => {
2272
+ if (!n(a) || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2273
+ const h = r(a.target);
2274
+ s(a);
2275
+ const g = a.clipboardData?.getData("text/plain") ?? "";
2276
+ g && (a.preventDefault(), this.ignoreNextInputEvent = !0, this.pasteText(g));
2054
2277
  try {
2055
- c.tagName === "TEXTAREA" ? c.value = "" : c.textContent = "";
2278
+ h?.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2056
2279
  } catch {
2057
2280
  }
2058
2281
  };
2059
2282
  this.disposables.push(
2060
2283
  // Capture phase to ensure we see events even if Monaco stops propagation.
2061
- C(t, "keydown", s, !0),
2062
- C(t, "beforeinput", u, !0),
2063
- C(t, "input", d, !0),
2064
- C(t, "paste", f, !0),
2065
- C(t, "compositionstart", n, !0),
2066
- C(t, "compositionupdate", a, !0),
2067
- C(t, "compositionend", r, !0)
2284
+ x(i, "keydown", l, !0),
2285
+ x(i, "beforeinput", f, !0),
2286
+ x(i, "input", p, !0),
2287
+ x(i, "paste", m, !0),
2288
+ x(i, "compositionstart", c, !0),
2289
+ x(i, "compositionupdate", d, !0),
2290
+ x(i, "compositionend", u, !0)
2068
2291
  );
2069
2292
  }
2070
2293
  scheduleResync() {
@@ -2086,7 +2309,7 @@ ${c}`), i < n && s.length > 0 && (c += `
2086
2309
  const i = (t && t.length ? t : [""]).join(`
2087
2310
  `);
2088
2311
  if (e.getValue() === i) return;
2089
- const s = this.lastCursorPos ?? this.editor.getPosition() ?? new g.Position(1, 1);
2312
+ const s = this.lastCursorPos ?? this.editor.getPosition() ?? new y.Position(1, 1);
2090
2313
  if (this.suppressCursorSync = !0, this.applyingFromNvim = !0, e.setValue(i), this.applyingFromNvim = !1, s && this.editor.setPosition(s), this.suppressCursorSync = !1, this.delegateInsertToMonaco) {
2091
2314
  const n = this.getActiveState();
2092
2315
  if (n && n.model === e)
@@ -2097,16 +2320,22 @@ ${c}`), i < n && s.length > 0 && (c += `
2097
2320
  }
2098
2321
  }
2099
2322
  ensureVisualStyle() {
2100
- if (this.visualStyleEl) return;
2101
- const t = document.createElement("style");
2102
- t.id = "monaco-neovim-wasm-visual-style", t.textContent = `
2323
+ if (!this.visualStyleEl) {
2324
+ const t = document.createElement("style");
2325
+ t.id = "monaco-neovim-wasm-visual-style", t.textContent = `
2103
2326
  .monaco-neovim-visual-line {
2104
2327
  background-color: ${this.visualBgCss};
2105
2328
  }
2106
2329
  .monaco-neovim-visual-inline {
2107
2330
  background-color: ${this.visualBgCss};
2108
2331
  }
2332
+ .monaco-neovim-visual-virtual {
2333
+ position: absolute;
2334
+ background-color: ${this.visualBgCss};
2335
+ pointer-events: none;
2336
+ }
2109
2337
  `, document.head.appendChild(t), this.visualStyleEl = t;
2338
+ }
2110
2339
  }
2111
2340
  setVisualBgCss(t) {
2112
2341
  const e = t || this.visualBgCss;
@@ -2117,17 +2346,86 @@ ${c}`), i < n && s.length > 0 && (c += `
2117
2346
  .monaco-neovim-visual-inline {
2118
2347
  background-color: ${this.visualBgCss};
2119
2348
  }
2349
+ .monaco-neovim-visual-virtual {
2350
+ position: absolute;
2351
+ background-color: ${this.visualBgCss};
2352
+ pointer-events: none;
2353
+ }
2120
2354
  `));
2121
2355
  }
2122
- clearVisualDecorations() {
2123
- if (this.visualDecorationIds.length) {
2356
+ ensureVisualVirtualOverlay() {
2357
+ if (this.visualVirtualOverlayEl && this.visualVirtualOverlayEl.isConnected) return this.visualVirtualOverlayEl;
2358
+ const t = this.editor.getDomNode();
2359
+ if (!t) return null;
2360
+ const e = t.querySelector(".lines-content .view-overlays") ?? t.querySelector(".view-overlays") ?? t, i = document.createElement("div");
2361
+ return i.id = "monaco-neovim-wasm-visual-virtual-overlay", i.style.position = "absolute", i.style.left = "0", i.style.top = "0", i.style.width = "100%", i.style.height = "100%", i.style.pointerEvents = "none", e.appendChild(i), this.visualVirtualOverlayEl = i, i;
2362
+ }
2363
+ clearVisualVirtualOverlay() {
2364
+ if (this.visualVirtualActive = !1, this.visualVirtualRawRanges = [], this.visualVirtualOverlayEl)
2365
+ try {
2366
+ this.visualVirtualOverlayEl.replaceChildren();
2367
+ } catch {
2368
+ }
2369
+ }
2370
+ renderVisualVirtualOverlay() {
2371
+ if (!this.visualVirtualActive) return;
2372
+ const t = this.ensureVisualVirtualOverlay();
2373
+ if (!t) return;
2374
+ const e = this.editor.getModel();
2375
+ if (!e) {
2376
+ try {
2377
+ t.replaceChildren();
2378
+ } catch {
2379
+ }
2380
+ return;
2381
+ }
2382
+ const i = this.editor.getDomNode();
2383
+ let s = 0, n = 0;
2384
+ if (i && t !== i)
2385
+ try {
2386
+ const d = i.getBoundingClientRect(), f = t.getBoundingClientRect();
2387
+ s = Number.isFinite(f.left - d.left) ? f.left - d.left : 0, n = Number.isFinite(f.top - d.top) ? f.top - d.top : 0;
2388
+ } catch {
2389
+ }
2390
+ const r = Array.isArray(this.visualVirtualRawRanges) ? this.visualVirtualRawRanges : [];
2391
+ if (r.length === 0) {
2124
2392
  try {
2125
- this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, []);
2393
+ t.replaceChildren();
2126
2394
  } catch {
2127
- this.visualDecorationIds = [];
2128
2395
  }
2396
+ return;
2397
+ }
2398
+ const l = this.editor.getOption(y.editor.EditorOption.fontInfo), c = Math.max(1, Number(l?.typicalHalfwidthCharacterWidth ?? l?.maxDigitWidth ?? 0) || 0), u = document.createDocumentFragment();
2399
+ for (const d of r) {
2400
+ const f = Number(d?.start?.line), p = Number(d?.start_vcol), m = Number(d?.end_vcol), a = Number(d?.disp);
2401
+ if (!Number.isFinite(f) || !Number.isFinite(p) || !Number.isFinite(m) || !Number.isFinite(a)) continue;
2402
+ const h = f + 1;
2403
+ if (h < 1 || h > e.getLineCount()) continue;
2404
+ const g = Math.min(p, m), b = Math.max(p, m), _ = this.editor.getScrolledVisiblePosition(new y.Position(h, 1));
2405
+ if (!_) continue;
2406
+ const v = b - g + 1;
2407
+ if (v <= 0) continue;
2408
+ const T = _.left + Math.max(0, g - 1) * c, E = v * c;
2409
+ if (!Number.isFinite(E) || E <= 0) continue;
2410
+ const C = document.createElement("div");
2411
+ C.className = "monaco-neovim-visual-virtual", C.style.left = `${Math.max(0, T - s)}px`, C.style.top = `${Math.max(0, _.top - n)}px`, C.style.width = `${E}px`, C.style.height = `${Math.max(0, _.height)}px`, u.appendChild(C);
2412
+ }
2413
+ try {
2414
+ t.replaceChildren(u);
2415
+ } catch {
2416
+ }
2417
+ }
2418
+ clearVisualDecorations() {
2419
+ if (this.clearVisualVirtualOverlay(), !this.visualDecorationIds.length) {
2129
2420
  this.visualSelectionActive = !1;
2421
+ return;
2130
2422
  }
2423
+ try {
2424
+ this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, []);
2425
+ } catch {
2426
+ this.visualDecorationIds = [];
2427
+ }
2428
+ this.visualSelectionActive = !1;
2131
2429
  }
2132
2430
  ensureSearchStyle() {
2133
2431
  if (this.searchStyleEl) return;
@@ -2188,90 +2486,189 @@ ${c}`), i < n && s.length > 0 && (c += `
2188
2486
  this.clearSearchHighlights();
2189
2487
  return;
2190
2488
  }
2191
- const n = Array.isArray(i.matches) ? i.matches : [], r = i.current && typeof i.current == "object" ? i.current : null, a = r && Number.isFinite(Number(r.l)) ? `${Number(r.l)}:${Number(r.s)}:${Number(r.e)}` : null, u = [], d = 2e3, f = /* @__PURE__ */ new Map(), l = (c) => {
2192
- if (f.has(c)) return f.get(c);
2193
- const h = e.getLineContent(c) ?? "";
2194
- return f.set(c, h), h;
2489
+ 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 = [], u = 2e3, d = /* @__PURE__ */ new Map(), f = (p) => {
2490
+ if (d.has(p)) return d.get(p);
2491
+ const m = e.getLineContent(p) ?? "";
2492
+ return d.set(p, m), m;
2195
2493
  };
2196
- for (let c = 0; c < n.length && u.length < d; c += 1) {
2197
- const h = n[c], m = Number(h?.l), p = Number(h?.s), y = Number(h?.e);
2198
- if (!Number.isFinite(m) || !Number.isFinite(p) || !Number.isFinite(y)) continue;
2199
- const b = m + 1;
2494
+ for (let p = 0; p < n.length && c.length < u; p += 1) {
2495
+ const m = n[p], a = Number(m?.l), h = Number(m?.s), g = Number(m?.e);
2496
+ if (!Number.isFinite(a) || !Number.isFinite(h) || !Number.isFinite(g)) continue;
2497
+ const b = a + 1;
2200
2498
  if (b < 1 || b > e.getLineCount()) continue;
2201
- const I = l(b), P = S(I, Math.max(0, p)) + 1, D = S(I, Math.max(0, y)) + 1;
2202
- if (D <= P) continue;
2203
- const W = `${m}:${p}:${y}`, z = a && W === a ? "monaco-neovim-search-current" : "monaco-neovim-search-match";
2204
- u.push({
2205
- range: new g.Range(b, P, b, D),
2206
- options: { inlineClassName: z }
2499
+ const _ = f(b), v = I(_, Math.max(0, h)) + 1, T = I(_, Math.max(0, g)) + 1;
2500
+ if (T <= v) continue;
2501
+ const E = `${a}:${h}:${g}`, C = l && E === l ? "monaco-neovim-search-current" : "monaco-neovim-search-match";
2502
+ c.push({
2503
+ range: new y.Range(b, v, b, T),
2504
+ options: { inlineClassName: C }
2207
2505
  });
2208
2506
  }
2209
2507
  this.ensureSearchStyle();
2210
2508
  try {
2211
- this.searchDecorationIds = this.editor.deltaDecorations(this.searchDecorationIds, u);
2509
+ this.searchDecorationIds = this.editor.deltaDecorations(this.searchDecorationIds, c);
2212
2510
  } catch {
2213
2511
  this.searchDecorationIds = [];
2214
2512
  }
2215
2513
  }
2216
- applyVisualDecorations(t, e) {
2514
+ applyVisualDecorations(t, e, i = [], s = "") {
2217
2515
  this.ensureVisualStyle();
2218
- const s = ge(e) === "V", n = [];
2219
- if (s) {
2220
- let r = 1 / 0, a = -1 / 0;
2221
- for (const u of t) {
2222
- const d = u.getStartPosition(), f = u.getEndPosition();
2223
- r = Math.min(r, d.lineNumber, f.lineNumber), a = Math.max(a, d.lineNumber, f.lineNumber);
2224
- }
2225
- Number.isFinite(r) && Number.isFinite(a) && a >= r && n.push({
2226
- range: new g.Range(r, 1, a, 1),
2516
+ const n = typeof s == "string" && s ? s : ye(e), r = n === "V", l = n === "", c = [];
2517
+ if (r) {
2518
+ let u = 1 / 0, d = -1 / 0;
2519
+ for (const f of t) {
2520
+ const p = f.getStartPosition(), m = f.getEndPosition();
2521
+ u = Math.min(u, p.lineNumber, m.lineNumber), d = Math.max(d, p.lineNumber, m.lineNumber);
2522
+ }
2523
+ Number.isFinite(u) && Number.isFinite(d) && d >= u && c.push({
2524
+ range: new y.Range(u, 1, d, 1),
2227
2525
  options: { isWholeLine: !0, className: "monaco-neovim-visual-line" }
2228
2526
  });
2229
- } else
2230
- for (const r of t) {
2231
- const a = r.getStartPosition(), u = r.getEndPosition();
2232
- n.push({
2233
- range: g.Range.fromPositions(a, u),
2234
- options: { inlineClassName: "monaco-neovim-visual-inline" }
2527
+ } else if (!l)
2528
+ for (const u of t) {
2529
+ const d = u.getStartPosition(), f = u.getEndPosition();
2530
+ c.push({
2531
+ range: y.Range.fromPositions(d, f),
2532
+ options: { className: "monaco-neovim-visual-inline" }
2235
2533
  });
2236
2534
  }
2535
+ l ? (this.visualVirtualRawRanges = Array.isArray(i) ? i : [], this.visualVirtualActive = !0, this.renderVisualVirtualOverlay()) : this.clearVisualVirtualOverlay();
2237
2536
  try {
2238
- this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, n), this.visualSelectionActive = n.length > 0;
2537
+ this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, c), this.visualSelectionActive = c.length > 0 || l;
2239
2538
  } catch {
2240
2539
  this.visualDecorationIds = [], this.visualSelectionActive = !1;
2241
2540
  }
2541
+ if (this.visualSelectionActive && !this.compositionActive) {
2542
+ const u = this.editor.getPosition();
2543
+ if (u)
2544
+ try {
2545
+ this.suppressCursorSync = !0, this.editor.setSelection(new y.Selection(u.lineNumber, u.column, u.lineNumber, u.column));
2546
+ } catch {
2547
+ } finally {
2548
+ this.suppressCursorSync = !1;
2549
+ }
2550
+ }
2242
2551
  }
2243
2552
  applyNvimMode(t) {
2244
2553
  const e = typeof t == "string" ? t : "";
2245
2554
  if (!e || e === this.lastMode) return;
2246
2555
  const i = this.lastMode;
2247
- this.lastMode = e, v(e) && (this.ignoreNextInputEvent = !1);
2248
- const s = $(e) && !this.recordingRegister;
2556
+ this.lastMode = e, M(e) && (this.ignoreNextInputEvent = !1), this.setMonacoHighlightsSuppressed(S(e));
2557
+ const s = H(e) && !this.recordingRegister;
2249
2558
  if (s !== this.delegateInsertToMonaco) {
2250
- this.delegateInsertToMonaco = s;
2559
+ this.delegateInsertToMonaco = s, this.setEditorReadOnly(!s);
2251
2560
  const n = this.ensureActiveState();
2252
- 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);
2561
+ if (s) {
2562
+ this.setPreedit(null), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !0;
2563
+ {
2564
+ const r = this.recentNormalKeys, l = r.slice(-1), c = r.slice(-2), u = /* @__PURE__ */ new Set(["i", "a", "I", "A", "o", "O", "s", "S", "C", "R"]);
2565
+ if (c === "cc")
2566
+ this.lastDelegatedInsertPrefix = "cc";
2567
+ else if (u.has(l))
2568
+ this.lastDelegatedInsertPrefix = l;
2569
+ else {
2570
+ const d = r.lastIndexOf("c");
2571
+ if (d >= 0) {
2572
+ let f = d;
2573
+ for (; f > 0 && /\d/.test(r[f - 1] ?? ""); ) f -= 1;
2574
+ this.lastDelegatedInsertPrefix = r.slice(f);
2575
+ } else
2576
+ this.lastDelegatedInsertPrefix = null;
2577
+ }
2578
+ }
2579
+ n && (n.shadowLines = this.editor.getModel()?.getLinesContent() ?? null, n.pendingBufEdits = [], n.pendingFullSync = !1, n.pendingCursorSync = !1);
2580
+ } else
2581
+ 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, this.delegatedInsertReplayPossible = !1;
2253
2582
  }
2254
- if (this.exitingInsertMode && !$(e)) {
2583
+ if (this.exitingInsertMode && !H(e)) {
2255
2584
  this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.exitingInsertMode = !1;
2256
2585
  const n = this.pendingKeysAfterExit;
2257
2586
  this.pendingKeysAfterExit = "", n && this.sendInput(n);
2258
2587
  }
2259
- if (this.applyCursorStyle(e), this.opts.onModeChange && this.opts.onModeChange(e), this.updateVisualSelection(e), w(i) && !w(e))
2588
+ if (this.applyCursorStyle(e), this.opts.onModeChange && this.opts.onModeChange(e), this.updateVisualSelection(e), S(i) && !S(e))
2260
2589
  try {
2261
2590
  const n = this.editor.getPosition() ?? this.lastCursorPos;
2262
- n && !this.compositionActive && (this.suppressCursorSync = !0, this.editor.setSelection(new g.Selection(n.lineNumber, n.column, n.lineNumber, n.column)), this.suppressCursorSync = !1);
2591
+ n && !this.compositionActive && (this.suppressCursorSync = !0, this.editor.setSelection(new y.Selection(n.lineNumber, n.column, n.lineNumber, n.column)), this.suppressCursorSync = !1);
2263
2592
  } catch {
2264
2593
  this.suppressCursorSync = !1;
2265
2594
  }
2266
2595
  this.scheduleSearchHighlightRefresh();
2267
2596
  }
2597
+ setEditorReadOnly(t) {
2598
+ const e = !!t;
2599
+ if (this.editorReadOnly !== e)
2600
+ try {
2601
+ this.editor.updateOptions({ readOnly: e }), this.editorReadOnly = e;
2602
+ } catch {
2603
+ }
2604
+ }
2268
2605
  armInsertExit() {
2269
- this.exitingInsertMode = !0, this.pendingKeysAfterExit = "", this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.exitInsertTimer = window.setTimeout(() => {
2606
+ this.exitingInsertMode = !0, this.ignoreMonacoCursorSyncToNvimUntil = this.nowMs() + 250, this.pendingKeysAfterExit = "", this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.cursorSyncTimer && (clearTimeout(this.cursorSyncTimer), this.cursorSyncTimer = null), this.exitInsertTimer = window.setTimeout(() => {
2270
2607
  if (this.exitInsertTimer = null, !this.exitingInsertMode) return;
2271
- this.exitingInsertMode = !1;
2272
2608
  const t = this.pendingKeysAfterExit;
2273
2609
  this.pendingKeysAfterExit = "", t && this.sendInput(t);
2274
- }, 200);
2610
+ }, 800);
2611
+ }
2612
+ finalizeDelegatedInsertDotRepeat() {
2613
+ const t = this.lastDelegatedInsertPrefix, e = this.dotRepeatKeys;
2614
+ this.delegatedInsertReplayPossible && t && e ? this.lastDelegatedDotRepeat = { prefix: t, keys: e } : this.lastDelegatedDotRepeat = null, this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1;
2615
+ }
2616
+ exitDelegatedInsertMode(t) {
2617
+ this.debugLog(`exitDelegatedInsertMode: key=${JSON.stringify(t)} prefix=${JSON.stringify(this.lastDelegatedInsertPrefix)} dotKeysLen=${this.dotRepeatKeys.length} replayPossible=${this.delegatedInsertReplayPossible}`), this.finalizeDelegatedInsertDotRepeat(), this.armInsertExit(), this.performDelegatedInsertExit(t);
2618
+ }
2619
+ async performDelegatedInsertExit(t) {
2620
+ if (!(!this.session || !this.session.isRunning()) && this.bufHandle) {
2621
+ try {
2622
+ this.debugLog(`performDelegatedInsertExit: begin buf=${this.bufHandle} exitKey=${JSON.stringify(t)}`);
2623
+ } catch {
2624
+ }
2625
+ try {
2626
+ await this.flushPendingMonacoSyncBlocking();
2627
+ } catch {
2628
+ }
2629
+ try {
2630
+ const e = this.editor.getModel(), i = this.editor.getPosition();
2631
+ if (e && i) {
2632
+ const s = e.getLineContent(i.lineNumber) ?? "", n = w(s, Math.max(0, i.column - 1));
2633
+ this.ignoreInsertExitCursor = { line: i.lineNumber, col0: n, untilMs: this.nowMs() + 400 }, this.debugLog(`exitDelegatedInsertMode: sync cursor before exit: line=${i.lineNumber} col=${i.column} (byteCol0=${n})`), await this.rpcCall("nvim_win_set_cursor", [0, [i.lineNumber, n]]);
2634
+ }
2635
+ } catch {
2636
+ }
2637
+ try {
2638
+ await this.rpcCall("nvim_input", [t]);
2639
+ } catch {
2640
+ this.sendInput(t);
2641
+ }
2642
+ try {
2643
+ this.debugLog(`performDelegatedInsertExit: sent exitKey=${JSON.stringify(t)}`);
2644
+ } catch {
2645
+ }
2646
+ }
2647
+ }
2648
+ async flushPendingMonacoSyncBlocking() {
2649
+ if (!this.session || !this.session.isRunning() || !this.bufHandle) return;
2650
+ const t = this.getActiveState();
2651
+ if (!t) return;
2652
+ const e = this.editor.getModel();
2653
+ if (e && t.model === e) {
2654
+ try {
2655
+ this.debugLog(`flushPendingMonacoSyncBlocking: full=${t.pendingFullSync} edits=${t.pendingBufEdits.length} cursor=${t.pendingCursorSync} buf=${this.bufHandle}`);
2656
+ } catch {
2657
+ }
2658
+ if (!t.pendingFullSync && t.pendingBufEdits.length > 1 && (t.pendingFullSync = !0), t.pendingFullSync) {
2659
+ const i = e.getLinesContent();
2660
+ t.pendingFullSync = !1, t.pendingBufEdits = [], t.pendingCursorSync = !1, t.shadowLines = i.slice(), this.delegateInsertToMonaco && (this.ignoreActiveBufLinesEventsUntil = this.nowMs() + 120), await this.rpcCall("nvim_buf_set_lines", [this.bufHandle, 0, -1, !1, i]);
2661
+ return;
2662
+ }
2663
+ if (t.pendingBufEdits.length) {
2664
+ const i = t.pendingBufEdits.slice();
2665
+ t.pendingBufEdits = [], t.pendingCursorSync = !1, this.delegateInsertToMonaco && (this.ignoreActiveBufLinesEventsUntil = this.nowMs() + 120);
2666
+ for (const s of i)
2667
+ await this.rpcCall("nvim_buf_set_text", [this.bufHandle, s.startRow, s.startColByte, s.endRow, s.endColByte, s.lines]);
2668
+ return;
2669
+ }
2670
+ t.pendingCursorSync && (t.pendingCursorSync = !1);
2671
+ }
2275
2672
  }
2276
2673
  syncDotRepeatToNvim() {
2277
2674
  if (!this.session || !this.session.isRunning()) return;
@@ -2327,7 +2724,11 @@ vim.opt.ei = ei
2327
2724
  }
2328
2725
  if (this.delegateInsertToMonaco) {
2329
2726
  if (e.key === "Escape") {
2330
- t.preventDefault(), this.armInsertExit(), this.flushPendingMonacoSync(), this.syncDotRepeatToNvim(), this.sendInput("<Esc>");
2727
+ if (this.compositionActive || e.isComposing) {
2728
+ this.pendingEscAfterComposition = !0;
2729
+ return;
2730
+ }
2731
+ t.preventDefault(), this.exitDelegatedInsertMode("<Esc>");
2331
2732
  return;
2332
2733
  }
2333
2734
  if (!this.opts.shouldHandleKey(e) || this.hasExplicitModAllowlist(!0) && !this.shouldForwardModifiedKeys(e, !0))
@@ -2335,12 +2736,12 @@ vim.opt.ei = ei
2335
2736
  if ((e.ctrlKey || e.altKey || e.metaKey) && !e.getModifierState?.("AltGraph")) {
2336
2737
  const s = this.opts.translateKey(e);
2337
2738
  if (!s) return;
2338
- t.preventDefault(), this.flushPendingMonacoSync(), this.sendInput(s);
2739
+ t.preventDefault(), s === "<C-[>" || s === "<C-c>" ? this.exitDelegatedInsertMode(s) : (this.flushPendingMonacoSync(), this.sendInput(s));
2339
2740
  return;
2340
2741
  }
2341
2742
  return;
2342
2743
  }
2343
- if (v(this.lastMode) && !this.compositionActive && !e.isComposing && !e.getModifierState?.("AltGraph") && !e.ctrlKey && !e.metaKey && typeof e.key == "string" && e.key.length === 1) {
2744
+ if (M(this.lastMode) && !this.compositionActive && !e.isComposing && !e.getModifierState?.("AltGraph") && !e.ctrlKey && !e.metaKey && typeof e.key == "string" && e.key.length === 1) {
2344
2745
  const s = /^[\x20-\x7E]$/.test(e.key);
2345
2746
  if (!!!(e.altKey && s))
2346
2747
  return this.opts.shouldHandleKey(e), void 0;
@@ -2354,22 +2755,32 @@ vim.opt.ei = ei
2354
2755
  if (this.compositionActive || e.isComposing || !this.opts.shouldHandleKey(e) || this.hasExplicitModAllowlist(!1) && !this.shouldForwardModifiedKeys(e, !1))
2355
2756
  return;
2356
2757
  const i = this.opts.translateKey(e);
2357
- 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));
2758
+ if (i) {
2759
+ if (this.ignoreNextInputEvent = !0, t.preventDefault(), this.lastMode.startsWith("n")) {
2760
+ if (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()), i === "." && this.lastDelegatedDotRepeat) {
2761
+ const { prefix: s, keys: n } = this.lastDelegatedDotRepeat;
2762
+ this.sendInput(s), this.sendInput(n), this.sendInput("<Esc>");
2763
+ return;
2764
+ }
2765
+ this.lastDelegatedDotRepeat && (/* @__PURE__ */ new Set(["c", "d", "y", "p", "x", "s", "r", "~", "J", ":"])).has(i) && (this.lastDelegatedDotRepeat = null);
2766
+ }
2767
+ !H(this.lastMode) && i.length === 1 && !i.startsWith("<") && (this.recentNormalKeys = (this.recentNormalKeys + i).slice(-16)), this.sendInput(i);
2768
+ }
2358
2769
  }
2359
2770
  handleMouse(t) {
2360
2771
  if (this.delegateInsertToMonaco || !this.bufHandle || !t.target?.position) return;
2361
- const { lineNumber: e, column: i } = t.target.position, s = this.editor.getModel(), n = Math.max(0, i - 1), r = s ? s.getLineContent(e) ?? "" : "", a = s ? _(r, n) : n;
2362
- this.sendNotify("nvim_win_set_cursor", [0, [e, a]]);
2772
+ 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 ? w(r, n) : n;
2773
+ this.sendNotify("nvim_win_set_cursor", [0, [e, l]]);
2363
2774
  }
2364
2775
  handleSelection(t) {
2365
2776
  if (this.delegateInsertToMonaco || !this.session || !this.session.isRunning() || !this.bufHandle || this.suppressCursorSync || this.compositionActive || t?.isComposing) return;
2366
2777
  const e = t.selection;
2367
2778
  if (e && t.source === "mouse") {
2368
2779
  if (e.isEmpty()) {
2369
- w(this.lastMode) && this.sendInput("<Esc>");
2780
+ S(this.lastMode) && this.sendInput("<Esc>");
2370
2781
  return;
2371
2782
  }
2372
- this.pendingSelection = new g.Selection(
2783
+ this.pendingSelection = new y.Selection(
2373
2784
  e.selectionStartLineNumber,
2374
2785
  e.selectionStartColumn,
2375
2786
  e.positionLineNumber,
@@ -2386,7 +2797,7 @@ vim.opt.ei = ei
2386
2797
  if (this.delegateInsertToMonaco || !this.session || !this.session.isRunning() || !this.bufHandle) return;
2387
2798
  const e = this.editor.getModel();
2388
2799
  if (!e) return;
2389
- const i = t.selectionStartLineNumber, s = Math.max(0, t.selectionStartColumn - 1), n = t.positionLineNumber, r = Math.max(0, t.positionColumn - 1), a = e.getLineContent(i) ?? "", u = e.getLineContent(n) ?? "", d = _(a, s), f = _(u, r);
2800
+ 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) ?? "", u = w(l, s), d = w(c, r);
2390
2801
  await this.execLua(`
2391
2802
  local api = vim.api
2392
2803
  local fn = vim.fn
@@ -2399,7 +2810,7 @@ end
2399
2810
  api.nvim_win_set_cursor(0, { a_line, a_col0 })
2400
2811
  api.nvim_feedkeys("v", "n", false)
2401
2812
  api.nvim_win_set_cursor(0, { b_line, b_col0 })
2402
- `, [i, d, n, f]);
2813
+ `, [i, u, n, d]);
2403
2814
  }
2404
2815
  sendInput(t) {
2405
2816
  this.sendNotify("nvim_input", [t]);
@@ -2465,30 +2876,30 @@ api.nvim_win_set_cursor(0, { b_line, b_col0 })
2465
2876
  const i = this.ensureActiveState();
2466
2877
  if (!i || i.model !== e) return;
2467
2878
  if (!i.shadowLines) {
2468
- i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.scheduleFlushPendingMonacoSync();
2879
+ i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1, this.scheduleFlushPendingMonacoSync();
2469
2880
  return;
2470
2881
  }
2471
2882
  if (!t.changes || t.changes.length !== 1) {
2472
- i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.scheduleFlushPendingMonacoSync();
2883
+ i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1, this.scheduleFlushPendingMonacoSync();
2473
2884
  return;
2474
2885
  }
2475
- const s = t.changes[0], n = s.range, r = n.startLineNumber - 1, a = n.endLineNumber - 1;
2476
- if (r < 0 || a < 0) return;
2477
- if (i.shadowLines[r] == null || i.shadowLines[a] == null) {
2478
- i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.scheduleFlushPendingMonacoSync();
2886
+ const s = t.changes[0], n = s.range, r = n.startLineNumber - 1, l = n.endLineNumber - 1;
2887
+ if (r < 0 || l < 0) return;
2888
+ if (i.shadowLines[r] == null || i.shadowLines[l] == null) {
2889
+ i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1, this.scheduleFlushPendingMonacoSync();
2479
2890
  return;
2480
2891
  }
2481
- const u = i.shadowLines[r] ?? "", d = i.shadowLines[a] ?? "", f = Math.max(0, n.startColumn - 1), l = Math.max(0, n.endColumn - 1), c = _(u, f), h = _(d, l), m = String(s.text ?? ""), p = m.length ? m.split(/\r?\n/) : [];
2892
+ const c = i.shadowLines[r] ?? "", u = i.shadowLines[l] ?? "", d = Math.max(0, n.startColumn - 1), f = Math.max(0, n.endColumn - 1), p = w(c, d), m = w(u, f), a = String(s.text ?? ""), h = a.length ? a.split(/\r?\n/) : [];
2482
2893
  try {
2483
- const y = Math.max(0, Number(s.rangeLength ?? 0) || 0);
2484
- r === a && !m.includes(`
2485
- `) ? (y > 0 && (this.dotRepeatBackspaces += y, this.dotRepeatKeys += "<BS>".repeat(y)), m && (this.dotRepeatKeys += k(m, !0)), this.dotRepeatKeys.length > 2e4 && (this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0)) : (this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0);
2894
+ const g = Math.max(0, Number(s.rangeLength ?? 0) || 0);
2895
+ r === l && !a.includes(`
2896
+ `) ? (g > 0 && (this.dotRepeatBackspaces += g, this.dotRepeatKeys += "<BS>".repeat(g)), a && (this.dotRepeatKeys += F(a, !0)), this.dotRepeatKeys.length > 2e4 && (this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1)) : (this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1);
2486
2897
  } catch {
2487
2898
  }
2488
- i.pendingBufEdits.push({ startRow: r, startColByte: c, endRow: a, endColByte: h, lines: p }), ye(i.shadowLines, r, f, a, l, m), i.pendingCursorSync = !0, this.scheduleFlushPendingMonacoSync();
2899
+ i.pendingBufEdits.push({ startRow: r, startColByte: p, endRow: l, endColByte: m, lines: h }), be(i.shadowLines, r, d, l, f, a), i.pendingCursorSync = !0, this.scheduleFlushPendingMonacoSync();
2489
2900
  }
2490
2901
  scheduleCursorSyncToNvim() {
2491
- if (!this.delegateInsertToMonaco) return;
2902
+ if (!this.delegateInsertToMonaco || this.exitingInsertMode) return;
2492
2903
  const t = this.getActiveState();
2493
2904
  t && (t.pendingCursorSync = !0, this.scheduleFlushPendingMonacoSync());
2494
2905
  }
@@ -2505,23 +2916,23 @@ api.nvim_win_set_cursor(0, { b_line, b_col0 })
2505
2916
  if (!t) return;
2506
2917
  const e = this.editor.getModel();
2507
2918
  if (e && t.model === e) {
2508
- if (!t.pendingFullSync && t.pendingBufEdits.length > 1 && (t.pendingFullSync = !0), t.pendingFullSync) {
2919
+ if (this.debugLog(`flushPendingMonacoSync: full=${t.pendingFullSync} edits=${t.pendingBufEdits.length} cursor=${t.pendingCursorSync} delegateInsert=${this.delegateInsertToMonaco} exitingInsert=${this.exitingInsertMode} mode=${JSON.stringify(this.lastMode)}`), !t.pendingFullSync && t.pendingBufEdits.length > 1 && (t.pendingFullSync = !0), t.pendingFullSync) {
2509
2920
  const i = e.getLinesContent();
2510
- this.sendNotify("nvim_buf_set_lines", [this.bufHandle, 0, -1, !1, i]), t.pendingFullSync = !1, t.pendingBufEdits = [], t.shadowLines = i.slice();
2921
+ this.sendNotify("nvim_buf_set_lines", [this.bufHandle, 0, -1, !1, i]), this.delegateInsertToMonaco && (this.ignoreActiveBufLinesEventsUntil = this.nowMs() + 120), t.pendingFullSync = !1, t.pendingBufEdits = [], t.shadowLines = i.slice();
2511
2922
  } else if (t.pendingBufEdits.length) {
2512
2923
  for (const i of t.pendingBufEdits)
2513
2924
  this.sendNotify("nvim_buf_set_text", [this.bufHandle, i.startRow, i.startColByte, i.endRow, i.endColByte, i.lines]);
2514
- t.pendingBufEdits = [];
2925
+ this.delegateInsertToMonaco && (this.ignoreActiveBufLinesEventsUntil = this.nowMs() + 120), t.pendingBufEdits = [];
2515
2926
  }
2516
- t.pendingCursorSync && (t.pendingCursorSync = !1, this.syncCursorToNvimNow());
2927
+ t.pendingCursorSync && (t.pendingCursorSync = !1, this.delegateInsertToMonaco && !this.exitingInsertMode && this.syncCursorToNvimNow());
2517
2928
  }
2518
2929
  }
2519
- syncCursorToNvimNow() {
2520
- if (!this.session || !this.session.isRunning() || !this.bufHandle) return;
2521
- const t = this.editor.getModel(), e = this.editor.getPosition();
2522
- if (!t || !e) return;
2523
- const i = e.lineNumber, s = t.getLineContent(i) ?? "", n = _(s, Math.max(0, e.column - 1));
2524
- this.sendNotify("nvim_win_set_cursor", [0, [i, n]]);
2930
+ syncCursorToNvimNow(t = !1) {
2931
+ if (!this.session || !this.session.isRunning() || !this.bufHandle || !t && !this.delegateInsertToMonaco) return;
2932
+ const e = this.editor.getModel(), i = this.editor.getPosition();
2933
+ if (!e || !i) return;
2934
+ const s = i.lineNumber, n = e.getLineContent(s) ?? "", r = w(n, Math.max(0, i.column - 1));
2935
+ this.sendNotify("nvim_win_set_cursor", [0, [s, r]]);
2525
2936
  }
2526
2937
  scheduleVisualSelectionRefresh() {
2527
2938
  this.visualSelectionRefreshTimer || (this.visualSelectionRefreshTimer = window.setTimeout(() => {
@@ -2534,11 +2945,36 @@ api.nvim_win_set_cursor(0, { b_line, b_col0 })
2534
2945
  const t = await this.rpcCall("nvim_get_api_info", []), e = Array.isArray(t) ? Number(t[0]) : NaN;
2535
2946
  if (!Number.isFinite(e) || e <= 0) return;
2536
2947
  this.nvimChannelId = e;
2537
- 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", a = `
2948
+ 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", c = `
2538
2949
  local chan = ...
2539
2950
  local api = vim.api
2540
2951
  vim.g.monaco_neovim_wasm_chan = chan
2541
2952
 
2953
+ local function setup_clipboard()
2954
+ -- If user provided a clipboard provider (or explicitly disabled it), don't override.
2955
+ if vim.g.clipboard ~= nil then return end
2956
+ local function copy(lines, regtype)
2957
+ vim.rpcnotify(chan, "wasm-clipboard-copy", lines, regtype)
2958
+ end
2959
+ local function paste()
2960
+ local ok, res = pcall(vim.rpcrequest, chan, "wasm-clipboard-paste")
2961
+ if not ok then return {}, "v" end
2962
+ local lines = res and res[1] or {}
2963
+ local regtype = res and res[2] or "v"
2964
+ return lines, regtype
2965
+ end
2966
+ vim.g.clipboard = {
2967
+ name = "wasm",
2968
+ copy = { ["+"] = copy, ["*"] = copy },
2969
+ paste = { ["+"] = paste, ["*"] = paste },
2970
+ cache_enabled = 0,
2971
+ }
2972
+ end
2973
+
2974
+ if ${this.opts.clipboard === null ? "false" : "true"} then
2975
+ pcall(setup_clipboard)
2976
+ end
2977
+
2542
2978
  local function send_cursor()
2543
2979
  local cur = api.nvim_win_get_cursor(0)
2544
2980
  vim.rpcnotify(chan, "monaco_cursor", cur[1], cur[2])
@@ -2622,7 +3058,7 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2622
3058
  filetype = (vim.bo[api.nvim_get_current_buf()] and vim.bo[api.nvim_get_current_buf()].filetype) or "",
2623
3059
  })
2624
3060
  `;
2625
- await this.execLua(a, [e]), this.hostAutocmdInstalled = !0;
3061
+ await this.execLua(c, [e]), this.hostAutocmdInstalled = !0;
2626
3062
  } catch {
2627
3063
  }
2628
3064
  }
@@ -2655,8 +3091,8 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2655
3091
  try {
2656
3092
  const r = await s.readFile(n);
2657
3093
  if (r == null) return;
2658
- const a = r instanceof Uint8Array ? new TextDecoder().decode(r) : String(r);
2659
- await this.openText({ path: n, text: a }), this.opts.status(`opened: ${n}`);
3094
+ const l = r instanceof Uint8Array ? new TextDecoder().decode(r) : String(r);
3095
+ await this.openText({ path: n, text: l }), this.opts.status(`opened: ${n}`);
2660
3096
  } catch (r) {
2661
3097
  this.opts.status(`edit failed: ${r?.message ?? r}`, !0);
2662
3098
  }
@@ -2664,31 +3100,31 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2664
3100
  }
2665
3101
  if (e === "write" || e === "wq") {
2666
3102
  const n = this.editor.getModel(), r = n ? n.getValue() : "";
2667
- let a = typeof i.path == "string" ? String(i.path) : "";
2668
- if (!a)
3103
+ let l = typeof i.path == "string" ? String(i.path) : "";
3104
+ if (!l)
2669
3105
  try {
2670
- const u = await this.rpcCall("nvim_buf_get_name", [0]);
2671
- typeof u == "string" && (a = u);
3106
+ const c = await this.rpcCall("nvim_buf_get_name", [0]);
3107
+ typeof c == "string" && (l = c);
2672
3108
  } catch {
2673
3109
  }
2674
- if (a || (a = this.opts.seedName), !s.writeFile) {
3110
+ if (l || (l = this.opts.seedName), !s.writeFile) {
2675
3111
  this.opts.status("write: fileSystem.writeFile not set", !0);
2676
3112
  return;
2677
3113
  }
2678
3114
  try {
2679
- await s.writeFile(a, r);
3115
+ await s.writeFile(l, r);
2680
3116
  try {
2681
- await this.rpcCall("nvim_buf_set_name", [0, a]);
3117
+ await this.rpcCall("nvim_buf_set_name", [0, l]);
2682
3118
  } catch {
2683
3119
  }
2684
3120
  try {
2685
3121
  await this.rpcCall("nvim_buf_set_option", [0, "modified", !1]);
2686
3122
  } catch {
2687
3123
  }
2688
- const u = this.getActiveState();
2689
- u && (u.name && u.name !== a && this.buffersByName.delete(u.name), u.name = a, this.buffersByName.set(a, u.id)), this.opts.status(`written: ${a}`), e === "wq" && this.opts.status("wq requested (provide onHostCommand to close the editor)");
2690
- } catch (u) {
2691
- this.opts.status(`write failed: ${u?.message ?? u}`, !0);
3124
+ const c = this.getActiveState();
3125
+ 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)");
3126
+ } catch (c) {
3127
+ this.opts.status(`write failed: ${c?.message ?? c}`, !0);
2692
3128
  }
2693
3129
  return;
2694
3130
  }
@@ -2709,19 +3145,19 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2709
3145
  await this.rpcCall("nvim_buf_attach", [e, !1, {}]);
2710
3146
  } catch {
2711
3147
  }
2712
- let a = [""];
3148
+ let l = [""];
2713
3149
  try {
2714
- const c = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
2715
- Array.isArray(c) && (a = c.map((h) => String(h ?? "")));
3150
+ const p = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
3151
+ Array.isArray(p) && (l = p.map((m) => String(m ?? "")));
2716
3152
  } catch {
2717
3153
  }
2718
- const u = g.Uri.from({ scheme: "nvim", authority: "buf", path: `/${e}` }), d = a.join(`
2719
- `), f = s && g.languages.getLanguages().some((c) => c.id === s), l = g.editor.createModel(d, f ? s : void 0, u);
3154
+ const c = y.Uri.from({ scheme: "nvim", authority: "buf", path: `/${e}` }), u = l.join(`
3155
+ `), d = s && y.languages.getLanguages().some((p) => p.id === s), f = y.editor.createModel(u, d ? s : void 0, c);
2720
3156
  n = {
2721
3157
  id: e,
2722
3158
  name: i || "",
2723
3159
  filetype: s || "",
2724
- model: l,
3160
+ model: f,
2725
3161
  createdModel: !0,
2726
3162
  shadowLines: null,
2727
3163
  pendingBufEdits: [],
@@ -2765,7 +3201,7 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2765
3201
  if (!n)
2766
3202
  try {
2767
3203
  const r = await this.rpcCall("nvim_create_buf", [!0, !1]);
2768
- n = x(r) ?? Number(r);
3204
+ n = N(r) ?? Number(r);
2769
3205
  } catch {
2770
3206
  n = null;
2771
3207
  }
@@ -2818,13 +3254,16 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2818
3254
  e(s || "");
2819
3255
  return;
2820
3256
  }
2821
- navigator.clipboard.readText().then((s) => e(s || "")).catch(() => e(""));
3257
+ navigator.clipboard.readText().then((s) => e(s || "")).catch(() => {
3258
+ const s = window.prompt("Paste text");
3259
+ e(s || "");
3260
+ });
2822
3261
  }
2823
3262
  updateCursor(t, e) {
2824
- 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);
2825
- if (this.lastCursorPos = r, this.compositionActive) return;
2826
- const a = this.editor.getPosition();
2827
- a && a.lineNumber === r.lineNumber && a.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();
3263
+ const i = this.editor.getModel(), s = Math.max(1, Number(t) || 1), n = Math.max(1, Number(e) || 1), r = i ? i.validatePosition(new y.Position(s, n)) : new y.Position(s, n);
3264
+ if (this.debugLog(`updateCursor: line=${r.lineNumber} col=${r.column} (from line=${s} col=${n}) visual=${this.visualSelectionActive} mode=${JSON.stringify(this.lastMode)}`), this.lastCursorPos = r, this.compositionActive) return;
3265
+ const l = this.editor.getPosition();
3266
+ l && l.lineNumber === r.lineNumber && l.column === r.column || (this.suppressCursorSync = !0, this.editor.setPosition(this.lastCursorPos), this.visualSelectionActive && this.editor.setSelection(new y.Selection(r.lineNumber, r.column, r.lineNumber, r.column)), this.applyScrolloff(this.lastCursorPos) || this.editor.revealPositionInCenterIfOutsideViewport(this.lastCursorPos), this.suppressCursorSync = !1), this.scheduleSearchHighlightRefresh();
2828
3267
  }
2829
3268
  scheduleCursorRefresh() {
2830
3269
  if (this.compositionActive || this.cursorRefreshTimer) return;
@@ -2848,10 +3287,10 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2848
3287
  try {
2849
3288
  const e = await this.rpcCall("nvim_win_get_cursor", [0]);
2850
3289
  if (Array.isArray(e) && e.length >= 2) {
2851
- const i = Number(e[0]), s = Number(e[1]), n = B(this.editor, i, s), r = new g.Position(n.line, n.col), a = typeof performance < "u" && performance.now ? performance.now() : Date.now();
2852
- if (this.optimisticCursorPos && this.optimisticCursorUntil > a) {
2853
- const u = this.optimisticCursorPrevPos;
2854
- u && r.lineNumber === u.lineNumber && r.column === u.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));
3290
+ const i = Number(e[0]), s = Number(e[1]), n = K(this.editor, i, s), r = new y.Position(n.line, n.col), l = typeof performance < "u" && performance.now ? performance.now() : Date.now();
3291
+ if (this.optimisticCursorPos && this.optimisticCursorUntil > l) {
3292
+ const c = this.optimisticCursorPrevPos;
3293
+ 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));
2855
3294
  } else
2856
3295
  this.optimisticCursorPos = null, this.optimisticCursorPrevPos = null, this.optimisticCursorUntil = 0, this.updateCursor(r.lineNumber, r.column);
2857
3296
  }
@@ -2875,26 +3314,33 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2875
3314
  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);
2876
3315
  }
2877
3316
  async updateVisualSelection(t) {
2878
- const e = w(t), i = ++this.visualSelectionToken;
3317
+ const e = S(t), i = ++this.visualSelectionToken;
2879
3318
  if (!e) {
2880
3319
  this.clearVisualDecorations();
2881
3320
  return;
2882
3321
  }
2883
3322
  try {
2884
- const s = await this.fetchVisualRanges();
3323
+ const { selections: s, raw: n, tail: r } = await this.fetchVisualRanges();
2885
3324
  if (i !== this.visualSelectionToken || !s.length) return;
2886
- this.applyVisualDecorations(s, t);
3325
+ this.applyVisualDecorations(s, t, n, r);
2887
3326
  } catch {
2888
3327
  }
2889
3328
  }
2890
3329
  async fetchVisualRanges() {
2891
- const t = await this.execLua(oe, []);
2892
- return Array.isArray(t) ? t.map(de(this.editor)).filter((i) => !!i) : [];
3330
+ const t = await this.execLua(le, []);
3331
+ let e = [], i = "";
3332
+ if (Array.isArray(t))
3333
+ e = t;
3334
+ else if (t && typeof t == "object") {
3335
+ const n = t;
3336
+ i = typeof n.tail == "string" ? n.tail : "", e = Array.isArray(n.ranges) ? n.ranges : [];
3337
+ }
3338
+ return { selections: e.map(fe(this.editor)).filter((n) => !!n), raw: e, tail: i };
2893
3339
  }
2894
3340
  async syncVisualSelectionColor() {
2895
3341
  try {
2896
- const e = await this.fetchVisualBg() || "#3e4451", i = H(e, 0.45), s = H(e, 0.3);
2897
- g.editor.defineTheme(this.opts.visualThemeName, {
3342
+ const e = await this.fetchVisualBg() || "#3e4451", i = V(e, 0.45), s = V(e, 0.3);
3343
+ y.editor.defineTheme(this.opts.visualThemeName, {
2898
3344
  base: "vs-dark",
2899
3345
  inherit: !0,
2900
3346
  rules: [],
@@ -2906,21 +3352,50 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2906
3352
  } catch {
2907
3353
  }
2908
3354
  }
3355
+ setMonacoHighlightsSuppressed(t) {
3356
+ const e = y.editor.EditorOption, i = !!t;
3357
+ if (i !== this.monacoHighlightsSuppressed) {
3358
+ if (i) {
3359
+ try {
3360
+ this.monacoPrevOccurrencesHighlight = this.editor.getOption(e.occurrencesHighlight), this.monacoPrevSelectionHighlight = this.editor.getOption(e.selectionHighlight), this.monacoPrevSelectionHighlightMultiline = this.editor.getOption(e.selectionHighlightMultiline);
3361
+ } catch {
3362
+ }
3363
+ try {
3364
+ this.editor.updateOptions({
3365
+ occurrencesHighlight: "off",
3366
+ selectionHighlight: !1,
3367
+ selectionHighlightMultiline: !1
3368
+ }), this.monacoHighlightsSuppressed = !0;
3369
+ } catch {
3370
+ }
3371
+ return;
3372
+ }
3373
+ try {
3374
+ this.editor.updateOptions({
3375
+ occurrencesHighlight: this.monacoPrevOccurrencesHighlight ?? "singleFile",
3376
+ selectionHighlight: this.monacoPrevSelectionHighlight ?? !0,
3377
+ selectionHighlightMultiline: this.monacoPrevSelectionHighlightMultiline ?? !0
3378
+ });
3379
+ } catch {
3380
+ }
3381
+ this.monacoHighlightsSuppressed = !1, this.monacoPrevOccurrencesHighlight = null, this.monacoPrevSelectionHighlight = null, this.monacoPrevSelectionHighlightMultiline = null;
3382
+ }
3383
+ }
2909
3384
  async fetchVisualBg() {
2910
3385
  try {
2911
- const t = await this.rpcCall("nvim_get_hl", [0, { name: "Visual", link: !1 }]), e = K(t);
3386
+ const t = await this.rpcCall("nvim_get_hl", [0, { name: "Visual", link: !1 }]), e = W(t);
2912
3387
  if (e) return e;
2913
3388
  } catch {
2914
3389
  }
2915
3390
  try {
2916
- const t = await this.rpcCall("nvim_get_hl_by_name", ["Visual", !0]), e = K(t);
3391
+ const t = await this.rpcCall("nvim_get_hl_by_name", ["Visual", !0]), e = W(t);
2917
3392
  if (e) return e;
2918
3393
  } catch {
2919
3394
  }
2920
3395
  return null;
2921
3396
  }
2922
3397
  async seedBuffer(t, e) {
2923
- const i = x(t);
3398
+ const i = N(t);
2924
3399
  if (!i || i <= 0) return null;
2925
3400
  const s = e ?? this.opts.seedLines;
2926
3401
  if (!s || !s.length) return null;
@@ -2931,27 +3406,27 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2931
3406
  }
2932
3407
  }
2933
3408
  }
2934
- function Ce(o, t = {}) {
2935
- return new le(o, t);
3409
+ function ve(o, t = {}) {
3410
+ return new ce(o, t);
2936
3411
  }
2937
- function M(o, t, e) {
3412
+ function R(o, t, e) {
2938
3413
  return Math.min(Math.max(o, t), e);
2939
3414
  }
2940
- function A(o) {
3415
+ function B(o) {
2941
3416
  try {
2942
3417
  return new TextEncoder().encode(String(o ?? "")).length;
2943
3418
  } catch {
2944
3419
  return String(o ?? "").length;
2945
3420
  }
2946
3421
  }
2947
- function B(o, t, e) {
3422
+ function K(o, t, e) {
2948
3423
  const i = Math.max(1, Number(t) || 1), s = Math.max(1, (Number(e) || 0) + 1), n = o.getModel();
2949
3424
  if (!n)
2950
3425
  return { line: i, col: s };
2951
- const r = n.getLineCount(), a = M(i, 1, r), u = n.getLineContent(a) ?? "", d = n.getLineMaxColumn(a), f = Math.max(0, Number(e) || 0), l = S(u, f), c = M(l + 1, 1, d);
2952
- return { line: a, col: c };
3426
+ const r = n.getLineCount(), l = R(i, 1, r), c = n.getLineContent(l) ?? "", u = n.getLineMaxColumn(l), d = Math.max(0, Number(e) || 0), f = I(c, d), p = R(f + 1, 1, u);
3427
+ return { line: l, col: p };
2953
3428
  }
2954
- function k(o, t = !0) {
3429
+ function F(o, t = !0) {
2955
3430
  const e = String(o ?? "");
2956
3431
  if (!e) return "";
2957
3432
  const i = e.replace(/\r\n/g, `
@@ -2959,72 +3434,72 @@ function k(o, t = !0) {
2959
3434
  `).replace(/</g, "<lt>");
2960
3435
  return t ? i.replace(/\n/g, "<CR>") : i;
2961
3436
  }
2962
- function ce(o) {
3437
+ function ue(o) {
2963
3438
  const t = o.key;
2964
3439
  if (!t || t === "Dead" || t === "Unidentified") return null;
2965
- const e = !!o.getModifierState?.("AltGraph"), i = o.ctrlKey && !e, s = o.altKey && !e, n = o.metaKey && !e, r = o.shiftKey, a = (l) => l === "<" ? "lt" : l, u = (l) => l === "<" ? "<lt>" : l, d = (l, c = !1) => {
2966
- const h = [];
2967
- i && h.push("C-"), c && r && h.push("S-"), s && h.push("A-"), n && h.push("D-");
2968
- const m = a(l);
2969
- return h.length ? `<${h.join("")}${m}>` : `<${m}>`;
3440
+ const e = !!o.getModifierState?.("AltGraph"), i = o.ctrlKey && !e, s = o.altKey && !e, n = o.metaKey && !e, r = o.shiftKey, l = (f) => f === "<" ? "lt" : f, c = (f) => f === "<" ? "<lt>" : f, u = (f, p = !1) => {
3441
+ const m = [];
3442
+ i && m.push("C-"), p && r && m.push("S-"), s && m.push("A-"), n && m.push("D-");
3443
+ const a = l(f);
3444
+ return m.length ? `<${m.join("")}${a}>` : `<${a}>`;
2970
3445
  };
2971
3446
  if (typeof o.code == "string" && o.code.startsWith("Numpad") || typeof KeyboardEvent?.DOM_KEY_LOCATION_NUMPAD == "number" && o.location === KeyboardEvent.DOM_KEY_LOCATION_NUMPAD) {
2972
3447
  switch (o.code) {
2973
3448
  case "NumpadEnter":
2974
- return d("kEnter", !0);
3449
+ return u("kEnter", !0);
2975
3450
  case "NumpadAdd":
2976
- return d("kPlus", !0);
3451
+ return u("kPlus", !0);
2977
3452
  case "NumpadSubtract":
2978
- return d("kMinus", !0);
3453
+ return u("kMinus", !0);
2979
3454
  case "NumpadMultiply":
2980
- return d("kMultiply", !0);
3455
+ return u("kMultiply", !0);
2981
3456
  case "NumpadDivide":
2982
- return d("kDivide", !0);
3457
+ return u("kDivide", !0);
2983
3458
  case "NumpadDecimal":
2984
- return d("kPoint", !0);
3459
+ return u("kPoint", !0);
2985
3460
  }
2986
- if (/^\d$/.test(t)) return d(`k${t}`, !0);
3461
+ if (/^\d$/.test(t)) return u(`k${t}`, !0);
2987
3462
  }
2988
3463
  switch (t) {
2989
3464
  case "Backspace":
2990
- return d("BS", !0);
3465
+ return u("BS", !0);
2991
3466
  case "Enter":
2992
- return d("CR", !0);
3467
+ return u("CR", !0);
2993
3468
  case "Escape":
2994
- return d("Esc", !0);
3469
+ return u("Esc", !0);
2995
3470
  case "Tab":
2996
- return r && !i && !s && !n ? "<S-Tab>" : d("Tab", !0);
3471
+ return r && !i && !s && !n ? "<S-Tab>" : u("Tab", !0);
2997
3472
  case "ArrowUp":
2998
- return d("Up", !0);
3473
+ return u("Up", !0);
2999
3474
  case "ArrowDown":
3000
- return d("Down", !0);
3475
+ return u("Down", !0);
3001
3476
  case "ArrowLeft":
3002
- return d("Left", !0);
3477
+ return u("Left", !0);
3003
3478
  case "ArrowRight":
3004
- return d("Right", !0);
3479
+ return u("Right", !0);
3005
3480
  case "Delete":
3006
- return d("Del", !0);
3481
+ return u("Del", !0);
3007
3482
  case "Home":
3008
- return d("Home", !0);
3483
+ return u("Home", !0);
3009
3484
  case "End":
3010
- return d("End", !0);
3485
+ return u("End", !0);
3011
3486
  case "PageUp":
3012
- return d("PageUp", !0);
3487
+ return u("PageUp", !0);
3013
3488
  case "PageDown":
3014
- return d("PageDown", !0);
3489
+ return u("PageDown", !0);
3015
3490
  case "Insert":
3016
- return d("Insert", !0);
3491
+ return u("Insert", !0);
3017
3492
  }
3018
- if (/^F\d{1,2}$/.test(t)) return d(t, !0);
3493
+ if (/^F\d{1,2}$/.test(t)) return u(t, !0);
3019
3494
  if (t.length === 1) {
3020
- if (!i && !s && !n) return u(t);
3495
+ if (!i && !s && !n) return c(t);
3021
3496
  if (t === " " && i && !s && !n) return "<Nul>";
3022
- const l = /^[A-Za-z]$/.test(t) ? t.toLowerCase() : t, c = a(l);
3023
- return `<${(i ? "C-" : "") + (s ? "A-" : "") + (n ? "D-" : "")}${c}>`;
3497
+ const f = /^[A-Za-z]$/.test(t) ? t.toLowerCase() : t, p = l(f);
3498
+ return `<${(i ? "C-" : "") + (s ? "A-" : "") + (n ? "D-" : "")}${p}>`;
3024
3499
  }
3025
3500
  return null;
3026
3501
  }
3027
- function ue(o) {
3502
+ function he(o) {
3028
3503
  if (!o || o.length === 0) return null;
3029
3504
  const t = o[0];
3030
3505
  if (o.length === 1)
@@ -3051,74 +3526,75 @@ function ue(o) {
3051
3526
  return t;
3052
3527
  }
3053
3528
  }
3054
- function he(o) {
3529
+ function de(o) {
3055
3530
  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;
3056
3531
  }
3057
- function x(o) {
3532
+ function N(o) {
3058
3533
  if (o && typeof o == "object" && typeof o.type == "number") {
3059
- const e = he(o.data);
3534
+ const e = de(o.data);
3060
3535
  if (e) {
3061
- const i = ue(e);
3536
+ const i = he(e);
3062
3537
  if (i != null && i > 0) return i;
3063
3538
  }
3064
3539
  }
3065
3540
  const t = Number(o);
3066
3541
  return Number.isInteger(t) && t > 0 ? t : null;
3067
3542
  }
3068
- function S(o, t) {
3543
+ function I(o, t) {
3069
3544
  let e = 0, i = 0;
3070
3545
  const s = Math.max(0, Number(t) || 0);
3071
3546
  for (; e < s; ) {
3072
3547
  if (i >= o.length)
3073
3548
  return i + (s - e);
3074
- const n = o.codePointAt(i), r = L(n ?? 0);
3549
+ const n = o.codePointAt(i), r = $(n ?? 0);
3550
+ if (e + r > s) return i;
3075
3551
  e += r, i += r === 4 ? 2 : 1;
3076
3552
  }
3077
3553
  return i;
3078
3554
  }
3079
- function _(o, t) {
3555
+ function w(o, t) {
3080
3556
  const e = Math.max(0, Number(t) || 0);
3081
3557
  let i = 0, s = 0;
3082
3558
  for (; s < e && s < o.length; ) {
3083
- const n = o.codePointAt(s), r = L(n ?? 0);
3559
+ const n = o.codePointAt(s), r = $(n ?? 0);
3084
3560
  i += r, s += r === 4 ? 2 : 1;
3085
3561
  }
3086
3562
  return i;
3087
3563
  }
3088
- function L(o) {
3564
+ function $(o) {
3089
3565
  return o == null ? 0 : o <= 127 ? 1 : o <= 2047 ? 2 : o >= 55296 && o <= 57343 ? 4 : o < 65535 ? 3 : 4;
3090
3566
  }
3091
- function de(o) {
3567
+ function fe(o) {
3092
3568
  return (t) => {
3093
3569
  if (!t || !t.start || !t.end) return null;
3094
- const e = F(o, t.start, !1), i = F(o, t.end, !!t.inclusive);
3095
- return !e || !i ? null : new g.Selection(e.lineNumber, e.column, i.lineNumber, i.column);
3570
+ const e = O(o, t.start, !1), i = O(o, t.end, !!t.inclusive);
3571
+ return !e || !i ? null : new y.Selection(e.lineNumber, e.column, i.lineNumber, i.column);
3096
3572
  };
3097
3573
  }
3098
- function F(o, t, e) {
3574
+ function O(o, t, e) {
3099
3575
  if (!t || typeof t.line != "number" || typeof t.col != "number") return null;
3100
- const i = o.getModel(), s = M(t.line + 1, 1, i?.getLineCount() || 1 / 0);
3576
+ const i = o.getModel(), s = R(t.line + 1, 1, i?.getLineCount() || 1 / 0);
3101
3577
  if (!i) {
3102
- const f = Math.max(1, t.col + 1);
3103
- return { lineNumber: s, column: f + (e ? 1 : 0) };
3578
+ const d = Math.max(1, t.col + 1);
3579
+ return { lineNumber: s, column: d + (e ? 1 : 0) };
3104
3580
  }
3105
3581
  const n = i.getLineContent(s) ?? "", r = Math.max(0, Number(t.col) || 0);
3106
- let a = r;
3582
+ let l = r;
3107
3583
  if (e) {
3108
- const f = S(n, r);
3109
- if (f < n.length) {
3110
- const l = n.codePointAt(f);
3111
- a = r + L(l ?? 0);
3584
+ const d = I(n, r);
3585
+ if (d < n.length) {
3586
+ const f = n.codePointAt(d);
3587
+ l = r + $(f ?? 0);
3112
3588
  }
3113
3589
  }
3114
- const u = S(n, a) + 1, d = i.getLineMaxColumn(s);
3115
- return { lineNumber: s, column: M(u, 1, d) };
3590
+ const c = I(n, l) + 1, u = i.getLineMaxColumn(s);
3591
+ return { lineNumber: s, column: R(c, 1, u) };
3116
3592
  }
3117
- function w(o) {
3593
+ function S(o) {
3118
3594
  const t = typeof o == "string" ? o : "";
3119
3595
  return t.includes("v") || t.includes("V") || t.includes("") || t.includes("s") || t.includes("S") || t.includes("");
3120
3596
  }
3121
- function fe(o) {
3597
+ function pe(o) {
3122
3598
  if (!o || !Array.isArray(o) || o.length === 0) return;
3123
3599
  const t = new TextEncoder(), e = [];
3124
3600
  for (const i of o) {
@@ -3134,47 +3610,46 @@ function me(o, t) {
3134
3610
  const e = Array.isArray(t) ? t.filter(Boolean) : [], i = Array.isArray(o) ? o.filter(Boolean) : [], s = [...e, ...i];
3135
3611
  return s.length ? s : void 0;
3136
3612
  }
3137
- function H(o, t) {
3613
+ function V(o, t) {
3138
3614
  const e = o.replace("#", "");
3139
3615
  if (e.length !== 6) return o;
3140
- const i = Math.round(M(t, 0, 1) * 255);
3616
+ const i = Math.round(R(t, 0, 1) * 255);
3141
3617
  return `#${e}${i.toString(16).padStart(2, "0")}`;
3142
3618
  }
3143
- function K(o) {
3619
+ function W(o) {
3144
3620
  if (!o || typeof o != "object") return null;
3145
3621
  const t = o, e = t.background ?? t.bg;
3146
- 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;
3622
+ return typeof e == "number" && e >= 0 ? ge(e) : typeof t.background == "string" && t.background.startsWith("#") ? t.background : typeof t.bg == "string" && t.bg.startsWith("#") ? t.bg : null;
3147
3623
  }
3148
- function pe(o) {
3624
+ function ge(o) {
3149
3625
  return `#${Number(o >>> 0).toString(16).padStart(6, "0").slice(-6)}`;
3150
3626
  }
3151
- function C(o, t, e, i) {
3627
+ function x(o, t, e, i) {
3152
3628
  return o.addEventListener(t, e, i), { dispose: () => o.removeEventListener(t, e, i) };
3153
3629
  }
3154
- function $(o) {
3155
- const t = typeof o == "string" ? o : "";
3156
- return t.startsWith("i") || t.startsWith("R");
3630
+ function H(o) {
3631
+ return (typeof o == "string" ? o : "").startsWith("i");
3157
3632
  }
3158
- function ge(o) {
3633
+ function ye(o) {
3159
3634
  const t = typeof o == "string" ? o : "";
3160
3635
  return t.length ? t[t.length - 1] : "";
3161
3636
  }
3162
- function v(o) {
3637
+ function M(o) {
3163
3638
  const t = typeof o == "string" ? o : "";
3164
3639
  return t === "c" || t.startsWith("c");
3165
3640
  }
3166
- function ye(o, t, e, i, s, n) {
3167
- const r = o[t] ?? "", a = o[i] ?? "", u = r.slice(0, Math.max(0, e)), d = a.slice(Math.max(0, s)), l = String(n ?? "").split(/\r?\n/);
3168
- let c;
3169
- if (l.length <= 1)
3170
- c = [`${u}${l[0] ?? ""}${d}`];
3641
+ function be(o, t, e, i, s, n) {
3642
+ const r = o[t] ?? "", l = o[i] ?? "", c = r.slice(0, Math.max(0, e)), u = l.slice(Math.max(0, s)), f = String(n ?? "").split(/\r?\n/);
3643
+ let p;
3644
+ if (f.length <= 1)
3645
+ p = [`${c}${f[0] ?? ""}${u}`];
3171
3646
  else {
3172
- const h = `${u}${l[0] ?? ""}`, m = `${l[l.length - 1] ?? ""}${d}`, p = l.slice(1, -1);
3173
- c = [h, ...p, m];
3647
+ const m = `${c}${f[0] ?? ""}`, a = `${f[f.length - 1] ?? ""}${u}`, h = f.slice(1, -1);
3648
+ p = [m, ...h, a];
3174
3649
  }
3175
- o.splice(t, Math.max(0, i - t + 1), ...c);
3650
+ o.splice(t, Math.max(0, i - t + 1), ...p);
3176
3651
  }
3177
- function T(o) {
3652
+ function k(o) {
3178
3653
  if (!o) return "";
3179
3654
  if (typeof o == "string") return o;
3180
3655
  if (!Array.isArray(o)) return "";
@@ -3185,14 +3660,14 @@ function T(o) {
3185
3660
  continue;
3186
3661
  }
3187
3662
  if (Array.isArray(e)) {
3188
- typeof e[1] == "string" ? t += e[1] : typeof e[0] == "string" ? t += e[0] : t += T(e);
3663
+ typeof e[1] == "string" ? t += e[1] : typeof e[0] == "string" ? t += e[0] : t += k(e);
3189
3664
  continue;
3190
3665
  }
3191
3666
  e && typeof e == "object" && typeof e.text == "string" && (t += e.text);
3192
3667
  }
3193
3668
  return t;
3194
3669
  }
3195
- function be(o) {
3670
+ function _e(o) {
3196
3671
  if (!Array.isArray(o)) return [];
3197
3672
  const t = [];
3198
3673
  for (const e of o) {
@@ -3212,12 +3687,12 @@ function be(o) {
3212
3687
  }
3213
3688
  return t.filter((e) => e.word.length > 0);
3214
3689
  }
3215
- const _e = new URL("./nvimWorker.js", import.meta.url), we = new URL("./nvimWorkerAsyncify.js", import.meta.url);
3690
+ const Ce = new URL("./nvimWorker.js", import.meta.url), we = new URL("./nvimWorkerAsyncify.js", import.meta.url);
3216
3691
  export {
3217
- le as MonacoNeovimClient,
3218
- te as NeovimWasmSession,
3219
- Ce as createMonacoNeovim,
3220
- _e as defaultWorkerUrl,
3692
+ ce as MonacoNeovimClient,
3693
+ ie as NeovimWasmSession,
3694
+ ve as createMonacoNeovim,
3695
+ Ce as defaultWorkerUrl,
3221
3696
  we as defaultWorkerUrlAsyncify,
3222
- Z as isSharedArrayBufferAvailable
3697
+ ee as isSharedArrayBufferAvailable
3223
3698
  };