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
//! The breadcrumbs bar and sticky scroll: the crumbs for the caret, each crumb's picker of
//! folders, files or sibling symbols and its filter, View: Focus and Select Breadcrumbs,
//! and the scope headers pinned above the editor.
use super::*;
impl App {
/// The breadcrumb segments for the active editor: the workspace-relative
/// path folders and file name, then the enclosing symbol chain at the
/// caret. Empty for the welcome screen, preview tabs, and buffers with no
/// path (so the bar hides). Symbol crumbs carry a jump target; path crumbs
/// are informational.
pub(in crate::app) fn build_breadcrumbs(&self) -> Vec<crate::widgets::editor::Crumb> {
use crate::widgets::editor::Crumb;
let Some(path) = self.editor.path.as_deref() else {
return Vec::new();
};
if self.editor.has_non_text_view() {
return Vec::new();
}
let mut crumbs = Vec::new();
// Strip against the OWNING root (#147): a secondary-root file
// crumbs from its own root (folder-relative, like status_path),
// never the primary — which yielded the full absolute chain.
let rel = self
.roots
.owning_root(path)
.and_then(|r| path.strip_prefix(r).ok())
.unwrap_or(path);
for comp in rel.components() {
if let std::path::Component::Normal(os) = comp {
crumbs.push(Crumb {
label: os.to_string_lossy().into_owned(),
target: None,
icon: None,
});
}
}
// The file crumb wears its Explorer icon (drawn on a `surfaces`
// theme only); the folders before it stay plain.
if let Some(file) = crumbs.last_mut() {
let suffix = path
.extension()
.map(|e| format!(".{}", e.to_string_lossy()))
.unwrap_or_default();
let icon = crate::icons::for_path(&file.label, &suffix);
file.icon = Some((icon.glyph, icon.color));
}
// Symbol scope chain, only when the outline belongs to this file so a
// stale outline from the previous tab never leaks into the bar.
if self.outline.path() == Some(path) {
// While scrubbing (#371) the outline is the commit's, so the
// chain follows the historical view's caret.
let line = self
.scrub
.view
.as_ref()
.map_or(self.editor.cursor_row, |v| v.cursor_row) as u32;
let symbols = self.outline.symbols();
let col = self
.scrub
.view
.as_ref()
.map_or(self.editor.cursor_col, |v| v.cursor_col) as u32;
for i in breadcrumb_symbol_chain(symbols, line, col) {
let s = &symbols[i];
let icon = crate::icons::for_outline_kind(s.kind);
crumbs.push(Crumb {
label: s.name.clone(),
target: Some((s.line, s.character)),
icon: Some((icon.glyph, icon.color)),
});
}
}
crumbs
}
/// What each crumb [`build_breadcrumbs`](Self::build_breadcrumbs) makes
/// stands for, in the same order (#1608).
pub(in crate::app) fn breadcrumb_targets(&self) -> Vec<CrumbTarget> {
let Some(path) = self.editor.path.as_deref() else {
return Vec::new();
};
if self.editor.has_non_text_view() {
return Vec::new();
}
let root = self.roots.owning_root(path);
let rel = root.and_then(|r| path.strip_prefix(r).ok()).unwrap_or(path);
let mut at = root.map(Path::to_path_buf).unwrap_or_default();
let names: Vec<_> = rel
.components()
.filter_map(|c| match c {
std::path::Component::Normal(os) => Some(os.to_os_string()),
_ => None,
})
.collect();
if root.is_none() {
// An absolute path outside every root: its crumbs start at `/`.
at = path
.ancestors()
.last()
.map(Path::to_path_buf)
.unwrap_or_default();
}
let mut targets = Vec::new();
for (k, name) in names.iter().enumerate() {
at.push(name);
targets.push(if k + 1 == names.len() {
CrumbTarget::File(at.clone())
} else {
CrumbTarget::Folder(at.clone())
});
}
if self.outline.path() == Some(path) {
let line = self
.scrub
.view
.as_ref()
.map_or(self.editor.cursor_row, |v| v.cursor_row) as u32;
let col = self
.scrub
.view
.as_ref()
.map_or(self.editor.cursor_col, |v| v.cursor_col) as u32;
targets.extend(
breadcrumb_symbol_chain(self.outline.symbols(), line, col)
.into_iter()
.map(CrumbTarget::Symbol),
);
}
targets
}
/// Open crumb `i`'s picker under it (#1608): a folder crumb lists the
/// folder, the file crumb lists its folder with the file selected, and a
/// symbol crumb lists the symbols beside it with it selected.
pub(in crate::app) fn open_breadcrumb_picker(&mut self, i: usize) {
let Some(target) = self.breadcrumb_targets().into_iter().nth(i) else {
return;
};
match target {
CrumbTarget::Folder(dir) => self.open_crumb_folder_picker(&dir, i, None),
CrumbTarget::File(path) => {
let dir = path.parent().map(Path::to_path_buf).unwrap_or_default();
self.open_crumb_folder_picker(&dir, i, Some(&path));
}
CrumbTarget::Symbol(s) => {
let Some(path) = self.editor.path.clone() else {
return;
};
let symbols = self.outline.symbols();
let siblings = sibling_symbols(symbols, s);
let selected = siblings.iter().position(|&k| k == s).unwrap_or(0);
let items = siblings
.into_iter()
.map(|k| {
let sym = &symbols[k];
(
sym.name.clone(),
MenuAction::GoToLocation {
path: path.clone(),
line: sym.line,
col: sym.character,
},
)
})
.collect();
self.show_crumb_picker(i, items, selected);
}
}
}
/// The picker of crumb `i` listing `dir`: folders first, then files,
/// each by name; `select` is the row to start on.
pub(in crate::app) fn open_crumb_folder_picker(
&mut self,
dir: &Path,
i: usize,
select: Option<&Path>,
) {
let mut entries: Vec<(bool, String, PathBuf)> = std::fs::read_dir(dir)
.map(|rd| {
rd.filter_map(Result::ok)
.filter(|e| e.file_name() != ".git")
.map(|e| {
let path = e.path();
(
path.is_dir(),
e.file_name().to_string_lossy().into_owned(),
path,
)
})
.collect()
})
.unwrap_or_default();
entries.sort_by(|a, b| {
b.0.cmp(&a.0)
.then_with(|| a.1.to_lowercase().cmp(&b.1.to_lowercase()))
});
if entries.is_empty() {
self.status = format!("{} is empty", dir.display());
return;
}
let selected = select
.and_then(|p| entries.iter().position(|e| e.2 == p))
.unwrap_or(0);
let items = entries
.into_iter()
.map(|(is_dir, name, path)| {
if is_dir {
(
format!("{name}/"),
MenuAction::BrowseCrumbFolder {
dir: path,
crumb: i,
},
)
} else {
(name, MenuAction::OpenCrumbFile(path))
}
})
.collect();
self.show_crumb_picker(i, items, selected);
}
pub(in crate::app) fn show_crumb_picker(
&mut self,
i: usize,
items: Vec<(String, MenuAction)>,
selected: usize,
) {
let Some(origin) = self.editor.breadcrumb_anchor(i) else {
return;
};
let mut menu = ContextMenu::flat(origin, items, self.tree.root.clone());
menu.selected = selected;
menu.crumb = Some(i);
self.crumb_filter = (String::new(), menu.items.clone());
self.context_menu = Some(menu);
}
/// Re-derive a breadcrumb picker's rows from `crumb_filter`: the rows
/// whose label contains the typed text (case-insensitive), keeping the
/// highlighted row when it survives and otherwise starting from the top.
pub(in crate::app) fn apply_crumb_filter(&mut self) {
let Some(menu) = self.context_menu.as_mut() else {
return;
};
let (text, all) = &self.crumb_filter;
let kept = menu.items.get(menu.selected).and_then(|e| match e {
MenuEntry::Item { label, .. } => Some(label.clone()),
_ => None,
});
let needle = text.trim().to_lowercase();
menu.items = all
.iter()
.filter(|e| match e {
MenuEntry::Item { label, .. } => {
needle.is_empty() || label.to_lowercase().contains(&needle)
}
_ => needle.is_empty(),
})
.cloned()
.collect();
menu.selected = kept
.and_then(|k| {
menu.items
.iter()
.position(|e| matches!(e, MenuEntry::Item { label, .. } if *label == k))
})
.unwrap_or(0);
menu.scroll.set(0);
}
/// A file picked from a breadcrumb picker opens as a Quick Open pick does.
pub(in crate::app) fn open_crumb_file(&mut self, path: &Path) {
match self.editor.open_preview(path) {
Ok(()) => {
self.sync_open_file_poll_mtime();
self.tree.reveal_path(path);
self.focus_pane(Pane::Editor);
self.status = format!("Opened {}", self.status_path(path));
}
Err(e) => self.status = format!("Error: {e}"),
}
}
/// View: Focus and Select Breadcrumbs (#1608): the last crumb's picker,
/// from which Left / Right walk the bar.
pub(in crate::app) fn focus_breadcrumbs(&mut self) {
// A row too narrow for the whole chain focuses the last crumb that
// fit on it.
let n = self.editor.breadcrumbs.len();
let Some(last) = (0..n)
.rev()
.find(|&i| self.editor.breadcrumb_anchor(i).is_some())
else {
self.status = String::from("No breadcrumbs to focus");
return;
};
self.focus_pane(Pane::Editor);
self.open_breadcrumb_picker(last);
}
/// The scope-header lines to pin as sticky scroll: the enclosing symbols of
/// the top visible line whose header has scrolled above the viewport,
/// outermost first, capped. Empty when the feature does not apply (welcome
/// screen, preview tab, no outline, or nothing scrolled off). The header
/// is the symbol's name line (its `selectionRange`), not where its range
/// starts: a decorated Python `def` or `class` starts at `@route(...)`,
/// which names nothing and could be pinned over the `class` line (#940).
pub(in crate::app) fn build_sticky_lines(&self) -> Vec<u32> {
let Some(path) = self.editor.path.as_deref() else {
return Vec::new();
};
if self.editor.has_non_text_view() {
return Vec::new();
}
if self.outline.path() != Some(path) {
return Vec::new();
}
let top = self.editor.scroll as u32;
let symbols = self.outline.symbols();
breadcrumb_symbol_chain(symbols, top, 0)
.into_iter()
.map(|i| symbols[i].line)
.filter(|&l| l < top)
.take(STICKY_SCROLL_MAX)
.collect()
}
}
/// The breadcrumb scope chain for the caret at (`line`, `col`): the indices
/// of every outline symbol whose range encloses that position, ordered
/// outermost first (the enclosing class before the method inside it).
/// Columns count (#1221): symbols sharing a line, such as a one-line
/// interface's fields or a minified bundle's functions, are siblings, and
/// only the one the caret is in belongs to the chain. Enclosing ranges nest,
/// so the earliest start (then the latest end) is the outermost.
pub(in crate::app) fn breadcrumb_symbol_chain(
symbols: &[crate::lsp::manager::OutlineSymbol],
line: u32,
col: u32,
) -> Vec<usize> {
let start =
|s: &crate::lsp::manager::OutlineSymbol| (s.range_start_line, s.range_start_character);
let end = |s: &crate::lsp::manager::OutlineSymbol| (s.range_end_line, s.range_end_character);
let mut hits: Vec<usize> = symbols
.iter()
.enumerate()
.filter(|(_, s)| start(s) <= (line, col) && (line, col) <= end(s))
.map(|(i, _)| i)
.collect();
hits.sort_by(|&a, &b| {
start(&symbols[a])
.cmp(&start(&symbols[b]))
.then(end(&symbols[b]).cmp(&end(&symbols[a])))
});
hits
}
/// The symbols at `i`'s level under its parent, `i` among them, in outline
/// order (#1608): the outline is a pre-order walk, so they are the symbols
/// of `i`'s depth between the parent and the next symbol shallower than `i`.
pub(in crate::app) fn sibling_symbols(
symbols: &[crate::lsp::manager::OutlineSymbol],
i: usize,
) -> Vec<usize> {
let Some(depth) = symbols.get(i).map(|s| s.depth) else {
return Vec::new();
};
let start = (0..i)
.rev()
.find(|&k| symbols[k].depth < depth)
.map_or(0, |parent| parent + 1);
let end = (i + 1..symbols.len())
.find(|&k| symbols[k].depth < depth)
.unwrap_or(symbols.len());
(start..end)
.filter(|&k| symbols[k].depth == depth)
.collect()
}