Skip to content

Commit

Permalink
Add column configurations for existing pickers
Browse files Browse the repository at this point in the history
This removes the menu::Item implementations for picker item types and
adds `Vec<Column<_,_>>` configurations.
  • Loading branch information
the-mikedavis committed Sep 4, 2023
1 parent 2817049 commit 93cfbbf
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 294 deletions.
231 changes: 116 additions & 115 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub use dap::*;
use helix_vcs::Hunk;
pub use lsp::*;
use tokio::sync::oneshot;
use tui::widgets::Row;
pub use typed::*;

use helix_core::{
Expand Down Expand Up @@ -51,10 +50,9 @@ use crate::{
compositor::{self, Component, Compositor},
filter_picker_entry,
job::Callback,
keymap::ReverseKeymap,
ui::{
self, editor::InsertEvent, lsp::SignatureHelp, overlay::overlaid, CompletionItem, Picker,
Popup, Prompt, PromptEvent,
PickerColumn, Popup, Prompt, PromptEvent,
},
};

Expand Down Expand Up @@ -2101,32 +2099,15 @@ 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) -> Self {
fn new(path: &Path, line_num: usize, line_content: String) -> Self {
Self {
path: path.to_path_buf(),
line_num,
}
}
}

impl ui::menu::Item for FileResult {
type Data = Option<PathBuf>;

fn format(&self, current_path: &Self::Data) -> Row {
let relative_path = helix_core::path::get_relative_path(&self.path)
.to_string_lossy()
.into_owned();
if current_path
.as_ref()
.map(|p| p == &self.path)
.unwrap_or(false)
{
format!("{} (*)", relative_path).into()
} else {
relative_path.into()
line_content,
}
}
}
Expand Down Expand Up @@ -2172,8 +2153,28 @@ fn global_search(cx: &mut Context) {
return;
}

// TODO
let columns = vec![];
let columns = vec![
PickerColumn::new(
"path",
|item: &FileResult, current_path: &Option<PathBuf>| {
let relative_path = helix_core::path::get_relative_path(&item.path)
.to_string_lossy()
.into_owned();
if current_path
.as_ref()
.map(|p| p == &item.path)
.unwrap_or(false)
{
format!("{} (*)", relative_path).into()
} else {
relative_path.into()
}
},
),
PickerColumn::new("contents", |item: &FileResult, _| {
item.line_content.as_str().into()
}),
];
let (picker, injector) = Picker::stream(columns, current_path);

let dedup_symlinks = file_picker_config.deduplicate_links;
Expand Down Expand Up @@ -2217,9 +2218,13 @@ fn global_search(cx: &mut Context) {
};

