on master help sign in
Remove more push_layer calls
commit96a4eb84838d4cbfb9313968278985ea1986a3bc authorBlaž Hrastnik <blaz@mxxn.io> dateTue 22 Mar 2022, 23:25 (5 years ago) parent5c162ef
3 files changed+35−33
helix-term/src/commands.rs+15−20 helix-term/src/commands/lsp.rs+7−13 helix-term/src/ui/mod.rs+13
helix-term/src/commands.rs+15−20
@@ -2187,7 +2187,7 @@ async fn make_format_callback(
21872187 format: impl Future<Output = helix_lsp::util::LspFormatting> + Send + 'static,
21882188) -> anyhow::Result<job::Callback> {
21892189 let format = format.await;
2190− let call: job::Callback = Box::new(move |editor: &mut Editor, _compositor: &mut Compositor| {
2190+ let call: job::Callback = Box::new(move |editor, _compositor| {
21912191 let view_id = view!(editor).id;
21922192 if let Some(doc) = editor.document_mut(doc_id) {
21932193 if doc.version() == doc_version {
@@ -3475,9 +3475,7 @@ pub fn completion(cx: &mut Context) {
34753475
34763476 cx.callback(
34773477 future,
3478− move |editor: &mut Editor,
3479− compositor: &mut Compositor,
3480− response: Option<lsp::CompletionResponse>| {
3478+ move |editor, compositor, response: Option<lsp::CompletionResponse>| {
34813479 let doc = doc!(editor);
34823480 if doc.mode() != Mode::Insert {
34833481 // we're not in insert mode anymore
@@ -4123,11 +4121,12 @@ fn shell_append_output(cx: &mut Context) {
41234121}
41244122
41254123fn shell_keep_pipe(cx: &mut Context) {
4126− let prompt = Prompt::new(
4124+ ui::prompt(
4125+ cx,
41274126 "keep-pipe:".into(),
41284127 Some('|'),
41294128 ui::completers::none,
4130− move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
4129+ move |cx, input: &str, event: PromptEvent| {
41314130 let shell = &cx.editor.config().shell;
41324131 if event != PromptEvent::Validate {
41334132 return;
@@ -4171,8 +4170,6 @@ fn shell_keep_pipe(cx: &mut Context) {
41714170 doc.set_selection(view.id, Selection::new(ranges, index));
41724171 },
41734172 );
4174−
4175− cx.push_layer(Box::new(prompt));
41764173}
41774174
41784175fn shell_impl(
@@ -4219,11 +4216,13 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
42194216 ShellBehavior::Replace | ShellBehavior::Ignore => true,
42204217 ShellBehavior::Insert | ShellBehavior::Append => false,
42214218 };
4222− let prompt = Prompt::new(
4219+
4220+ ui::prompt(
4221+ cx,
42234222 prompt,
42244223 Some('|'),
42254224 ui::completers::none,
4226− move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
4225+ move |cx, input: &str, event: PromptEvent| {
42274226 let config = cx.editor.config();
42284227 let shell = &config.shell;
42294228 if event != PromptEvent::Validate {
@@ -4273,8 +4272,6 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
42734272 view.ensure_cursor_in_view(doc, config.scrolloff);
42744273 },
42754274 );
4276−
4277− cx.push_layer(Box::new(prompt));
42784275}
42794276
42804277fn suspend(_cx: &mut Context) {
@@ -4420,13 +4417,11 @@ fn replay_macro(cx: &mut Context) {
44204417 };
44214418
44224419 let count = cx.count();
4423− cx.callback = Some(Box::new(
4424− move |compositor: &mut Compositor, cx: &mut compositor::Context| {
4425− for _ in 0..count {
4426− for &key in keys.iter() {
4427− compositor.handle_event(crossterm::event::Event::Key(key.into()), cx);
4428− }
4420+ cx.callback = Some(Box::new(move |compositor, cx| {
4421+ for _ in 0..count {
4422+ for &key in keys.iter() {
4423+ compositor.handle_event(crossterm::event::Event::Key(key.into()), cx);
44294424 }
4430− },
4431− ));
4425+ }
4426+ }));
44324427}
helix-term/src/commands/lsp.rs+7−13
@@ -11,7 +11,7 @@ use helix_view::editor::Action;
1111
1212use crate::{
1313 compositor::{self, Compositor},
14− ui::{self, overlay::overlayed, FileLocation, FilePicker, Popup, Prompt, PromptEvent},
14+ ui::{self, overlay::overlayed, FileLocation, FilePicker, Popup, PromptEvent},
1515};
1616
1717use std::borrow::Cow;
@@ -138,9 +138,7 @@ pub fn symbol_picker(cx: &mut Context) {
138138
139139 cx.callback(
140140 future,
141− move |editor: &mut Editor,
142− compositor: &mut Compositor,
143− response: Option<lsp::DocumentSymbolResponse>| {
141+ move |editor, compositor, response: Option<lsp::DocumentSymbolResponse>| {
144142 if let Some(symbols) = response {
145143 // lsp has two ways to represent symbols (flat/nested)
146144 // convert the nested variant to flat, so that we have a homogeneous list
@@ -172,9 +170,7 @@ pub fn workspace_symbol_picker(cx: &mut Context) {
172170
173171 cx.callback(
174172 future,
175− move |_editor: &mut Editor,
176− compositor: &mut Compositor,
177− response: Option<Vec<lsp::SymbolInformation>>| {
173+ move |_editor, compositor, response: Option<Vec<lsp::SymbolInformation>>| {
178174 if let Some(symbols) = response {
179175 let picker = sym_picker(symbols, current_url, offset_encoding);
180176 compositor.push(Box::new(overlayed(picker)))
@@ -208,9 +204,7 @@ pub fn code_action(cx: &mut Context) {
208204
209205 cx.callback(
210206 future,
211− move |editor: &mut Editor,
212− compositor: &mut Compositor,
213− response: Option<lsp::CodeActionResponse>| {
207+ move |editor, compositor, response: Option<lsp::CodeActionResponse>| {
214208 let actions = match response {
215209 Some(a) => a,
216210 None => return,
@@ -613,7 +607,7 @@ pub fn hover(cx: &mut Context) {
613607
614608 cx.callback(
615609 future,
616− move |editor: &mut Editor, compositor: &mut Compositor, response: Option<lsp::Hover>| {
610+ move |editor, compositor, response: Option<lsp::Hover>| {
617611 if let Some(hover) = response {
618612 // hover.contents / .range <- used for visualizing
619613
@@ -650,7 +644,8 @@ pub fn hover(cx: &mut Context) {
650644 );
651645}
652646pub fn rename_symbol(cx: &mut Context) {
653− let prompt = Prompt::new(
647+ ui::prompt(
648+ cx,
654649 "rename-to:".into(),
655650 None,
656651 ui::completers::none,
@@ -670,5 +665,4 @@ pub fn rename_symbol(cx: &mut Context) {
670665 apply_workspace_edit(cx.editor, offset_encoding, &edits);
671666 },
672667 );
673− cx.push_layer(Box::new(prompt));
674668}
helix-term/src/ui/mod.rs+13
@@ -26,6 +26,19 @@ use helix_view::{Document, Editor, View};
2626
2727use std::path::PathBuf;
2828
29+pub fn prompt(
30+ cx: &mut crate::commands::Context,
31+ prompt: std::borrow::Cow<'static, str>,
32+ history_register: Option<char>,
33+ completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
34+ callback_fn: impl FnMut(&mut crate::compositor::Context, &str, PromptEvent) + 'static,
35+) {
36+ let mut prompt = Prompt::new(prompt, history_register, completion_fn, callback_fn);
37+ // Calculate initial completion
38+ prompt.recalculate_completion(cx.editor);
39+ cx.push_layer(Box::new(prompt));
40+}
41+
2942pub fn regex_prompt(
3043 cx: &mut crate::commands::Context,
3144 prompt: std::borrow::Cow<'static, str>,