@monaco-neovim-wasm/lib 0.1.11 → 0.1.13

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,19 @@ 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
+ lastClipboardText = null;
975
+ ignoreInsertExitCursor = null;
976
+ ignoreMonacoCursorSyncToNvimUntil = 0;
887
977
  ignoreTextKeydownUntil = 0;
888
978
  lastImeCommitAt = 0;
889
979
  lastImeCommitText = "";
@@ -894,21 +984,27 @@ class le {
894
984
  optimisticCursorPos = null;
895
985
  optimisticCursorPrevPos = null;
896
986
  delegateInsertToMonaco = !1;
987
+ editorReadOnly = null;
897
988
  applyingFromNvim = !1;
898
989
  buffers = /* @__PURE__ */ new Map();
899
990
  buffersByName = /* @__PURE__ */ new Map();
900
991
  cursorSyncTimer = null;
992
+ ignoreSelectionSyncUntil = 0;
993
+ ignoreActiveBufLinesEventsUntil = 0;
901
994
  modelContentDisposable = null;
902
995
  originalOptions = null;
903
996
  resyncTimer = null;
904
997
  debugLog(t) {
905
998
  if (this.opts.debug)
906
999
  try {
907
- const e = `[monaco-neovim] ${t}`;
908
- this.opts.debugLog ? this.opts.debugLog(e) : typeof console < "u" && console.debug && console.debug(e);
1000
+ const e = `[monaco-neovim-wasm] ${t}`;
1001
+ this.opts.debugLog ? this.opts.debugLog(e) : typeof console < "u" && console.log(e);
909
1002
  } catch {
910
1003
  }
911
1004
  }
1005
+ nowMs() {
1006
+ return typeof performance < "u" && performance.now ? performance.now() : Date.now();
1007
+ }
912
1008
  scheduleRecordingRefresh() {
913
1009
  this.recordingRefreshTimer || (this.recordingRefreshTimer = window.setTimeout(() => {
914
1010
  this.recordingRefreshTimer = null, this.refreshRecordingState();
@@ -926,10 +1022,26 @@ class le {
926
1022
  const e = String(t ?? "");
927
1023
  if (!e) return;
928
1024
  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)));
1025
+ e.length > 1 && e === this.lastImeCommitText && i - this.lastImeCommitAt < 60 || (this.lastImeCommitText = e, this.lastImeCommitAt = i, this.sendInput(F(e, !0)));
930
1026
  }
931
1027
  constructor(t, e = {}) {
932
- this.editor = t, this.opts = {
1028
+ this.editor = t;
1029
+ const i = (() => {
1030
+ try {
1031
+ return !!globalThis?.__MONACO_NEOVIM_WASM_DEBUG__;
1032
+ } catch {
1033
+ return !1;
1034
+ }
1035
+ })(), s = (() => {
1036
+ try {
1037
+ if (typeof location > "u" || !location?.search) return !1;
1038
+ const l = new URLSearchParams(location.search);
1039
+ return l.has("monaco-neovim-wasm-debug") || l.has("mnw_debug");
1040
+ } catch {
1041
+ return !1;
1042
+ }
1043
+ })(), r = i || s ? !0 : !!e.debug;
1044
+ this.opts = {
933
1045
  worker: e.worker ?? null,
934
1046
  workerUrl: e.workerUrl ?? new URL("./nvimWorker.js", import.meta.url),
935
1047
  reuseWorker: e.reuseWorker ?? !1,
@@ -938,7 +1050,7 @@ class le {
938
1050
  inputMode: e.inputMode ?? "shared",
939
1051
  env: e.env,
940
1052
  files: e.files,
941
- sharedInputBytes: e.sharedInputBytes ?? N,
1053
+ sharedInputBytes: e.sharedInputBytes ?? L,
942
1054
  cols: e.cols ?? 120,
943
1055
  rows: e.rows ?? 40,
944
1056
  minCols: e.minCols ?? 20,
@@ -964,7 +1076,7 @@ class le {
964
1076
  onHostCommand: e.onHostCommand,
965
1077
  status: e.status ?? (() => {
966
1078
  }),
967
- seedLines: e.seedLines ?? re,
1079
+ seedLines: e.seedLines ?? oe,
968
1080
  seedName: e.seedName ?? "monaco-demo.lua",
969
1081
  seedFiletype: e.seedFiletype ?? "lua",
970
1082
  uiAttach: e.uiAttach ?? !0,
@@ -978,7 +1090,7 @@ class le {
978
1090
  "set noswapfile signcolumn=no number norelativenumber",
979
1091
  "set nowrap laststatus=0 cmdheight=1",
980
1092
  "set shortmess+=F",
981
- "set clipboard=unnamedplus"
1093
+ ...e.clipboard === null ? [] : ["set clipboard=unnamedplus"]
982
1094
  ],
983
1095
  startupLua: e.startupLua ?? "",
984
1096
  visualThemeName: e.visualThemeName ?? "nvim-visual",
@@ -994,11 +1106,11 @@ class le {
994
1106
  onPopupmenu: e.onPopupmenu,
995
1107
  cmdlineContainer: e.cmdlineContainer,
996
1108
  insertSyncDebounceMs: Number.isFinite(e.insertSyncDebounceMs) ? Math.max(0, Number(e.insertSyncDebounceMs)) : 20,
997
- debug: e.debug ?? !1,
1109
+ debug: r,
998
1110
  debugLog: e.debugLog,
999
1111
  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;
1112
+ translateKey: e.translateKey ?? ue
1113
+ }, 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
1114
  }
1003
1115
  async start(t) {
1004
1116
  this.stop(!0), this.nextSeedLines = t ?? null;
@@ -1049,7 +1161,7 @@ class le {
1049
1161
  this.opts.status(s, !0);
1050
1162
  }
1051
1163
  };
1052
- if (!this.session || !this.opts.reuseWorker ? this.session = new te({
1164
+ if (!this.session || !this.opts.reuseWorker ? this.session = new ie({
1053
1165
  worker: this.opts.worker,
1054
1166
  workerUrl: this.opts.workerUrl,
1055
1167
  inputMode: this.opts.inputMode,
@@ -1066,12 +1178,12 @@ class le {
1066
1178
  runtimePath: this.opts.runtimePath,
1067
1179
  inputMode: this.opts.inputMode,
1068
1180
  env: this.opts.env,
1069
- files: fe(me(
1181
+ files: pe(me(
1070
1182
  this.opts.files,
1071
1183
  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 }
1184
+ { path: "home/.config/nvim/monaco-neovim-wasm/motion.vim", data: ne },
1185
+ { path: "home/.config/nvim/monaco-neovim-wasm/scrolling.vim", data: se },
1186
+ { path: "home/.config/nvim/monaco-neovim-wasm/host-commands.vim", data: re }
1075
1187
  ] : []
1076
1188
  ))
1077
1189
  }), this.opts.status("starting..."), this.primeSent = !1, setTimeout(() => {
@@ -1101,7 +1213,7 @@ class le {
1101
1213
  this.sendNotify("nvim_input", [String(t ?? "")]);
1102
1214
  }
1103
1215
  type(t, e = !0) {
1104
- const i = k(String(t ?? ""), e);
1216
+ const i = F(String(t ?? ""), e);
1105
1217
  i && this.sendNotify("nvim_input", [i]);
1106
1218
  }
1107
1219
  paste(t) {
@@ -1114,8 +1226,8 @@ class le {
1114
1226
  const r = await this.rpcCall("nvim_exec_lua", [i, s]);
1115
1227
  return this.execLuaAvailable = !0, r;
1116
1228
  } catch (r) {
1117
- const a = r?.message || String(r);
1118
- if (a.includes("Invalid method") && a.includes("nvim_exec_lua"))
1229
+ const l = r?.message || String(r);
1230
+ if (l.includes("Invalid method") && l.includes("nvim_exec_lua"))
1119
1231
  this.execLuaAvailable = !1;
1120
1232
  else
1121
1233
  throw r;
@@ -1139,7 +1251,7 @@ end)(unpack(_A))`;
1139
1251
  }
1140
1252
  attachEditorListeners() {
1141
1253
  this.disposeEditorListeners();
1142
- const t = g.editor.EditorOption;
1254
+ const t = y.editor.EditorOption;
1143
1255
  try {
1144
1256
  const e = this.editor.getOption(t.fontInfo);
1145
1257
  this.initialCursorWidth = this.editor.getOption(t.cursorWidth) || 0, this.typicalFullWidth = e?.typicalFullwidthCharacterWidth || 2;
@@ -1167,12 +1279,12 @@ end)(unpack(_A))`;
1167
1279
  } catch {
1168
1280
  this.originalOptions = null;
1169
1281
  }
1170
- if (this.editor.updateOptions({ readOnly: !1, domReadOnly: !1 }), this.opts.syncWrap) {
1282
+ if (this.setEditorReadOnly(!this.delegateInsertToMonaco), this.editor.updateOptions({ domReadOnly: !1 }), this.opts.syncWrap) {
1171
1283
  const e = this.uiCols || this.opts.cols;
1172
1284
  e > 0 && this.applyMonacoWrap(e);
1173
1285
  }
1174
1286
  this.attachActiveModelListener(), this.disposables.push(
1175
- this.editor.onDidChangeModel(() => this.attachActiveModelListener()),
1287
+ this.editor.onDidChangeModel(() => this.handleActiveModelChanged()),
1176
1288
  this.editor.onKeyDown((e) => this.handleKey(e)),
1177
1289
  this.editor.onMouseDown((e) => this.handleMouse(e)),
1178
1290
  this.editor.onDidChangeCursorSelection((e) => this.handleSelection(e)),
@@ -1182,11 +1294,23 @@ end)(unpack(_A))`;
1182
1294
  this.compositionActive && this.positionPreedit(), this.scheduleCursorSyncToNvim();
1183
1295
  return;
1184
1296
  }
1185
- if (this.compositionActive) {
1186
- this.positionPreedit();
1187
- return;
1297
+ if (!(this.nowMs() < this.ignoreMonacoCursorSyncToNvimUntil && e.source !== "mouse")) {
1298
+ if (this.compositionActive) {
1299
+ this.positionPreedit();
1300
+ return;
1301
+ }
1302
+ if (!(this.suppressCursorSync || !this.lastCursorPos)) {
1303
+ if (e.source === "mouse") {
1304
+ const s = this.editor.getSelection();
1305
+ s && s.isEmpty() && this.syncCursorToNvimNow(!0);
1306
+ return;
1307
+ }
1308
+ if (e.source === "keyboard") {
1309
+ this.suppressCursorSync = !0, this.editor.setPosition(this.lastCursorPos), this.suppressCursorSync = !1;
1310
+ return;
1311
+ }
1312
+ }
1188
1313
  }
1189
- this.suppressCursorSync || !this.lastCursorPos || e.source === "keyboard" && (this.suppressCursorSync = !0, this.editor.setPosition(this.lastCursorPos), this.suppressCursorSync = !1);
1190
1314
  }),
1191
1315
  this.editor.onDidScrollChange(() => {
1192
1316
  this.compositionActive && this.positionPreedit();
@@ -1195,6 +1319,20 @@ end)(unpack(_A))`;
1195
1319
  this.editor.onDidScrollChange(() => {
1196
1320
  this.opts.searchHighlights && (this.compositionActive || this.scheduleSearchHighlightRefresh());
1197
1321
  })
1322
+ ), this.disposables.push(
1323
+ this.editor.onDidScrollChange(() => {
1324
+ this.visualVirtualActive && this.renderVisualVirtualOverlay();
1325
+ }),
1326
+ this.editor.onDidLayoutChange(() => {
1327
+ this.visualVirtualActive && this.renderVisualVirtualOverlay();
1328
+ }),
1329
+ this.editor.onDidChangeConfiguration((e) => {
1330
+ this.visualVirtualActive && (e.hasChanged(t.fontInfo) || e.hasChanged(t.lineHeight) || e.hasChanged(t.fontSize) || e.hasChanged(t.fontFamily)) && this.renderVisualVirtualOverlay();
1331
+ })
1332
+ ), this.disposables.push(
1333
+ this.editor.onDidChangeConfiguration((e) => {
1334
+ e.hasChanged(t.readOnly) && !this.delegateInsertToMonaco && this.setEditorReadOnly(!0);
1335
+ })
1198
1336
  ), this.opts.autoResize && this.disposables.push(
1199
1337
  this.editor.onDidLayoutChange(() => this.scheduleResizeToEditor()),
1200
1338
  this.editor.onDidChangeConfiguration((e) => {
@@ -1202,10 +1340,32 @@ end)(unpack(_A))`;
1202
1340
  })
1203
1341
  ), this.initCmdlineUi(), this.initTextInputListeners();
1204
1342
  }
1343
+ handleActiveModelChanged() {
1344
+ if (this.attachActiveModelListener(), !this.session || !this.session.isRunning() || !this.bufHandle) return;
1345
+ const t = this.editor.getModel();
1346
+ if (!t) return;
1347
+ try {
1348
+ const i = t.uri, s = i?.scheme ?? "", n = i?.authority ?? "", r = i?.path ?? "";
1349
+ if (s === "nvim" && n === "buf") {
1350
+ const l = /^\/(\d+)$/.exec(r), c = l ? Number(l[1]) : NaN;
1351
+ Number.isFinite(c) && c > 0 && (this.bufHandle = c);
1352
+ }
1353
+ } catch {
1354
+ }
1355
+ const e = this.ensureActiveState();
1356
+ if (e && e.model !== t) {
1357
+ 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;
1358
+ try {
1359
+ const i = t.getLinesContent?.() ?? t.getValue().split(/\r?\n/);
1360
+ this.sendNotify("nvim_buf_set_lines", [this.bufHandle, 0, -1, !1, i]), this.syncCursorToNvimNow(!0);
1361
+ } catch {
1362
+ }
1363
+ }
1364
+ }
1205
1365
  computeGridSize() {
1206
1366
  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 };
1367
+ 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));
1368
+ if (Number.isFinite(l) && Number.isFinite(c) && l > 0 && c > 0) return { cols: l, rows: c };
1209
1369
  } catch {
1210
1370
  }
1211
1371
  return { cols: this.opts.cols, rows: this.opts.rows };
@@ -1228,7 +1388,7 @@ end)(unpack(_A))`;
1228
1388
  const e = document.createElement("div");
1229
1389
  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
1390
  try {
1231
- const i = this.editor.getOption(g.editor.EditorOption.fontInfo);
1391
+ const i = this.editor.getOption(y.editor.EditorOption.fontInfo);
1232
1392
  i?.fontFamily && (e.style.fontFamily = i.fontFamily), i?.fontSize && (e.style.fontSize = `${i.fontSize}px`), i?.lineHeight && (e.style.lineHeight = `${i.lineHeight}px`);
1233
1393
  } catch {
1234
1394
  }
@@ -1247,11 +1407,11 @@ end)(unpack(_A))`;
1247
1407
  if (!t || this.cmdlineVisible) return;
1248
1408
  const e = this.editor.getModel();
1249
1409
  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);
1410
+ 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/);
1411
+ let l = n.lineNumber, c = n.column;
1412
+ r.length <= 1 ? c += Array.from(r[0] ?? "").length : (l += r.length - 1, c = 1 + Array.from(r[r.length - 1] ?? "").length);
1413
+ const u = e.validatePosition(new y.Position(l, c)), d = this.editor.getPosition() ?? this.lastCursorPos ?? n;
1414
+ 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
1415
  }
1256
1416
  positionPreedit() {
1257
1417
  if (!this.preeditEl || !this.preeditVisible) return;
@@ -1268,7 +1428,7 @@ end)(unpack(_A))`;
1268
1428
  }
1269
1429
  this.modelContentDisposable = null;
1270
1430
  }
1271
- if (this.clearVisualDecorations(), this.clearSearchHighlights(), this.searchStyleEl) {
1431
+ if (this.clearVisualDecorations(), this.clearSearchHighlights(), this.setMonacoHighlightsSuppressed(!1), this.searchStyleEl) {
1272
1432
  try {
1273
1433
  this.searchStyleEl.remove();
1274
1434
  } catch {
@@ -1282,7 +1442,14 @@ end)(unpack(_A))`;
1282
1442
  }
1283
1443
  this.visualStyleEl = null;
1284
1444
  }
1285
- if (this.cmdlineEl) {
1445
+ if (this.visualVirtualOverlayEl) {
1446
+ try {
1447
+ this.visualVirtualOverlayEl.remove();
1448
+ } catch {
1449
+ }
1450
+ this.visualVirtualOverlayEl = null;
1451
+ }
1452
+ if (this.visualVirtualRawRanges = [], this.visualVirtualActive = !1, this.cmdlineEl) {
1286
1453
  try {
1287
1454
  this.cmdlineEl.remove();
1288
1455
  } catch {
@@ -1317,6 +1484,7 @@ end)(unpack(_A))`;
1317
1484
  }
1318
1485
  this.originalOptions = null;
1319
1486
  }
1487
+ this.editorReadOnly = null;
1320
1488
  }
1321
1489
  attachActiveModelListener() {
1322
1490
  if (this.modelContentDisposable) {
@@ -1377,7 +1545,7 @@ end)(unpack(_A))`;
1377
1545
  await this.execLua(this.opts.startupLua, []);
1378
1546
  } catch {
1379
1547
  }
1380
- const t = await this.rpcCall("nvim_get_current_buf", []), e = x(t) ?? 1;
1548
+ const t = await this.rpcCall("nvim_get_current_buf", []), e = N(t) ?? 1;
1381
1549
  if (this.bufHandle = e, await this.rpcCall("nvim_buf_attach", [e, !0, {}]) !== !0) throw new Error("nvim_buf_attach failed");
1382
1550
  this.ensureActiveState(), this.opts.syncTabstop && this.syncTabstopFromMonaco();
1383
1551
  const s = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
@@ -1387,11 +1555,11 @@ end)(unpack(_A))`;
1387
1555
  try {
1388
1556
  const r = this.ensureActiveState();
1389
1557
  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);
1558
+ const l = await this.rpcCall("nvim_buf_get_name", [e]);
1559
+ r.name = typeof l == "string" ? l : "", r.name && this.buffersByName.set(r.name, e);
1392
1560
  try {
1393
- const u = await this.rpcCall("nvim_buf_get_option", [e, "filetype"]);
1394
- r.filetype = typeof u == "string" ? u : "";
1561
+ const c = await this.rpcCall("nvim_buf_get_option", [e, "filetype"]);
1562
+ r.filetype = typeof c == "string" ? c : "";
1395
1563
  } catch {
1396
1564
  }
1397
1565
  }
@@ -1405,15 +1573,25 @@ end)(unpack(_A))`;
1405
1573
  }
1406
1574
  handleClipboardCopy(t) {
1407
1575
  const e = (t ?? []).join(`
1408
- `), i = this.opts.clipboard;
1576
+ `);
1577
+ this.lastClipboardText = e;
1578
+ const i = this.opts.clipboard;
1409
1579
  if (i !== null) {
1410
1580
  if (i?.writeText) {
1411
1581
  i.writeText(e).catch(() => {
1582
+ navigator.clipboard?.writeText ? navigator.clipboard.writeText(e).catch(() => {
1583
+ A(e);
1584
+ }) : A(e);
1412
1585
  });
1413
1586
  return;
1414
1587
  }
1415
- navigator.clipboard?.writeText && navigator.clipboard.writeText(e).catch(() => {
1416
- });
1588
+ if (navigator.clipboard?.writeText) {
1589
+ navigator.clipboard.writeText(e).catch(() => {
1590
+ A(e);
1591
+ });
1592
+ return;
1593
+ }
1594
+ A(e);
1417
1595
  }
1418
1596
  }
1419
1597
  handleRequest(t, e, i) {
@@ -1422,22 +1600,22 @@ end)(unpack(_A))`;
1422
1600
  async handleNotify(t, e) {
1423
1601
  if (t === "monaco_cursorMove") {
1424
1602
  const i = e?.[0];
1425
- i && typeof i == "object" && (this.applyMonacoCursorMove(i), this.syncCursorToNvimNow());
1603
+ i && typeof i == "object" && (this.applyMonacoCursorMove(i), this.exitingInsertMode || this.syncCursorToNvimNow(!0));
1426
1604
  return;
1427
1605
  }
1428
1606
  if (t === "monaco_scroll") {
1429
1607
  const i = e?.[0];
1430
- i && typeof i == "object" && this.applyMonacoScroll(i) && this.syncCursorToNvimNow();
1608
+ i && typeof i == "object" && this.applyMonacoScroll(i) && !this.exitingInsertMode && this.syncCursorToNvimNow(!0);
1431
1609
  return;
1432
1610
  }
1433
1611
  if (t === "monaco_reveal") {
1434
1612
  const i = e?.[0];
1435
- i && typeof i == "object" && this.applyMonacoReveal(i) && this.syncCursorToNvimNow();
1613
+ i && typeof i == "object" && this.applyMonacoReveal(i) && !this.exitingInsertMode && this.syncCursorToNvimNow(!0);
1436
1614
  return;
1437
1615
  }
1438
1616
  if (t === "monaco_moveCursor") {
1439
1617
  const i = e?.[0];
1440
- i && typeof i == "object" && (this.applyMonacoMoveCursor(i), this.syncCursorToNvimNow());
1618
+ i && typeof i == "object" && (this.applyMonacoMoveCursor(i), this.exitingInsertMode || this.syncCursorToNvimNow(!0));
1441
1619
  return;
1442
1620
  }
1443
1621
  if (t === "monaco_scrolloff") {
@@ -1461,18 +1639,28 @@ end)(unpack(_A))`;
1461
1639
  return;
1462
1640
  }
1463
1641
  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;
1642
+ const [i, s] = e;
1643
+ try {
1644
+ const r = this.ignoreInsertExitCursor, l = this.nowMs();
1645
+ if (r && l < r.untilMs && !this.delegateInsertToMonaco && Number(i) === r.line && Number(s) === r.col0 && r.col0 > 0) {
1646
+ this.debugLog(`nvim->monaco cursor: ignore stale post-exit insert cursor ln=${Number(i)} col0=${Number(s)}`);
1647
+ return;
1648
+ }
1649
+ r && l >= r.untilMs && (this.ignoreInsertExitCursor = null);
1650
+ } catch {
1651
+ }
1652
+ const n = K(this.editor, Number(i), Number(s));
1653
+ 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) {
1654
+ const r = this.editor.getModel(), l = r ? r.validatePosition(new y.Position(n.line, n.col)) : new y.Position(n.line, n.col);
1655
+ this.lastCursorPos = l;
1468
1656
  return;
1469
1657
  }
1470
- this.updateCursor(n.line, n.col), w(this.lastMode) && this.scheduleVisualSelectionRefresh();
1658
+ this.updateCursor(n.line, n.col), S(this.lastMode) && this.scheduleVisualSelectionRefresh();
1471
1659
  return;
1472
1660
  }
1473
1661
  if (t === "monaco_mode") {
1474
1662
  const i = typeof e?.[0] == "string" ? String(e[0]) : "";
1475
- this.applyNvimMode(i);
1663
+ this.hostAutocmdInstalled || (this.hostAutocmdInstalled = !0), this.debugLog(`nvim->monaco mode: ${JSON.stringify(this.lastMode)} -> ${JSON.stringify(i)}`), this.applyNvimMode(i);
1476
1664
  return;
1477
1665
  }
1478
1666
  if (t === "monaco_recording") {
@@ -1481,45 +1669,49 @@ end)(unpack(_A))`;
1481
1669
  return;
1482
1670
  }
1483
1671
  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) {
1672
+ const [i, s, n, r, l] = e, c = N(i);
1673
+ if (!c) return;
1674
+ const u = this.bufHandle != null && c === this.bufHandle ? this.ensureActiveState() : this.buffers.get(c) ?? null;
1675
+ if (!u || this.bufHandle != null && c === this.bufHandle && this.delegateInsertToMonaco && !this.exitingInsertMode && this.nowMs() < this.ignoreActiveBufLinesEventsUntil)
1676
+ return;
1677
+ if (this.bufHandle != null && c === this.bufHandle && this.compositionActive) {
1489
1678
  this.pendingResyncAfterComposition = !0;
1490
1679
  return;
1491
1680
  }
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)
1681
+ const d = u.model, f = Number(n), p = Number(r), m = Array.isArray(l) ? l.map((h) => String(h ?? "")) : null;
1682
+ if (d && Number.isInteger(f) && Number.isInteger(p) && f >= 0 && p >= f && m)
1494
1683
  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);
1684
+ const h = this.bufHandle != null && c === this.bufHandle && this.editor.getModel() === d;
1685
+ if (h && this.delegateInsertToMonaco) {
1686
+ const g = this.computeLinePatch(d, f, p, m);
1498
1687
  let b = !1;
1499
1688
  try {
1500
- b = f.getValueInRange(y.range) === y.text;
1689
+ b = d.getValueInRange(g.range) === g.text;
1501
1690
  } catch {
1502
1691
  }
1503
1692
  if (!b) {
1504
- if (d.pendingFullSync || d.pendingBufEdits.length) return;
1505
- this.applyLinePatchToModel(f, l, c, h);
1693
+ if (u.pendingFullSync || u.pendingBufEdits.length) {
1694
+ this.scheduleResync();
1695
+ return;
1696
+ }
1697
+ this.applyLinePatchToModel(d, f, p, m);
1506
1698
  try {
1507
- d.shadowLines = f.getLinesContent();
1699
+ u.shadowLines = d.getLinesContent();
1508
1700
  } catch {
1509
1701
  }
1510
1702
  }
1511
- } else p ? this.applyLinePatch(f, l, c, h) : this.applyLinePatchToModel(f, l, c, h);
1703
+ } else h ? this.applyLinePatch(d, f, p, m) : this.applyLinePatchToModel(d, f, p, m);
1512
1704
  } catch {
1513
1705
  }
1514
1706
  else
1515
1707
  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);
1708
+ const h = await this.rpcCall("nvim_buf_get_lines", [c, 0, -1, !1]), g = Array.isArray(h) ? h : [""];
1709
+ this.bufHandle != null && c === this.bufHandle && this.editor.getModel() === d ? this.applyBuffer(g) : this.setModelText(d, g);
1518
1710
  } catch {
1519
1711
  }
1520
- this.bufHandle != null && u === this.bufHandle && w(this.lastMode) && this.scheduleVisualSelectionRefresh();
1712
+ this.bufHandle != null && c === this.bufHandle && S(this.lastMode) && this.scheduleVisualSelectionRefresh();
1521
1713
  } else if (t === "nvim_buf_detach_event") {
1522
- const i = x(e?.[0]);
1714
+ const i = N(e?.[0]);
1523
1715
  if (i && this.buffers.has(i)) {
1524
1716
  const s = this.buffers.get(i);
1525
1717
  if (s.name && this.buffersByName.delete(s.name), s.createdModel)
@@ -1533,24 +1725,24 @@ end)(unpack(_A))`;
1533
1725
  } else t === "redraw" && this.handleRedraw(e);
1534
1726
  }
1535
1727
  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(`
1728
+ 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;
1729
+ let p = s.join(`
1538
1730
  `);
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 };
1731
+ return e >= n && i >= n && s.length > 0 && (p = `
1732
+ ${p}`), i < n && s.length > 0 && (p += `
1733
+ `), { range: new y.Range(d.lineNumber, d.column, f.lineNumber, f.column), text: p };
1542
1734
  }
1543
1735
  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);
1736
+ const n = this.lastCursorPos ?? this.editor.getPosition() ?? new y.Position(1, 1), r = this.computeLinePatch(t, e, i, s);
1545
1737
  try {
1546
1738
  if (t.getValueInRange(r.range) === r.text) return;
1547
1739
  } catch {
1548
1740
  }
1549
1741
  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)
1742
+ const l = this.getActiveState();
1743
+ if (l && l.model === t)
1552
1744
  try {
1553
- a.shadowLines = t.getLinesContent();
1745
+ l.shadowLines = t.getLinesContent();
1554
1746
  } catch {
1555
1747
  }
1556
1748
  }
@@ -1624,7 +1816,7 @@ ${c}`), i < n && s.length > 0 && (c += `
1624
1816
  this.cmdlineEl.textContent = this.cmdlineTextRaw, this.cmdlineCursorByte = null;
1625
1817
  return;
1626
1818
  }
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)));
1819
+ 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
1820
  this.cmdlineCursorByte = i, this.cmdlineEl.textContent = `${e.slice(0, s)}▏${e.slice(s)}`;
1629
1821
  }
1630
1822
  setMessage(t) {
@@ -1660,8 +1852,8 @@ ${c}`), i < n && s.length > 0 && (c += `
1660
1852
  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
1853
  const r = document.createElement("span");
1662
1854
  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);
1855
+ const l = document.createElement("span");
1856
+ l.style.opacity = "0.7", l.textContent = s.menu ?? s.kind ?? "", n.appendChild(r), n.appendChild(l), this.popupEl.appendChild(n);
1665
1857
  }
1666
1858
  this.popupEl.style.display = "block";
1667
1859
  }
@@ -1689,10 +1881,10 @@ ${c}`), i < n && s.length > 0 && (c += `
1689
1881
  } catch {
1690
1882
  }
1691
1883
  if (i === "wrappedLine" && (e === "down" || e === "up")) {
1692
- const a = e === "down" ? "cursorDown" : "cursorUp";
1693
- for (let u = 0; u < s; u += 1)
1884
+ const l = e === "down" ? "cursorDown" : "cursorUp";
1885
+ for (let c = 0; c < s; c += 1)
1694
1886
  try {
1695
- this.editor.trigger("monaco-neovim-wasm", a, null);
1887
+ this.editor.trigger("monaco-neovim-wasm", l, null);
1696
1888
  } catch {
1697
1889
  }
1698
1890
  return !0;
@@ -1712,90 +1904,90 @@ ${c}`), i < n && s.length > 0 && (c += `
1712
1904
  if (e <= 0) return !1;
1713
1905
  const i = t ?? this.editor.getPosition();
1714
1906
  if (!i) return !1;
1715
- const s = Math.max(1, Number(this.editor.getOption(g.editor.EditorOption.lineHeight)) || 0), n = e * s;
1907
+ const s = Math.max(1, Number(this.editor.getOption(y.editor.EditorOption.lineHeight)) || 0), n = e * s;
1716
1908
  if (n <= 0) return !1;
1717
1909
  let r = 0;
1718
1910
  try {
1719
- const m = this.editor.getLayoutInfo();
1720
- r = Math.max(0, Number(m?.contentHeight ?? m?.height ?? 0) || 0);
1911
+ const a = this.editor.getLayoutInfo();
1912
+ r = Math.max(0, Number(a?.contentHeight ?? a?.height ?? 0) || 0);
1721
1913
  } catch {
1722
1914
  }
1723
1915
  if (r <= 0) return !1;
1724
- const a = () => {
1916
+ const l = () => {
1725
1917
  try {
1726
1918
  this.editor.revealPositionInCenterIfOutsideViewport(i);
1727
1919
  } catch {
1728
1920
  }
1729
1921
  };
1730
- let u = null;
1922
+ let c = null;
1731
1923
  try {
1732
- u = this.editor.getScrolledVisiblePosition(i);
1924
+ c = this.editor.getScrolledVisiblePosition(i);
1733
1925
  } catch {
1734
1926
  }
1735
- if (!u) {
1736
- a();
1927
+ if (!c) {
1928
+ l();
1737
1929
  try {
1738
- u = this.editor.getScrolledVisiblePosition(i);
1930
+ c = this.editor.getScrolledVisiblePosition(i);
1739
1931
  } catch {
1740
1932
  }
1741
1933
  }
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;
1934
+ if (!c || !Number.isFinite(c.top) || !Number.isFinite(c.height)) return !1;
1935
+ const u = this.editor.getScrollTop(), d = c.top, f = c.top + c.height, p = Math.max(0, r - n);
1936
+ let m = 0;
1937
+ if (d < n ? m = d - n : f > p && (m = f - p), !m) return !1;
1746
1938
  try {
1747
- this.editor.setScrollTop(Math.max(0, d + h));
1939
+ this.editor.setScrollTop(Math.max(0, u + m));
1748
1940
  } catch {
1749
1941
  }
1750
1942
  return !0;
1751
1943
  }
1752
1944
  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;
1945
+ 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));
1946
+ let p = s;
1947
+ if (e === "page") p = d * s;
1948
+ else if (e === "halfPage") p = f * s;
1949
+ else if (e === "line") p = s;
1758
1950
  else return !1;
1759
- const h = i === "up" ? -1 : i === "down" ? 1 : 0;
1760
- if (!h) return !1;
1951
+ const m = i === "up" ? -1 : i === "down" ? 1 : 0;
1952
+ if (!m) return !1;
1761
1953
  this.suppressCursorSync = !0;
1762
1954
  try {
1763
- const p = this.editor.getScrollTop(), y = Math.max(0, p + h * c * d);
1764
- this.editor.setScrollTop(y);
1955
+ const h = this.editor.getScrollTop(), g = Math.max(0, h + m * p * u);
1956
+ this.editor.setScrollTop(g);
1765
1957
  } catch {
1766
1958
  }
1767
- let m = !1;
1959
+ let a = !1;
1768
1960
  if (n)
1769
1961
  try {
1770
1962
  this.applyMonacoCursorMove({
1771
- to: h > 0 ? "down" : "up",
1963
+ to: m > 0 ? "down" : "up",
1772
1964
  by: r,
1773
- value: c
1774
- }), m = !0;
1965
+ value: p
1966
+ }), a = !0;
1775
1967
  } catch {
1776
1968
  }
1777
- return this.suppressCursorSync = !1, m;
1969
+ return this.suppressCursorSync = !1, a;
1778
1970
  }
1779
1971
  applyMonacoReveal(t) {
1780
1972
  const e = typeof t.direction == "string" ? t.direction : "", i = !!t.resetCursor, s = this.editor.getPosition();
1781
1973
  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);
1974
+ 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
1975
  this.suppressCursorSync = !0;
1784
1976
  try {
1785
1977
  if (e === "top") {
1786
- const d = this.editor.getTopForLineNumber(n);
1787
- this.editor.setScrollTop(d);
1978
+ const u = this.editor.getTopForLineNumber(n);
1979
+ this.editor.setScrollTop(u);
1788
1980
  } else if (e === "center")
1789
1981
  this.editor.revealLineInCenter(n);
1790
1982
  else if (e === "bottom") {
1791
- const d = this.editor.getTopForLineNumber(n), f = Math.max(0, d - (r - 1) * u);
1792
- this.editor.setScrollTop(f);
1983
+ const u = this.editor.getTopForLineNumber(n), d = Math.max(0, u - (r - 1) * c);
1984
+ this.editor.setScrollTop(d);
1793
1985
  }
1794
1986
  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;
1987
+ const u = this.editor.getModel();
1988
+ if (u) {
1989
+ const d = u.getLineContent(n) ?? "", f = /\S/.exec(d), p = f ? f.index + 1 : 1, m = u.validatePosition(new y.Position(n, p));
1990
+ this.editor.setPosition(m), this.lastCursorPos = m;
1799
1991
  }
1800
1992
  }
1801
1993
  } catch {
@@ -1810,28 +2002,28 @@ ${c}`), i < n && s.length > 0 && (c += `
1810
2002
  const s = Math.max(1, Number(t.value ?? 1) || 1);
1811
2003
  let n = 1, r = i.getLineCount();
1812
2004
  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)));
2005
+ const p = this.editor.getVisibleRanges();
2006
+ p && p.length && (n = Math.min(...p.map((m) => m.startLineNumber)), r = Math.max(...p.map((m) => m.endLineNumber)));
1815
2007
  } catch {
1816
2008
  }
1817
2009
  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;
2010
+ let l = n;
1819
2011
  if (e === "top")
1820
- a = n + (s - 1);
2012
+ l = n + (s - 1);
1821
2013
  else if (e === "middle")
1822
- a = Math.floor((n + r) / 2);
2014
+ l = Math.floor((n + r) / 2);
1823
2015
  else if (e === "bottom")
1824
- a = r - (s - 1);
2016
+ l = r - (s - 1);
1825
2017
  else
1826
2018
  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));
2019
+ l = Math.max(n, Math.min(r, l));
2020
+ const c = i.getLineContent(l) ?? "", u = /\S/.exec(c), d = u ? u.index + 1 : 1, f = i.validatePosition(new y.Position(l, d));
1829
2021
  this.suppressCursorSync = !0;
1830
2022
  try {
1831
- this.editor.setPosition(l);
2023
+ this.editor.setPosition(f);
1832
2024
  } catch {
1833
2025
  }
1834
- this.lastCursorPos = l, this.suppressCursorSync = !1;
2026
+ this.lastCursorPos = f, this.suppressCursorSync = !1;
1835
2027
  }
1836
2028
  syncTabstopFromMonaco() {
1837
2029
  const t = this.editor.getModel();
@@ -1865,17 +2057,17 @@ ${c}`), i < n && s.length > 0 && (c += `
1865
2057
  continue;
1866
2058
  }
1867
2059
  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;
2060
+ 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}`;
2061
+ this.setCmdline(a);
2062
+ const h = B(`${p}${f}`), g = B(m);
2063
+ this.cmdlineCursorOffsetBytes = h, this.cmdlineCursorContentBytes = g;
2064
+ const b = l <= g ? this.cmdlineCursorOffsetBytes + l : l;
1873
2065
  this.setCmdlineCursor(b), this.scheduleSearchHighlightRefresh();
1874
2066
  continue;
1875
2067
  }
1876
2068
  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();
2069
+ const r = Math.max(0, Number(n[0] ?? 0) || 0), l = this.cmdlineCursorOffsetBytes > 0 && r <= this.cmdlineCursorContentBytes ? this.cmdlineCursorOffsetBytes + r : r;
2070
+ this.setCmdlineCursor(l), this.scheduleSearchHighlightRefresh();
1879
2071
  continue;
1880
2072
  }
1881
2073
  if (i === "msg_clear") {
@@ -1883,26 +2075,26 @@ ${c}`), i < n && s.length > 0 && (c += `
1883
2075
  continue;
1884
2076
  }
1885
2077
  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) {
2078
+ const r = typeof n[0] == "string" ? n[0] : "", l = n[1], c = !!n[2], u = !!n[4], d = k(l);
2079
+ if (r === "empty" && !d) {
1888
2080
  this.setMessage(null);
1889
2081
  continue;
1890
2082
  }
1891
- if (!f) continue;
1892
- if (d && this.messageEl?.textContent) {
1893
- this.setMessage(`${this.messageEl.textContent}${f}`);
2083
+ if (!d) continue;
2084
+ if (u && this.messageEl?.textContent) {
2085
+ this.setMessage(`${this.messageEl.textContent}${d}`);
1894
2086
  continue;
1895
2087
  }
1896
- if (u) {
1897
- this.setMessage(f);
2088
+ if (c) {
2089
+ this.setMessage(d);
1898
2090
  continue;
1899
2091
  }
1900
- this.setMessage(f);
2092
+ this.setMessage(d);
1901
2093
  continue;
1902
2094
  }
1903
2095
  if (i === "msg_showmode" || i === "msg_showcmd" || i === "msg_ruler") {
1904
- const r = n[0], a = T(r);
1905
- this.setMessage(a || null);
2096
+ const r = n[0], l = k(r);
2097
+ this.setMessage(l || null);
1906
2098
  continue;
1907
2099
  }
1908
2100
  if (i === "popupmenu_hide") {
@@ -1910,8 +2102,8 @@ ${c}`), i < n && s.length > 0 && (c += `
1910
2102
  continue;
1911
2103
  }
1912
2104
  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);
2105
+ const r = n[0], l = Number(n[1] ?? -1), c = _e(r);
2106
+ this.setPopupmenu(c, Number.isFinite(l) ? l : -1);
1915
2107
  continue;
1916
2108
  }
1917
2109
  if (i === "popupmenu_select") {
@@ -1921,7 +2113,7 @@ ${c}`), i < n && s.length > 0 && (c += `
1921
2113
  }
1922
2114
  if (i === "mode_change") {
1923
2115
  const r = typeof n[0] == "string" ? n[0] : "";
1924
- this.applyNvimMode(r);
2116
+ this.hostAutocmdInstalled || this.applyNvimMode(r);
1925
2117
  continue;
1926
2118
  }
1927
2119
  }
