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

Make editor remember the latest search register #5244

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 21 additions & 5 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,9 @@ fn searcher(cx: &mut Context, direction: Direction) {
.collect()
},
move |editor, regex, event| {
if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
if event == PromptEvent::Validate {
editor.registers.last_search_register = reg;
} else if event != PromptEvent::Update {
return;
}
search_impl(
Expand All @@ -1914,7 +1916,9 @@ fn searcher(cx: &mut Context, direction: Direction) {

fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
let count = cx.count();
let register = cx.register.unwrap_or('/');
let register = cx
.register
.unwrap_or(cx.editor.registers.last_search_register);
let config = cx.editor.config();
let scrolloff = config.scrolloff;
if let Some(query) = cx.editor.registers.first(register, cx.editor) {
Expand Down Expand Up @@ -1982,13 +1986,21 @@ fn search_selection(cx: &mut Context) {

let msg = format!("register '{}' set to '{}'", register, &regex);
match cx.editor.registers.push(register, regex) {
Ok(_) => cx.editor.set_status(msg),
Ok(_) => {
cx.editor.registers.last_search_register = register;
cx.editor.set_status(msg)
}
Err(err) => cx.editor.set_error(err.to_string()),
}
}

fn make_search_word_bounded(cx: &mut Context) {
let register = cx.register.unwrap_or('/');
// Defaults to the active search register instead `/` to be more ergonomic assuming most people
// would use this command following `search_selection`. This avoids selecting the register
// twice.
let register = cx
.register
.unwrap_or(cx.editor.registers.last_search_register);
let regex = match cx.editor.registers.first(register, cx.editor) {
Some(regex) => regex,
None => return,
Expand All @@ -2014,7 +2026,10 @@ fn make_search_word_bounded(cx: &mut Context) {

let msg = format!("register '{}' set to '{}'", register, &new_regex);
match cx.editor.registers.push(register, new_regex) {
Ok(_) => cx.editor.set_status(msg),
Ok(_) => {
cx.editor.registers.last_search_register = register;
cx.editor.set_status(msg)
}
Err(err) => cx.editor.set_error(err.to_string()),
}
}
Expand Down Expand Up @@ -2078,6 +2093,7 @@ fn global_search(cx: &mut Context) {
if event != PromptEvent::Validate {
return;
}
editor.registers.last_search_register = reg;

let documents: Vec<_> = editor
.documents()
Expand Down
2 changes: 2 additions & 0 deletions helix-view/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ pub struct Registers {
/// efficiently prepend new values in `Registers::push`.
inner: HashMap<char, Vec<String>>,
clipboard_provider: Box<dyn ClipboardProvider>,
pub last_search_register: char,
}

impl Default for Registers {
fn default() -> Self {
Self {
inner: Default::default(),
clipboard_provider: get_clipboard_provider(),
last_search_register: '/',
}
}
}
Expand Down
Loading