let mut stop = false;
let sink = sinks::UTF8(|line_num, _| {
let sink = sinks::UTF8(|line_num, line_content| {
stop = injector
.push(FileResult::new(entry.path(), line_num as usize - 1))
.push(FileResult::new(
entry.path(),
line_num as usize - 1,
line_content.to_string(),
))
.is_err();

Ok(!stop)
Expand Down Expand Up @@ -2272,7 +2277,7 @@ fn global_search(cx: &mut Context) {
picker,
0,
injector,
move |cx, FileResult { path, line_num }, action| {
move |cx, FileResult { path, line_num, .. }, action| {
let doc = match cx.editor.open(path, action) {
Ok(id) => doc_mut!(cx.editor, &id),
Err(e) => {
Expand Down Expand Up @@ -2304,7 +2309,7 @@ fn global_search(cx: &mut Context) {
},
)
.with_preview(
|_editor, FileResult { path, line_num }| {
|_editor, FileResult { path, line_num, .. }| {
Some((path.clone().into(), Some((*line_num, *line_num))))
},
);
Expand Down Expand Up @@ -2695,31 +2700,6 @@ fn buffer_picker(cx: &mut Context) {
focused_at: std::time::Instant,
}

impl ui::menu::Item for BufferMeta {
type Data = ();

fn format(&self, _data: &Self::Data) -> Row {
let path = self
.path
.as_deref()
.map(helix_core::path::get_relative_path);
let path = match path.as_deref().and_then(Path::to_str) {
Some(path) => path,
None => SCRATCH_BUFFER_NAME,
};

let mut flags = String::new();
if self.is_modified {
flags.push('+');
}
if self.is_current {
flags.push('*');
}

Row::new([self.id.to_string(), flags, path.to_string()])
}
}

let new_meta = |doc: &Document| BufferMeta {
id: doc.id(),
path: doc.path().cloned(),
Expand All @@ -2738,8 +2718,31 @@ fn buffer_picker(cx: &mut Context) {
// mru
items.sort_unstable_by_key(|item| std::cmp::Reverse(item.focused_at));

let columns = vec![];
let picker = Picker::new(columns, 0, items, (), |cx, meta, action| {
let columns = vec![
PickerColumn::new("id", |meta: &BufferMeta, _| meta.id.to_string().into()),
PickerColumn::new("flags", |meta: &BufferMeta, _| {
let mut flags = String::new();
if meta.is_modified {
flags.push('+');
}
if meta.is_current {
flags.push('*');
}
flags.into()
}),
PickerColumn::new("path", |meta: &BufferMeta, _| {
let path = meta
.path
.as_deref()
.map(helix_core::path::get_relative_path);
path.as_deref()
.and_then(Path::to_str)
.unwrap_or(SCRATCH_BUFFER_NAME)
.to_string()
.into()
}),
];
let picker = Picker::new(columns, 2, items, (), |cx, meta, action| {
cx.editor.switch(meta.id, action);
})
.with_preview(|editor, meta| {
Expand All @@ -2763,33 +2766,6 @@ fn jumplist_picker(cx: &mut Context) {
is_current: bool,
}

impl ui::menu::Item for JumpMeta {
type Data = ();

fn format(&self, _data: &Self::Data) -> Row {
let path = self
.path
.as_deref()
.map(helix_core::path::get_relative_path);
let path = match path.as_deref().and_then(Path::to_str) {
Some(path) => path,
None => SCRATCH_BUFFER_NAME,
};

let mut flags = Vec::new();
if self.is_current {
flags.push("*");
}

let flag = if flags.is_empty() {
"".into()
} else {
format!(" ({})", flags.join(""))
};
format!("{} {}{} {}", self.id, path, flag, self.text).into()
}
}

for (view, _) in cx.editor.tree.views_mut() {
for doc_id in view.jumps.iter().map(|e| e.0).collect::<Vec<_>>().iter() {
let doc = doc_mut!(cx.editor, doc_id);
Expand All @@ -2816,10 +2792,37 @@ fn jumplist_picker(cx: &mut Context) {
}
};

let columns = vec![];
let columns = vec![
ui::PickerColumn::new("id", |item: &JumpMeta, _| item.id.to_string().into()),
ui::PickerColumn::new("path", |item: &JumpMeta, _| {
let path = item
.path
.as_deref()
.map(helix_core::path::get_relative_path);
path.as_deref()
.and_then(Path::to_str)
.unwrap_or(SCRATCH_BUFFER_NAME)
.to_string()
.into()
}),
ui::PickerColumn::new("flags", |item: &JumpMeta, _| {
let mut flags = Vec::new();
if item.is_current {
flags.push("*");
}

if flags.is_empty() {
"".into()
} else {
format!(" ({})", flags.join("")).into()
}
}),
ui::PickerColumn::new("contents", |item: &JumpMeta, _| item.text.as_str().into()),
];

let picker = Picker::new(
columns,
0,
1, // path
cx.editor
.tree
.views()
Expand Down Expand Up @@ -2848,35 +2851,6 @@ fn jumplist_picker(cx: &mut Context) {
cx.push_layer(Box::new(overlaid(picker)));
}

impl ui::menu::Item for MappableCommand {
type Data = ReverseKeymap;

fn format(&self, keymap: &Self::Data) -> Row {
let fmt_binding = |bindings: &Vec<Vec<KeyEvent>>| -> String {
bindings.iter().fold(String::new(), |mut acc, bind| {
if !acc.is_empty() {
acc.push(' ');
}
for key in bind {
acc.push_str(&key.key_sequence_format());
}
acc
})
};

match self {
MappableCommand::Typable { doc, name, .. } => match keymap.get(name as &String) {
Some(bindings) => format!("{} ({}) [:{}]", doc, fmt_binding(bindings), name).into(),
None => format!("{} [:{}]", doc, name).into(),
},
MappableCommand::Static { doc, name, .. } => match keymap.get(*name) {
Some(bindings) => format!("{} ({}) [{}]", doc, fmt_binding(bindings), name).into(),
None => format!("{} [{}]", doc, name).into(),
},
}
}
}

pub fn command_palette(cx: &mut Context) {
let register = cx.register;
let count = cx.count;
Expand All @@ -2896,7 +2870,34 @@ pub fn command_palette(cx: &mut Context) {
}
}));

let columns = vec![];
let columns = vec![
ui::PickerColumn::new("name", |item, _| match item {
MappableCommand::Typable { name, .. } => format!(":{name}").into(),
MappableCommand::Static { name, .. } => (*name).into(),
}),
ui::PickerColumn::new(
"bindings",
|item: &MappableCommand, keymap: &crate::keymap::ReverseKeymap| {
keymap
.get(item.name())
.map(|bindings| {
bindings.iter().fold(String::new(), |mut acc, bind| {
if !acc.is_empty() {
acc.push(' ');
}
for key in bind {
acc.push_str(&key.key_sequence_format());
}
acc
})
})
.unwrap_or_default()
.into()
},
),
ui::PickerColumn::new("doc", |item: &MappableCommand, _| item.doc().into()),
];

let picker = Picker::new(columns, 0, commands, keymap, move |cx, command, _action| {
let mut ctx = Context {
register,
Expand Down
Loading

0 comments on commit 93cfbbf

Please sign in to comment.