Make state fields read-only from outside the crate.

pull/1/head
Blaž Hrastnik 4 years ago
parent 96db02742e
commit 31999d6528

@ -4,6 +4,7 @@ use anyhow::Error;
use std::path::PathBuf;
#[derive(Copy, Clone)]
pub enum Mode {
Normal,
Insert,
@ -12,10 +13,10 @@ pub enum Mode {
/// A state represents the current editor state of a single buffer.
pub struct State {
/// Path to file on disk.
pub path: Option<PathBuf>,
pub doc: Rope,
pub selection: Selection,
pub mode: Mode,
pub(crate) path: Option<PathBuf>,
pub(crate) doc: Rope,
pub(crate) selection: Selection,
pub(crate) mode: Mode,
}
#[derive(Copy, Clone, PartialEq, Eq)]
@ -58,6 +59,29 @@ impl State {
// TODO: doc/selection accessors
// TODO: be able to take either Rope or RopeSlice
#[inline]
pub fn doc(&self) -> &Rope {
&self.doc
}
#[inline]
pub fn selection(&self) -> &Selection {
&self.selection
}
#[inline]
pub fn mode(&self) -> Mode {
self.mode
}
// pub fn doc<R>(&self, range: R) -> RopeSlice
// where
// R: std::ops::RangeBounds<usize>,
// {
// self.doc.slice(range)
// }
// update/transact:
// update(desc) => transaction ? transaction.doc() for applied doc
// transaction.apply(doc)

@ -108,7 +108,7 @@ impl Editor {
let mut stdout = stdout();
// TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
let source_code = state.doc.to_string();
let source_code = state.doc().to_string();
// TODO: cache highlight results
// TODO: only recalculate when state.doc is actually modified
@ -137,10 +137,10 @@ impl Editor {
HighlightEvent::Source { start, end } => {
// TODO: filter out spans out of viewport for now..
let start = state.doc.byte_to_char(start);
let end = state.doc.byte_to_char(end);
let start = state.doc().byte_to_char(start);
let end = state.doc().byte_to_char(end);
let text = state.doc.slice(start..end);
let text = state.doc().slice(start..end);
use helix_core::graphemes::{grapheme_width, RopeGraphemes};
@ -225,14 +225,14 @@ impl Editor {
self.surface = surface;
// set cursor shape
match state.mode {
match state.mode() {
Mode::Insert => write!(stdout, "\x1B[6 q"),
Mode::Normal => write!(stdout, "\x1B[2 q"),
};
// render the cursor
let pos = state.selection.cursor();
let coords = coords_at_pos(&state.doc.slice(..), pos);
let pos = state.selection().cursor();
let coords = coords_at_pos(&state.doc().slice(..), pos);
execute!(
stdout,
cursor::MoveTo((coords.1 + 2) as u16, coords.0 as u16)
@ -261,7 +261,7 @@ impl Editor {
}
Some(Ok(Event::Key(event))) => {
if let Some(state) = &mut self.state {
match state.mode {
match state.mode() {
Mode::Insert => {
match event {
KeyEvent {

Loading…
Cancel
Save