1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
use anyhow::Result;
use crossterm::{
cursor::SetCursorStyle,
event::{self, Event, KeyCode, KeyModifiers},
execute,
};
use hjkl_engine::{CursorShape, Host, VimMode};
use ratatui::{Terminal, backend::CrosstermBackend};
use std::io::Stdout;
use std::time::Duration;
use super::{App, STATUS_LINE_HEIGHT, SearchDir, prompt_cursor_shape};
use crate::render;
impl App {
/// Main event loop. Draws every frame, routes key events through
/// the vim FSM, handles resize, exits on Ctrl-C.
pub fn run(&mut self, terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
loop {
// Sync the focused window's stored scroll into the active editor
// so the engine's scrolloff math starts from the correct baseline.
self.sync_viewport_to_editor();
// Drain any pending LSP events (non-blocking).
self.drain_lsp_events();
// Update host viewport dimensions from the current terminal size.
{
let size = terminal.size()?;
let vp = self.active_mut().editor.host_mut().viewport_mut();
vp.width = size.width;
vp.height = size.height.saturating_sub(STATUS_LINE_HEIGHT);
}
// Emit cursor shape before the draw call, once per transition.
let current_shape = if let Some(ref f) = self.command_field {
prompt_cursor_shape(f)
} else if let Some(ref f) = self.search_field {
prompt_cursor_shape(f)
} else {
self.active().editor.host().cursor_shape()
};
if current_shape != self.last_cursor_shape {
match current_shape {
CursorShape::Block => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyBlock);
}
CursorShape::Bar => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyBar);
}
CursorShape::Underline => {
let _ = execute!(terminal.backend_mut(), SetCursorStyle::SteadyUnderScore);
}
}
self.last_cursor_shape = current_shape;
}
// Draw the current frame.
terminal.draw(|frame| render::frame(frame, self))?;
// Poll any in-flight async grammar loads each tick so a freshly
// compiled grammar installs without needing a keypress.
if self.poll_grammar_loads() {
self.recompute_and_install();
}
// Wait for the next event with a 120 ms ceiling so the splash
// animation can repaint without input. The splash itself reads
// the wall clock — we just need a redraw cadence here.
if !event::poll(Duration::from_millis(120))? {
continue;
}
match event::read()? {
Event::Key(key) => {
if key.code == KeyCode::Char('c')
&& key.modifiers.contains(KeyModifiers::CONTROL)
{
if self.command_field.is_some() {
self.command_field = None;
continue;
}
if self.search_field.is_some() {
self.cancel_search_prompt();
continue;
}
break;
}
// Dismiss the start screen on any non-Ctrl-C keypress and
// let the key fall through to normal handling so `:`,
// `/`, `i`, etc. take effect on the same press.
if self.start_screen.is_some() {
self.start_screen = None;
}
self.status_message = None;
// ── Info popup dismissal ──────────────────────────────────
if self.info_popup.is_some() {
self.info_popup = None;
continue;
}
// ── Command palette (`:` prompt) ─────────────────────────
if self.command_field.is_some() {
self.handle_command_field_key(key);
if self.exit_requested {
break;
}
continue;
}
// ── Search prompt (`/` `?`) ──────────────────────────────
if self.search_field.is_some() {
self.handle_search_field_key(key);
if self.exit_requested {
break;
}
continue;
}
// ── Picker overlay ────────────────────────────────────────
if self.picker.is_some() {
self.handle_picker_key(key);
if self.exit_requested {
break;
}
continue;
}
// ── LSP chord resolution (<leader>c{a} / <leader>r{n}) ───
if let Some(lsp_prefix) = self.pending_lsp.take() {
if self.active().editor.vim_mode() == VimMode::Normal {
self.pending_leader = false;
match (lsp_prefix, key.code) {
('c', KeyCode::Char('a')) => {
self.lsp_code_actions();
}
('r', KeyCode::Char('n')) => {
// Phase 5 MVP: prompt user to use :Rename <newname>.
// TODO: open inline prompt pre-filled with word-at-cursor.
self.status_message =
Some("use :Rename <newname> to rename".into());
}
_ => {}
}
}
continue;
}
// ── Git sub-command resolution ───────────────────────────
if self.pending_git && self.active().editor.vim_mode() == VimMode::Normal {
self.pending_git = false;
self.pending_leader = false;
match key.code {
KeyCode::Char('s') if key.modifiers == KeyModifiers::NONE => {
self.open_git_status_picker();
}
KeyCode::Char('l') if key.modifiers == KeyModifiers::NONE => {
self.open_git_log_picker();
}
KeyCode::Char('b') if key.modifiers == KeyModifiers::NONE => {
self.open_git_branch_picker();
}
// <leader>gB — file history for the current buffer.
// Uppercase B (Shift+b).
KeyCode::Char('B')
if key.modifiers == KeyModifiers::NONE
|| key.modifiers == KeyModifiers::SHIFT =>
{
self.open_git_file_history_picker();
}
// <leader>gS — stashes picker (uppercase S).
KeyCode::Char('S')
if key.modifiers == KeyModifiers::NONE
|| key.modifiers == KeyModifiers::SHIFT =>
{
self.open_git_stash_picker();
}
// <leader>gt — tags picker.
KeyCode::Char('t') if key.modifiers == KeyModifiers::NONE => {
self.open_git_tags_picker();
}
// <leader>gr — remotes picker.
KeyCode::Char('r') if key.modifiers == KeyModifiers::NONE => {
self.open_git_remotes_picker();
}
_ => {}
}
continue;
}
// ── Leader resolution ────────────────────────────────────
let leader = self.config.editor.leader;
if self.pending_leader && self.active().editor.vim_mode() == VimMode::Normal {
self.pending_leader = false;
if key.modifiers == KeyModifiers::NONE {
match key.code {
// The leader key itself + 'f' both open the file picker
// (matches buffr-style "press leader twice or leader+f").
KeyCode::Char(c) if c == leader => {
self.open_picker();
}
KeyCode::Char('f') => {
self.open_picker();
}
KeyCode::Char('b') => {
self.open_buffer_picker();
}
KeyCode::Char('/') => {
self.open_grep_picker(None);
}
KeyCode::Char('g') => {
// Begin git sub-command chord.
self.pending_git = true;
}
KeyCode::Char('d') => {
// <leader>d — show diag-at-cursor in info popup.
self.show_diag_at_cursor();
}
KeyCode::Char('c') => {
// Begin LSP 'c' sub-command chord (<leader>ca = code actions).
self.pending_lsp = Some('c');
self.pending_leader = false;
}
KeyCode::Char('r') => {
// Begin LSP 'r' sub-command chord (<leader>rn = rename).
self.pending_lsp = Some('r');
self.pending_leader = false;
}
_ => {}
}
}
continue;
}
// ── Leader prefix ────────────────────────────────────────
if key.code == KeyCode::Char(leader)
&& key.modifiers == KeyModifiers::NONE
&& self.active().editor.vim_mode() == VimMode::Normal
{
self.pending_leader = true;
continue;
}
// ── Ctrl-w window motion chord ───────────────────────────
if self.active().editor.vim_mode() == VimMode::Normal {
// Second key of a Ctrl-w chord.
if self.pending_window_motion {
self.pending_window_motion = false;
match key.code {
KeyCode::Char('j') => {
self.focus_below();
}
KeyCode::Char('k') => {
self.focus_above();
}
KeyCode::Char('h') => {
self.focus_left();
}
KeyCode::Char('l') => {
self.focus_right();
}
KeyCode::Char('w') => {
self.focus_next();
}
KeyCode::Char('W') => {
self.focus_previous();
}
KeyCode::Char('c') => {
self.close_focused_window();
}
// Ctrl-w q: vim parity — close window when multiple,
// quit app when last.
KeyCode::Char('q') => {
if self.layout().leaves().len() > 1 {
self.close_focused_window();
} else {
self.exit_requested = true;
}
}
// Ctrl-w o: close all windows except focused (:only).
KeyCode::Char('o') => {
self.only_focused_window();
}
// Ctrl-w x / r / R: swap focused leaf with sibling.
KeyCode::Char('x') | KeyCode::Char('r') | KeyCode::Char('R') => {
self.swap_with_sibling();
}
// Ctrl-w T: move focused window to a new tab.
KeyCode::Char('T') => match self.move_window_to_new_tab() {
Ok(()) => {
self.status_message =
Some("moved window to new tab".into());
}
Err(msg) => {
self.status_message = Some(msg.to_string());
}
},
// Ctrl-w n: horizontal split with empty buffer (:new).
KeyCode::Char('n') => {
self.dispatch_ex("new");
}
KeyCode::Char('+') => {
self.resize_height(1);
}
KeyCode::Char('-') => {
self.resize_height(-1);
}
KeyCode::Char('>') => {
self.resize_width(1);
}
KeyCode::Char('<') => {
self.resize_width(-1);
}
KeyCode::Char('=') => {
self.equalize_layout();
}
KeyCode::Char('_') => {
self.maximize_height();
}
KeyCode::Char('|') => {
self.maximize_width();
}
_ => {} // unknown second key — consume and ignore
}
continue;
}
// First key: Ctrl-w sets pending.
if key.code == KeyCode::Char('w')
&& key.modifiers.contains(KeyModifiers::CONTROL)
{
self.pending_window_motion = true;
continue;
}
// tmux-navigator: bare Ctrl-h/j/k/l in Normal mode.
// When a hjkl neighbour exists → focus it.
// When at the edge → fall through to `tmux select-pane`
// if $TMUX is set; otherwise silently no-op so we don't
// accidentally trigger engine bindings (Ctrl-h = BS,
// Ctrl-l = redraw) while the user intends navigation.
//
// Ctrl-h note: many terminals deliver Ctrl-h as
// KeyCode::Backspace with CONTROL modifier rather than
// KeyCode::Char('h') + CONTROL. We match both forms.
if key.modifiers.contains(KeyModifiers::CONTROL) {
let focused = self.focused_window();
let is_ctrl_h =
key.code == KeyCode::Char('h') || key.code == KeyCode::Backspace;
let is_ctrl_j = key.code == KeyCode::Char('j');
let is_ctrl_k = key.code == KeyCode::Char('k');
let is_ctrl_l = key.code == KeyCode::Char('l');
if is_ctrl_h || is_ctrl_j || is_ctrl_k || is_ctrl_l {
let neighbour = if is_ctrl_h {
self.layout().neighbor_left(focused)
} else if is_ctrl_j {
self.layout().neighbor_below(focused)
} else if is_ctrl_k {
self.layout().neighbor_above(focused)
} else {
self.layout().neighbor_right(focused)
};
if neighbour.is_some() {
// Neighbour exists — move focus within hjkl.
if is_ctrl_h {
self.focus_left();
} else if is_ctrl_j {
self.focus_below();
} else if is_ctrl_k {
self.focus_above();
} else {
self.focus_right();
}
} else {
// At the edge — hand off to tmux when available.
if std::env::var("TMUX").is_ok() {
let flag = if is_ctrl_h {
"-L"
} else if is_ctrl_j {
"-D"
} else if is_ctrl_k {
"-U"
} else {
"-R"
};
// Ignore errors: non-zero exit (no tmux pane
// in that direction) is a silent no-op.
let _ = std::process::Command::new("tmux")
.args(["select-pane", flag])
.status();
}
// $TMUX not set → silent no-op.
}
continue;
}
}
} else {
// Any non-Normal mode clears the pending flag.
self.pending_window_motion = false;
}
// ── Alt-buffer toggle (Ctrl-^ / Ctrl-6) ─────────────────
if self.active().editor.vim_mode() == VimMode::Normal
&& key.modifiers.contains(KeyModifiers::CONTROL)
&& (key.code == KeyCode::Char('^') || key.code == KeyCode::Char('6'))
{
self.buffer_alt();
continue;
}
// ── Shift-H / Shift-L cycle buffers ──────────────────────
// Only when more than one buffer is open; with a single
// slot fall through to the engine's H/L viewport motions.
if self.active().editor.vim_mode() == VimMode::Normal
&& self.slots.len() > 1
&& (key.modifiers == KeyModifiers::SHIFT
|| key.modifiers == KeyModifiers::NONE)
{
if key.code == KeyCode::Char('H') {
self.buffer_prev();
continue;
}
if key.code == KeyCode::Char('L') {
self.buffer_next();
continue;
}
}
// ── Buffer-motion pending state ──────────────────────────
if self.active().editor.vim_mode() == VimMode::Normal
&& key.modifiers == KeyModifiers::NONE
{
if let Some(prefix) = self.pending_buffer_motion.take() {
match (prefix, key.code) {
// TODO: [N]gt count prefix (e.g. `3gt` → jump to tab 3)
// is a separate effort; not wired here.
('g', KeyCode::Char('t')) => {
self.dispatch_ex("tabnext");
continue;
}
('g', KeyCode::Char('T')) => {
self.dispatch_ex("tabprev");
continue;
}
// LSP goto motions (g-prefix)
('g', KeyCode::Char('d')) => {
self.lsp_goto_definition();
continue;
}
('g', KeyCode::Char('D')) => {
self.lsp_goto_declaration();
continue;
}
('g', KeyCode::Char('r')) => {
self.lsp_goto_references();
continue;
}
('g', KeyCode::Char('i')) => {
self.lsp_goto_implementation();
continue;
}
('g', KeyCode::Char('y')) => {
self.lsp_goto_type_definition();
continue;
}
(']', KeyCode::Char('b')) => {
self.buffer_next();
continue;
}
('[', KeyCode::Char('b')) => {
self.buffer_prev();
continue;
}
// ]d / [d — navigate diagnostics
(']', KeyCode::Char('d')) => {
self.dispatch_ex("lnext");
continue;
}
('[', KeyCode::Char('d')) => {
self.dispatch_ex("lprev");
continue;
}
// ]D / [D — navigate error-only diagnostics
(']', KeyCode::Char('D')) => {
self.lnext_severity(Some(super::DiagSeverity::Error));
continue;
}
('[', KeyCode::Char('D')) => {
self.lprev_severity(Some(super::DiagSeverity::Error));
continue;
}
// Didn't match — forward only the current key;
// drop the pending prefix (g/]/[ alone has no
// other mapped meaning in our engine yet).
// Engine-handled motions like gg/gj/gk/G need
// the viewport synced back so the focused
// window's stored top_row picks up the
// engine's auto-scroll.
_ => {
self.active_mut().editor.handle_key(key);
self.sync_viewport_from_editor();
continue;
}
}
}
} else {
// Any non-Normal key clears pending motions.
self.pending_buffer_motion = None;
self.pending_git = false;
self.pending_leader = false;
}
// ── LSP hover (`K`) in Normal mode ───────────────────────
// TODO: Ctrl-w K (split-then-hover) deferred; popup hover works.
if key.code == KeyCode::Char('K')
&& (key.modifiers == KeyModifiers::NONE
|| key.modifiers == KeyModifiers::SHIFT)
&& self.active().editor.vim_mode() == VimMode::Normal
{
self.lsp_hover();
continue;
}
// ── Intercept `:` in Normal mode ─────────────────────────
if key.code == KeyCode::Char(':')
&& key.modifiers == KeyModifiers::NONE
&& self.active().editor.vim_mode() == VimMode::Normal
{
self.open_command_prompt();
continue;
}
// ── Intercept `/` and `?` in Normal mode ─────────────────
if key.modifiers == KeyModifiers::NONE
&& self.active().editor.vim_mode() == VimMode::Normal
{
if key.code == KeyCode::Char('/') {
self.open_search_prompt(SearchDir::Forward);
continue;
}
if key.code == KeyCode::Char('?') {
self.open_search_prompt(SearchDir::Backward);
continue;
}
}
// ── Set pending buffer-motion prefix ─────────────────────
if self.active().editor.vim_mode() == VimMode::Normal
&& key.modifiers == KeyModifiers::NONE
&& matches!(
key.code,
KeyCode::Char('g') | KeyCode::Char(']') | KeyCode::Char('[')
)
&& let KeyCode::Char(c) = key.code
{
self.pending_buffer_motion = Some(c);
// Fall through: also forward the key to the engine
// so its own `g`-pending state is updated correctly
// (the engine handles gj/gk/gg/G etc).
}
// ── Insert-mode completion key handling ──────────────────
// This block intercepts specific keys in insert mode to
// manage the completion popup, before forwarding to the engine.
if self.active().editor.vim_mode() == VimMode::Insert {
// <C-x><C-o> manual omni-completion trigger.
if key.modifiers.contains(KeyModifiers::CONTROL)
&& key.code == KeyCode::Char('x')
{
self.pending_ctrl_x = true;
continue;
}
if self.pending_ctrl_x {
self.pending_ctrl_x = false;
if key.modifiers.contains(KeyModifiers::CONTROL)
&& key.code == KeyCode::Char('o')
{
self.lsp_request_completion();
continue;
}
// Any other key: fall through normally (consume pending_ctrl_x).
}
// Keys that navigate/accept/dismiss the popup (popup must be open).
if self.completion.is_some() {
match key.code {
// <C-n> / <C-p> navigate selection.
KeyCode::Char('n')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
if let Some(ref mut p) = self.completion {
p.select_next();
}
continue;
}
KeyCode::Char('p')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
if let Some(ref mut p) = self.completion {
p.select_prev();
}
continue;
}
// <Tab> or <C-y> accept selected item.
KeyCode::Tab => {
self.accept_completion();
continue;
}
KeyCode::Char('y')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.accept_completion();
continue;
}
// <C-e> dismiss without accepting.
KeyCode::Char('e')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.dismiss_completion();
continue;
}
// <Esc> dismisses popup and falls through to engine
// which exits insert mode.
KeyCode::Esc => {
self.dismiss_completion();
// fall through to engine
}
// Printable char or backspace: update prefix, maybe dismiss.
KeyCode::Char(c) if key.modifiers == KeyModifiers::NONE => {
// Let engine handle it first — we update prefix after.
self.active_mut().editor.handle_key(key);
self.sync_viewport_from_editor();
if self.active_mut().editor.take_dirty() {
let elapsed =
self.active_mut().refresh_dirty_against_saved();
self.last_signature_us = elapsed;
if self.active().dirty {
self.active_mut().is_new_file = false;
}
}
let buffer_id = self.active().buffer_id;
if self.active_mut().editor.take_content_reset() {
self.syntax.reset(buffer_id);
}
let edits = self.active_mut().editor.take_content_edits();
if !edits.is_empty() {
self.syntax.apply_edits(buffer_id, &edits);
}
self.lsp_notify_change_active();
self.recompute_and_install();
// Update popup prefix.
let anchor_col =
self.completion.as_ref().map(|p| p.anchor_col).unwrap_or(0);
let cur_col = self.active().editor.buffer().cursor().col;
let cur_row = self.active().editor.buffer().cursor().row;
let anchor_row = self
.completion
.as_ref()
.map(|p| p.anchor_row)
.unwrap_or(cur_row);
if cur_row != anchor_row || cur_col < anchor_col {
// Cursor moved out of anchor range — dismiss.
self.dismiss_completion();
} else {
let new_prefix = {
let line = self
.active()
.editor
.buffer()
.lines()
.get(cur_row)
.cloned()
.unwrap_or_default();
line[anchor_col.min(line.len())
..cur_col.min(line.len())]
.to_string()
};
if let Some(ref mut popup) = self.completion {
popup.set_prefix(&new_prefix);
if popup.is_empty() {
self.completion = None;
}
}
}
// Auto-trigger on trigger chars when popup just closed.
if self.completion.is_none() {
self.maybe_auto_trigger_completion(c);
}
continue;
}
KeyCode::Backspace if key.modifiers == KeyModifiers::NONE => {
// Let engine handle backspace, then update prefix.
self.active_mut().editor.handle_key(key);
self.sync_viewport_from_editor();
if self.active_mut().editor.take_dirty() {
let elapsed =
self.active_mut().refresh_dirty_against_saved();
self.last_signature_us = elapsed;
if self.active().dirty {
self.active_mut().is_new_file = false;
}
}
let buffer_id = self.active().buffer_id;
if self.active_mut().editor.take_content_reset() {
self.syntax.reset(buffer_id);
}
let edits = self.active_mut().editor.take_content_edits();
if !edits.is_empty() {
self.syntax.apply_edits(buffer_id, &edits);
}
self.lsp_notify_change_active();
self.recompute_and_install();
let anchor_col =
self.completion.as_ref().map(|p| p.anchor_col).unwrap_or(0);
let cur_col = self.active().editor.buffer().cursor().col;
let cur_row = self.active().editor.buffer().cursor().row;
let anchor_row = self
.completion
.as_ref()
.map(|p| p.anchor_row)
.unwrap_or(cur_row);
if cur_row != anchor_row || cur_col < anchor_col {
self.dismiss_completion();
} else {
let new_prefix = {
let line = self
.active()
.editor
.buffer()
.lines()
.get(cur_row)
.cloned()
.unwrap_or_default();
line[anchor_col.min(line.len())
..cur_col.min(line.len())]
.to_string()
};
if let Some(ref mut popup) = self.completion {
popup.set_prefix(&new_prefix);
if popup.is_empty() {
self.completion = None;
}
}
}
continue;
}
_ => {
// Any other key dismisses the popup.
self.dismiss_completion();
}
}
} else {
// Popup is closed. Handle <C-n>/<C-p> as manual trigger.
if key.modifiers.contains(KeyModifiers::CONTROL)
&& matches!(key.code, KeyCode::Char('n') | KeyCode::Char('p'))
{
self.lsp_request_completion();
continue;
}
// Auto-trigger on trigger chars.
if key.modifiers == KeyModifiers::NONE
&& let KeyCode::Char(c) = key.code
{
// Let engine handle it first.
self.active_mut().editor.handle_key(key);
self.sync_viewport_from_editor();
if self.active_mut().editor.take_dirty() {
let elapsed = self.active_mut().refresh_dirty_against_saved();
self.last_signature_us = elapsed;
if self.active().dirty {
self.active_mut().is_new_file = false;
}
}
let buffer_id = self.active().buffer_id;
if self.active_mut().editor.take_content_reset() {
self.syntax.reset(buffer_id);
}
let edits = self.active_mut().editor.take_content_edits();
if !edits.is_empty() {
self.syntax.apply_edits(buffer_id, &edits);
}
self.lsp_notify_change_active();
self.recompute_and_install();
self.maybe_auto_trigger_completion(c);
continue;
}
}
} else {
// Left insert mode — dismiss popup.
if self.completion.is_some() {
self.dismiss_completion();
}
}
// ── Normal editor key handling ───────────────────────────
self.active_mut().editor.handle_key(key);
// Persist auto-scroll changes made by the engine back into
// the focused window so they survive a window-focus switch.
self.sync_viewport_from_editor();
// Drain dirty for the persistent UI flag.
if self.active_mut().editor.take_dirty() {
let elapsed = self.active_mut().refresh_dirty_against_saved();
self.last_signature_us = elapsed;
if self.active().dirty {
self.active_mut().is_new_file = false;
}
}
// Fan engine ContentEdits into the syntax tree.
let buffer_id = self.active().buffer_id;
if self.active_mut().editor.take_content_reset() {
self.syntax.reset(buffer_id);
}
let edits = self.active_mut().editor.take_content_edits();
if !edits.is_empty() {
self.syntax.apply_edits(buffer_id, &edits);
}
// Notify LSP of content change (dirty-gen-gated debounce).
self.lsp_notify_change_active();
self.recompute_and_install();
}
Event::Mouse(me) => {
use crossterm::event::MouseEventKind;
// Skip while overlays are active — let the
// overlay's own key handling (or future mouse
// handling) keep ownership of input.
if self.command_field.is_some()
|| self.search_field.is_some()
|| self.picker.is_some()
|| self.info_popup.is_some()
{
continue;
}
// 3 lines per wheel notch — vim's `mousescroll` default.
const WHEEL_LINES: i16 = 3;
match me.kind {
MouseEventKind::ScrollDown => {
self.active_mut().editor.scroll_down(WHEEL_LINES);
self.sync_viewport_from_editor();
self.recompute_and_install();
}
MouseEventKind::ScrollUp => {
self.active_mut().editor.scroll_up(WHEEL_LINES);
self.sync_viewport_from_editor();
self.recompute_and_install();
}
_ => {}
}
}
Event::Resize(w, h) => {
// Update the active editor viewport so the engine sees
// the new dimensions; the renderer will repaint all panes.
let vp = self.active_mut().editor.host_mut().viewport_mut();
vp.width = w;
vp.height = h.saturating_sub(STATUS_LINE_HEIGHT);
}
Event::FocusGained => {
self.checktime_all();
}
_ => {}
}
if self.exit_requested {
break;
}
}
Ok(())
}
}