persist and load search history

This commit is contained in:
Ingrid 2023-12-30 16:34:26 +01:00
parent 69e40a3ea5
commit 6f6f3495d5
4 changed files with 32 additions and 1 deletions

View File

@ -17,6 +17,8 @@
static COMMAND_HISTFILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
static SEARCH_HISTFILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
pub fn initialize_config_file(specified_file: Option<PathBuf>) {
let config_file = specified_file.unwrap_or_else(default_config_file);
ensure_parent_dir(&config_file);
@ -35,6 +37,12 @@ pub fn initialize_command_histfile(specified_file: Option<PathBuf>) {
COMMAND_HISTFILE.set(command_histfile).ok();
}
pub fn initialize_search_histfile(specified_file: Option<PathBuf>) {
let search_histfile = specified_file.unwrap_or_else(default_search_histfile);
ensure_parent_dir(&search_histfile);
SEARCH_HISTFILE.set(search_histfile).ok();
}
/// A list of runtime directories from highest to lowest priority
///
/// The priority is:
@ -163,6 +171,13 @@ pub fn command_histfile() -> PathBuf {
.unwrap()
}
pub fn search_histfile() -> PathBuf {
SEARCH_HISTFILE
.get()
.map(|path| path.to_path_buf())
.unwrap()
}
pub fn workspace_config_file() -> PathBuf {
find_workspace().0.join(".helix").join("config.toml")
}
@ -179,6 +194,10 @@ pub fn default_command_histfile() -> PathBuf {
state_dir().join("command_history")
}
pub fn default_search_histfile() -> PathBuf {
state_dir().join("search_history")
}
/// Merge two TOML documents, merging values from `right` onto `left`
///
/// When an array exists in both `left` and `right`, `right`'s array is

View File

@ -156,6 +156,12 @@ pub fn new(args: Args, config: Config, lang_loader: syntax::Loader) -> Result<Se
.write_unreversed(':', session::read_command_history())
// TODO: do something about this unwrap
.unwrap();
#[cfg(not(feature = "integration"))]
editor
.registers
.write_unreversed('/', session::read_search_history())
// TODO: do something about this unwrap
.unwrap();
let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| {
&config.keys

View File

@ -81,6 +81,7 @@ async fn main_impl() -> Result<i32> {
helix_loader::initialize_config_file(args.config_file.clone());
helix_loader::initialize_log_file(args.log_file.clone());
helix_loader::initialize_command_histfile(None);
helix_loader::initialize_search_histfile(None);
// Help has a higher priority and should be handled separately.
if args.display_help {

View File

@ -1,4 +1,4 @@
use helix_loader::command_histfile;
use helix_loader::{command_histfile, search_histfile};
use std::{
fs::{File, OpenOptions},
io::{self, BufRead, BufReader, Write},
@ -8,6 +8,7 @@
pub fn push_history(register: char, line: &str) {
let filepath = match register {
':' => command_histfile(),
'/' => search_histfile(),
_ => return,
};
@ -42,3 +43,7 @@ fn read_histfile(filepath: PathBuf) -> Vec<String> {
pub fn read_command_history() -> Vec<String> {
read_histfile(command_histfile())
}
pub fn read_search_history() -> Vec<String> {
read_histfile(search_histfile())
}