mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-19 13:37:06 +04:00
Implement in-memory prompt history
Implementation is similar to kakoune: we store the entries into a register.
This commit is contained in:
parent
29cefa1be8
commit
63e54e30a7
@ -22,13 +22,17 @@ pub fn name(&self) -> char {
|
||||
self.name
|
||||
}
|
||||
|
||||
pub fn read(&self) -> &Vec<String> {
|
||||
pub fn read(&self) -> &[String] {
|
||||
&self.values
|
||||
}
|
||||
|
||||
pub fn write(&mut self, values: Vec<String>) {
|
||||
self.values = values;
|
||||
}
|
||||
|
||||
pub fn push(&mut self, value: String) {
|
||||
self.values.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// Currently just wraps a `HashMap` of `Register`s
|
||||
@ -42,11 +46,7 @@ pub fn get(&self, name: char) -> Option<&Register> {
|
||||
self.inner.get(&name)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, name: char) -> Option<&mut Register> {
|
||||
self.inner.get_mut(&name)
|
||||
}
|
||||
|
||||
pub fn get_or_insert(&mut self, name: char) -> &mut Register {
|
||||
pub fn get_mut(&mut self, name: char) -> &mut Register {
|
||||
self.inner
|
||||
.entry(name)
|
||||
.or_insert_with(|| Register::new(name))
|
||||
@ -57,7 +57,7 @@ pub fn write(&mut self, name: char, values: Vec<String>) {
|
||||
.insert(name, Register::new_with_values(name, values));
|
||||
}
|
||||
|
||||
pub fn read(&self, name: char) -> Option<&Vec<String>> {
|
||||
pub fn read(&self, name: char) -> Option<&[String]> {
|
||||
self.get(name).map(|reg| reg.read())
|
||||
}
|
||||
}
|
||||
|
@ -1126,7 +1126,7 @@ fn delete_selection(cx: &mut Context) {
|
||||
let reg_name = cx.selected_register.name();
|
||||
let (view, doc) = current!(cx.editor);
|
||||
let registers = &mut cx.editor.registers;
|
||||
let reg = registers.get_or_insert(reg_name);
|
||||
let reg = registers.get_mut(reg_name);
|
||||
delete_selection_impl(reg, doc, view.id);
|
||||
|
||||
doc.append_changes_to_history(view.id);
|
||||
@ -1139,7 +1139,7 @@ fn change_selection(cx: &mut Context) {
|
||||
let reg_name = cx.selected_register.name();
|
||||
let (view, doc) = current!(cx.editor);
|
||||
let registers = &mut cx.editor.registers;
|
||||
let reg = registers.get_or_insert(reg_name);
|
||||
let reg = registers.get_mut(reg_name);
|
||||
delete_selection_impl(reg, doc, view.id);
|
||||
enter_insert_mode(doc);
|
||||
}
|
||||
@ -1920,6 +1920,7 @@ fn reload(
|
||||
fn command_mode(cx: &mut Context) {
|
||||
let mut prompt = Prompt::new(
|
||||
":".to_owned(),
|
||||
Some(':'),
|
||||
|input: &str| {
|
||||
// we use .this over split_whitespace() because we care about empty segments
|
||||
let parts = input.split(' ').collect::<Vec<&str>>();
|
||||
|
@ -36,6 +36,7 @@ pub fn regex_prompt(
|
||||
|
||||
Prompt::new(
|
||||
prompt,
|
||||
None,
|
||||
|_input: &str| Vec::new(), // this is fine because Vec::new() doesn't allocate
|
||||
move |cx: &mut crate::compositor::Context, input: &str, event: PromptEvent| {
|
||||
match event {
|
||||
|
@ -43,6 +43,7 @@ pub fn new(
|
||||
) -> Self {
|
||||
let prompt = Prompt::new(
|
||||
"".to_string(),
|
||||
None,
|
||||
|_pattern: &str| Vec::new(),
|
||||
|_editor: &mut Context, _pattern: &str, _event: PromptEvent| {
|
||||
//
|
||||
|
@ -20,6 +20,8 @@ pub struct Prompt {
|
||||
cursor: usize,
|
||||
completion: Vec<Completion>,
|
||||
selection: Option<usize>,
|
||||
history_register: Option<char>,
|
||||
history_pos: Option<usize>,
|
||||
completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
|
||||
callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
|
||||
pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
|
||||
@ -54,6 +56,7 @@ pub enum Movement {
|
||||
impl Prompt {
|
||||
pub fn new(
|
||||
prompt: String,
|
||||
history_register: Option<char>,
|
||||
mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
|
||||
callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
|
||||
) -> Self {
|
||||
@ -63,6 +66,8 @@ pub fn new(
|
||||
cursor: 0,
|
||||
completion: completion_fn(""),
|
||||
selection: None,
|
||||
history_register,
|
||||
history_pos: None,
|
||||
completion_fn: Box::new(completion_fn),
|
||||
callback_fn: Box::new(callback_fn),
|
||||
doc_fn: Box::new(|_| None),
|
||||
@ -226,6 +231,28 @@ pub fn clear(&mut self) {
|
||||
self.exit_selection();
|
||||
}
|
||||
|
||||
pub fn change_history(&mut self, register: &[String], direction: CompletionDirection) {
|
||||
if register.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let end = register.len().saturating_sub(1);
|
||||
|
||||
let index = match direction {
|
||||
CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1),
|
||||
CompletionDirection::Backward => {
|
||||
self.history_pos.unwrap_or(register.len()).saturating_sub(1)
|
||||
}
|
||||
}
|
||||
.min(end);
|
||||
|
||||
self.line = register[index].clone();
|
||||
|
||||
self.history_pos = Some(index);
|
||||
|
||||
self.move_end();
|
||||
}
|
||||
|
||||
pub fn change_completion_selection(&mut self, direction: CompletionDirection) {
|
||||
if self.completion.is_empty() {
|
||||
return;
|
||||
@ -468,9 +495,40 @@ fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
||||
self.exit_selection();
|
||||
} else {
|
||||
(self.callback_fn)(cx, &self.line, PromptEvent::Validate);
|
||||
|
||||
if let Some(register) = self.history_register {
|
||||
// store in history
|
||||
let register = cx.editor.registers.get_mut(register);
|
||||
register.push(self.line.clone());
|
||||
}
|
||||
return close_fn;
|
||||
}
|
||||
}
|
||||
KeyEvent {
|
||||
code: KeyCode::Char('p'),
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
}
|
||||
| KeyEvent {
|
||||
code: KeyCode::Up, ..
|
||||
} => {
|
||||
if let Some(register) = self.history_register {
|
||||
let register = cx.editor.registers.get_mut(register);
|
||||
self.change_history(register.read(), CompletionDirection::Backward);
|
||||
}
|
||||
}
|
||||
KeyEvent {
|
||||
code: KeyCode::Char('n'),
|
||||
modifiers: KeyModifiers::CONTROL,
|
||||
}
|
||||
| KeyEvent {
|
||||
code: KeyCode::Down,
|
||||
..
|
||||
} => {
|
||||
if let Some(register) = self.history_register {
|
||||
let register = cx.editor.registers.get_mut(register);
|
||||
self.change_history(register.read(), CompletionDirection::Forward);
|
||||
}
|
||||
}
|
||||
KeyEvent {
|
||||
code: KeyCode::Tab, ..
|
||||
} => self.change_completion_selection(CompletionDirection::Forward),
|
||||
|
Loading…
Reference in New Issue
Block a user