diff --git a/helix-loader/src/persistence.rs b/helix-loader/src/persistence.rs index 7619ff36a..926e0d4da 100644 --- a/helix-loader/src/persistence.rs +++ b/helix-loader/src/persistence.rs @@ -1,4 +1,3 @@ -use crate::{command_histfile, file_histfile, search_histfile}; use bincode::{deserialize_from, serialize_into}; use serde::{Deserialize, Serialize}; use std::{ @@ -7,34 +6,7 @@ use std::{ path::PathBuf, }; -// TODO: should this contain a ViewPosition? -// it would require exposing that type in a new crate, re-exporting in helix-view, -// since this crate is a dependency of helix-view -#[derive(Debug, Serialize, Deserialize)] -pub struct FileHistoryEntry { - pub path: PathBuf, - pub anchor: usize, - pub vertical_offset: usize, - pub horizontal_offset: usize, -} - -impl FileHistoryEntry { - pub fn new( - path: PathBuf, - anchor: usize, - vertical_offset: usize, - horizontal_offset: usize, - ) -> Self { - Self { - path, - anchor, - vertical_offset, - horizontal_offset, - } - } -} - -fn push_history(filepath: PathBuf, entry: T) { +pub fn push_history(filepath: PathBuf, entry: T) { let file = OpenOptions::new() .append(true) .create(true) @@ -46,7 +18,7 @@ fn push_history(filepath: PathBuf, entry: T) { serialize_into(file, &entry).unwrap(); } -fn read_history Deserialize<'a>>(filepath: PathBuf) -> Vec { +pub fn read_history Deserialize<'a>>(filepath: PathBuf) -> Vec { match File::open(filepath) { Ok(file) => { let mut read = BufReader::new(file); @@ -64,37 +36,3 @@ fn read_history Deserialize<'a>>(filepath: PathBuf) -> Vec { }, } } - -pub fn push_file_history(entry: FileHistoryEntry) { - push_history(file_histfile(), entry) -} - -pub fn read_file_history() -> Vec { - read_history(file_histfile()) -} - -pub fn push_reg_history(register: char, line: &str) { - let filepath = match register { - ':' => command_histfile(), - '/' => search_histfile(), - _ => return, - }; - - push_history(filepath, line) -} - -fn read_reg_history(filepath: PathBuf) -> Vec { - read_history(filepath) -} - -pub fn read_command_history() -> Vec { - let mut hist = read_reg_history(command_histfile()); - hist.reverse(); - hist -} - -pub fn read_search_history() -> Vec { - let mut hist = read_reg_history(search_histfile()); - hist.reverse(); - hist -} diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index d3d6f73cf..e462edc96 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -1,7 +1,6 @@ use arc_swap::{access::Map, ArcSwap}; use futures_util::Stream; use helix_core::{diagnostic::Severity, pos_at_coords, syntax, Selection}; -use helix_loader::persistence; use helix_lsp::{ lsp::{self, notification::Notification}, util::lsp_range_to_range, @@ -14,7 +13,7 @@ use helix_view::{ editor::{ConfigEvent, EditorEvent}, events::DiagnosticsDidChange, graphics::Rect, - theme, + persistence, theme, tree::Layout, view::ViewPosition, Align, Editor, diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index e64deadab..ef87f49cd 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -2,7 +2,6 @@ use crate::compositor::{Component, Compositor, Context, Event, EventResult}; use crate::{alt, ctrl, key, shift, ui}; use arc_swap::ArcSwap; use helix_core::syntax; -use helix_loader::persistence; use helix_view::document::Mode; use helix_view::input::KeyEvent; use helix_view::keyboard::KeyCode; @@ -16,7 +15,7 @@ use helix_core::{ }; use helix_view::{ graphics::{CursorKind, Margin, Rect}, - Editor, + persistence, Editor, }; type PromptCharHandler = Box; diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 8ef3a940e..778fb38d2 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -7,13 +7,13 @@ use crate::{ handlers::Handlers, info::Info, input::KeyEvent, + persistence::{self, FileHistoryEntry}, register::Registers, theme::{self, Theme}, tree::{self, Tree}, Document, DocumentId, View, ViewId, }; use dap::StackFrame; -use helix_loader::persistence::{push_file_history, FileHistoryEntry}; use helix_vcs::DiffProviderRegistry; use futures_util::stream::select_all::SelectAll; @@ -1746,7 +1746,7 @@ impl Editor { // TODO: do something about this unwrap let doc = self.document(view.doc).unwrap(); if let Some(path) = doc.path() { - push_file_history(FileHistoryEntry::new( + persistence::push_file_history(FileHistoryEntry::new( path.clone(), view.offset.anchor, view.offset.vertical_offset, diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index d54b49ef5..0044ac618 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -13,6 +13,7 @@ pub mod handlers; pub mod info; pub mod input; pub mod keyboard; +pub mod persistence; pub mod register; pub mod theme; pub mod tree; diff --git a/helix-view/src/persistence.rs b/helix-view/src/persistence.rs new file mode 100644 index 000000000..7d9b24c0c --- /dev/null +++ b/helix-view/src/persistence.rs @@ -0,0 +1,66 @@ +use helix_loader::{ + command_histfile, file_histfile, + persistence::{push_history, read_history}, + search_histfile, +}; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +// TODO: should this contain a ViewPosition? +#[derive(Debug, Serialize, Deserialize)] +pub struct FileHistoryEntry { + pub path: PathBuf, + pub anchor: usize, + pub vertical_offset: usize, + pub horizontal_offset: usize, +} + +impl FileHistoryEntry { + pub fn new( + path: PathBuf, + anchor: usize, + vertical_offset: usize, + horizontal_offset: usize, + ) -> Self { + Self { + path, + anchor, + vertical_offset, + horizontal_offset, + } + } +} + +pub fn push_file_history(entry: FileHistoryEntry) { + push_history(file_histfile(), entry) +} + +pub fn read_file_history() -> Vec { + read_history(file_histfile()) +} + +pub fn push_reg_history(register: char, line: &str) { + let filepath = match register { + ':' => command_histfile(), + '/' => search_histfile(), + _ => return, + }; + + push_history(filepath, line) +} + +fn read_reg_history(filepath: PathBuf) -> Vec { + read_history(filepath) +} + +pub fn read_command_history() -> Vec { + let mut hist = read_reg_history(command_histfile()); + hist.reverse(); + hist +} + +pub fn read_search_history() -> Vec { + let mut hist = read_reg_history(search_histfile()); + hist.reverse(); + hist +}