@@ -1929,142 +2121,176 @@ ${c}`), i < n && s.length > 0 && (c += `
1929
2121
  initTextInputListeners() {
1930
2122
  const t = this.editor.getDomNode();
1931
2123
  if (!t) return;
1932
- const e = (l) => {
2124
+ const e = t.ownerDocument || document, i = e.defaultView || window, s = (a) => {
1933
2125
  try {
1934
- l.stopImmediatePropagation?.();
2126
+ a.stopImmediatePropagation?.();
1935
2127
  } catch {
1936
2128
  }
1937
2129
  try {
1938
- l.stopPropagation();
2130
+ a.stopPropagation();
1939
2131
  } catch {
1940
2132
  }
1941
- }, i = (l) => {
2133
+ }, n = (a) => {
1942
2134
  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;
2135
+ if (typeof this.editor.hasTextFocus == "function" ? !!this.editor.hasTextFocus() : !1) return !0;
2136
+ const g = a.target;
2137
+ if (g && t.contains(g)) return !0;
2138
+ const b = e.activeElement;
2139
+ return !!(b && t.contains(b));
2140
+ } catch {
2141
+ return !1;
2142
+ }
2143
+ }, r = (a) => {
2144
+ try {
2145
+ const h = a;
2146
+ return !h || typeof h.tagName != "string" ? null : h;
1947
2147
  } catch {
1948
2148
  return null;
1949
2149
  }
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);
2150
+ }, l = (a) => {
2151
+ if (!n(a) || this.compositionActive || a.isComposing || a.getModifierState?.("AltGraph")) return;
2152
+ const h = this.delegateInsertToMonaco && !this.exitingInsertMode;
2153
+ if (h) {
2154
+ if (a.key === "Escape") {
2155
+ if (!this.opts.shouldHandleKey(a)) return;
2156
+ s(a);
2157
+ try {
2158
+ a.preventDefault();
2159
+ } catch {
2160
+ }
2161
+ if (this.compositionActive || a.isComposing) {
2162
+ this.pendingEscAfterComposition = !0;
2163
+ return;
2164
+ }
2165
+ this.exitDelegatedInsertMode("<Esc>");
2166
+ return;
2167
+ }
2168
+ const _ = a.key;
2169
+ (_ === "ArrowLeft" || _ === "ArrowRight" || _ === "ArrowUp" || _ === "ArrowDown" || _ === "Home" || _ === "End" || _ === "PageUp" || _ === "PageDown" || _ === "Tab" || _ === "Enter") && (this.delegatedInsertReplayPossible = !1);
2170
+ }
2171
+ if (!h && (a.key === "Backspace" || a.key === "Delete" || a.key === "Escape")) {
2172
+ if (!this.opts.shouldHandleKey(a)) return;
2173
+ const _ = this.opts.translateKey(a);
2174
+ if (!_) return;
2175
+ s(a);
1958
2176
  try {
1959
- l.preventDefault();
2177
+ a.preventDefault();
1960
2178
  } catch {
1961
2179
  }
1962
- this.exitingInsertMode ? this.pendingKeysAfterExit += p : this.sendInput(p);
2180
+ if (this.exitingInsertMode && _ === "<Esc>") return;
2181
+ this.exitingInsertMode ? this.pendingKeysAfterExit += _ : this.sendInput(_);
1963
2182
  return;
1964
2183
  }
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);
2184
+ if (M(this.lastMode) && !this.delegateInsertToMonaco && !this.exitingInsertMode && !a.ctrlKey && !a.metaKey && typeof a.key == "string" && a.key.length === 1) {
2185
+ const _ = /^[\x20-\x7E]$/.test(a.key);
2186
+ if (!!!(a.altKey && _)) {
2187
+ if (!this.opts.shouldHandleKey(a)) return;
2188
+ s(a);
1970
2189
  return;
1971
2190
  }
1972
2191
  }
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);
2192
+ if (!a.ctrlKey && !a.altKey && !a.metaKey) return;
2193
+ const g = this.modifiedKeyName(a);
2194
+ if (!g || !this.opts.shouldHandleKey(a)) return;
2195
+ if (this.hasExplicitModAllowlist(h)) {
2196
+ if (!this.shouldForwardModifiedKeys(a, h)) return;
2197
+ } else if (h || !a.ctrlKey || a.altKey || a.metaKey || g !== "f" && g !== "b" && g !== "d" && g !== "u" && g !== "e" && g !== "y" && g !== "v") return;
2198
+ const b = this.opts.translateKey(a);
2199
+ if (b) {
2200
+ s(a);
1982
2201
  try {
1983
- l.preventDefault();
2202
+ a.preventDefault();
1984
2203
  } catch {
1985
2204
  }
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));
2205
+ 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
2206
  }
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;
2207
+ }, c = (a) => {
2208
+ if (n(a)) {
2209
+ if (this.compositionActive = !0, this.debugLog(`compositionstart delegateInsert=${this.delegateInsertToMonaco} mode=${JSON.stringify(this.lastMode)}`), this.delegateInsertToMonaco) {
2210
+ this.setPreedit(null);
2211
+ return;
2212
+ }
2213
+ this.setPreedit("");
1992
2214
  }
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();
2215
+ }, u = (a) => {
2216
+ if (!n(a)) return;
2217
+ 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) {
2218
+ if (this.pendingEscAfterComposition) {
2219
+ this.pendingEscAfterComposition = !1, this.exitDelegatedInsertMode("<Esc>");
2220
+ return;
2221
+ }
2222
+ this.exitingInsertMode || this.scheduleCursorSyncToNvim();
1997
2223
  return;
1998
2224
  }
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);
2225
+ const h = r(a.target);
2226
+ if (M(this.lastMode)) {
2227
+ 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;
2228
+ _ && this.sendCmdlineImeText(_);
2003
2229
  }
