Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Picker titles #12520

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,7 @@ fn global_search(cx: &mut Context) {
cx.editor.registers.last_search_register = reg;

let picker = Picker::new(
Some("Global Search"),
columns,
1, // contents
[],
Expand Down Expand Up @@ -3037,9 +3038,16 @@ fn buffer_picker(cx: &mut Context) {
.into()
}),
];
let picker = Picker::new(columns, 2, items, (), |cx, meta, action| {
cx.editor.switch(meta.id, action);
})
let picker = Picker::new(
Some("Buffers"),
columns,
2,
items,
(),
|cx, meta, action| {
cx.editor.switch(meta.id, action);
},
)
.with_preview(|editor, meta| {
let doc = &editor.documents.get(&meta.id)?;
let &view_id = doc.selections().keys().next()?;
Expand Down Expand Up @@ -3116,6 +3124,7 @@ fn jumplist_picker(cx: &mut Context) {
];

let picker = Picker::new(
Some("Jump List"),
columns,
1, // path
cx.editor.tree.views().flat_map(|(view, _)| {
Expand Down Expand Up @@ -3198,6 +3207,7 @@ fn changed_file_picker(cx: &mut Context) {
];

let picker = Picker::new(
Some("Changed Files"),
columns,
1, // path
[],
Expand Down Expand Up @@ -3288,32 +3298,39 @@ pub fn command_palette(cx: &mut Context) {
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,
count,
editor: cx.editor,
callback: Vec::new(),
on_next_key_callback: None,
jobs: cx.jobs,
};
let focus = view!(ctx.editor).id;
let picker = Picker::new(
Some("Commands"),
columns,
0,
commands,
keymap,
move |cx, command, _action| {
let mut ctx = Context {
register,
count,
editor: cx.editor,
callback: Vec::new(),
on_next_key_callback: None,
jobs: cx.jobs,
};
let focus = view!(ctx.editor).id;

command.execute(&mut ctx);
command.execute(&mut ctx);

if ctx.editor.tree.contains(focus) {
let config = ctx.editor.config();
let mode = ctx.editor.mode();
let view = view_mut!(ctx.editor, focus);
let doc = doc_mut!(ctx.editor, &view.doc);
if ctx.editor.tree.contains(focus) {
let config = ctx.editor.config();
let mode = ctx.editor.mode();
let view = view_mut!(ctx.editor, focus);
let doc = doc_mut!(ctx.editor, &view.doc);

view.ensure_cursor_in_view(doc, config.scrolloff);
view.ensure_cursor_in_view(doc, config.scrolloff);

if mode != Mode::Insert {
doc.append_changes_to_history(view);
if mode != Mode::Insert {
doc.append_changes_to_history(view);
}
}
}
});
},
);
compositor.push(Box::new(overlaid(picker)));
},
));
Expand Down
4 changes: 3 additions & 1 deletion helix-term/src/commands/dap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn thread_picker(
}),
];
let picker = Picker::new(
None,
columns,
0,
threads,
Expand Down Expand Up @@ -257,6 +258,7 @@ pub fn dap_launch(cx: &mut Context) {
)];

