on master help sign in
Make state fields read-only from outside the crate.
commit31999d6528b247e223774d1a4edec7966dda3d9e authorBlaž Hrastnik <blaz@mxxn.io> dateMon 14 Sep 2020, 19:50 (6 years ago) parent96db027
2 files changed+36−12
helix-core/src/state.rs+28−4 helix-term/src/editor.rs+8−8
helix-core/src/state.rs+28−4
@@ -4,6 +4,7 @@ use anyhow::Error;
44
55use std::path::PathBuf;
66
7+#[derive(Copy, Clone)]
78pub enum Mode {
89 Normal,
910 Insert,
@@ -12,10 +13,10 @@ pub enum Mode {
1213/// A state represents the current editor state of a single buffer.
1314pub struct State {
1415 /// Path to file on disk.
15− pub path: Option<PathBuf>,
16− pub doc: Rope,
17− pub selection: Selection,
18− pub mode: Mode,
16+ pub(crate) path: Option<PathBuf>,
17+ pub(crate) doc: Rope,
18+ pub(crate) selection: Selection,
19+ pub(crate) mode: Mode,
1920}
2021
2122#[derive(Copy, Clone, PartialEq, Eq)]
@@ -58,6 +59,29 @@ impl State {
5859
5960 // TODO: doc/selection accessors
6061
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+
6185 // update/transact:
6286 // update(desc) => transaction ? transaction.doc() for applied doc
6387 // transaction.apply(doc)
helix-term/src/editor.rs+8−8
@@ -108,7 +108,7 @@ impl Editor {
108108 let mut stdout = stdout();
109109
110110 // 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();
112112
113113 // TODO: cache highlight results
114114 // TODO: only recalculate when state.doc is actually modified
@@ -137,10 +137,10 @@ impl Editor {
137137 HighlightEvent::Source { start, end } => {
138138 // TODO: filter out spans out of viewport for now..
139139
140− let start = state.doc.byte_to_char(start);
141− let end = state.doc.byte_to_char(end);
140+ let start = state.doc().byte_to_char(start);
141+ let end = state.doc().byte_to_char(end);
142142
143− let text = state.doc.slice(start..end);
143+ let text = state.doc().slice(start..end);
144144
145145 use helix_core::graphemes::{grapheme_width, RopeGraphemes};
146146
@@ -225,14 +225,14 @@ impl Editor {
225225 self.surface = surface;
226226
227227 // set cursor shape
228− match state.mode {
228+ match state.mode() {
229229 Mode::Insert => write!(stdout, "\x1B[6 q"),
230230 Mode::Normal => write!(stdout, "\x1B[2 q"),
231231 };
232232
233233 // render the cursor
234− let pos = state.selection.cursor();
235− let coords = coords_at_pos(&state.doc.slice(..), pos);
234+ let pos = state.selection().cursor();
235+ let coords = coords_at_pos(&state.doc().slice(..), pos);
236236 execute!(
237237 stdout,
238238 cursor::MoveTo((coords.1 + 2) as u16, coords.0 as u16)
@@ -261,7 +261,7 @@ impl Editor {
261261 }
262262 Some(Ok(Event::Key(event))) => {
263263 if let Some(state) = &mut self.state {
264− match state.mode {
264+ match state.mode() {
265265 Mode::Insert => {
266266 match event {
267267 KeyEvent {