Skip to content

Commit

Permalink
Simplify the persistence options.
Browse files Browse the repository at this point in the history
The options can now be set for all at once with the `all` option. These
can be overwritten but specific options (like "commands", "search",
etc). The options include "enabled", "max_entries", and "scope".

TODO:
 * Update the documentation.
  • Loading branch information
useche committed Dec 27, 2024
1 parent 456efba commit a279d85
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 52 deletions.
13 changes: 7 additions & 6 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use helix_view::{
editor::{ConfigEvent, EditorEvent},
events::DiagnosticsDidChange,
graphics::Rect,
persistence::PersistenceType,
theme,
tree::Layout,
Align, Editor,
Expand Down Expand Up @@ -150,19 +151,19 @@ impl Application {
);

// Should we be doing these in background tasks?
if persistence_config.commands {
if persistence_config.enabled(PersistenceType::Command) {
editor
.registers
.write(':', editor.persistence.read_command_history())
.unwrap();
}
if persistence_config.search {
if persistence_config.enabled(PersistenceType::Search) {
editor
.registers
.write('/', editor.persistence.read_search_history())
.unwrap();
}
if persistence_config.clipboard {
if persistence_config.enabled(PersistenceType::Clipboard) {
editor
.registers
.write('"', editor.persistence.read_clipboard_file())
Expand Down Expand Up @@ -272,7 +273,7 @@ impl Application {
.context("build signal handler")?;

let jobs = Jobs::new();
if persistence_config.old_files {
if persistence_config.enabled(PersistenceType::File) {
let persistence = editor.persistence.clone();
jobs.add(
Job::new(async move {
Expand All @@ -282,7 +283,7 @@ impl Application {
.wait_before_exiting(),
);
}
if persistence_config.commands {
if persistence_config.enabled(PersistenceType::Command) {
let persistence = editor.persistence.clone();
jobs.add(
Job::new(async move {
Expand All @@ -292,7 +293,7 @@ impl Application {
.wait_before_exiting(),
);
}
if persistence_config.search {
if persistence_config.enabled(PersistenceType::Search) {
let persistence = editor.persistence.clone();
jobs.add(
Job::new(async move {
Expand Down
7 changes: 6 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use helix_view::{
info::Info,
input::KeyEvent,
keyboard::KeyCode,
persistence::PersistenceType,
theme::Style,
tree,
view::View,
Expand Down Expand Up @@ -4301,7 +4302,11 @@ fn yank_impl(editor: &mut Editor, register: char) {
.collect();
let selections = values.len();

if editor.config().persistence.clipboard {
if editor
.config()
.persistence
.enabled(PersistenceType::Clipboard)
{
editor.persistence.write_clipboard_file(&values);
}

Expand Down
29 changes: 25 additions & 4 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use helix_core::indent::MAX_INDENT;
use helix_core::{line_ending, shellwords::Shellwords};
use helix_view::document::{read_to_string, DEFAULT_LANGUAGE_NAME};
use helix_view::editor::{CloseError, ConfigEvent};
use helix_view::persistence::PersistenceType;
use serde_json::Value;
use ui::completers::{self, Completer};

Expand Down Expand Up @@ -2540,7 +2541,12 @@ fn reload_history(
return Ok(());
}

if cx.editor.config().persistence.old_files {
if cx
.editor
.config()
.persistence
.enabled(PersistenceType::File)
{
cx.editor.old_file_locs = HashMap::from_iter(
cx.editor
.persistence
Expand All @@ -2557,7 +2563,12 @@ fn reload_history(
.wait_before_exiting(),
);
}
if cx.editor.config().persistence.commands {
if cx
.editor
.config()
.persistence
.enabled(PersistenceType::Command)
{
cx.editor
.registers
.write(':', cx.editor.persistence.read_command_history())?;
Expand All @@ -2570,7 +2581,12 @@ fn reload_history(
.wait_before_exiting(),
);
}
if cx.editor.config().persistence.search {
if cx
.editor
.config()
.persistence
.enabled(PersistenceType::Search)
{
cx.editor
.registers
.write('/', cx.editor.persistence.read_search_history())?;
Expand All @@ -2583,7 +2599,12 @@ fn reload_history(
.wait_before_exiting(),
);
}
if cx.editor.config().persistence.clipboard {
if cx
.editor
.config()
.persistence
.enabled(PersistenceType::Clipboard)
{
cx.editor
.registers
.write('"', cx.editor.persistence.read_clipboard_file())?;
Expand Down
15 changes: 13 additions & 2 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use helix_core::syntax;
use helix_view::document::Mode;
use helix_view::input::KeyEvent;
use helix_view::keyboard::KeyCode;
use helix_view::persistence::PersistenceType;
use std::sync::Arc;
use std::{borrow::Cow, ops::RangeFrom};
use tui::buffer::Buffer as Surface;
Expand Down Expand Up @@ -614,8 +615,18 @@ impl Component for Prompt {
{
cx.editor.set_error(err.to_string());
}
if (cx.editor.config().persistence.commands && register == ':')
|| (cx.editor.config().persistence.search && register == '/')
if (cx
.editor
.config()
.persistence
.enabled(PersistenceType::Command)
&& register == ':')
|| (cx
.editor
.config()
.persistence
.enabled(PersistenceType::Search)
&& register == '/')
{
cx.editor.persistence.push_reg_history(register, &self.line);
}
Expand Down
9 changes: 3 additions & 6 deletions helix-term/tests/test/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ use tempfile::{tempdir, NamedTempFile};

fn config_with_persistence(config_tmp_dir: &Path) -> Config {
let mut editor_config = editor::Config::default();
editor_config.persistence.old_files = true;
editor_config.persistence.commands = true;
editor_config.persistence.search = true;
editor_config.persistence.clipboard = true;
editor_config.persistence.search_trim = 3;
editor_config.persistence.scope =
editor_config.persistence.all.enabled = true;
editor_config.persistence.all.max_entries = 3;
editor_config.persistence.all.scope =
editor::PersistenceScope::Dir(config_tmp_dir.to_str().unwrap().to_string());

Config {
Expand Down
61 changes: 39 additions & 22 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
handlers::Handlers,
info::Info,
input::KeyEvent,
persistence::{self, FileHistoryEntry, SplitEntryLeaf},
persistence::{self, FileHistoryEntry, PersistenceType, SplitEntryLeaf},
regex::EqRegex,
register::Registers,
theme::{self, Theme},
Expand Down Expand Up @@ -966,40 +966,57 @@ pub enum PersistenceScope {
Dir(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct PersistenceConfigOption {
pub enabled: bool,
pub max_entries: usize,
pub scope: PersistenceScope,
}

impl Default for PersistenceConfigOption {
fn default() -> Self {
Self {
enabled: false,
max_entries: 100,
scope: PersistenceScope::AllInOne,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct PersistenceConfig {
pub old_files: bool,
pub commands: bool,
pub search: bool,
pub clipboard: bool,
// Default value if an specific value is not set.
pub all: PersistenceConfigOption,

// Specific values.
pub old_files: OptionToml<PersistenceConfigOption>,
pub commands: OptionToml<PersistenceConfigOption>,
pub search: OptionToml<PersistenceConfigOption>,
pub clipboard: OptionToml<PersistenceConfigOption>,
pub splits: OptionToml<PersistenceConfigOption>,

// Additional options.
pub autostart_splits: bool,
pub old_files_exclusions: Vec<EqRegex>,
pub old_files_trim: usize,
pub commands_trim: usize,
pub search_trim: usize,
pub splits_trim: usize,
pub scope: PersistenceScope,
}

impl Default for PersistenceConfig {
fn default() -> Self {
Self {
old_files: false,
commands: false,
search: false,
clipboard: false,
all: PersistenceConfigOption::default(),
old_files: OptionToml::None,
commands: OptionToml::None,
search: OptionToml::None,
clipboard: OptionToml::None,
splits: OptionToml::None,
autostart_splits: false,
// TODO: any more defaults we should add here?
old_files_exclusions: [r".*/\.git/.*", r".*/COMMIT_EDITMSG"]
.iter()
.map(|s| Regex::new(s).unwrap().into())
.collect(),
old_files_trim: 100,
commands_trim: 100,
search_trim: 100,
splits_trim: 100,
scope: PersistenceScope::AllInOne,
}
}
}
Expand Down Expand Up @@ -1247,7 +1264,7 @@ impl Editor {
area.height -= 1;

let persistence = persistence::Persistence::new(conf.persistence.clone());
let old_file_locs = if conf.persistence.old_files {
let old_file_locs = if conf.persistence.enabled(PersistenceType::File) {
HashMap::from_iter(
persistence
.read_file_history()
Expand Down Expand Up @@ -2038,7 +2055,7 @@ impl Editor {
doc.remove_view(id);
}

if self.config().persistence.old_files {
if self.config().persistence.enabled(PersistenceType::File) {
for loc in file_locs {
if !self
.config()
Expand Down Expand Up @@ -2111,7 +2128,7 @@ impl Editor {
})
.collect();

if self.config().persistence.old_files {
if self.config().persistence.enabled(PersistenceType::File) {
for loc in file_locs {
if !self
.config()
Expand Down
Loading

0 comments on commit a279d85

Please sign in to comment.