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
//! Buffer-list management: `:bn`/`:bp`/`:bd`. Cycles and deletes
//! against the MRU `opened_paths` and the sleeping snapshot map; the
//! actual file-open work lives in [`super::open`].
use anyhow::Result;
use crate::buffer_ref::BufferRef;
use crate::editor::Buffer;
use super::pane::PaneLayout;
use super::{App, Toast, root_cause};
impl App {
/// `:bn` / `:bp` — cycle through `opened_paths`. Same semantics
/// as vim's `:bnext` / `:bprev`: forward wraps to the start, back
/// wraps to the end. No-op when there's only one buffer.
pub fn buffer_cycle(&mut self, forward: bool) -> Result<()> {
if self.opened_paths.len() <= 1 {
self.push_toast(Toast::info("only one buffer"));
return Ok(());
}
let current_ref = self.active_ref();
let len = self.opened_paths.len();
let idx = self
.opened_paths
.iter()
.position(|r| r == ¤t_ref)
.unwrap_or(0);
let target_idx = if forward {
(idx + 1) % len
} else {
(idx + len - 1) % len
};
let target = self.opened_paths[target_idx].clone();
self.switch_to_buffer(target)
}
/// `:bd` / `:bd!` — drop the current buffer from MRU and
/// sleeping, then switch to the most-recent remaining buffer
/// (falling back to a fresh scratch). Refuses on dirty without
/// `force`. The deleted buffer is *not* stashed — its content
/// is gone, same as vim's `:bd`.
pub fn buffer_delete(&mut self, force: bool) -> Result<()> {
if !force && self.active_doc().dirty {
self.push_toast(Toast::error("unsaved changes (use :bd!)"));
return Ok(());
}
let current_ref = self.active_ref();
// Pick a successor before mutating state — the most-recent
// entry that *isn't* the one we're deleting.
let target = self
.opened_paths
.iter()
.rev()
.find(|r| *r != ¤t_ref)
.cloned();
// Drop the deleted buffer from all bookkeeping. The document
// truly goes away — pull it out of the pool too. For
// file-backed buffers we also tell every LSP client to
// forget about the document — `:bd` is the one path where
// the buffer truly goes away, so the server should release
// its copy. Plain `detach_current` would leave the URI
// `didOpen`'d (the right call for plain buffer switches,
// see [`LspCoordinator::detach_current`]).
self.opened_paths.retain(|r| r != ¤t_ref);
self.sleeping.remove(¤t_ref);
self.documents.remove(¤t_ref);
if let BufferRef::File(path) = ¤t_ref {
let uri = crate::lsp::path_to_uri(path);
self.lsp.close_uri(&uri);
} else {
self.lsp.detach_current();
}
match target {
Some(BufferRef::Scratch(id)) => {
let key = BufferRef::Scratch(id);
self.ensure_doc_pooled(&key, Buffer::new);
self.install_buffer(key.clone());
self.current_scratch_id = Some(id);
self.open_gen = self.open_gen.wrapping_add(1);
self.lsp.set_last_synced_version(self.active_doc().version);
self.record_opened(key);
self.push_toast(Toast::info(format!(
"deleted, {}",
BufferRef::scratch_label(id)
)));
Ok(())
}
Some(BufferRef::File(path)) => {
let key = BufferRef::File(path.clone());
// Restore from the pool / sleeping when available;
// otherwise re-read disk. Both paths set up LSP/highlighter.
if self.documents.contains_key(&key) || self.sleeping.contains_key(&key) {
self.ensure_doc_pooled(&key, Buffer::new);
self.install_buffer(key.clone());
self.open_gen = self.open_gen.wrapping_add(1);
self.lsp.set_last_synced_version(self.active_doc().version);
self.record_opened(key);
self.spawn_engine_worker(&path);
self.spawn_vcs_worker();
self.spawn_lsp_worker(&path);
self.push_toast(Toast::info(format!("deleted, restored {}", path.display())));
} else {
// Successor isn't pooled or sleeping (rare — would
// mean it was evicted by MRU cap while being in the
// picker). Fresh-load from disk.
let loaded = match Buffer::load(&path) {
Ok(b) => b,
Err(e) => {
let id = self.mint_scratch_id();
let key = BufferRef::Scratch(id);
self.documents.insert(key.clone(), Buffer::new());
self.install_buffer(key.clone());
self.current_scratch_id = Some(id);
self.open_gen = self.open_gen.wrapping_add(1);
self.record_opened(key);
self.push_toast(Toast::fatal(format!(
"deleted; failed to open {}: {} — using scratch",
path.display(),
root_cause(&e)
)));
return Ok(());
}
};
self.documents.insert(key.clone(), loaded);
self.install_buffer(key.clone());
self.current_scratch_id = None;
self.record_opened(key);
self.open_gen = self.open_gen.wrapping_add(1);
self.lsp.set_last_synced_version(self.active_doc().version);
self.spawn_engine_worker(&path);
self.spawn_vcs_worker();
self.spawn_lsp_worker(&path);
self.push_toast(Toast::info(format!("deleted, opened {}", path.display())));
}
Ok(())
}
None => {
// Nothing left — start a fresh scratch.
let id = self.mint_scratch_id();
let key = BufferRef::Scratch(id);
self.documents.insert(key.clone(), Buffer::new());
self.install_buffer(key.clone());
self.current_scratch_id = Some(id);
self.open_gen = self.open_gen.wrapping_add(1);
self.record_opened(key);
self.push_toast(Toast::info(format!(
"deleted, {}",
BufferRef::scratch_label(id)
)));
Ok(())
}
}
}
/// `:bca` — force-discard every buffer (active, parked, sleeping)
/// and land on a fresh scratch. Unsaved edits are dropped without
/// confirmation; LSP clients are told to forget every file URI we
/// had open. The pane layout is collapsed back to a single leaf
/// since the parked buffers backing the inactive panes are gone.
pub fn buffer_delete_all(&mut self) -> Result<()> {
// Tell every LSP client to release the URIs we had open —
// covers the active buffer plus all parked / sleeping file
// buffers. Scratch buffers don't have URIs.
if let Some(path) = self.active_doc().path.clone() {
let uri = crate::lsp::path_to_uri(&path);
self.lsp.close_uri(&uri);
} else {
self.lsp.detach_current();
}
let file_refs: Vec<std::path::PathBuf> = self
.documents
.keys()
.chain(self.sleeping.keys())
.filter_map(|r| match r {
BufferRef::File(p) => Some(p.clone()),
BufferRef::Scratch(_) => None,
})
.collect();
for path in &file_refs {
let uri = crate::lsp::path_to_uri(path);
self.lsp.close_uri(&uri);
}
self.documents.clear();
self.pane_content.clear();
self.sleeping.clear();
self.opened_paths.clear();
// The agent *process* (`App.agent`) survives — only the editor
// buffers are wiped — but its pane is gone with the collapsed
// layout, so drop the pane reference.
self.agent_pane = None;
// Collapse to a single pane displaying the new scratch. Use the
// editor pane as the surviving leaf so `App.editor` keeps
// backing it; the active pane may have been the (now-gone) agent.
self.layout = PaneLayout::Leaf(self.editor_pane);
self.active_pane = self.editor_pane;
let id = self.mint_scratch_id();
let key = BufferRef::Scratch(id);
self.documents.insert(key.clone(), Buffer::new());
self.install_buffer(key.clone());
self.current_scratch_id = Some(id);
self.open_gen = self.open_gen.wrapping_add(1);
self.lsp.set_last_synced_version(self.active_doc().version);
self.record_opened(key);
self.push_toast(Toast::info("deleted all buffers"));
Ok(())
}
/// Allocate a fresh scratch id and bump the counter. Never reuses
/// an id even after the corresponding buffer is deleted, so a
/// stashed sleeping scratch can't be conflated with a brand-new
/// one minted by `:new` later.
pub(super) fn mint_scratch_id(&mut self) -> u32 {
let id = self.next_scratch_id;
self.next_scratch_id = self.next_scratch_id.saturating_add(1);
id
}
}