2004
2230
  try {
2005
- c && c.tagName === "TEXTAREA" ? c.value = "" : c && (c.textContent = "");
2231
+ h && h.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2006
2232
  } catch {
2007
2233
  }
2008
2234
  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);
2235
+ }, d = (a) => {
2236
+ if (!n(a) || (this.compositionActive || (this.compositionActive = !0), this.delegateInsertToMonaco)) return;
2237
+ 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) : "";
2238
+ this.setPreedit(g || "");
2239
+ }, f = (a) => {
2240
+ if (n(a) && !(this.delegateInsertToMonaco && !this.exitingInsertMode)) {
2241
+ r(a.target), s(a);
2016
2242
  try {
2017
- l.preventDefault?.();
2243
+ a.preventDefault?.();
2018
2244
  } catch {
2019
2245
  }
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);
2246
+ if (M(this.lastMode) && !this.compositionActive) {
2247
+ const h = a, g = typeof h.data == "string" ? h.data : "", b = typeof h.inputType == "string" ? String(h.inputType) : "";
2248
+ !!!h.isComposing && !b.includes("Composition") && g && this.sendCmdlineImeText(g);
2023
2249
  }
2024
2250
  }
2025
- }, d = (l) => {
2026
- const c = i(l.target);
2027
- if (!c || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2028
- e(l);
2029
- const h = l;
2251
+ }, p = (a) => {
2252
+ if (!n(a) || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2253
+ const h = r(a.target);
2254
+ s(a);
2255
+ const g = a;
2030
2256
  if (this.ignoreNextInputEvent) {
2031
2257
  this.ignoreNextInputEvent = !1;
2032
2258
  try {
2033
- c.tagName === "TEXTAREA" ? c.value = "" : c.textContent = "";
2259
+ h?.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2034
2260
  } catch {
2035
2261
  }
2036
2262
  return;
2037
2263
  }
2038
2264
  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);
2265
+ if (M(this.lastMode)) {
2266
+ 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 || _;
2267
+ v && this.sendCmdlineImeText(v);
2042
2268
  }
2043
2269
  try {
2044
- c.tagName === "TEXTAREA" ? c.value = "" : c.textContent = "";
2270
+ h?.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2045
2271
  } catch {
2046
2272
  }
2047
2273
  }
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));
2274
+ }, m = (a) => {
2275
+ if (!n(a) || this.delegateInsertToMonaco && !this.exitingInsertMode) return;
2276
+ const h = r(a.target);
2277
+ s(a);
2278
+ const g = a.clipboardData?.getData("text/plain") ?? "";
2279
+ g && (a.preventDefault(), this.ignoreNextInputEvent = !0, this.pasteText(g));
2054
2280
  try {
2055
- c.tagName === "TEXTAREA" ? c.value = "" : c.textContent = "";
2281
+ h?.tagName === "TEXTAREA" ? h.value = "" : h && (h.textContent = "");
2056
2282
  } catch {
2057
2283
  }
2058
2284
  };
