From 8ba1e15d296fd3350332d8fa372c4c7703024a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Thu, 18 Mar 2021 14:48:42 +0900 Subject: [PATCH] Expose doc.syntax() via accessor. --- helix-term/src/commands.rs | 18 +++++------------- helix-term/src/ui/editor.rs | 2 +- helix-term/src/ui/prompt.rs | 8 ++++---- helix-view/src/document.rs | 12 ++++++++---- helix-view/src/editor.rs | 2 +- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 9179a5ce..5b742e43 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -770,12 +770,8 @@ pub fn open_below(cx: &mut Context) { let changes: Vec = positions .map(|index| { // TODO: share logic with insert_newline for indentation - let indent_level = helix_core::indent::suggested_indent_for_pos( - doc.syntax.as_ref(), - text, - index, - true, - ); + let indent_level = + helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true); let indent = " ".repeat(TAB_WIDTH).repeat(indent_level); let mut text = String::with_capacity(1 + indent.len()); text.push('\n'); @@ -970,12 +966,8 @@ pub mod insert { let doc = cx.doc(); let text = doc.text().slice(..); let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| { - let indent_level = helix_core::indent::suggested_indent_for_pos( - doc.syntax.as_ref(), - text, - range.head, - true, - ); + let indent_level = + helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true); let indent = " ".repeat(TAB_WIDTH).repeat(indent_level); let mut text = String::with_capacity(1 + indent.len()); text.push('\n'); @@ -1434,7 +1426,7 @@ pub fn toggle_comments(cx: &mut Context) { pub fn expand_selection(cx: &mut Context) { let doc = cx.doc(); - if let Some(syntax) = &doc.syntax { + if let Some(syntax) = doc.syntax() { let text = doc.text().slice(..); let selection = object::expand_selection(syntax, text, doc.selection()); doc.set_selection(selection); diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 8ba6d901..5e8ef05e 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -90,7 +90,7 @@ impl EditorView { // TODO: range doesn't actually restrict source, just highlight range // TODO: cache highlight results // TODO: only recalculate when state.doc is actually modified - let highlights: Vec<_> = match &view.doc.syntax { + let highlights: Vec<_> = match view.doc.syntax() { Some(syntax) => { syntax .highlight_iter(text.slice(..), Some(range), None, |_| None) diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 700bc8a0..19885c02 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -7,11 +7,11 @@ use std::borrow::Cow; use std::string::String; pub struct Prompt { - pub prompt: String, + prompt: String, pub line: String, - pub cursor: usize, - pub completion: Vec>, - pub completion_selection_index: Option, + cursor: usize, + completion: Vec>, + completion_selection_index: Option, completion_fn: Box Vec>>, callback_fn: Box, } diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 6ccbfbec..76a3b8e9 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -18,15 +18,13 @@ pub enum Mode { pub struct Document { pub state: State, // rope + selection - /// File path on disk. path: Option, /// Current editing mode. pub mode: Mode, pub restore_cursor: bool, - /// Tree-sitter AST tree - pub syntax: Option, + syntax: Option, // /// Corresponding language scope name. Usually `source.`. pub(crate) language: Option>, @@ -36,7 +34,6 @@ pub struct Document { old_state: Option, /// Undo tree. history: History, - /// Current document version, incremented at each change. version: i32, // should be usize? pub diagnostics: Vec, @@ -284,6 +281,7 @@ impl Document { } #[inline] + /// Current document version, incremented at each change. pub fn version(&self) -> i32 { self.version } @@ -292,7 +290,13 @@ impl Document { self.language_server.as_deref() } + /// Tree-sitter AST tree + pub fn syntax(&self) -> Option<&Syntax> { + self.syntax.as_ref() + } + #[inline] + /// File path on disk. pub fn path(&self) -> Option<&PathBuf> { self.path.as_ref() } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 9b32b335..3de2bfb8 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -95,7 +95,7 @@ impl Editor { self.tree.remove(id) } - pub fn should_close(&mut self) -> bool { + pub fn should_close(&self) -> bool { self.tree.is_empty() }