cx.push_layer(Box::new(overlaid(Picker::new(
None,
columns,
0,
templates,
Expand Down Expand Up @@ -731,7 +733,7 @@ pub fn dap_switch_stack_frame(cx: &mut Context) {
let columns = [ui::PickerColumn::new("frame", |item: &StackFrame, _| {
item.name.as_str().into() // TODO: include thread_states in the label
})];
let picker = Picker::new(columns, 0, frames, (), move |cx, frame, _action| {
let picker = Picker::new(None, columns, 0, frames, (), move |cx, frame, _action| {
let debugger = debugger!(cx.editor);
// TODO: this should be simpler to find
let pos = debugger.stack_frames[&thread_id]
Expand Down
27 changes: 22 additions & 5 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ fn diag_picker(
}

Picker::new(
Some("Diagnostics"),
columns,
primary_column,
flat_diag,
Expand Down Expand Up @@ -405,6 +406,7 @@ pub fn symbol_picker(cx: &mut Context) {
];

let picker = Picker::new(
Some("Document Symbols"),
columns,
1, // name column
symbols,
Expand Down Expand Up @@ -516,6 +518,7 @@ pub fn workspace_symbol_picker(cx: &mut Context) {
];

let picker = Picker::new(
Some("Workspace Symbols"),
columns,
1, // name column
[],
Expand Down Expand Up @@ -854,6 +857,7 @@ impl Display for ApplyEditErrorKind {

/// Precondition: `locations` should be non-empty.
fn goto_impl(
title: &str,
editor: &mut Editor,
compositor: &mut Compositor,
locations: Vec<Location>,
Expand All @@ -880,9 +884,16 @@ fn goto_impl(
},
)];

let picker = Picker::new(columns, 0, locations, cwdir, move |cx, location, action| {
jump_to_location(cx.editor, location, offset_encoding, action)
})
let picker = Picker::new(
Some(title),
columns,
0,
locations,
cwdir,
move |cx, location, action| {
jump_to_location(cx.editor, location, offset_encoding, action)
},
)
.with_preview(move |_editor, location| location_to_file_location(location));
compositor.push(Box::new(overlaid(picker)));
}
Expand Down Expand Up @@ -928,7 +939,13 @@ where
if items.is_empty() {
editor.set_error("No definition found.");
} else {
goto_impl(editor, compositor, items, offset_encoding);
goto_impl(
"Goto Implementation",
editor,
compositor,
items,
offset_encoding,
);
}
},
);
Expand Down Expand Up @@ -996,7 +1013,7 @@ pub fn goto_reference(cx: &mut Context) {
if items.is_empty() {
editor.set_error("No references found.");
} else {
goto_impl(editor, compositor, items, offset_encoding);
goto_impl("Goto Reference", editor, compositor, items, offset_encoding);
}
},
);
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ fn lsp_workspace_command(
},
)];
let picker = ui::Picker::new(
Some("Workspace Symbols"),
nik-rev marked this conversation as resolved.
Show resolved Hide resolved
columns,
0,
commands,
Expand Down
27 changes: 17 additions & 10 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,23 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
.into()
},
)];
let picker = Picker::new(columns, 0, [], root, move |cx, path: &PathBuf, action| {
if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() {
format!("{}", err)
} else {
format!("unable to open \"{}\"", path.display())
};
cx.editor.set_error(err);
}
})
let picker = Picker::new(
Some("Files"),
columns,
0,
[],
root,
move |cx, path: &PathBuf, action| {
if let Err(e) = cx.editor.open(path, action) {
let err = if let Some(err) = e.source() {
format!("{}", err)
} else {
format!("unable to open \"{}\"", path.display())
};
cx.editor.set_error(err);
}
},
)
.with_preview(|_editor, path| Some((path.as_path().into(), None)));
let injector = picker.injector();
let timeout = std::time::Instant::now() + std::time::Duration::from_millis(30);
Expand Down
17 changes: 13 additions & 4 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ pub struct Picker<T: 'static + Send + Sync, D: 'static> {
/// An event handler for syntax highlighting the currently previewed file.
preview_highlight_handler: Sender<Arc<Path>>,
dynamic_query_handler: Option<Sender<DynamicQueryChange>>,
title: Option<String>,
}

impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
Expand Down Expand Up @@ -287,6 +288,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
}

pub fn new<C, O, F>(
title: Option<&str>,
nik-rev marked this conversation as resolved.
Show resolved Hide resolved
columns: C,
primary_column: usize,
options: O,
Expand All @@ -312,6 +314,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
inject_nucleo_item(&injector, &columns, item, &editor_data);
}
Self::with(
title,
matcher,
columns,
primary_column,
Expand All @@ -322,12 +325,14 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
}

pub fn with_stream(
title: Option<&str>,
matcher: Nucleo<T>,
primary_column: usize,
injector: Injector<T, D>,
callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
) -> Self {
Self::with(
title,
matcher,
injector.columns,
primary_column,
Expand All @@ -338,6 +343,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
}

fn with(
title: Option<&str>,
matcher: Nucleo<T>,
columns: Arc<[Column<T, D>]>,
default_column: usize,
Expand Down Expand Up @@ -380,6 +386,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
file_fn: None,
preview_highlight_handler: PreviewHighlightHandler::<T, D>::default().spawn(),
dynamic_query_handler: None,
title: title.map(|title| title.into()),
}
}

Expand Down Expand Up @@ -640,12 +647,14 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
let background = cx.editor.theme.get("ui.background");
surface.clear_with(area, background);

const BLOCK: Block<'_> = Block::bordered();
let block: Block<'_> = self.title.as_ref().map_or(Block::bordered(), |title| {
Block::bordered().title(title.to_string())
});

// calculate the inner area inside the box
let inner = BLOCK.inner(area);
let inner = block.inner(area);

BLOCK.render(area, surface);
block.render(area, surface);

// -- Render the input bar:

Expand Down Expand Up @@ -934,7 +943,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {

impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, D> {
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
// +---------+ +---------+
// +--title--+ +---------+
// |prompt | |preview |
// +---------+ | |
// |picker | | |
Expand Down
Loading