2059
2285
  this.disposables.push(
2060
2286
  // 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)
2287
+ x(i, "keydown", l, !0),
2288
+ x(i, "beforeinput", f, !0),
2289
+ x(i, "input", p, !0),
2290
+ x(i, "paste", m, !0),
2291
+ x(i, "compositionstart", c, !0),
2292
+ x(i, "compositionupdate", d, !0),
2293
+ x(i, "compositionend", u, !0)
2068
2294
  );
2069
2295
  }
2070
2296
  scheduleResync() {
@@ -2086,7 +2312,7 @@ ${c}`), i < n && s.length > 0 && (c += `
2086
2312
  const i = (t && t.length ? t : [""]).join(`
2087
2313
  `);
2088
2314
  if (e.getValue() === i) return;
2089
- const s = this.lastCursorPos ?? this.editor.getPosition() ?? new g.Position(1, 1);
2315
+ const s = this.lastCursorPos ?? this.editor.getPosition() ?? new y.Position(1, 1);
2090
2316
  if (this.suppressCursorSync = !0, this.applyingFromNvim = !0, e.setValue(i), this.applyingFromNvim = !1, s && this.editor.setPosition(s), this.suppressCursorSync = !1, this.delegateInsertToMonaco) {
2091
2317
  const n = this.getActiveState();
2092
2318
  if (n && n.model === e)
@@ -2097,16 +2323,22 @@ ${c}`), i < n && s.length > 0 && (c += `
2097
2323
  }
2098
2324
  }
2099
2325
  ensureVisualStyle() {
2100
- if (this.visualStyleEl) return;
2101
- const t = document.createElement("style");
2102
- t.id = "monaco-neovim-wasm-visual-style", t.textContent = `
2326
+ if (!this.visualStyleEl) {
2327
+ const t = document.createElement("style");
2328
+ t.id = "monaco-neovim-wasm-visual-style", t.textContent = `
2103
2329
  .monaco-neovim-visual-line {
2104
2330
  background-color: ${this.visualBgCss};
2105
2331
  }
2106
2332
  .monaco-neovim-visual-inline {
2107
2333
  background-color: ${this.visualBgCss};
2108
2334
  }
2335
+ .monaco-neovim-visual-virtual {
2336
+ position: absolute;
2337
+ background-color: ${this.visualBgCss};
2338
+ pointer-events: none;
2339
+ }
2109
2340
  `, document.head.appendChild(t), this.visualStyleEl = t;
2341
+ }
2110
2342
  }
2111
2343
  setVisualBgCss(t) {
2112
2344
  const e = t || this.visualBgCss;
@@ -2117,17 +2349,86 @@ ${c}`), i < n && s.length > 0 && (c += `
2117
2349
  .monaco-neovim-visual-inline {
2118
2350
  background-color: ${this.visualBgCss};
2119
2351
  }
2352
+ .monaco-neovim-visual-virtual {
2353
+ position: absolute;
2354
+ background-color: ${this.visualBgCss};
2355
+ pointer-events: none;
2356
+ }
2120
2357
  `));
2121
2358
  }
