Add c-s to pick word under doc cursor to prompt line & search completion (#831)

* Add prompt shourtcut to book
Add completions to search
Add c-s to pick word under doc cursor to prompt line

* limit 20 last items of search completion, update book

* Update book/src/keymap.md

Co-authored-by: Ivan Tham <pickfire@riseup.net>

* limit search completions 200

Co-authored-by: Ivan Tham <pickfire@riseup.net>
imgbot
CossonLeo 3 years ago committed by GitHub
parent 70d21a903f
commit 39584cbccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -258,3 +258,25 @@ Keys to use within picker. Remapping currently not supported.
| `Ctrl-s` | Open horizontally | | `Ctrl-s` | Open horizontally |
| `Ctrl-v` | Open vertically | | `Ctrl-v` | Open vertically |
| `Escape`, `Ctrl-c` | Close picker | | `Escape`, `Ctrl-c` | Close picker |
# Prompt
Keys to use within prompt, Remapping currently not supported.
| Key | Description |
| ----- | ------------- |
| `Escape`, `Ctrl-c` | Close prompt |
| `Alt-b`, `Alt-Left` | Backward a word |
| `Ctrl-b`, `Left` | Backward a char |
| `Alt-f`, `Alt-Right` | Forward a word |
| `Ctrl-f`, `Right` | Forward a char |
| `Ctrl-e`, `End` | move prompt end |
| `Ctrl-a`, `Home` | move prompt start |
| `Ctrl-w` | delete previous word |
| `Ctrl-k` | delete to end of line |
| `backspace` | delete previous char |
| `Ctrl-s` | insert a word under doc cursor, may be changed to Ctrl-r Ctrl-w later |
| `Ctrl-p`, `Up` | select previous history |
| `Ctrl-n`, `Down` | select next history |
| `Tab` | slect next completion item |
| `BackTab` | slect previous completion item |
| `Enter` | Open selected |

@ -1087,6 +1087,7 @@ fn select_regex(cx: &mut Context) {
cx, cx,
"select:".into(), "select:".into(),
Some(reg), Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| { move |view, doc, regex, event| {
if event != PromptEvent::Update { if event != PromptEvent::Update {
return; return;
@ -1109,6 +1110,7 @@ fn split_selection(cx: &mut Context) {
cx, cx,
"split:".into(), "split:".into(),
Some(reg), Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| { move |view, doc, regex, event| {
if event != PromptEvent::Update { if event != PromptEvent::Update {
return; return;
@ -1172,6 +1174,15 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege
}; };
} }
fn search_completions(cx: &mut Context, reg: Option<char>) -> Vec<String> {
let mut items = reg
.and_then(|reg| cx.editor.registers.get(reg))
.map_or(Vec::new(), |reg| reg.read().iter().take(200).collect());
items.sort_unstable();
items.dedup();
items.into_iter().cloned().collect()
}
// TODO: use one function for search vs extend // TODO: use one function for search vs extend
fn search(cx: &mut Context) { fn search(cx: &mut Context) {
let reg = cx.register.unwrap_or('/'); let reg = cx.register.unwrap_or('/');
@ -1182,11 +1193,19 @@ fn search(cx: &mut Context) {
// HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
// feed chunks into the regex yet // feed chunks into the regex yet
let contents = doc.text().slice(..).to_string(); let contents = doc.text().slice(..).to_string();
let completions = search_completions(cx, Some(reg));
let prompt = ui::regex_prompt( let prompt = ui::regex_prompt(
cx, cx,
"search:".into(), "search:".into(),
Some(reg), Some(reg),
move |input: &str| {
completions
.iter()
.filter(|comp| comp.starts_with(input))
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.collect()
},
move |view, doc, regex, event| { move |view, doc, regex, event| {
if event != PromptEvent::Update { if event != PromptEvent::Update {
return; return;
@ -1246,10 +1265,19 @@ fn global_search(cx: &mut Context) {
let (all_matches_sx, all_matches_rx) = let (all_matches_sx, all_matches_rx) =
tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>(); tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
let smart_case = cx.editor.config.smart_case; let smart_case = cx.editor.config.smart_case;
let completions = search_completions(cx, None);
let prompt = ui::regex_prompt( let prompt = ui::regex_prompt(
cx, cx,
"global search:".into(), "global search:".into(),
None, None,
move |input: &str| {
completions
.iter()
.filter(|comp| comp.starts_with(input))
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
.collect()
},
move |_view, _doc, regex, event| { move |_view, _doc, regex, event| {
if event != PromptEvent::Validate { if event != PromptEvent::Validate {
return; return;
@ -4086,6 +4114,7 @@ fn keep_selections(cx: &mut Context) {
cx, cx,
"keep:".into(), "keep:".into(),
Some(reg), Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| { move |view, doc, regex, event| {
if event != PromptEvent::Update { if event != PromptEvent::Update {
return; return;

@ -29,6 +29,7 @@ pub fn regex_prompt(
cx: &mut crate::commands::Context, cx: &mut crate::commands::Context,
prompt: std::borrow::Cow<'static, str>, prompt: std::borrow::Cow<'static, str>,
history_register: Option<char>, history_register: Option<char>,
completion_fn: impl FnMut(&str) -> Vec<prompt::Completion> + 'static,
fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static, fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
) -> Prompt { ) -> Prompt {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
@ -38,7 +39,7 @@ pub fn regex_prompt(
Prompt::new( Prompt::new(
prompt, prompt,
history_register, history_register,
|_input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate completion_fn,
move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| { move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
match event { match event {
PromptEvent::Abort => { PromptEvent::Abort => {

@ -185,6 +185,11 @@ impl Prompt {
self.exit_selection(); self.exit_selection();
} }
pub fn insert_str(&mut self, s: &str) {
self.line.insert_str(self.cursor, s);
self.cursor += s.len();
}
pub fn move_cursor(&mut self, movement: Movement) { pub fn move_cursor(&mut self, movement: Movement) {
let pos = self.eval_movement(movement); let pos = self.eval_movement(movement);
self.cursor = pos self.cursor = pos
@ -473,6 +478,26 @@ impl Component for Prompt {
self.delete_char_backwards(); self.delete_char_backwards();
(self.callback_fn)(cx, &self.line, PromptEvent::Update); (self.callback_fn)(cx, &self.line, PromptEvent::Update);
} }
KeyEvent {
code: KeyCode::Char('s'),
modifiers: KeyModifiers::CONTROL,
} => {
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
use helix_core::textobject;
let range = textobject::textobject_word(
text,
doc.selection(view.id).primary(),
textobject::TextObject::Inside,
1,
);
let line = text.slice(range.from()..range.to()).to_string();
if !line.is_empty() {
self.insert_str(line.as_str());
(self.callback_fn)(cx, &self.line, PromptEvent::Update);
}
}
KeyEvent { KeyEvent {
code: KeyCode::Enter, code: KeyCode::Enter,
.. ..
@ -520,11 +545,17 @@ impl Component for Prompt {
} }
KeyEvent { KeyEvent {
code: KeyCode::Tab, .. code: KeyCode::Tab, ..
} => self.change_completion_selection(CompletionDirection::Forward), } => {
self.change_completion_selection(CompletionDirection::Forward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
KeyEvent { KeyEvent {
code: KeyCode::BackTab, code: KeyCode::BackTab,
.. ..
} => self.change_completion_selection(CompletionDirection::Backward), } => {
self.change_completion_selection(CompletionDirection::Backward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
KeyEvent { KeyEvent {
code: KeyCode::Char('q'), code: KeyCode::Char('q'),
modifiers: KeyModifiers::CONTROL, modifiers: KeyModifiers::CONTROL,

Loading…
Cancel
Save