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

prompt add a shortcut c-s to pick a word under cursor. command search make histories as completions. #831

Merged
merged 4 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
22 changes: 22 additions & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,25 @@ Keys to use within picker. Remapping currently not supported.
| `Ctrl-s` | Open horizontally |
| `Ctrl-v` | Open vertically |
| `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 |

41 changes: 41 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@ fn select_regex(cx: &mut Context) {
cx,
"select:".into(),
Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand All @@ -1109,6 +1110,7 @@ fn split_selection(cx: &mut Context) {
cx,
"split:".into(),
Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand Down Expand Up @@ -1169,6 +1171,27 @@ 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 exist_elems = std::collections::BTreeSet::new();
reg.and_then(|reg| cx.editor.registers.get(reg))
.map_or(Vec::new(), |reg| {
reg.read()
.iter()
.rev()
.filter(|&item| {
if exist_elems.contains(item) {
false
} else {
exist_elems.insert(item);
true
}
})
cossonleo marked this conversation as resolved.
Show resolved Hide resolved
.take(20)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we limit the amount of items? The rendering UI will already limit it to a maximum

Copy link
Contributor Author

@cossonleo cossonleo Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may affect performance When completions computed compete_fn with clone is large.
I have changed to 200. Do you think it need to cancel limit?

.cloned()
.collect()
})
}

// TODO: use one function for search vs extend
fn search(cx: &mut Context) {
let reg = cx.register.unwrap_or('/');
Expand All @@ -1179,11 +1202,19 @@ fn search(cx: &mut Context) {
// HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
// feed chunks into the regex yet
let contents = doc.text().slice(..).to_string();
let completions = search_completions(cx, Some(reg));

let prompt = ui::regex_prompt(
cx,
"search:".into(),
Some(reg),
move |input: &str| {
completions
.iter()
.filter(|comp| comp.starts_with(input))
.map(|comp| (0.., std::borrow::Cow::Owned(comp.clone())))
cossonleo marked this conversation as resolved.
Show resolved Hide resolved
.collect()
},
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand Down Expand Up @@ -1243,10 +1274,19 @@ fn global_search(cx: &mut Context) {
let (all_matches_sx, all_matches_rx) =
tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>();
let smart_case = cx.editor.config.smart_case;

let completions = search_completions(cx, None);
let prompt = ui::regex_prompt(
cx,
"global search:".into(),
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| {
if event != PromptEvent::Validate {
return;
Expand Down Expand Up @@ -4083,6 +4123,7 @@ fn keep_selections(cx: &mut Context) {
cx,
"keep:".into(),
Some(reg),
|_input: &str| Vec::new(),
move |view, doc, regex, event| {
if event != PromptEvent::Update {
return;
Expand Down
3 changes: 2 additions & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn regex_prompt(
cx: &mut crate::commands::Context,
prompt: std::borrow::Cow<'static, str>,
history_register: Option<char>,
completion_fn: impl FnMut(&str) -> Vec<prompt::Completion> + 'static,
fun: impl Fn(&mut View, &mut Document, Regex, PromptEvent) + 'static,
) -> Prompt {
let (view, doc) = current!(cx.editor);
Expand All @@ -38,7 +39,7 @@ pub fn regex_prompt(
Prompt::new(
prompt,
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| {
match event {
PromptEvent::Abort => {
Expand Down
35 changes: 33 additions & 2 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ impl Prompt {
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) {
let pos = self.eval_movement(movement);
self.cursor = pos
Expand Down Expand Up @@ -473,6 +478,26 @@ impl Component for Prompt {
self.delete_char_backwards();
(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 {
code: KeyCode::Enter,
..
Expand Down Expand Up @@ -520,11 +545,17 @@ impl Component for Prompt {
}
KeyEvent {
code: KeyCode::Tab, ..
} => self.change_completion_selection(CompletionDirection::Forward),
} => {
self.change_completion_selection(CompletionDirection::Forward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
KeyEvent {
code: KeyCode::BackTab,
..
} => self.change_completion_selection(CompletionDirection::Backward),
} => {
self.change_completion_selection(CompletionDirection::Backward);
(self.callback_fn)(cx, &self.line, PromptEvent::Update)
}
KeyEvent {
code: KeyCode::Char('q'),
modifiers: KeyModifiers::CONTROL,
Expand Down