2122
- clearVisualDecorations() {
2123
- if (this.visualDecorationIds.length) {
2359
+ ensureVisualVirtualOverlay() {
2360
+ if (this.visualVirtualOverlayEl && this.visualVirtualOverlayEl.isConnected) return this.visualVirtualOverlayEl;
2361
+ const t = this.editor.getDomNode();
2362
+ if (!t) return null;
2363
+ const e = t.querySelector(".lines-content .view-overlays") ?? t.querySelector(".view-overlays") ?? t, i = document.createElement("div");
2364
+ 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;
2365
+ }
2366
+ clearVisualVirtualOverlay() {
2367
+ if (this.visualVirtualActive = !1, this.visualVirtualRawRanges = [], this.visualVirtualOverlayEl)
2124
2368
  try {
2125
- this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, []);
2369
+ this.visualVirtualOverlayEl.replaceChildren();
2126
2370
  } catch {
2127
- this.visualDecorationIds = [];
2128
2371
  }
2372
+ }
2373
+ renderVisualVirtualOverlay() {
2374
+ if (!this.visualVirtualActive) return;
2375
+ const t = this.ensureVisualVirtualOverlay();
2376
+ if (!t) return;
2377
+ const e = this.editor.getModel();
2378
+ if (!e) {
2379
+ try {
2380
+ t.replaceChildren();
2381
+ } catch {
2382
+ }
2383
+ return;
2384
+ }
2385
+ const i = this.editor.getDomNode();
2386
+ let s = 0, n = 0;
2387
+ if (i && t !== i)
2388
+ try {
2389
+ const d = i.getBoundingClientRect(), f = t.getBoundingClientRect();
2390
+ s = Number.isFinite(f.left - d.left) ? f.left - d.left : 0, n = Number.isFinite(f.top - d.top) ? f.top - d.top : 0;
2391
+ } catch {
2392
+ }
2393
+ const r = Array.isArray(this.visualVirtualRawRanges) ? this.visualVirtualRawRanges : [];
2394
+ if (r.length === 0) {
2395
+ try {
2396
+ t.replaceChildren();
2397
+ } catch {
2398
+ }
2399
+ return;
2400
+ }
2401
+ const l = this.editor.getOption(y.editor.EditorOption.fontInfo), c = Math.max(1, Number(l?.typicalHalfwidthCharacterWidth ?? l?.maxDigitWidth ?? 0) || 0), u = document.createDocumentFragment();
2402
+ for (const d of r) {
2403
+ const f = Number(d?.start?.line), p = Number(d?.start_vcol), m = Number(d?.end_vcol), a = Number(d?.disp);
2404
+ if (!Number.isFinite(f) || !Number.isFinite(p) || !Number.isFinite(m) || !Number.isFinite(a)) continue;
2405
+ const h = f + 1;
2406
+ if (h < 1 || h > e.getLineCount()) continue;
2407
+ const g = Math.min(p, m), b = Math.max(p, m), _ = this.editor.getScrolledVisiblePosition(new y.Position(h, 1));
2408
+ if (!_) continue;
2409
+ const v = b - g + 1;
2410
+ if (v <= 0) continue;
2411
+ const T = _.left + Math.max(0, g - 1) * c, E = v * c;
2412
+ if (!Number.isFinite(E) || E <= 0) continue;
2413
+ const C = document.createElement("div");
2414
+ 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);
2415
+ }
2416
+ try {
2417
+ t.replaceChildren(u);
2418
+ } catch {
2419
+ }
2420
+ }
2421
+ clearVisualDecorations() {
2422
+ if (this.clearVisualVirtualOverlay(), !this.visualDecorationIds.length) {
2129
2423
  this.visualSelectionActive = !1;
2424
+ return;
2130
2425
  }
2426
+ try {
2427
+ this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, []);
2428
+ } catch {
2429
+ this.visualDecorationIds = [];
2430
+ }
2431
+ this.visualSelectionActive = !1;
2131
2432
  }
2132
2433
  ensureSearchStyle() {
2133
2434
  if (this.searchStyleEl) return;
@@ -2188,90 +2489,189 @@ ${c}`), i < n && s.length > 0 && (c += `
2188
2489
  this.clearSearchHighlights();
2189
2490
  return;
2190
2491
  }
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;
2492
+ 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) => {
2493
+ if (d.has(p)) return d.get(p);
2494
+ const m = e.getLineContent(p) ?? "";
2495
+ return d.set(p, m), m;
2195
2496
  };
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;
2497
+ for (let p = 0; p < n.length && c.length < u; p += 1) {
2498
+ const m = n[p], a = Number(m?.l), h = Number(m?.s), g = Number(m?.e);
2499
+ if (!Number.isFinite(a) || !Number.isFinite(h) || !Number.isFinite(g)) continue;
2500
+ const b = a + 1;
2200
2501
  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 }
2502
+ const _ = f(b), v = I(_, Math.max(0, h)) + 1, T = I(_, Math.max(0, g)) + 1;
2503
+ if (T <= v) continue;
2504
+ const E = `${a}:${h}:${g}`, C = l && E === l ? "monaco-neovim-search-current" : "monaco-neovim-search-match";
2505
+ c.push({
2506
+ range: new y.Range(b, v, b, T),
2507
+ options: { inlineClassName: C }
2207
2508
  });
2208
2509
  }
2209
2510
  this.ensureSearchStyle();
2210
2511
  try {
2211
- this.searchDecorationIds = this.editor.deltaDecorations(this.searchDecorationIds, u);
2512
+ this.searchDecorationIds = this.editor.deltaDecorations(this.searchDecorationIds, c);
2212
2513
  } catch {
2213
2514
  this.searchDecorationIds = [];
2214
2515
  }
2215
2516
  }
2216
- applyVisualDecorations(t, e) {
2517
+ applyVisualDecorations(t, e, i = [], s = "") {
2217
2518
  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),
2519
+ const n = typeof s == "string" && s ? s : ye(e), r = n === "V", l = n === "", c = [];
2520
+ if (r) {
2521
+ let u = 1 / 0, d = -1 / 0;
2522
+ for (const f of t) {
2523
+ const p = f.getStartPosition(), m = f.getEndPosition();
2524
+ u = Math.min(u, p.lineNumber, m.lineNumber), d = Math.max(d, p.lineNumber, m.lineNumber);
2525
+ }
2526
+ Number.isFinite(u) && Number.isFinite(d) && d >= u && c.push({
2527
+ range: new y.Range(u, 1, d, 1),
2227
2528
  options: { isWholeLine: !0, className: "monaco-neovim-visual-line" }
2228
2529
  });
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" }
2530
+ } else if (!l)
2531
+ for (const u of t) {
2532
+ const d = u.getStartPosition(), f = u.getEndPosition();
2533
+ c.push({
2534
+ range: y.Range.fromPositions(d, f),
2535
+ options: { className: "monaco-neovim-visual-inline" }
2235
2536
  });
2236
2537
  }
2538
+ l ? (this.visualVirtualRawRanges = Array.isArray(i) ? i : [], this.visualVirtualActive = !0, this.renderVisualVirtualOverlay()) : this.clearVisualVirtualOverlay();
2237
2539
  try {
2238
- this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, n), this.visualSelectionActive = n.length > 0;
2540
+ this.visualDecorationIds = this.editor.deltaDecorations(this.visualDecorationIds, c), this.visualSelectionActive = c.length > 0 || l;
2239
2541
  } catch {
2240
2542
  this.visualDecorationIds = [], this.visualSelectionActive = !1;
2241
2543
  }
2544
+ if (this.visualSelectionActive && !this.compositionActive) {
2545
+ const u = this.editor.getPosition();
2546
+ if (u)
2547
+ try {
2548
+ this.suppressCursorSync = !0, this.editor.setSelection(new y.Selection(u.lineNumber, u.column, u.lineNumber, u.column));
2549
+ } catch {
2550
+ } finally {
2551
+ this.suppressCursorSync = !1;
2552
+ }
2553
+ }
2242
2554
  }
2243
2555
  applyNvimMode(t) {
2244
2556
  const e = typeof t == "string" ? t : "";
2245
2557
  if (!e || e === this.lastMode) return;
2246
2558
  const i = this.lastMode;
2247
- this.lastMode = e, v(e) && (this.ignoreNextInputEvent = !1);
2248
- const s = $(e) && !this.recordingRegister;
2559
+ this.lastMode = e, M(e) && (this.ignoreNextInputEvent = !1), this.setMonacoHighlightsSuppressed(S(e));
2560
+ const s = H(e) && !this.recordingRegister;
2249
2561
  if (s !== this.delegateInsertToMonaco) {
2250
- this.delegateInsertToMonaco = s;
2562
+ this.delegateInsertToMonaco = s, this.setEditorReadOnly(!s);
2251
2563
  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);
2564
+ if (s) {
2565
+ this.setPreedit(null), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !0;
2566
+ {
2567
+ 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"]);
2568
+ if (c === "cc")
2569
+ this.lastDelegatedInsertPrefix = "cc";
2570
+ else if (u.has(l))
2571
+ this.lastDelegatedInsertPrefix = l;
2572
+ else {
2573
+ const d = r.lastIndexOf("c");
2574
+ if (d >= 0) {
2575
+ let f = d;
2576
+ for (; f > 0 && /\d/.test(r[f - 1] ?? ""); ) f -= 1;
2577
+ this.lastDelegatedInsertPrefix = r.slice(f);
2578
+ } else
2579
+ this.lastDelegatedInsertPrefix = null;
2580
+ }
2581
+ }
2582
+ n && (n.shadowLines = this.editor.getModel()?.getLinesContent() ?? null, n.pendingBufEdits = [], n.pendingFullSync = !1, n.pendingCursorSync = !1);
2583
+ } else
2584
+ 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
2585
  }
2254
- if (this.exitingInsertMode && !$(e)) {
2586
+ if (this.exitingInsertMode && !H(e)) {
2255
2587
  this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.exitingInsertMode = !1;
2256
2588
  const n = this.pendingKeysAfterExit;
2257
2589
  this.pendingKeysAfterExit = "", n && this.sendInput(n);
2258
2590
  }
2259
- if (this.applyCursorStyle(e), this.opts.onModeChange && this.opts.onModeChange(e), this.updateVisualSelection(e), w(i) && !w(e))
2591
+ if (this.applyCursorStyle(e), this.opts.onModeChange && this.opts.onModeChange(e), this.updateVisualSelection(e), S(i) && !S(e))
2260
2592
  try {
2261
2593
  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);
2594
+ n && !this.compositionActive && (this.suppressCursorSync = !0, this.editor.setSelection(new y.Selection(n.lineNumber, n.column, n.lineNumber, n.column)), this.suppressCursorSync = !1);
2263
2595
  } catch {
2264
2596
  this.suppressCursorSync = !1;
2265
2597
  }
2266
2598
  this.scheduleSearchHighlightRefresh();
2267
2599
  }
2600
+ setEditorReadOnly(t) {
2601
+ const e = !!t;
2602
+ if (this.editorReadOnly !== e)
2603
+ try {
2604
+ this.editor.updateOptions({ readOnly: e }), this.editorReadOnly = e;
2605
+ } catch {
2606
+ }
2607
+ }
2268
2608
  armInsertExit() {
2269
- this.exitingInsertMode = !0, this.pendingKeysAfterExit = "", this.exitInsertTimer && (clearTimeout(this.exitInsertTimer), this.exitInsertTimer = null), this.exitInsertTimer = window.setTimeout(() => {
2609
+ 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
2610
  if (this.exitInsertTimer = null, !this.exitingInsertMode) return;
2271
- this.exitingInsertMode = !1;
2272
2611
  const t = this.pendingKeysAfterExit;
2273
2612
  this.pendingKeysAfterExit = "", t && this.sendInput(t);
2274
- }, 200);
2613
+ }, 800);
2614
+ }
2615
+ finalizeDelegatedInsertDotRepeat() {
2616
+ const t = this.lastDelegatedInsertPrefix, e = this.dotRepeatKeys;
2617
+ this.delegatedInsertReplayPossible && t && e ? this.lastDelegatedDotRepeat = { prefix: t, keys: e } : this.lastDelegatedDotRepeat = null, this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1;
2618
+ }
2619
+ exitDelegatedInsertMode(t) {
2620
+ 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);
2621
+ }
2622
+ async performDelegatedInsertExit(t) {
2623
+ if (!(!this.session || !this.session.isRunning()) && this.bufHandle) {
2624
+ try {
2625
+ this.debugLog(`performDelegatedInsertExit: begin buf=${this.bufHandle} exitKey=${JSON.stringify(t)}`);
2626
+ } catch {
2627
+ }
2628
+ try {
2629
+ await this.flushPendingMonacoSyncBlocking();
2630
+ } catch {
2631
+ }
2632
+ try {
2633
+ const e = this.editor.getModel(), i = this.editor.getPosition();
2634
+ if (e && i) {
2635
+ const s = e.getLineContent(i.lineNumber) ?? "", n = w(s, Math.max(0, i.column - 1));
2636
+ 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]]);
2637
+ }
2638
+ } catch {
2639
+ }
2640
+ try {
2641
+ await this.rpcCall("nvim_input", [t]);
2642
+ } catch {
2643
+ this.sendInput(t);
2644
+ }
2645
+ try {
2646
+ this.debugLog(`performDelegatedInsertExit: sent exitKey=${JSON.stringify(t)}`);
2647
+ } catch {
2648
+ }
2649
+ }
2650
+ }
2651
+ async flushPendingMonacoSyncBlocking() {
2652
+ if (!this.session || !this.session.isRunning() || !this.bufHandle) return;
2653
+ const t = this.getActiveState();
2654
+ if (!t) return;
2655
+ const e = this.editor.getModel();
2656
+ if (e && t.model === e) {
2657
+ try {
2658
+ this.debugLog(`flushPendingMonacoSyncBlocking: full=${t.pendingFullSync} edits=${t.pendingBufEdits.length} cursor=${t.pendingCursorSync} buf=${this.bufHandle}`);
2659
+ } catch {
2660
+ }
2661
+ if (!t.pendingFullSync && t.pendingBufEdits.length > 1 && (t.pendingFullSync = !0), t.pendingFullSync) {
2662
+ const i = e.getLinesContent();
2663
+ 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]);
2664
+ return;
2665
+ }
2666
+ if (t.pendingBufEdits.length) {
2667
+ const i = t.pendingBufEdits.slice();
2668
+ t.pendingBufEdits = [], t.pendingCursorSync = !1, this.delegateInsertToMonaco && (this.ignoreActiveBufLinesEventsUntil = this.nowMs() + 120);
2669
+ for (const s of i)
2670
+ await this.rpcCall("nvim_buf_set_text", [this.bufHandle, s.startRow, s.startColByte, s.endRow, s.endColByte, s.lines]);
2671
+ return;
2672
+ }
2673
+ t.pendingCursorSync && (t.pendingCursorSync = !1);
2674
+ }
2275
2675
  }
