From 8b3e152126ec8f0e67dad378dc62c734ea97a4b0 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Mon, 7 Sep 2020 11:28:52 +0900 Subject: cleanup: Make Buffer just a part of State. --- helix-core/src/state.rs | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'helix-core/src/state.rs') diff --git a/helix-core/src/state.rs b/helix-core/src/state.rs index 8568c3c3..c3a5c3cd 100644 --- a/helix-core/src/state.rs +++ b/helix-core/src/state.rs @@ -1,5 +1,8 @@ use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes}; -use crate::{Buffer, Rope, RopeSlice, Selection, SelectionRange}; +use crate::{Rope, RopeSlice, Selection, SelectionRange}; +use anyhow::Error; + +use std::path::PathBuf; pub enum Mode { Normal, @@ -8,7 +11,9 @@ pub enum Mode { /// A state represents the current editor state of a single buffer. pub struct State { - pub doc: Buffer, + /// Path to file on disk. + pub path: Option, + pub doc: Rope, pub selection: Selection, pub mode: Mode, } @@ -28,15 +33,31 @@ pub enum Granularity { impl State { #[must_use] - pub fn new(doc: Buffer) -> Self { + pub fn new(doc: Rope) -> Self { Self { + path: None, doc, selection: Selection::single(0, 0), mode: Mode::Normal, } } - // TODO: buf/selection accessors + #[must_use] + pub fn load(path: PathBuf) -> Result { + use std::{env, fs::File, io::BufReader, path::PathBuf}; + let _current_dir = env::current_dir()?; + + let doc = Rope::from_reader(BufReader::new(File::open(path.clone())?))?; + + // TODO: create if not found + + let mut state = Self::new(doc); + state.path = Some(path); + + Ok(state) + } + + // TODO: doc/selection accessors // update/transact: // update(desc) => transaction ? transaction.doc() for applied doc @@ -68,7 +89,7 @@ impl State { granularity: Granularity, count: usize, ) -> usize { - let text = &self.doc.contents; + let text = &self.doc; match (dir, granularity) { // TODO: clamp movement to line, prevent moving onto \n at the end (Direction::Backward, Granularity::Character) => { @@ -125,16 +146,6 @@ impl State { Selection::new(ranges.collect(), sel.primary_index) // TODO: update selection in state via transaction } - - pub fn contents(&self) -> &Rope { - // used to access file contents for rendering to screen - &self.doc.contents - } - - pub fn contents_mut(&mut self) -> &mut Rope { - // used to access file contents for rendering to screen - &mut self.doc.contents - } } /// Coordinates are a 0-indexed line and column pair. -- cgit v1.2.3-70-g09d2