aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/editor.rs
diff options
context:
space:
mode:
authorBlaž Hrastnik2020-09-17 05:57:49 +0000
committerBlaž Hrastnik2020-09-17 07:32:40 +0000
commit088f8a82af1b90e422c495cde92b537dedb1e419 (patch)
treec0607eb3f08e4cc782aa4f627046f439820425b5 /helix-term/src/editor.rs
parent31999d6528b247e223774d1a4edec7966dda3d9e (diff)
Incremental parsing: rough draft.
Diffstat (limited to 'helix-term/src/editor.rs')
-rw-r--r--helix-term/src/editor.rs55
1 files changed, 17 insertions, 38 deletions
diff --git a/helix-term/src/editor.rs b/helix-term/src/editor.rs
index 080c05aa..b3788260 100644
--- a/helix-term/src/editor.rs
+++ b/helix-term/src/editor.rs
@@ -38,8 +38,6 @@ pub struct Editor {
size: (u16, u16),
surface: Surface,
theme: Theme,
- highlighter: Highlighter,
- highlight_config: HighlightConfiguration,
}
impl Editor {
@@ -49,33 +47,8 @@ impl Editor {
let mut terminal = Terminal::new(backend)?;
let size = terminal::size().unwrap();
let area = Rect::new(0, 0, size.0, size.1);
-
let theme = Theme::default();
- // let mut parser = tree_sitter::Parser::new();
- // parser.set_language(language).unwrap();
- // let tree = parser.parse(source_code, None).unwrap();
-
- let language = helix_syntax::get_language(&helix_syntax::LANG::Rust);
-
- let highlighter = Highlighter::new();
-
- let mut highlight_config = HighlightConfiguration::new(
- language,
- &std::fs::read_to_string(
- "../helix-syntax/languages/tree-sitter-rust/queries/highlights.scm",
- )
- .unwrap(),
- &std::fs::read_to_string(
- "../helix-syntax/languages/tree-sitter-rust/queries/injections.scm",
- )
- .unwrap(),
- "", // locals.scm
- )
- .unwrap();
-
- highlight_config.configure(theme.scopes());
-
let mut editor = Editor {
terminal,
state: None,
@@ -84,8 +57,6 @@ impl Editor {
surface: Surface::empty(area),
theme,
// TODO; move to state
- highlighter,
- highlight_config,
};
if let Some(file) = args.files.pop() {
@@ -96,12 +67,19 @@ impl Editor {
}
pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
- self.state = Some(State::load(path)?);
+ let mut state = State::load(path)?;
+ state
+ .syntax
+ .as_mut()
+ .unwrap()
+ .configure(self.theme.scopes());
+ self.state = Some(state);
Ok(())
}
fn render(&mut self) {
- match &self.state {
+ // TODO: ideally not mut but highlights require it because of cursor cache
+ match &mut self.state {
Some(state) => {
let area = Rect::new(0, 0, self.size.0, self.size.1);
let mut surface = Surface::empty(area);
@@ -112,12 +90,13 @@ impl Editor {
// TODO: cache highlight results
// TODO: only recalculate when state.doc is actually modified
- let highlights = self
- .highlighter
- .highlight(&self.highlight_config, source_code.as_bytes(), None, |_| {
- None
- })
- .unwrap();
+ let highlights: Vec<_> = state
+ .syntax
+ .as_mut()
+ .unwrap()
+ .highlight_iter(source_code.as_bytes(), None, |_| None)
+ .unwrap()
+ .collect(); // TODO: we collect here to avoid double borrow, fix later
let mut spans = Vec::new();
@@ -235,7 +214,7 @@ impl Editor {
let coords = coords_at_pos(&state.doc().slice(..), pos);
execute!(
stdout,
- cursor::MoveTo((coords.1 + 2) as u16, coords.0 as u16)
+ cursor::MoveTo((coords.col + 2) as u16, coords.row as u16)
);
}
None => (),