2276
2676
  syncDotRepeatToNvim() {
2277
2677
  if (!this.session || !this.session.isRunning()) return;
@@ -2327,7 +2727,11 @@ vim.opt.ei = ei
2327
2727
  }
2328
2728
  if (this.delegateInsertToMonaco) {
2329
2729
  if (e.key === "Escape") {
2330
- t.preventDefault(), this.armInsertExit(), this.flushPendingMonacoSync(), this.syncDotRepeatToNvim(), this.sendInput("<Esc>");
2730
+ if (this.compositionActive || e.isComposing) {
2731
+ this.pendingEscAfterComposition = !0;
2732
+ return;
2733
+ }
2734
+ t.preventDefault(), this.exitDelegatedInsertMode("<Esc>");
2331
2735
  return;
2332
2736
  }
2333
2737
  if (!this.opts.shouldHandleKey(e) || this.hasExplicitModAllowlist(!0) && !this.shouldForwardModifiedKeys(e, !0))
@@ -2335,12 +2739,12 @@ vim.opt.ei = ei
2335
2739
  if ((e.ctrlKey || e.altKey || e.metaKey) && !e.getModifierState?.("AltGraph")) {
2336
2740
  const s = this.opts.translateKey(e);
2337
2741
  if (!s) return;
2338
- t.preventDefault(), this.flushPendingMonacoSync(), this.sendInput(s);
2742
+ t.preventDefault(), s === "<C-[>" || s === "<C-c>" ? this.exitDelegatedInsertMode(s) : (this.flushPendingMonacoSync(), this.sendInput(s));
2339
2743
  return;
2340
2744
  }
2341
2745
  return;
2342
2746
  }
2343
- if (v(this.lastMode) && !this.compositionActive && !e.isComposing && !e.getModifierState?.("AltGraph") && !e.ctrlKey && !e.metaKey && typeof e.key == "string" && e.key.length === 1) {
2747
+ if (M(this.lastMode) && !this.compositionActive && !e.isComposing && !e.getModifierState?.("AltGraph") && !e.ctrlKey && !e.metaKey && typeof e.key == "string" && e.key.length === 1) {
2344
2748
  const s = /^[\x20-\x7E]$/.test(e.key);
2345
2749
  if (!!!(e.altKey && s))
2346
2750
  return this.opts.shouldHandleKey(e), void 0;
@@ -2354,22 +2758,32 @@ vim.opt.ei = ei
2354
2758
  if (this.compositionActive || e.isComposing || !this.opts.shouldHandleKey(e) || this.hasExplicitModAllowlist(!1) && !this.shouldForwardModifiedKeys(e, !1))
2355
2759
  return;
2356
2760
  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));
2761
+ if (i) {
2762
+ if (this.ignoreNextInputEvent = !0, t.preventDefault(), this.lastMode.startsWith("n")) {
2763
+ 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) {
2764
+ const { prefix: s, keys: n } = this.lastDelegatedDotRepeat;
2765
+ this.sendInput(s), this.sendInput(n), this.sendInput("<Esc>");
2766
+ return;
2767
+ }
2768
+ this.lastDelegatedDotRepeat && (/* @__PURE__ */ new Set(["c", "d", "y", "p", "x", "s", "r", "~", "J", ":"])).has(i) && (this.lastDelegatedDotRepeat = null);
2769
+ }
2770
+ !H(this.lastMode) && i.length === 1 && !i.startsWith("<") && (this.recentNormalKeys = (this.recentNormalKeys + i).slice(-16)), this.sendInput(i);
2771
+ }
2358
2772
  }
2359
2773
  handleMouse(t) {
2360
2774
  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]]);
2775
+ 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;
2776
+ this.sendNotify("nvim_win_set_cursor", [0, [e, l]]);
2363
2777
  }
2364
2778
  handleSelection(t) {
2365
2779
  if (this.delegateInsertToMonaco || !this.session || !this.session.isRunning() || !this.bufHandle || this.suppressCursorSync || this.compositionActive || t?.isComposing) return;
2366
2780
  const e = t.selection;
2367
2781
  if (e && t.source === "mouse") {
2368
2782
  if (e.isEmpty()) {
2369
- w(this.lastMode) && this.sendInput("<Esc>");
2783
+ S(this.lastMode) && this.sendInput("<Esc>");
2370
2784
  return;
2371
2785
  }
2372
- this.pendingSelection = new g.Selection(
2786
+ this.pendingSelection = new y.Selection(
2373
2787
  e.selectionStartLineNumber,
2374
2788
  e.selectionStartColumn,
2375
2789
  e.positionLineNumber,
@@ -2386,7 +2800,7 @@ vim.opt.ei = ei
2386
2800
  if (this.delegateInsertToMonaco || !this.session || !this.session.isRunning() || !this.bufHandle) return;
2387
2801
  const e = this.editor.getModel();
2388
2802
  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);
2803
+ 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
2804
  await this.execLua(`
2391
2805
  local api = vim.api
2392
2806
  local fn = vim.fn
@@ -2399,7 +2813,7 @@ end
2399
2813
  api.nvim_win_set_cursor(0, { a_line, a_col0 })
2400
2814
  api.nvim_feedkeys("v", "n", false)
2401
2815
  api.nvim_win_set_cursor(0, { b_line, b_col0 })
2402
- `, [i, d, n, f]);
2816
+ `, [i, u, n, d]);
2403
2817
  }
2404
2818
  sendInput(t) {
2405
2819
  this.sendNotify("nvim_input", [t]);
@@ -2465,30 +2879,30 @@ api.nvim_win_set_cursor(0, { b_line, b_col0 })
2465
2879
  const i = this.ensureActiveState();
2466
2880
  if (!i || i.model !== e) return;
2467
2881
  if (!i.shadowLines) {
2468
- i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.scheduleFlushPendingMonacoSync();
2882
+ i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1, this.scheduleFlushPendingMonacoSync();
2469
2883
  return;
2470
2884
  }
2471
2885
  if (!t.changes || t.changes.length !== 1) {
2472
- i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.scheduleFlushPendingMonacoSync();
2886
+ i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1, this.scheduleFlushPendingMonacoSync();
2473
2887
  return;
2474
2888
  }
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();
2889
+ const s = t.changes[0], n = s.range, r = n.startLineNumber - 1, l = n.endLineNumber - 1;
2890
+ if (r < 0 || l < 0) return;
2891
+ if (i.shadowLines[r] == null || i.shadowLines[l] == null) {
2892
+ i.pendingFullSync = !0, i.pendingCursorSync = !0, i.shadowLines = e.getLinesContent(), this.dotRepeatKeys = "", this.dotRepeatBackspaces = 0, this.delegatedInsertReplayPossible = !1, this.scheduleFlushPendingMonacoSync();
2479
2893
  return;
2480
2894
  }
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/) : [];
2895
+ 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
2896
  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);
2897
+ const g = Math.max(0, Number(s.rangeLength ?? 0) || 0);
2898
+ r === l && !a.includes(`
2899
+ `) ? (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
2900
  } catch {
2487
2901
  }
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();
2902
+ 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
2903
  }
2490
2904
  scheduleCursorSyncToNvim() {
2491
- if (!this.delegateInsertToMonaco) return;
2905
+ if (!this.delegateInsertToMonaco || this.exitingInsertMode) return;
2492
2906
  const t = this.getActiveState();
2493
2907
  t && (t.pendingCursorSync = !0, this.scheduleFlushPendingMonacoSync());
2494
2908
  }
@@ -2505,23 +2919,23 @@ api.nvim_win_set_cursor(0, { b_line, b_col0 })
2505
2919
  if (!t) return;
2506
2920
  const e = this.editor.getModel();
2507
2921
  if (e && t.model === e) {
2508
- if (!t.pendingFullSync && t.pendingBufEdits.length > 1 && (t.pendingFullSync = !0), t.pendingFullSync) {
2922
+ 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
2923
  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();
2924
+ 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
2925
  } else if (t.pendingBufEdits.length) {
2512
2926
  for (const i of t.pendingBufEdits)
2513
2927
  this.sendNotify("nvim_buf_set_text", [this.bufHandle, i.startRow, i.startColByte, i.endRow, i.endColByte, i.lines]);
2514
- t.pendingBufEdits = [];
2928
+ this.delegateInsertToMonaco && (this.ignoreActiveBufLinesEventsUntil = this.nowMs() + 120), t.pendingBufEdits = [];
2515
2929
  }
2516
- t.pendingCursorSync && (t.pendingCursorSync = !1, this.syncCursorToNvimNow());
2930
+ t.pendingCursorSync && (t.pendingCursorSync = !1, this.delegateInsertToMonaco && !this.exitingInsertMode && this.syncCursorToNvimNow());
2517
2931
  }
2518
2932
  }
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]]);
2933
+ syncCursorToNvimNow(t = !1) {
2934
+ if (!this.session || !this.session.isRunning() || !this.bufHandle || !t && !this.delegateInsertToMonaco) return;
2935
+ const e = this.editor.getModel(), i = this.editor.getPosition();
2936
+ if (!e || !i) return;
2937
+ const s = i.lineNumber, n = e.getLineContent(s) ?? "", r = w(n, Math.max(0, i.column - 1));
2938
+ this.sendNotify("nvim_win_set_cursor", [0, [s, r]]);
2525
2939
  }
2526
2940
  scheduleVisualSelectionRefresh() {
2527
2941
  this.visualSelectionRefreshTimer || (this.visualSelectionRefreshTimer = window.setTimeout(() => {
@@ -2534,11 +2948,36 @@ api.nvim_win_set_cursor(0, { b_line, b_col0 })
2534
2948
  const t = await this.rpcCall("nvim_get_api_info", []), e = Array.isArray(t) ? Number(t[0]) : NaN;
2535
2949
  if (!Number.isFinite(e) || e <= 0) return;
2536
2950
  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 = `
2951
+ 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
2952
  local chan = ...
2539
2953
  local api = vim.api
2540
2954
  vim.g.monaco_neovim_wasm_chan = chan
2541
2955
 
2956
+ local function setup_clipboard()
2957
+ -- If user provided a clipboard provider (or explicitly disabled it), don't override.
2958
+ if vim.g.clipboard ~= nil then return end
2959
+ local function copy(lines, regtype)
2960
+ vim.rpcnotify(chan, "wasm-clipboard-copy", lines, regtype)
2961
+ end
2962
+ local function paste()
2963
+ local ok, res = pcall(vim.rpcrequest, chan, "wasm-clipboard-paste")
2964
+ if not ok then return {}, "v" end
2965
+ local lines = res and res[1] or {}
2966
+ local regtype = res and res[2] or "v"
2967
+ return lines, regtype
2968
+ end
2969
+ vim.g.clipboard = {
2970
+ name = "wasm",
2971
+ copy = { ["+"] = copy, ["*"] = copy },
2972
+ paste = { ["+"] = paste, ["*"] = paste },
2973
+ cache_enabled = 0,
2974
+ }
2975
+ end
2976
+
2977
+ if ${this.opts.clipboard === null ? "false" : "true"} then
2978
+ pcall(setup_clipboard)
2979
+ end
2980
+
2542
2981
  local function send_cursor()
2543
2982
  local cur = api.nvim_win_get_cursor(0)
2544
2983
  vim.rpcnotify(chan, "monaco_cursor", cur[1], cur[2])
@@ -2622,7 +3061,7 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2622
3061
  filetype = (vim.bo[api.nvim_get_current_buf()] and vim.bo[api.nvim_get_current_buf()].filetype) or "",
2623
3062
  })
2624
3063
  `;
2625
- await this.execLua(a, [e]), this.hostAutocmdInstalled = !0;
3064
+ await this.execLua(c, [e]), this.hostAutocmdInstalled = !0;
2626
3065
  } catch {
2627
3066
  }
2628
3067
  }
@@ -2655,8 +3094,8 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2655
3094
  try {
2656
3095
  const r = await s.readFile(n);
2657
3096
  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}`);
