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
//! Drag-a-tab-onto-a-pane → split (VS Code-style drag-to-split).
//!
//! Builds on the existing bufferline tab drag-reorder (`bufferline_drag_tab`
//! in `PaneRects`). When a tab drag ends over a *pane body* (rather than the
//! tab strip), the dragged pane is moved into the layout next to the pane under
//! the cursor:
//!
//! * an **edge** zone (left/right/top/bottom) splits the target pane in that
//! direction and drops the dragged pane into the new half;
//! * the **center** zone moves the dragged pane *into* the target's slot
//! (replacing what's shown there — the displaced pane stays open as a
//! background bufferline tab, nothing is destroyed).
//!
//! Both are pure `Layout` mutations: `remove_leaf` detaches the dragged pane
//! from the visible tree (a no-op if it was already a background tab), then
//! `replace_leaf` re-inserts it. `App::panes` is never touched, so no buffer is
//! closed and no `PaneId` shifts.
use crate::app::App;
use crate::focus::Focus;
use crate::layout::{Layout, PaneId, SplitDir};
use crate::pane::Pane;
use ratatui::layout::Rect;
use std::path::PathBuf;
/// Which region of a pane body the cursor is over during a tab drag.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DropZone {
Left,
Right,
Top,
Bottom,
Center,
}
impl App {
/// While a bufferline tab is being dragged, record which pane body + zone
/// the cursor is over (or clear it when over the tab strip / off any pane).
/// Read by the drop-hint overlay in `ui::draw`.
pub fn update_tab_drop_target(&mut self, x: u16, y: u16) {
self.rects.tab_drop_target = hit_pane(self, x, y);
}
/// Complete a bufferline tab drag that ended at `(x, y)`. If it ended over
/// a pane body, split that pane (edge zones) or move the dragged pane into
/// it (center zone). No-op when released over the tab strip / off any pane,
/// or when dropped onto its own pane.
pub fn drop_tab_on_pane(&mut self, src: PaneId, x: u16, y: u16) {
self.rects.tab_drop_target = None;
let Some((target, zone)) = hit_pane(self, x, y) else {
return;
};
if src == target {
// Dropping a tab onto its own pane body — happens when
// the user drags the active tab from the bufferline
// onto the visible pane (the only pane in single-leaf
// layouts). Center is a no-op; edges mean "split this
// leaf and put src in the new half" — split_tab_into
// already does that (finds an integration tab, splices).
if matches!(zone, DropZone::Center) {
return;
}
self.split_tab_into(src, zone);
return;
}
self.splice_pane_at(src, target, zone);
self.active = Some(src);
self.focus = Focus::Pane;
}
/// Complete a *file-tree* drag that ended at `(x, y)`: open `path` (reusing
/// an already-open editor pane for it, else creating one) and place it next
/// to the pane under the cursor — the tree-file twin of `drop_tab_on_pane`.
/// Falls back to a plain `open_path` when not released over a pane body.
pub fn drop_tree_file_on_pane(&mut self, path: PathBuf, x: u16, y: u16) {
self.rects.tab_drop_target = None;
let Some((target, zone)) = hit_pane(self, x, y) else {
// Not over a pane — behave like a normal open.
self.open_path(&path);
return;
};
// Reuse an already-open editor pane for this file, else mint one.
let src = match self
.panes
.iter()
.position(|p| matches!(p, Pane::Editor(b) if b.is_at(&path)))
{
Some(id) => id,
None => {
let mut b = crate::buffer::Buffer::open(&path, &self.config)
.unwrap_or_else(|_| crate::buffer::Buffer::scratch(&self.config));
b.apply_editorconfig(&self.workspace);
self.panes.push(Pane::Editor(b));
self.panes.len() - 1
}
};
if src != target {
self.splice_pane_at(src, target, zone);
}
self.active = Some(src);
self.focus = Focus::Pane;
}
/// Split off `src` into a new half of the leaf containing it.
/// Mirrors the drag-to-edge gesture but driven by the keyboard /
/// right-click "Split right/down/left/up" menu items. Needs a
/// surviving sibling tab in the current leaf — splitting a
/// solo tab off itself is a no-op (just leaves an empty leaf).
/// Drop a dragged tab onto a tab strip (Chrome / VS Code tab-bar
/// drop). Finds the strip under `(x, y)`, computes the insert
/// index from the cursor's x position relative to chips on
/// that strip, and moves `src` into the strip's leaf at that
/// index. Falls back to no-op when not over any strip.
/// Returns true when the drop landed.
pub fn drop_tab_on_strip(&mut self, src: PaneId, x: u16, y: u16) -> bool {
// Hit the strip first.
let strip_hit = self
.rects
.split_tab_strip_areas
.iter()
.find(|(r, _)| crate::app::dispatch::contains(*r, x, y))
.copied();
let Some((_strip_rect, leaf_active)) = strip_hit else {
return false;
};
let insert_idx = self.tab_strip_insert_idx(leaf_active, x);
// vscode-user r3 SEV-1 (2026-08-05) — same-leaf reorder where
// src IS the active tab used to orphan src: `remove_leaf`
// reassigns the leaf's `active` to a surviving tab, so the
// subsequent `active_leaf_mut(leaf_active)` lookup returned
// None, hit the "target vanished" branch, and left src out
// of every leaf. Fix: capture a stable identifier for the
// destination leaf (any surviving tab ≠ src) BEFORE remove,
// then relocate the leaf after remove via
// `leaf_containing_mut(survivor)` — which works regardless
// of what happened to `active`.
let dest_survivor: Option<PaneId> = self
.layout()
.leaf_containing(leaf_active)
.and_then(|tabs| tabs.iter().copied().find(|&t| t != src));
// Two-in-one guard covering both cases the reviewer flagged
// on b08d66e0 / d8359908:
// (a) same-leaf drop of a SOLO tab — dest_survivor is None
// and the leaf's only tab is src, so a no-op is the
// correct semantic (dropping a tab onto itself).
// (b) STALE leaf_active — `leaf_containing(leaf_active)`
// returned None because the rect was captured before
// a layout mutation and no longer resolves. Any
// mutation from here would land on a dead id and
// leave src orphaned (the original 2026-08-05 bug).
// In both cases dest_survivor.is_none(); bail before touching
// the layout so src stays visible where it already was.
if dest_survivor.is_none() {
self.focus = Focus::Pane;
return true;
}
// dest_survivor is Some past the guard above. Unwrap is safe.
let survivor = dest_survivor.expect("guarded above");
// Remove src from wherever it lives, then insert at idx.
self.layout_mut().remove_leaf(src);
// Relocate the destination leaf via a surviving pane that
// remove_leaf couldn't have touched (survivor ≠ src, so
// survivor's leaf is intact — potentially collapsed a split
// if src's source leaf was elsewhere, but leaf_containing
// still finds it).
if let Some((active, tabs)) = self.layout_mut().leaf_containing_mut(survivor) {
let idx = insert_idx.min(tabs.len());
if !tabs.contains(&src) {
tabs.insert(idx, src);
}
*active = src;
self.active = Some(src);
self.focus = Focus::Pane;
return true;
}
// survivor vanished mid-remove? extremely unlikely — leave
// layout untouched rather than risk orphaning src.
false
}
/// Compute the insert index for a tab dropping at cursor `x`
/// on the strip belonging to `leaf_active`. Walks the strip's
/// chips in left-to-right order; the insert position is
/// "before the first chip whose center is to the right of x"
/// (or `tabs.len()` if no such chip — i.e. append).
pub(crate) fn tab_strip_insert_idx(&self, leaf_active: PaneId, x: u16) -> usize {
// mouse-round-9 SEV-2 2026-07-11 — was `r.x + r.width / 2`
// (midpoint). Dropping in the RIGHT HALF of a chip's rect
// treated the pointer as "past" that chip and returned the
// next slot, so a drop inside notes.md's rect landed at
// slot 4 (past notes.md) instead of slot 3 (notes.md's own).
// Use 3/4 of the width: dropping in the last quarter still
// means "insert after"; dropping in the first three quarters
// means "insert at this chip's slot" (which for a same-leaf
// reorder pushes the chip right — the expected VS Code feel).
let mut chips: Vec<(u16, PaneId)> = self
.rects
.split_tab_chips
.iter()
.filter_map(|(r, leaf, pane)| {
if *leaf == leaf_active {
Some((r.x + (r.width * 3 / 4), *pane))
} else {
None
}
})
.collect();
chips.sort_by_key(|(cx, _)| *cx);
let mut idx = 0;
for (cx, _pid) in &chips {
if x < *cx {
return idx;
}
idx += 1;
}
idx
}
/// Update `tab_insert_hint` while a tab drag is in flight.
/// Called from the Drag / Moved handlers. Sets None when the
/// cursor isn't over any strip.
pub fn update_tab_insert_hint(&mut self, x: u16, y: u16) {
let strip_hit = self
.rects
.split_tab_strip_areas
.iter()
.find(|(r, _)| crate::app::dispatch::contains(*r, x, y))
.copied();
let Some((strip_rect, leaf_active)) = strip_hit else {
self.rects.tab_insert_hint = None;
return;
};
let idx = self.tab_strip_insert_idx(leaf_active, x);
// Pick the insertion-x: midpoint between the chip before
// idx and the chip at idx. For idx==0 → left edge of strip.
// For idx==len → right of the last chip.
let mut chips: Vec<u16> = self
.rects
.split_tab_chips
.iter()
.filter(|(_, leaf, _)| *leaf == leaf_active)
.map(|(r, _, _)| r.x + r.width)
.collect();
chips.sort();
let insertion_x = if idx == 0 {
strip_rect.x
} else if idx >= chips.len() {
chips.last().copied().unwrap_or(strip_rect.x)
} else {
let prev_end = chips[idx - 1];
let next_start = self
.rects
.split_tab_chips
.iter()
.filter(|(_, leaf, _)| *leaf == leaf_active)
.map(|(r, _, _)| r.x)
.min()
.unwrap_or(strip_rect.x);
(prev_end + next_start) / 2
};
self.rects.tab_insert_hint = Some((strip_rect, insertion_x, leaf_active, idx));
}
pub fn split_tab_into(&mut self, src: PaneId, zone: DropZone) {
// First try: an integration tab in the same leaf (the
// common case — splice_pane_at handles it cleanly).
if let Some(target) = self
.layout()
.leaf_containing(src)
.and_then(|tabs| tabs.iter().find(|&&t| t != src).copied())
{
self.splice_pane_at(src, target, zone);
self.active = Some(src);
self.focus = Focus::Pane;
return;
}
// Fallback: src is alone in its leaf, but other panes
// exist in `app.panes` as orphans (visible in the global
// bufferline, not in any leaf — can happen via earlier
// buggy layout transitions or background-tab edge cases).
// Pick one orphan to fill the "stays behind" half of the
// split so we don't lose layout coverage.
let in_layout: std::collections::HashSet<PaneId> = (0..self.panes.len())
.filter(|&i| self.layouts.iter().any(|l| l.leaf_containing(i).is_some()))
.collect();
let orphan = (0..self.panes.len()).find(|&i| {
i != src
&& !in_layout.contains(&i)
&& !matches!(self.panes.get(i), Some(crate::pane::Pane::Pty(_)))
});
if let Some(orphan) = orphan {
// Bring the orphan into src's leaf as a tab, then run
// the normal split path. That way the orphan ends up
// in the original leaf's spot and src goes into the
// new half — same shape as the integration case.
if let Some((_active, tabs)) = self.layout_mut().leaf_containing_mut(src) {
tabs.insert(0, orphan); // before src
}
self.splice_pane_at(src, orphan, zone);
self.active = Some(src);
self.focus = Focus::Pane;
return;
}
self.toast("can't split: tab is alone in its pane");
}
/// Place pane `src` next to leaf `target` per `zone`. Detaches `src` from
/// the visible tree first (a no-op if it was a background tab; if it was a
/// visible split half, that split collapses into its sibling). `target`'s
/// id is stable across this — `remove_leaf` only reshapes the tree.
fn splice_pane_at(&mut self, src: PaneId, target: PaneId, zone: DropZone) {
self.layout_mut().remove_leaf(src);
match zone {
DropZone::Center => {
// 2026-06-22 multi-tab: add `src` as a new tab in
// the target leaf and flip it active. Previously
// replaced the leaf, orphaning the prior pane.
if let Some((active, tabs)) = self.layout_mut().active_leaf_mut(target) {
if !tabs.contains(&src) {
tabs.push(src);
}
*active = src;
} else {
self.layout_mut().replace_leaf(target, Layout::leaf(src));
}
}
_ => {
let (dir, src_first) = match zone {
DropZone::Left => (SplitDir::Horizontal, true),
DropZone::Right => (SplitDir::Horizontal, false),
DropZone::Top => (SplitDir::Vertical, true),
DropZone::Bottom => (SplitDir::Vertical, false),
DropZone::Center => unreachable!(),
};
// PRESERVE the target leaf's full tab list. Old code
// built `Layout::leaf(target)` which silently
// orphaned every other tab that shared the same
// leaf (e.g. 3-tab leaf → split → 1 tab lost).
// Pull the post-remove_leaf leaf's tabs + active and
// use them for the target half. Fallback to
// [target] solo when target isn't in a leaf for
// some reason.
let (target_active, target_tabs): (PaneId, Vec<PaneId>) =
if let Some((act, tabs)) = self.layout_mut().leaf_containing_mut(target) {
(*act, tabs.clone())
} else {
(target, vec![target])
};
let target_leaf = Layout::Leaf {
tabs: target_tabs,
active: target_active,
};
let src_leaf = Layout::leaf(src);
let (first, second) = if src_first {
(src_leaf, target_leaf)
} else {
(target_leaf, src_leaf)
};
self.layout_mut().replace_leaf(
target,
Layout::Split {
dir,
ratio: 50,
first: Box::new(first),
second: Box::new(second),
},
);
}
}
}
}
/// Find the pane body under `(x, y)` and the zone within it.
fn hit_pane(app: &App, x: u16, y: u16) -> Option<(PaneId, DropZone)> {
app.rects
.pane_bodies
.iter()
.find(|(r, _)| crate::app::dispatch::contains(*r, x, y))
.map(|(r, pid)| (*pid, zone_for(*r, x, y)))
}
/// Classify a point within a pane rect into a drop zone. The middle third on
/// both axes is `Center`; otherwise the nearest edge wins. Integer math only
/// (no float comparisons) so the result is deterministic and clippy-clean.
pub(crate) fn zone_for(r: Rect, x: u16, y: u16) -> DropZone {
let w = r.width.max(1) as u32;
let h = r.height.max(1) as u32;
let dx = x.saturating_sub(r.x) as u32;
let dy = y.saturating_sub(r.y) as u32;
// Middle third on each axis (dx in [w/3, 2w/3)).
let center_x = dx * 3 >= w && dx * 3 < w * 2;
let center_y = dy * 3 >= h && dy * 3 < h * 2;
if center_x && center_y {
return DropZone::Center;
}
// Normalize the distance to each edge onto a common 0..=1000 scale so panes
// of different proportions compare fairly, then pick the nearest edge.
let left = (dx * 1000) / w;
let right = 1000u32.saturating_sub(left);
let top = (dy * 1000) / h;
let bottom = 1000u32.saturating_sub(top);
let min = left.min(right).min(top).min(bottom);
if min == left {
DropZone::Left
} else if min == top {
DropZone::Top
} else if min == bottom {
DropZone::Bottom
} else {
DropZone::Right
}
}
/// The sub-rect of a pane body that a given drop zone occupies — used to paint
/// the drop-hint overlay so the user sees where the pane will land.
pub(crate) fn zone_rect(r: Rect, zone: DropZone) -> Rect {
match zone {
DropZone::Left => Rect::new(r.x, r.y, r.width / 2, r.height),
DropZone::Right => {
let w = r.width / 2;
Rect::new(r.x + (r.width - w), r.y, w, r.height)
}
DropZone::Top => Rect::new(r.x, r.y, r.width, r.height / 2),
DropZone::Bottom => {
let h = r.height / 2;
Rect::new(r.x, r.y + (r.height - h), r.width, h)
}
DropZone::Center => {
// Middle third box.
let w = (r.width / 3).max(1);
let h = (r.height / 3).max(1);
Rect::new(r.x + r.width / 3, r.y + r.height / 3, w, h)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::buffer::Buffer;
use crate::config::Config;
use crate::pane::Pane;
fn r() -> Rect {
Rect::new(10, 5, 30, 20)
}
/// An app with two editor panes (ids 0 | 1) shown as a horizontal split,
/// active = 0. No files / no session — pure in-memory layout state.
fn app_two_panes() -> App {
let mut app = App::new(std::env::temp_dir(), Config::default()).unwrap();
app.panes.clear();
app.panes.push(Pane::Editor(Buffer::scratch(&app.config)));
app.panes.push(Pane::Editor(Buffer::scratch(&app.config)));
*app.layout_mut() = Layout::Split {
dir: SplitDir::Horizontal,
ratio: 50,
first: Box::new(Layout::leaf(0)),
second: Box::new(Layout::leaf(1)),
};
app.active = Some(0);
app
}
#[test]
fn zones_classify_by_region() {
let rr = r();
// Center of the rect → Center.
assert_eq!(zone_for(rr, 10 + 15, 5 + 10), DropZone::Center);
// Far left edge → Left.
assert_eq!(zone_for(rr, 10, 5 + 10), DropZone::Left);
// Far right edge → Right.
assert_eq!(zone_for(rr, 10 + 29, 5 + 10), DropZone::Right);
// Top edge → Top.
assert_eq!(zone_for(rr, 10 + 15, 5), DropZone::Top);
// Bottom edge → Bottom.
assert_eq!(zone_for(rr, 10 + 15, 5 + 19), DropZone::Bottom);
}
#[test]
fn drop_on_right_edge_splits_target_with_src_on_the_right() {
let mut app = app_two_panes();
let target = 0;
let src = 1;
// Pane 0 body rect — fabricate one and register it.
let body = Rect::new(0, 1, 40, 20);
app.rects.pane_bodies = vec![(body, target)];
// Right edge of pane 0.
app.drop_tab_on_pane(src, body.x + body.width - 1, body.y + body.height / 2);
// Target should now be a Split whose right (second) child is `src`.
match app.layout() {
Layout::Split { first, second, .. } => {
assert!(matches!(**first, Layout::Leaf { active: id, .. } if id == target));
assert!(matches!(**second, Layout::Leaf { active: id, .. } if id == src));
}
other => panic!("expected a split, got {other:?}"),
}
assert_eq!(app.active, Some(src));
}
#[test]
fn center_drop_moves_src_into_target_without_a_split() {
let mut app = app_two_panes();
let target = 0;
let src = 1;
let body = Rect::new(0, 1, 40, 20);
app.rects.pane_bodies = vec![(body, target)];
// Center of the target pane.
app.drop_tab_on_pane(src, body.x + body.width / 2, body.y + body.height / 2);
// remove_leaf(1) collapses the split to Leaf(0), then replace_leaf(0,
// Leaf(1)) → the visible tree is just Leaf(1) (src moved in; old pane 0
// survives as a background tab).
assert!(matches!(app.layout(), Layout::Leaf { active: id, .. } if *id == src));
assert_eq!(app.active, Some(src));
}
#[test]
fn drop_on_own_pane_center_is_a_noop() {
// Center-zone drop on the own pane stays a no-op even after
// the 2026-06-27 edge-zone split fix — nothing to do when
// dragging a tab onto its own slot's center.
let mut app = app_two_panes();
// Single visible leaf, no orphans → genuinely solo.
app.panes.pop(); // drop the second pane so there's nothing to recover
*app.layout_mut() = Layout::leaf(0);
app.active = Some(0);
let only = 0;
let body = Rect::new(0, 1, 40, 20);
app.rects.pane_bodies = vec![(body, only)];
// Center of the pane body — landing zone for a center drop.
app.drop_tab_on_pane(only, body.x + body.width / 2, body.y + body.height / 2);
assert!(matches!(app.layout(), Layout::Leaf { active: id, .. } if *id == only));
}
/// vscode-user r3 SEV-1 (2026-08-05) — dropping the active tab
/// onto its own strip (reorder within same leaf) used to orphan
/// the tab entirely: after `remove_leaf(src)` the leaf's
/// `active` had shifted, so the follow-up `active_leaf_mut`
/// lookup missed. Now we identify the destination leaf via a
/// surviving sibling, and src ends up back in the leaf at the
/// requested index.
#[test]
fn same_leaf_reorder_of_active_tab_keeps_src_in_the_leaf() {
let mut app = app_two_panes();
// Collapse to a single leaf with two tabs.
*app.layout_mut() = crate::layout::Layout::leaf_with_tabs(0, vec![0, 1]);
app.active = Some(0);
// Register the strip + one chip so the drop can hit.
let strip = Rect::new(0, 0, 40, 1);
app.rects.split_tab_strip_areas = vec![(strip, 0)];
// (chip_rect, tab_pane, leaf_active_pane) — leaf_active is 0
app.rects.split_tab_chips = vec![
(Rect::new(0, 0, 10, 1), 0, 0),
(Rect::new(10, 0, 10, 1), 1, 0),
];
// Drop pane 0 (the active tab) past pane 1 to reorder it right.
assert!(app.drop_tab_on_strip(0, 25, 0));
// Layout must still contain both panes as tabs in one leaf.
match app.layout() {
crate::layout::Layout::Leaf { active, tabs } => {
assert_eq!(*active, 0, "src should be active after drop");
assert!(tabs.contains(&0), "src (0) must still be in the tabs list");
assert!(
tabs.contains(&1),
"sibling (1) must still be in the tabs list"
);
}
other => panic!("expected a single leaf, got {other:?}"),
}
assert_eq!(app.active, Some(0));
}
/// Reviewer follow-up (2026-08-06) — same-leaf drop of a SOLO
/// tab used to empty the layout: no `dest_survivor`, so
/// `remove_leaf(src)` destroyed the leaf and `replace_leaf`
/// couldn't find a target with `active==src`. Now bails early.
/// Reviewer follow-up (2026-08-06 #2) — a strip drop where the
/// captured `leaf_active` no longer resolves (rect was stale
/// between hint-update and drop) must bail cleanly, not orphan
/// src. Simulated by pointing the strip at a pane id that isn't
/// in the layout.
#[test]
fn stale_leaf_active_id_bails_without_orphaning_src() {
let mut app = app_two_panes();
// Two-tab leaf. leaf_active in strip points at pane 99
// which isn't in the layout — leaf_containing returns None.
*app.layout_mut() = crate::layout::Layout::leaf_with_tabs(0, vec![0, 1]);
app.active = Some(0);
let strip = Rect::new(0, 0, 40, 1);
app.rects.split_tab_strip_areas = vec![(strip, 99)];
// Should NOT crash and MUST leave both tabs in the leaf.
let landed = app.drop_tab_on_strip(0, 5, 0);
// Return value can be false (bailed) or true (no-op) — the
// invariant is layout integrity.
assert!(!landed || landed); // silence unused_variable warning
match app.layout() {
crate::layout::Layout::Leaf { tabs, .. } => {
assert!(tabs.contains(&0), "src (0) must not be orphaned");
assert!(tabs.contains(&1), "sibling (1) must survive");
}
other => panic!("expected the leaf intact, got {other:?}"),
}
}
#[test]
fn same_leaf_reorder_of_solo_tab_is_a_noop() {
let mut app = app_two_panes();
// Collapse to a single leaf with ONE tab (pane 0).
*app.layout_mut() = crate::layout::Layout::leaf(0);
app.active = Some(0);
let strip = Rect::new(0, 0, 40, 1);
app.rects.split_tab_strip_areas = vec![(strip, 0)];
app.rects.split_tab_chips = vec![(Rect::new(0, 0, 10, 1), 0, 0)];
assert!(app.drop_tab_on_strip(0, 5, 0));
// Layout must be unchanged.
match app.layout() {
crate::layout::Layout::Leaf { active, tabs } => {
assert_eq!(*active, 0);
assert_eq!(tabs, &vec![0]);
}
other => panic!("expected the single leaf preserved, got {other:?}"),
}
assert_eq!(app.active, Some(0));
}
}