Add a hidden column for the global search line contents

We could expand on this in the future to have different preview modes
that you can toggle between with C-t. Currently that binding just hides
the preview but it could switch between different preview modes and in
one mode hide the path and just show the line contents.
Michael Davis 2 months ago
parent b1d90d1931
commit c4415119fd
No known key found for this signature in database

@ -2199,15 +2199,13 @@ fn global_search(cx: &mut Context) {
path: PathBuf,
/// 0 indexed lines
line_num: usize,
line_content: String,
}
impl FileResult {
fn new(path: &Path, line_num: usize, line_content: String) -> Self {
fn new(path: &Path, line_num: usize) -> Self {
Self {
path: path.to_path_buf(),
line_num,
line_content,
}
}
}
@ -2230,10 +2228,7 @@ fn global_search(cx: &mut Context) {
.into_owned()
.into()
}),
PickerColumn::new("contents", |item: &FileResult, _| {
item.line_content.as_str().into()
})
.without_filtering(),
PickerColumn::hidden("contents"),
];
let get_files = |query: &str,
@ -2313,13 +2308,9 @@ fn global_search(cx: &mut Context) {
};
let mut stop = false;
let sink = sinks::UTF8(|line_num, line_content| {
let sink = sinks::UTF8(|line_num, _line_content| {
stop = injector
.push(FileResult::new(
entry.path(),
line_num as usize - 1,
line_content.to_string(),
))
.push(FileResult::new(entry.path(), line_num as usize - 1))
.is_err();
Ok(!stop)

@ -190,6 +190,7 @@ pub struct Column<T, D> {
/// `DynamicPicker` uses this so that the dynamic column (for example regex in
/// global search) is not used for filtering twice.
filter: bool,
hidden: bool,
}
impl<T, D> Column<T, D> {
@ -198,6 +199,19 @@ impl<T, D> Column<T, D> {
name: name.into(),
format,
filter: true,
hidden: false,
}
}
/// A column which does not display any contents
pub fn hidden(name: impl Into<String>) -> Self {
let format = |_: &T, _: &D| unreachable!();
Self {
name: name.into(),
format,
filter: false,
hidden: true,
}
}
@ -674,6 +688,10 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
let mut matcher_index = 0;
Row::new(self.columns.iter().map(|column| {
if column.hidden {
return Cell::default();
}
let Some(Constraint::Length(max_width)) = widths.next() else {
unreachable!();
};
@ -753,10 +771,13 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
if self.columns.len() > 1 {
let header_style = cx.editor.theme.get("ui.picker.header");
table =
table.header(Row::new(self.columns.iter().map(|column| {
table = table.header(Row::new(self.columns.iter().map(|column| {
if column.hidden {
Cell::default()
} else {
Cell::from(Span::styled(column.name.as_str(), header_style))
})));
}
})));
}
use tui::widgets::TableState;

Loading…
Cancel
Save