3097
+ const l = r instanceof Uint8Array ? new TextDecoder().decode(r) : String(r);
3098
+ await this.openText({ path: n, text: l }), this.opts.status(`opened: ${n}`);
2660
3099
  } catch (r) {
2661
3100
  this.opts.status(`edit failed: ${r?.message ?? r}`, !0);
2662
3101
  }
@@ -2664,31 +3103,31 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2664
3103
  }
2665
3104
  if (e === "write" || e === "wq") {
2666
3105
  const n = this.editor.getModel(), r = n ? n.getValue() : "";
2667
- let a = typeof i.path == "string" ? String(i.path) : "";
2668
- if (!a)
3106
+ let l = typeof i.path == "string" ? String(i.path) : "";
3107
+ if (!l)
2669
3108
  try {
2670
- const u = await this.rpcCall("nvim_buf_get_name", [0]);
2671
- typeof u == "string" && (a = u);
3109
+ const c = await this.rpcCall("nvim_buf_get_name", [0]);
3110
+ typeof c == "string" && (l = c);
2672
3111
  } catch {
2673
3112
  }
2674
- if (a || (a = this.opts.seedName), !s.writeFile) {
3113
+ if (l || (l = this.opts.seedName), !s.writeFile) {
2675
3114
  this.opts.status("write: fileSystem.writeFile not set", !0);
2676
3115
  return;
2677
3116
  }
2678
3117
  try {
2679
- await s.writeFile(a, r);
3118
+ await s.writeFile(l, r);
2680
3119
  try {
2681
- await this.rpcCall("nvim_buf_set_name", [0, a]);
3120
+ await this.rpcCall("nvim_buf_set_name", [0, l]);
2682
3121
  } catch {
2683
3122
  }
2684
3123
  try {
2685
3124
  await this.rpcCall("nvim_buf_set_option", [0, "modified", !1]);
2686
3125
  } catch {
2687
3126
  }
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);
3127
+ const c = this.getActiveState();
3128
+ 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)");
3129
+ } catch (c) {
3130
+ this.opts.status(`write failed: ${c?.message ?? c}`, !0);
2692
3131
  }
2693
3132
  return;
2694
3133
  }
@@ -2709,19 +3148,19 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2709
3148
  await this.rpcCall("nvim_buf_attach", [e, !1, {}]);
2710
3149
  } catch {
2711
3150
  }
2712
- let a = [""];
3151
+ let l = [""];
2713
3152
  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 ?? "")));
3153
+ const p = await this.rpcCall("nvim_buf_get_lines", [e, 0, -1, !1]);
3154
+ Array.isArray(p) && (l = p.map((m) => String(m ?? "")));
2716
3155
  } catch {
2717
3156
  }
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);
3157
+ const c = y.Uri.from({ scheme: "nvim", authority: "buf", path: `/${e}` }), u = l.join(`
3158
+ `), d = s && y.languages.getLanguages().some((p) => p.id === s), f = y.editor.createModel(u, d ? s : void 0, c);
2720
3159
  n = {
2721
3160
  id: e,
2722
3161
  name: i || "",
2723
3162
  filetype: s || "",
2724
- model: l,
3163
+ model: f,
2725
3164
  createdModel: !0,
2726
3165
  shadowLines: null,
2727
3166
  pendingBufEdits: [],
@@ -2765,7 +3204,7 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2765
3204
  if (!n)
2766
3205
  try {
2767
3206
  const r = await this.rpcCall("nvim_create_buf", [!0, !1]);
2768
- n = x(r) ?? Number(r);
3207
+ n = N(r) ?? Number(r);
2769
3208
  } catch {
2770
3209
  n = null;
2771
3210
  }
@@ -2798,33 +3237,43 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2798
3237
  return this.session ? this.session.call(t, e) : Promise.reject(new Error("session not started"));
2799
3238
  }
2800
3239
  doClipboardPaste(t) {
2801
- const e = (s) => {
2802
- const n = (s || "").split(/\r?\n/);
2803
- this.sendRpcResponse(t, null, [n, "v"]);
2804
- }, i = this.opts.clipboard;
2805
- if (i === null) {
3240
+ const e = (n) => {
3241
+ this.lastClipboardText = n == null ? "" : String(n);
3242
+ const r = (n || "").split(/\r?\n/);
3243
+ this.sendRpcResponse(t, null, [r, "v"]);
3244
+ }, i = () => {
3245
+ try {
3246
+ return typeof navigator < "u" && navigator?.webdriver || typeof window > "u" || typeof window.prompt != "function" ? null : window.prompt("Paste text");
3247
+ } catch {
3248
+ return null;
3249
+ }
3250
+ }, s = this.opts.clipboard;
3251
+ if (s === null) {
2806
3252
  e("");
2807
3253
  return;
2808
3254
  }
2809
- if (i?.readText) {
2810
- i.readText().then((s) => e(s || "")).catch(() => {
2811
- const s = window.prompt("Paste text");
2812
- e(s || "");
3255
+ if (s?.readText) {
3256
+ s.readText().then((n) => e(n || "")).catch(() => {
3257
+ const n = i();
3258
+ e(n ?? this.lastClipboardText ?? "");
2813
3259
  });
2814
3260
  return;
2815
3261
  }
2816
3262
  if (!navigator.clipboard?.readText) {
2817
- const s = window.prompt("Paste text");
2818
- e(s || "");
3263
+ const n = i();
3264
+ e(n ?? this.lastClipboardText ?? "");
2819
3265
  return;
2820
3266
  }
2821
- navigator.clipboard.readText().then((s) => e(s || "")).catch(() => e(""));
3267
+ navigator.clipboard.readText().then((n) => e(n || "")).catch(() => {
3268
+ const n = i();
3269
+ e(n ?? this.lastClipboardText ?? "");
3270
+ });
2822
3271
  }
2823
3272
  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();
3273
+ 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);
3274
+ 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;
3275
+ const l = this.editor.getPosition();
3276
+ 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
3277
  }
2829
3278
  scheduleCursorRefresh() {
2830
3279
  if (this.compositionActive || this.cursorRefreshTimer) return;
@@ -2848,10 +3297,10 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2848
3297
  try {
2849
3298
  const e = await this.rpcCall("nvim_win_get_cursor", [0]);
2850
3299
  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));
3300
+ 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();
3301
+ if (this.optimisticCursorPos && this.optimisticCursorUntil > l) {
3302
+ const c = this.optimisticCursorPrevPos;
3303
+ 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
3304
  } else
2856
3305
  this.optimisticCursorPos = null, this.optimisticCursorPrevPos = null, this.optimisticCursorUntil = 0, this.updateCursor(r.lineNumber, r.column);
2857
3306
  }
@@ -2875,26 +3324,33 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2875
3324
  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
3325
  }
2877
3326
  async updateVisualSelection(t) {
2878
- const e = w(t), i = ++this.visualSelectionToken;
3327
+ const e = S(t), i = ++this.visualSelectionToken;
2879
3328
  if (!e) {
2880
3329
  this.clearVisualDecorations();
2881
3330
  return;
2882
3331
  }
2883
3332
  try {
2884
- const s = await this.fetchVisualRanges();
3333
+ const { selections: s, raw: n, tail: r } = await this.fetchVisualRanges();
2885
3334
  if (i !== this.visualSelectionToken || !s.length) return;
2886
- this.applyVisualDecorations(s, t);
3335
+ this.applyVisualDecorations(s, t, n, r);
2887
3336
  } catch {
2888
3337
  }
2889
3338
  }
2890
3339
  async fetchVisualRanges() {
2891
- const t = await this.execLua(oe, []);
2892
- return Array.isArray(t) ? t.map(de(this.editor)).filter((i) => !!i) : [];
3340
+ const t = await this.execLua(le, []);
3341
+ let e = [], i = "";
3342
+ if (Array.isArray(t))
3343
+ e = t;
3344
+ else if (t && typeof t == "object") {
3345
+ const n = t;
3346
+ i = typeof n.tail == "string" ? n.tail : "", e = Array.isArray(n.ranges) ? n.ranges : [];
3347
+ }
3348
+ return { selections: e.map(fe(this.editor)).filter((n) => !!n), raw: e, tail: i };
2893
3349
  }
