@@ -1,5 +1,8 @@
1use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
1use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
2−use crate::{Buffer, Rope, RopeSlice, Selection, SelectionRange};
2+use crate::{Rope, RopeSlice, Selection, SelectionRange};
5+use std::path::PathBuf;
4pub enum Mode {
7pub enum Mode {
@@ -8,7 +11,9 @@ pub enum Mode {
9/// A state represents the current editor state of a single buffer.
12/// A state represents the current editor state of a single buffer.
10pub struct State {
13pub struct State {
14+ /// Path to file on disk.
15+ pub path: Option<PathBuf>,
11− pub doc: Buffer,
16+ pub doc: Rope,
12 pub selection: Selection,
17 pub selection: Selection,
13 pub mode: Mode,
18 pub mode: Mode,
@@ -28,15 +33,31 @@ pub enum Granularity {
29impl State {
34impl State {
30 #[must_use]
35 #[must_use]
31− pub fn new(doc: Buffer) -> Self {
36+ pub fn new(doc: Rope) -> Self {
34 selection: Selection::single(0, 0),
40 selection: Selection::single(0, 0),
35 mode: Mode::Normal,
41 mode: Mode::Normal,
46+ pub fn load(path: PathBuf) -> Result<Self, Error> {
47+ use std::{env, fs::File, io::BufReader, path::PathBuf};
48+ let _current_dir = env::current_dir()?;
50+ let doc = Rope::from_reader(BufReader::new(File::open(path.clone())?))?;
52+ // TODO: create if not found
54+ let mut state = Self::new(doc);
55+ state.path = Some(path);
39− // TODO: buf/selection accessors
60+ // TODO: doc/selection accessors
41 // update/transact:
62 // update/transact:
42 // update(desc) => transaction ? transaction.doc() for applied doc
63 // update(desc) => transaction ? transaction.doc() for applied doc
@@ -68,7 +89,7 @@ impl State {
68 granularity: Granularity,
89 granularity: Granularity,
69 count: usize,
90 count: usize,
70 ) -> usize {
91 ) -> usize {
71− let text = &self.doc.contents;
92+ let text = &self.doc;
72 match (dir, granularity) {
93 match (dir, granularity) {
73 // TODO: clamp movement to line, prevent moving onto \n at the end
94 // TODO: clamp movement to line, prevent moving onto \n at the end
74 (Direction::Backward, Granularity::Character) => {
95 (Direction::Backward, Granularity::Character) => {
@@ -125,16 +146,6 @@ impl State {
125 Selection::new(ranges.collect(), sel.primary_index)
146 Selection::new(ranges.collect(), sel.primary_index)
126 // TODO: update selection in state via transaction
147 // TODO: update selection in state via transaction
129− pub fn contents(&self) -> &Rope {
130− // used to access file contents for rendering to screen
134− pub fn contents_mut(&mut self) -> &mut Rope {
135− // used to access file contents for rendering to screen
136− &mut self.doc.contents
140/// Coordinates are a 0-indexed line and column pair.
151/// Coordinates are a 0-indexed line and column pair.