on master help sign in
Pass through Editor instead of Context
commitaf21e2a5b491a18b22de7c99d96536bdaf741056 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 17 Feb 2022, 13:55 (5 years ago) parente023a78
4 files changed+38−37
helix-term/src/commands.rs+10−10 helix-term/src/ui/mod.rs+11−13 helix-term/src/ui/picker.rs+2−2 helix-term/src/ui/prompt.rs+15−12
helix-term/src/commands.rs+10−10
@@ -1435,7 +1435,7 @@ fn select_regex(cx: &mut Context) {
14351435 cx,
14361436 "select:".into(),
14371437 Some(reg),
1438− |_ctx: &compositor::Context, _input: &str| Vec::new(),
1438+ ui::completers::none,
14391439 move |view, doc, regex, event| {
14401440 if event != PromptEvent::Update {
14411441 return;
@@ -1458,7 +1458,7 @@ fn split_selection(cx: &mut Context) {
14581458 cx,
14591459 "split:".into(),
14601460 Some(reg),
1461− |_ctx: &compositor::Context, _input: &str| Vec::new(),
1461+ ui::completers::none,
14621462 move |view, doc, regex, event| {
14631463 if event != PromptEvent::Update {
14641464 return;
@@ -1600,7 +1600,7 @@ fn searcher(cx: &mut Context, direction: Direction) {
16001600 cx,
16011601 "search:".into(),
16021602 Some(reg),
1603− move |_ctx: &compositor::Context, input: &str| {
1603+ move |_editor: &Editor, input: &str| {
16041604 completions
16051605 .iter()
16061606 .filter(|comp| comp.starts_with(input))
@@ -1701,7 +1701,7 @@ fn global_search(cx: &mut Context) {
17011701 cx,
17021702 "global-search:".into(),
17031703 None,
1704− move |_ctx: &compositor::Context, input: &str| {
1704+ move |_editor: &Editor, input: &str| {
17051705 completions
17061706 .iter()
17071707 .filter(|comp| comp.starts_with(input))
@@ -3298,7 +3298,7 @@ fn command_mode(cx: &mut Context) {
32983298 let mut prompt = Prompt::new(
32993299 ":".into(),
33003300 Some(':'),
3301− |ctx: &compositor::Context, input: &str| {
3301+ |editor: &Editor, input: &str| {
33023302 static FUZZY_MATCHER: Lazy<fuzzy_matcher::skim::SkimMatcherV2> =
33033303 Lazy::new(fuzzy_matcher::skim::SkimMatcherV2::default);
33043304
@@ -3330,7 +3330,7 @@ fn command_mode(cx: &mut Context) {
33303330 ..
33313331 }) = cmd::TYPABLE_COMMAND_MAP.get(parts[0])
33323332 {
3333− completer(ctx, part)
3333+ completer(editor, part)
33343334 .into_iter()
33353335 .map(|(range, file)| {
33363336 // offset ranges to input
@@ -5394,7 +5394,7 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
53945394 cx,
53955395 if !remove { "keep:" } else { "remove:" }.into(),
53965396 Some(reg),
5397− |_ctx: &compositor::Context, _input: &str| Vec::new(),
5397+ ui::completers::none,
53985398 move |view, doc, regex, event| {
53995399 if event != PromptEvent::Update {
54005400 return;
@@ -6158,7 +6158,7 @@ fn shell_keep_pipe(cx: &mut Context) {
61586158 let prompt = Prompt::new(
61596159 "keep-pipe:".into(),
61606160 Some('|'),
6161− |_ctx: &compositor::Context, _input: &str| Vec::new(),
6161+ ui::completers::none,
61626162 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
61636163 let shell = &cx.editor.config.shell;
61646164 if event != PromptEvent::Validate {
@@ -6254,7 +6254,7 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
62546254 let prompt = Prompt::new(
62556255 prompt,
62566256 Some('|'),
6257− |_ctx: &compositor::Context, _input: &str| Vec::new(),
6257+ ui::completers::none,
62586258 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
62596259 let shell = &cx.editor.config.shell;
62606260 if event != PromptEvent::Validate {
@@ -6350,7 +6350,7 @@ fn rename_symbol(cx: &mut Context) {
63506350 let prompt = Prompt::new(
63516351 "rename-to:".into(),
63526352 None,
6353− |_ctx: &compositor::Context, _input: &str| Vec::new(),
6353+ ui::completers::none,
63546354 move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
63556355 if event != PromptEvent::Validate {
63566356 return;
helix-term/src/ui/mod.rs+11−13
@@ -22,7 +22,7 @@ pub use text::Text;
2222
2323use helix_core::regex::Regex;
2424use helix_core::regex::RegexBuilder;
25−use helix_view::{Document, View};
25+use helix_view::{Document, Editor, View};
2626
2727use std::path::PathBuf;
2828
@@ -30,7 +30,7 @@ pub fn regex_prompt(
3030 cx: &mut crate::commands::Context,
3131 prompt: std::borrow::Cow<'static, str>,
3232 history_register: Option<char>,
33− completion_fn: impl FnMut(&crate::compositor::Context, &str) -> Vec<prompt::Completion> + 'static,
33+ completion_fn: impl FnMut(&Editor, &str) -> Vec<prompt::Completion> + 'static,
3434 fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
3535) -> Prompt {
3636 let (view, doc) = current!(cx.editor);
@@ -168,26 +168,24 @@ pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePi
168168}
169169
170170pub mod completers {
171− use crate::compositor::Context;
172171 use crate::ui::prompt::Completion;
173172 use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
174173 use fuzzy_matcher::FuzzyMatcher;
175174 use helix_view::document::SCRATCH_BUFFER_NAME;
176− use helix_view::editor::Config;
177175 use helix_view::theme;
176+ use helix_view::{editor::Config, Editor};
178177 use once_cell::sync::Lazy;
179178 use std::borrow::Cow;
180179 use std::cmp::Reverse;
181180
182− pub type Completer = fn(&Context, &str) -> Vec<Completion>;
181+ pub type Completer = fn(&Editor, &str) -> Vec<Completion>;
183182
184− pub fn none(_cx: &Context, _input: &str) -> Vec<Completion> {
183+ pub fn none(_editor: &Editor, _input: &str) -> Vec<Completion> {
185184 Vec::new()
186185 }
187186
188− pub fn buffer(cx: &Context, input: &str) -> Vec<Completion> {
189− let mut names: Vec<_> = cx
190− .editor
187+ pub fn buffer(editor: &Editor, input: &str) -> Vec<Completion> {
188+ let mut names: Vec<_> = editor
191189 .documents
192190 .iter()
193191 .map(|(_id, doc)| {
@@ -214,7 +212,7 @@ pub mod completers {
214212 names
215213 }
216214
217− pub fn theme(_cx: &Context, input: &str) -> Vec<Completion> {
215+ pub fn theme(_editor: &Editor, input: &str) -> Vec<Completion> {
218216 let mut names = theme::Loader::read_names(&helix_core::runtime_dir().join("themes"));
219217 names.extend(theme::Loader::read_names(
220218 &helix_core::config_dir().join("themes"),
@@ -242,7 +240,7 @@ pub mod completers {
242240 names
243241 }
244242
245− pub fn setting(_cx: &Context, input: &str) -> Vec<Completion> {
243+ pub fn setting(_editor: &Editor, input: &str) -> Vec<Completion> {
246244 static KEYS: Lazy<Vec<String>> = Lazy::new(|| {
247245 serde_json::to_value(Config::default())
248246 .unwrap()
@@ -267,7 +265,7 @@ pub mod completers {
267265 .collect()
268266 }
269267
270− pub fn filename(_cx: &Context, input: &str) -> Vec<Completion> {
268+ pub fn filename(_editor: &Editor, input: &str) -> Vec<Completion> {
271269 filename_impl(input, |entry| {
272270 let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
273271
@@ -279,7 +277,7 @@ pub mod completers {
279277 })
280278 }
281279
282− pub fn directory(_cx: &Context, input: &str) -> Vec<Completion> {
280+ pub fn directory(_editor: &Editor, input: &str) -> Vec<Completion> {
283281 filename_impl(input, |entry| {
284282 let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());
285283
helix-term/src/ui/picker.rs+2−2
@@ -1,7 +1,7 @@
11use crate::{
22 compositor::{Component, Compositor, Context, EventResult},
33 ctrl, key, shift,
4− ui::EditorView,
4+ ui::{self, EditorView},
55};
66use crossterm::event::Event;
77use tui::{
@@ -302,7 +302,7 @@ impl<T> Picker<T> {
302302 let prompt = Prompt::new(
303303 "".into(),
304304 None,
305− |_ctx: &Context, _pattern: &str| Vec::new(),
305+ ui::completers::none,
306306 |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {
307307 //
308308 },
helix-term/src/ui/prompt.rs+15−12
@@ -24,7 +24,7 @@ pub struct Prompt {
2424 selection: Option<usize>,
2525 history_register: Option<char>,
2626 history_pos: Option<usize>,
27− completion_fn: Box<dyn FnMut(&Context, &str) -> Vec<Completion>>,
27+ completion_fn: Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>,
2828 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
2929 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
3030}
@@ -59,7 +59,7 @@ impl Prompt {
5959 pub fn new(
6060 prompt: Cow<'static, str>,
6161 history_register: Option<char>,
62− completion_fn: impl FnMut(&Context, &str) -> Vec<Completion> + 'static,
62+ completion_fn: impl FnMut(&Editor, &str) -> Vec<Completion> + 'static,
6363 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
6464 ) -> Self {
6565 Self {
@@ -76,6 +76,10 @@ impl Prompt {
7676 }
7777 }
7878
79+ pub fn recalculate_completion(&mut self, editor: &Editor) {
80+ self.completion = (self.completion_fn)(editor, &self.line);
81+ }
82+
7983 /// Compute the cursor position after applying movement
8084 /// Taken from: https://github.com/wez/wezterm/blob/e0b62d07ca9bf8ce69a61e30a3c20e7abc48ce7e/termwiz/src/lineedit/mod.rs#L516-L611
8185 fn eval_movement(&self, movement: Movement) -> usize {
@@ -183,7 +187,7 @@ impl Prompt {
183187 if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) {
184188 self.cursor = pos;
185189 }
186− self.completion = (self.completion_fn)(cx, &self.line);
190+ self.recalculate_completion(cx.editor);
187191 self.exit_selection();
188192 }
189193
@@ -211,7 +215,7 @@ impl Prompt {
211215 self.cursor = pos;
212216
213217 self.exit_selection();
214− self.completion = (self.completion_fn)(cx, &self.line);
218+ self.recalculate_completion(cx.editor);
215219 }
216220
217221 pub fn delete_char_forwards(&mut self, cx: &Context) {
@@ -219,7 +223,7 @@ impl Prompt {
219223 self.line.replace_range(self.cursor..pos, "");
220224
221225 self.exit_selection();
222− self.completion = (self.completion_fn)(cx, &self.line);
226+ self.recalculate_completion(cx.editor);
223227 }
224228
225229 pub fn delete_word_backwards(&mut self, cx: &Context) {
@@ -228,7 +232,7 @@ impl Prompt {
228232 self.cursor = pos;
229233
230234 self.exit_selection();
231− self.completion = (self.completion_fn)(cx, &self.line);
235+ self.recalculate_completion(cx.editor);
232236 }
233237
234238 pub fn delete_word_forwards(&mut self, cx: &Context) {
@@ -236,7 +240,7 @@ impl Prompt {
236240 self.line.replace_range(self.cursor..pos, "");
237241
238242 self.exit_selection();
239− self.completion = (self.completion_fn)(cx, &self.line);
243+ self.recalculate_completion(cx.editor);
240244 }
241245
242246 pub fn kill_to_start_of_line(&mut self, cx: &Context) {
@@ -245,7 +249,7 @@ impl Prompt {
245249 self.cursor = pos;
246250
247251 self.exit_selection();
248− self.completion = (self.completion_fn)(cx, &self.line);
252+ self.recalculate_completion(cx.editor);
249253 }
250254
251255 pub fn kill_to_end_of_line(&mut self, cx: &Context) {
@@ -253,13 +257,13 @@ impl Prompt {
253257 self.line.replace_range(self.cursor..pos, "");
254258
255259 self.exit_selection();
256− self.completion = (self.completion_fn)(cx, &self.line);
260+ self.recalculate_completion(cx.editor);
257261 }
258262
259263 pub fn clear(&mut self, cx: &Context) {
260264 self.line.clear();
261265 self.cursor = 0;
262− self.completion = (self.completion_fn)(cx, &self.line);
266+ self.recalculate_completion(cx.editor);
263267 self.exit_selection();
264268 }
265269
@@ -474,7 +478,7 @@ impl Component for Prompt {
474478 }
475479 key!(Enter) => {
476480 if self.selection.is_some() && self.line.ends_with(std::path::MAIN_SEPARATOR) {
477− self.completion = (self.completion_fn)(cx, &self.line);
481+ self.recalculate_completion(cx.editor);
478482 self.exit_selection();
479483 } else {
480484 (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
@@ -525,7 +529,6 @@ impl Component for Prompt {
525529 }
526530
527531 fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
528− self.completion = (self.completion_fn)(cx, &self.line);
529532 self.render_prompt(area, surface, cx)
530533 }
531534