2894
3350
  async syncVisualSelectionColor() {
2895
3351
  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, {
3352
+ const e = await this.fetchVisualBg() || "#3e4451", i = V(e, 0.45), s = V(e, 0.3);
3353
+ y.editor.defineTheme(this.opts.visualThemeName, {
2898
3354
  base: "vs-dark",
2899
3355
  inherit: !0,
2900
3356
  rules: [],
@@ -2906,21 +3362,50 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2906
3362
  } catch {
2907
3363
  }
2908
3364
  }
3365
+ setMonacoHighlightsSuppressed(t) {
3366
+ const e = y.editor.EditorOption, i = !!t;
3367
+ if (i !== this.monacoHighlightsSuppressed) {
3368
+ if (i) {
3369
+ try {
3370
+ this.monacoPrevOccurrencesHighlight = this.editor.getOption(e.occurrencesHighlight), this.monacoPrevSelectionHighlight = this.editor.getOption(e.selectionHighlight), this.monacoPrevSelectionHighlightMultiline = this.editor.getOption(e.selectionHighlightMultiline);
3371
+ } catch {
3372
+ }
3373
+ try {
3374
+ this.editor.updateOptions({
3375
+ occurrencesHighlight: "off",
3376
+ selectionHighlight: !1,
3377
+ selectionHighlightMultiline: !1
3378
+ }), this.monacoHighlightsSuppressed = !0;
3379
+ } catch {
3380
+ }
3381
+ return;
3382
+ }
3383
+ try {
3384
+ this.editor.updateOptions({
3385
+ occurrencesHighlight: this.monacoPrevOccurrencesHighlight ?? "singleFile",
3386
+ selectionHighlight: this.monacoPrevSelectionHighlight ?? !0,
3387
+ selectionHighlightMultiline: this.monacoPrevSelectionHighlightMultiline ?? !0
3388
+ });
3389
+ } catch {
3390
+ }
3391
+ this.monacoHighlightsSuppressed = !1, this.monacoPrevOccurrencesHighlight = null, this.monacoPrevSelectionHighlight = null, this.monacoPrevSelectionHighlightMultiline = null;
3392
+ }
3393
+ }
2909
3394
  async fetchVisualBg() {
2910
3395
  try {
2911
- const t = await this.rpcCall("nvim_get_hl", [0, { name: "Visual", link: !1 }]), e = K(t);
3396
+ const t = await this.rpcCall("nvim_get_hl", [0, { name: "Visual", link: !1 }]), e = W(t);
2912
3397
  if (e) return e;
2913
3398
  } catch {
2914
3399
  }
2915
3400
  try {
2916
- const t = await this.rpcCall("nvim_get_hl_by_name", ["Visual", !0]), e = K(t);
3401
+ const t = await this.rpcCall("nvim_get_hl_by_name", ["Visual", !0]), e = W(t);
2917
3402
  if (e) return e;
2918
3403
  } catch {
2919
3404
  }
2920
3405
  return null;
2921
3406
  }
2922
3407
  async seedBuffer(t, e) {
2923
- const i = x(t);
3408
+ const i = N(t);
2924
3409
  if (!i || i <= 0) return null;
2925
3410
  const s = e ?? this.opts.seedLines;
2926
3411
  if (!s || !s.length) return null;
@@ -2931,27 +3416,27 @@ vim.rpcnotify(chan, "monaco_buf_enter", {
2931
3416
  }
2932
3417
  }
2933
3418
  }
2934
- function Ce(o, t = {}) {
2935
- return new le(o, t);
3419
+ function ve(o, t = {}) {
3420
+ return new ce(o, t);
2936
3421
  }
2937
- function M(o, t, e) {
3422
+ function R(o, t, e) {
2938
3423
  return Math.min(Math.max(o, t), e);
2939
3424
  }
2940
- function A(o) {
3425
+ function B(o) {
2941
3426
  try {
2942
3427
  return new TextEncoder().encode(String(o ?? "")).length;
2943
3428
  } catch {
2944
3429
  return String(o ?? "").length;
2945
3430
  }
2946
3431
  }
2947
- function B(o, t, e) {
3432
+ function K(o, t, e) {
2948
3433
  const i = Math.max(1, Number(t) || 1), s = Math.max(1, (Number(e) || 0) + 1), n = o.getModel();
2949
3434
  if (!n)
2950
3435
  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 };
3436
+ 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);
3437
+ return { line: l, col: p };
2953
3438
  }
2954
- function k(o, t = !0) {
3439
+ function F(o, t = !0) {
2955
3440
  const e = String(o ?? "");
2956
3441
  if (!e) return "";
2957
3442
  const i = e.replace(/\r\n/g, `
@@ -2959,72 +3444,72 @@ function k(o, t = !0) {
2959
3444
  `).replace(/</g, "<lt>");
2960
3445
  return t ? i.replace(/\n/g, "<CR>") : i;
2961
3446
  }
2962
- function ce(o) {
3447
+ function ue(o) {
2963
3448
  const t = o.key;
2964
3449
  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}>`;
3450
+ 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) => {
3451
+ const m = [];
3452
+ i && m.push("C-"), p && r && m.push("S-"), s && m.push("A-"), n && m.push("D-");
3453
+ const a = l(f);
3454
+ return m.length ? `<${m.join("")}${a}>` : `<${a}>`;
2970
3455
  };
2971
3456
  if (typeof o.code == "string" && o.code.startsWith("Numpad") || typeof KeyboardEvent?.DOM_KEY_LOCATION_NUMPAD == "number" && o.location === KeyboardEvent.DOM_KEY_LOCATION_NUMPAD) {
2972
3457
  switch (o.code) {
2973
3458
  case "NumpadEnter":
2974
- return d("kEnter", !0);
3459
+ return u("kEnter", !0);
2975
3460
  case "NumpadAdd":
2976
- return d("kPlus", !0);
3461
+ return u("kPlus", !0);
2977
3462
  case "NumpadSubtract":
2978
- return d("kMinus", !0);
3463
+ return u("kMinus", !0);
2979
3464
  case "NumpadMultiply":
2980
- return d("kMultiply", !0);
3465
+ return u("kMultiply", !0);
2981
3466
  case "NumpadDivide":
2982
- return d("kDivide", !0);
3467
+ return u("kDivide", !0);
2983
3468
  case "NumpadDecimal":
2984
- return d("kPoint", !0);
3469
+ return u("kPoint", !0);
2985
3470
  }
2986
- if (/^\d$/.test(t)) return d(`k${t}`, !0);
3471
+ if (/^\d$/.test(t)) return u(`k${t}`, !0);
2987
3472
  }
2988
3473
  switch (t) {
2989
3474
  case "Backspace":
2990
- return d("BS", !0);
3475
+ return u("BS", !0);
2991
3476
  case "Enter":
2992
- return d("CR", !0);
3477
+ return u("CR", !0);
2993
3478
  case "Escape":
2994
- return d("Esc", !0);
3479
+ return u("Esc", !0);
2995
3480
  case "Tab":
2996
- return r && !i && !s && !n ? "<S-Tab>" : d("Tab", !0);
3481
+ return r && !i && !s && !n ? "<S-Tab>" : u("Tab", !0);
2997
3482
  case "ArrowUp":
2998
- return d("Up", !0);
3483
+ return u("Up", !0);
2999
3484
  case "ArrowDown":
3000
- return d("Down", !0);
3485
+ return u("Down", !0);
3001
3486
  case "ArrowLeft":
3002
- return d("Left", !0);
3487
+ return u("Left", !0);
3003
3488
  case "ArrowRight":
3004
- return d("Right", !0);
3489
+ return u("Right", !0);
3005
3490
  case "Delete":
3006
- return d("Del", !0);
3491
+ return u("Del", !0);
3007
3492
  case "Home":
3008
- return d("Home", !0);
3493
+ return u("Home", !0);
3009
3494
  case "End":
3010
- return d("End", !0);
3495
+ return u("End", !0);
3011
3496
  case "PageUp":
3012
- return d("PageUp", !0);
3497
+ return u("PageUp", !0);
3013
3498
  case "PageDown":
3014
- return d("PageDown", !0);
3499
+ return u("PageDown", !0);
3015
3500
  case "Insert":
3016
- return d("Insert", !0);
3501
+ return u("Insert", !0);
3017
3502
  }
3018
- if (/^F\d{1,2}$/.test(t)) return d(t, !0);
3503
+ if (/^F\d{1,2}$/.test(t)) return u(t, !0);
3019
3504
  if (t.length === 1) {
3020
- if (!i && !s && !n) return u(t);
3505
+ if (!i && !s && !n) return c(t);
3021
3506
  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}>`;
3507
+ const f = /^[A-Za-z]$/.test(t) ? t.toLowerCase() : t, p = l(f);
3508
+ return `<${(i ? "C-" : "") + (s ? "A-" : "") + (n ? "D-" : "")}${p}>`;
3024
3509
  }
3025
3510
  return null;
3026
3511
  }
3027
- function ue(o) {
3512
+ function he(o) {
3028
3513
  if (!o || o.length === 0) return null;
3029
3514
  const t = o[0];
3030
3515
  if (o.length === 1)
@@ -3051,74 +3536,75 @@ function ue(o) {
3051
3536
  return t;
3052
3537
  }
3053
3538
  }
3054
- function he(o) {
3539
+ function de(o) {
3055
3540
  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
3541
  }
3057
- function x(o) {
3542
+ function N(o) {
3058
3543
  if (o && typeof o == "object" && typeof o.type == "number") {
3059
- const e = he(o.data);
3544
+ const e = de(o.data);
3060
3545
  if (e) {
3061
- const i = ue(e);
3546
+ const i = he(e);
3062
3547
  if (i != null && i > 0) return i;
3063
3548
  }
3064
3549
  }
3065
3550
  const t = Number(o);
3066
3551
  return Number.isInteger(t) && t > 0 ? t : null;
3067
3552
  }
3068
- function S(o, t) {
3553
+ function I(o, t) {
3069
3554
  let e = 0, i = 0;
3070
3555
  const s = Math.max(0, Number(t) || 0);
3071
3556
  for (; e < s; ) {
3072
3557
  if (i >= o.length)
3073
3558
  return i + (s - e);
3074
- const n = o.codePointAt(i), r = L(n ?? 0);
3559
+ const n = o.codePointAt(i), r = $(n ?? 0);
3560
+ if (e + r > s) return i;
3075
3561
  e += r, i += r === 4 ? 2 : 1;
3076
3562
  }
3077
3563
  return i;
3078
3564
  }
3079
- function _(o, t) {
3565
+ function w(o, t) {
3080
3566
  const e = Math.max(0, Number(t) || 0);
3081
3567
  let i = 0, s = 0;
3082
3568
  for (; s < e && s < o.length; ) {
3083
- const n = o.codePointAt(s), r = L(n ?? 0);
3569
+ const n = o.codePointAt(s), r = $(n ?? 0);
3084
3570
  i += r, s += r === 4 ? 2 : 1;
3085
3571
  }
3086
3572
  return i;
3087
3573
  }
3088
- function L(o) {
3574
+ function $(o) {
3089
3575
  return o == null ? 0 : o <= 127 ? 1 : o <= 2047 ? 2 : o >= 55296 && o <= 57343 ? 4 : o < 65535 ? 3 : 4;
3090
3576
  }
3091
- function de(o) {
3577
+ function fe(o) {
3092
3578
  return (t) => {
3093
3579
  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);
3580
+ const e = O(o, t.start, !1), i = O(o, t.end, !!t.inclusive);
3581
+ return !e || !i ? null : new y.Selection(e.lineNumber, e.column, i.lineNumber, i.column);
3096
3582
  };
3097
3583
  }
3098
- function F(o, t, e) {
3584
+ function O(o, t, e) {
3099
3585
  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);
3586
+ const i = o.getModel(), s = R(t.line + 1, 1, i?.getLineCount() || 1 / 0);
3101
3587
  if (!i) {
3102
- const f = Math.max(1, t.col + 1);
3103
- return { lineNumber: s, column: f + (e ? 1 : 0) };
3588
+ const d = Math.max(1, t.col + 1);
3589
+ return { lineNumber: s, column: d + (e ? 1 : 0) };
3104
3590
  }
3105
3591
  const n = i.getLineContent(s) ?? "", r = Math.max(0, Number(t.col) || 0);
3106
- let a = r;
3592
+ let l = r;
3107
3593
  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);
3594
+ const d = I(n, r);
3595
+ if (d < n.length) {
3596
+ const f = n.codePointAt(d);
3597
+ l = r + $(f ?? 0);
3112
3598
  }
3113
3599
  }
3114
- const u = S(n, a) + 1, d = i.getLineMaxColumn(s);
3115
- return { lineNumber: s, column: M(u, 1, d) };
3600
+ const c = I(n, l) + 1, u = i.getLineMaxColumn(s);
3601
+ return { lineNumber: s, column: R(c, 1, u) };
3116
3602
  }
3117
- function w(o) {
3603
+ function S(o) {
3118
3604
  const t = typeof o == "string" ? o : "";
3119
3605
  return t.includes("v") || t.includes("V") || t.includes("") || t.includes("s") || t.includes("S") || t.includes("");
3120
3606
  }
3121
- function fe(o) {
3607
+ function pe(o) {
3122
3608
  if (!o || !Array.isArray(o) || o.length === 0) return;
3123
3609
  const t = new TextEncoder(), e = [];
3124
3610
  for (const i of o) {
@@ -3134,47 +3620,46 @@ function me(o, t) {
3134
3620
  const e = Array.isArray(t) ? t.filter(Boolean) : [], i = Array.isArray(o) ? o.filter(Boolean) : [], s = [...e, ...i];
3135
3621
  return s.length ? s : void 0;
3136
3622
  }
3137
- function H(o, t) {
3623
+ function V(o, t) {
3138
3624
  const e = o.replace("#", "");
3139
3625
  if (e.length !== 6) return o;
3140
- const i = Math.round(M(t, 0, 1) * 255);
3626
+ const i = Math.round(R(t, 0, 1) * 255);
3141
3627
  return `#${e}${i.toString(16).padStart(2, "0")}`;
3142
3628
  }
3143
- function K(o) {
3629
+ function W(o) {
3144
3630
  if (!o || typeof o != "object") return null;
3145
3631
  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;
3632
+ 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
3633
  }
3148
- function pe(o) {
3634
+ function ge(o) {
3149
3635
  return `#${Number(o >>> 0).toString(16).padStart(6, "0").slice(-6)}`;
3150
3636
  }
3151
- function C(o, t, e, i) {
3637
+ function x(o, t, e, i) {
3152
3638
  return o.addEventListener(t, e, i), { dispose: () => o.removeEventListener(t, e, i) };
3153
3639
  }
3154
- function $(o) {
3155
- const t = typeof o == "string" ? o : "";
3156
- return t.startsWith("i") || t.startsWith("R");
3640
+ function H(o) {
3641
+ return (typeof o == "string" ? o : "").startsWith("i");
3157
3642
  }
3158
- function ge(o) {
3643
+ function ye(o) {
3159
3644
  const t = typeof o == "string" ? o : "";
3160
3645
  return t.length ? t[t.length - 1] : "";
3161
3646
  }
3162
- function v(o) {
3647
+ function M(o) {
3163
3648
  const t = typeof o == "string" ? o : "";
3164
3649
  return t === "c" || t.startsWith("c");
3165
3650
  }
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}`];
3651
+ function be(o, t, e, i, s, n) {
3652
+ 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/);
3653
+ let p;
3654
+ if (f.length <= 1)
3655
+ p = [`${c}${f[0] ?? ""}${u}`];
3171
3656
  else {
3172
- const h = `${u}${l[0] ?? ""}`, m = `${l[l.length - 1] ?? ""}${d}`, p = l.slice(1, -1);
3173
- c = [h, ...p, m];
3657
+ const m = `${c}${f[0] ?? ""}`, a = `${f[f.length - 1] ?? ""}${u}`, h = f.slice(1, -1);
3658
+ p = [m, ...h, a];
3174
3659
  }
3175
- o.splice(t, Math.max(0, i - t + 1), ...c);
3660
+ o.splice(t, Math.max(0, i - t + 1), ...p);
3176
3661
  }
3177
- function T(o) {
3662
+ function k(o) {
3178
3663
  if (!o) return "";
3179
3664
  if (typeof o == "string") return o;
3180
3665
  if (!Array.isArray(o)) return "";
@@ -3185,14 +3670,14 @@ function T(o) {
3185
3670
  continue;
3186
3671
  }
3187
3672
  if (Array.isArray(e)) {
3188
- typeof e[1] == "string" ? t += e[1] : typeof e[0] == "string" ? t += e[0] : t += T(e);
3673
+ typeof e[1] == "string" ? t += e[1] : typeof e[0] == "string" ? t += e[0] : t += k(e);
3189
3674
  continue;
3190
3675
  }
3191
3676
  e && typeof e == "object" && typeof e.text == "string" && (t += e.text);
3192
3677
  }
3193
3678
  return t;
3194
3679
  }
3195
- function be(o) {
3680
+ function _e(o) {
3196
3681
  if (!Array.isArray(o)) return [];
3197
3682
  const t = [];
3198
3683
  for (const e of o) {
@@ -3212,12 +3697,12 @@ function be(o) {
3212
3697
  }
3213
3698
  return t.filter((e) => e.word.length > 0);
3214
3699
  }
3215
- const _e = new URL("./nvimWorker.js", import.meta.url), we = new URL("./nvimWorkerAsyncify.js", import.meta.url);
3700
+ const Ce = new URL("./nvimWorker.js", import.meta.url), we = new URL("./nvimWorkerAsyncify.js", import.meta.url);
3216
3701
  export {
3217
- le as MonacoNeovimClient,
3218
- te as NeovimWasmSession,
3219
- Ce as createMonacoNeovim,
3220
- _e as defaultWorkerUrl,
3702
+ ce as MonacoNeovimClient,
3703
+ ie as NeovimWasmSession,
3704
+ ve as createMonacoNeovim,
3705
+ Ce as defaultWorkerUrl,
3221
3706
  we as defaultWorkerUrlAsyncify,
3222
- Z as isSharedArrayBufferAvailable
3707
+ ee as isSharedArrayBufferAvailable
3223
3708
  };