promote type/implementation-specific persistence logic to helix-view

pull/9143/head
Ingrid 10 months ago
parent 1e99b62828
commit 677a3dd3be

@ -1,4 +1,3 @@
use crate::{command_histfile, file_histfile, search_histfile};
use bincode::{deserialize_from, serialize_into}; use bincode::{deserialize_from, serialize_into};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@ -7,34 +6,7 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
// TODO: should this contain a ViewPosition? pub fn push_history<T: Serialize>(filepath: PathBuf, entry: T) {
// 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<T: Serialize>(filepath: PathBuf, entry: T) {
let file = OpenOptions::new() let file = OpenOptions::new()
.append(true) .append(true)
.create(true) .create(true)
@ -46,7 +18,7 @@ fn push_history<T: Serialize>(filepath: PathBuf, entry: T) {
serialize_into(file, &entry).unwrap(); serialize_into(file, &entry).unwrap();
} }
fn read_history<T: for<'a> Deserialize<'a>>(filepath: PathBuf) -> Vec<T> { pub fn read_history<T: for<'a> Deserialize<'a>>(filepath: PathBuf) -> Vec<T> {
match File::open(filepath) { match File::open(filepath) {
Ok(file) => { Ok(file) => {
let mut read = BufReader::new(file); let mut read = BufReader::new(file);
@ -64,37 +36,3 @@ fn read_history<T: for<'a> Deserialize<'a>>(filepath: PathBuf) -> Vec<T> {
}, },
} }
} }
pub fn push_file_history(entry: FileHistoryEntry) {
push_history(file_histfile(), entry)
}
pub fn read_file_history() -> Vec<FileHistoryEntry> {
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<String> {
read_history(filepath)
}
pub fn read_command_history() -> Vec<String> {
let mut hist = read_reg_history(command_histfile());
hist.reverse();
hist
}
pub fn read_search_history() -> Vec<String> {
let mut hist = read_reg_history(search_histfile());
hist.reverse();
hist
}

@ -1,7 +1,6 @@
use arc_swap::{access::Map, ArcSwap}; use arc_swap::{access::Map, ArcSwap};
use futures_util::Stream; use futures_util::Stream;
use helix_core::{diagnostic::Severity, pos_at_coords, syntax, Selection}; use helix_core::{diagnostic::Severity, pos_at_coords, syntax, Selection};
use helix_loader::persistence;
use helix_lsp::{ use helix_lsp::{
lsp::{self, notification::Notification}, lsp::{self, notification::Notification},
util::lsp_range_to_range, util::lsp_range_to_range,
@ -14,7 +13,7 @@ use helix_view::{
editor::{ConfigEvent, EditorEvent}, editor::{ConfigEvent, EditorEvent},
events::DiagnosticsDidChange, events::DiagnosticsDidChange,
graphics::Rect, graphics::Rect,
theme, persistence, theme,
tree::Layout, tree::Layout,
view::ViewPosition, view::ViewPosition,
Align, Editor, Align, Editor,

@ -2,7 +2,6 @@ use crate::compositor::{Component, Compositor, Context, Event, EventResult};
use crate::{alt, ctrl, key, shift, ui}; use crate::{alt, ctrl, key, shift, ui};
use arc_swap::ArcSwap; use arc_swap::ArcSwap;
use helix_core::syntax; use helix_core::syntax;
use helix_loader::persistence;
use helix_view::document::Mode; use helix_view::document::Mode;
use helix_view::input::KeyEvent; use helix_view::input::KeyEvent;
use helix_view::keyboard::KeyCode; use helix_view::keyboard::KeyCode;
@ -16,7 +15,7 @@ use helix_core::{
}; };
use helix_view::{ use helix_view::{
graphics::{CursorKind, Margin, Rect}, graphics::{CursorKind, Margin, Rect},
Editor, persistence, Editor,
}; };
type PromptCharHandler = Box<dyn Fn(&mut Prompt, char, &Context)>; type PromptCharHandler = Box<dyn Fn(&mut Prompt, char, &Context)>;

@ -7,13 +7,13 @@ use crate::{
handlers::Handlers, handlers::Handlers,
info::Info, info::Info,
input::KeyEvent, input::KeyEvent,
persistence::{self, FileHistoryEntry},
register::Registers, register::Registers,
theme::{self, Theme}, theme::{self, Theme},
tree::{self, Tree}, tree::{self, Tree},
Document, DocumentId, View, ViewId, Document, DocumentId, View, ViewId,
}; };
use dap::StackFrame; use dap::StackFrame;
use helix_loader::persistence::{push_file_history, FileHistoryEntry};
use helix_vcs::DiffProviderRegistry; use helix_vcs::DiffProviderRegistry;
use futures_util::stream::select_all::SelectAll; use futures_util::stream::select_all::SelectAll;
@ -1746,7 +1746,7 @@ impl Editor {
// TODO: do something about this unwrap // TODO: do something about this unwrap
let doc = self.document(view.doc).unwrap(); let doc = self.document(view.doc).unwrap();
if let Some(path) = doc.path() { if let Some(path) = doc.path() {
push_file_history(FileHistoryEntry::new( persistence::push_file_history(FileHistoryEntry::new(
path.clone(), path.clone(),
view.offset.anchor, view.offset.anchor,
view.offset.vertical_offset, view.offset.vertical_offset,

@ -13,6 +13,7 @@ pub mod handlers;
pub mod info; pub mod info;
pub mod input; pub mod input;
pub mod keyboard; pub mod keyboard;
pub mod persistence;
pub mod register; pub mod register;
pub mod theme; pub mod theme;
pub mod tree; pub mod tree;

@ -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<FileHistoryEntry> {
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<String> {
read_history(filepath)
}
pub fn read_command_history() -> Vec<String> {
let mut hist = read_reg_history(command_histfile());
hist.reverse();
hist
}
pub fn read_search_history() -> Vec<String> {
let mut hist = read_reg_history(search_histfile());
hist.reverse();
hist
}
Loading…
Cancel
Save