Skip to content

Commit

Permalink
WIP: Make splits persistent in the 'split' file.
Browse files Browse the repository at this point in the history
  • Loading branch information
useche committed Nov 30, 2024
1 parent 03139a7 commit 08bcd3d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
15 changes: 13 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,10 +1254,16 @@ impl Editor {
HashMap::new()
};

let splits = persistence.read_split_file();
let mut split_info = HashMap::with_capacity(splits.len());
for entry in splits {
split_info.insert(entry.name, entry.tree);
}

Self {
mode: Mode::Normal,
tree: Tree::new(area),
split_info: HashMap::new(),
split_info,
next_document_id: DocumentId::default(),
documents: BTreeMap::new(),
saves: HashMap::new(),
Expand Down Expand Up @@ -1802,9 +1808,14 @@ impl Editor {
let tree_info = self.tree.get_tree_info();

self.split_info.insert(
split_name,
split_name.clone(),
self.get_split_tree(tree_info.focus, tree_info.tree),
);

self.persistence.push_split_entry(&persistence::SplitEntry {
name: split_name.clone(),
tree: self.split_info.get(&split_name).unwrap().clone(),
});
}

fn load_split_tree(
Expand Down
32 changes: 28 additions & 4 deletions helix-view/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ impl FileHistoryEntry {
}

// TODO: Explain what's all of this about.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Layout {
Horizontal,
Vertical,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SplitEntryNode {
pub layout: Layout,
pub children: Vec<SplitEntryTree>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SplitEntryLeaf {
// Path to the document.
pub path: PathBuf,
Expand All @@ -48,17 +48,23 @@ pub struct SplitEntryLeaf {
// Whether this was the focused split or not.
pub focus: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SplitEntryTree {
Leaf(Option<SplitEntryLeaf>),
Node(SplitEntryNode),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SplitEntry {
pub name: String,
pub tree: SplitEntryTree,
}

enum PersistenceFiles {
Command,
Search,
File,
Clipboard,
Splits,
}

fn persistence_dir(config: &PersistenceConfig) -> PathBuf {
Expand All @@ -77,6 +83,7 @@ fn default_file_path(file: PersistenceFiles, config: &PersistenceConfig) -> Path
PersistenceFiles::Search => "search_history",
PersistenceFiles::File => "file_history",
PersistenceFiles::Clipboard => "clipboard",
PersistenceFiles::Splits => "splits",
};

let path = persistence_dir(config).join(filename);
Expand Down Expand Up @@ -171,4 +178,21 @@ impl Persistence {
&self.config,
))
}

pub fn push_split_entry(&self, entry: &SplitEntry) {
push_history(
default_file_path(PersistenceFiles::Splits, &self.config),
&entry,
);
}

pub fn read_split_file(&self) -> Vec<SplitEntry> {
read_history(&default_file_path(PersistenceFiles::Splits, &self.config))
}

// TODO: trim split file.
// TODO: save split automatically when closing the editor.
// - The logical thing to do is to save it in "quit-all". Check if other cases make sense.
// TODO: load split '' automatically when the editor just open.
// TODO: add configuration to load split '' or not automatically at startup.
}

0 comments on commit 08bcd3d

Please sign in to comment.