on master help sign in
Make state fields read-only from outside the crate.
31999d6Blaž Hrastnik
helix-core/src/state.rs+28−4
@@ -4,6 +4,7 @@ use anyhow::Error;
4
4
5use std::path::PathBuf;
5use std::path::PathBuf;
6
6
7+#[derive(Copy, Clone)]
7pub enum Mode {
8pub enum Mode {
8 Normal,
9 Normal,
9 Insert,
10 Insert,
@@ -12,10 +13,10 @@ pub enum Mode {
12/// A state represents the current editor state of a single buffer.
13/// A state represents the current editor state of a single buffer.
13pub struct State {
14pub struct State {
14 /// Path to file on disk.
15 /// Path to file on disk.
15− pub path: Option<PathBuf>,
16+ pub(crate) path: Option<PathBuf>,
16− pub doc: Rope,
17+ pub(crate) doc: Rope,
17− pub selection: Selection,
18+ pub(crate) selection: Selection,
18− pub mode: Mode,
19+ pub(crate) mode: Mode,
19}
20}
20
21
21#[derive(Copy, Clone, PartialEq, Eq)]
22#[derive(Copy, Clone, PartialEq, Eq)]
@@ -58,6 +59,29 @@ impl State {
58
59
59 // TODO: doc/selection accessors
60 // TODO: doc/selection accessors
60
61
62+ // TODO: be able to take either Rope or RopeSlice
63+ #[inline]
64+ pub fn doc(&self) -> &Rope {
65+ &self.doc
66+ }
67+
68+ #[inline]
69+ pub fn selection(&self) -> &Selection {
70+ &self.selection
71+ }
72+
73+ #[inline]
74+ pub fn mode(&self) -> Mode {
75+ self.mode
76+ }
77+
78+ // pub fn doc<R>(&self, range: R) -> RopeSlice
79+ // where
80+ // R: std::ops::RangeBounds<usize>,
81+ // {
82+ // self.doc.slice(range)
83+ // }
84+
61 // update/transact:
85 // update/transact:
62 // update(desc) => transaction ? transaction.doc() for applied doc
86 // update(desc) => transaction ? transaction.doc() for applied doc
63 // transaction.apply(doc)
87 // transaction.apply(doc)
helix-term/src/editor.rs+8−8
@@ -108,7 +108,7 @@ impl Editor {
108 let mut stdout = stdout();
108 let mut stdout = stdout();
109
109
110 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
110 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
111− let source_code = state.doc.to_string();
111+ let source_code = state.doc().to_string();
112
112
113 // TODO: cache highlight results
113 // TODO: cache highlight results
114 // TODO: only recalculate when state.doc is actually modified
114 // TODO: only recalculate when state.doc is actually modified
@@ -137,10 +137,10 @@ impl Editor {
137 HighlightEvent::Source { start, end } => {
137 HighlightEvent::Source { start, end } => {
138 // TODO: filter out spans out of viewport for now..
138 // TODO: filter out spans out of viewport for now..
139
139
140− let start = state.doc.byte_to_char(start);
140+ let start = state.doc().byte_to_char(start);
141− let end = state.doc.byte_to_char(end);
141+ let end = state.doc().byte_to_char(end);
142
142
143− let text = state.doc.slice(start..end);
143+ let text = state.doc().slice(start..end);
144
144
145 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
145 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
146
146
@@ -225,14 +225,14 @@ impl Editor {
225 self.surface = surface;
225 self.surface = surface;
226
226
227 // set cursor shape
227 // set cursor shape
228− match state.mode {
228+ match state.mode() {
229 Mode::Insert => write!(stdout, "\x1B[6 q"),
229 Mode::Insert => write!(stdout, "\x1B[6 q"),
230 Mode::Normal => write!(stdout, "\x1B[2 q"),
230 Mode::Normal => write!(stdout, "\x1B[2 q"),
231 };
231 };
232
232
233 // render the cursor
233 // render the cursor
234− let pos = state.selection.cursor();
234+ let pos = state.selection().cursor();
235− let coords = coords_at_pos(&state.doc.slice(..), pos);
235+ let coords = coords_at_pos(&state.doc().slice(..), pos);
236 execute!(
236 execute!(
237 stdout,
237 stdout,
238 cursor::MoveTo((coords.1 + 2) as u16, coords.0 as u16)
238 cursor::MoveTo((coords.1 + 2) as u16, coords.0 as u16)
@@ -261,7 +261,7 @@ impl Editor {
261 }
261 }
262 Some(Ok(Event::Key(event))) => {
262 Some(Ok(Event::Key(event))) => {
263 if let Some(state) = &mut self.state {
263 if let Some(state) = &mut self.state {
264− match state.mode {
264+ match state.mode() {
265 Mode::Insert => {
265 Mode::Insert => {
266 match event {
266 match event {
267 KeyEvent {
267 KeyEvent {