on master help sign in
Expose doc.syntax() via accessor.
commit8ba1e15d296fd3350332d8fa372c4c7703024a67 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 18 Mar 2021, 14:48 (6 years ago) parent2b64f49
5 files changed+19−23
helix-term/src/commands.rs+5−13 helix-term/src/ui/editor.rs+1−1 helix-term/src/ui/prompt.rs+4−4 helix-view/src/document.rs+8−4 helix-view/src/editor.rs+1−1
helix-term/src/commands.rs+5−13
@@ -770,12 +770,8 @@ pub fn open_below(cx: &mut Context) {
770770 let changes: Vec<Change> = positions
771771 .map(|index| {
772772 // TODO: share logic with insert_newline for indentation
773− let indent_level = helix_core::indent::suggested_indent_for_pos(
774− doc.syntax.as_ref(),
775− text,
776− index,
777− true,
778− );
773+ let indent_level =
774+ helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
779775 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
780776 let mut text = String::with_capacity(1 + indent.len());
781777 text.push('\n');
@@ -970,12 +966,8 @@ pub mod insert {
970966 let doc = cx.doc();
971967 let text = doc.text().slice(..);
972968 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
973− let indent_level = helix_core::indent::suggested_indent_for_pos(
974− doc.syntax.as_ref(),
975− text,
976− range.head,
977− true,
978− );
969+ let indent_level =
970+ helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
979971 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
980972 let mut text = String::with_capacity(1 + indent.len());
981973 text.push('\n');
@@ -1434,7 +1426,7 @@ pub fn toggle_comments(cx: &mut Context) {
14341426pub fn expand_selection(cx: &mut Context) {
14351427 let doc = cx.doc();
14361428
1437− if let Some(syntax) = &doc.syntax {
1429+ if let Some(syntax) = doc.syntax() {
14381430 let text = doc.text().slice(..);
14391431 let selection = object::expand_selection(syntax, text, doc.selection());
14401432 doc.set_selection(selection);
helix-term/src/ui/editor.rs+1−1
@@ -90,7 +90,7 @@ impl EditorView {
9090 // TODO: range doesn't actually restrict source, just highlight range
9191 // TODO: cache highlight results
9292 // TODO: only recalculate when state.doc is actually modified
93− let highlights: Vec<_> = match &view.doc.syntax {
93+ let highlights: Vec<_> = match view.doc.syntax() {
9494 Some(syntax) => {
9595 syntax
9696 .highlight_iter(text.slice(..), Some(range), None, |_| None)
helix-term/src/ui/prompt.rs+4−4
@@ -7,11 +7,11 @@ use std::borrow::Cow;
77use std::string::String;
88
99pub struct Prompt {
10− pub prompt: String,
10+ prompt: String,
1111 pub line: String,
12− pub cursor: usize,
13− pub completion: Vec<Cow<'static, str>>,
14− pub completion_selection_index: Option<usize>,
12+ cursor: usize,
13+ completion: Vec<Cow<'static, str>>,
14+ completion_selection_index: Option<usize>,
1515 completion_fn: Box<dyn FnMut(&str) -> Vec<Cow<'static, str>>>,
1616 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
1717}
helix-view/src/document.rs+8−4
@@ -18,15 +18,13 @@ pub enum Mode {
1818
1919pub struct Document {
2020 pub state: State, // rope + selection
21− /// File path on disk.
2221 path: Option<PathBuf>,
2322
2423 /// Current editing mode.
2524 pub mode: Mode,
2625 pub restore_cursor: bool,
2726
28− /// Tree-sitter AST tree
29− pub syntax: Option<Syntax>,
27+ syntax: Option<Syntax>,
3028 // /// Corresponding language scope name. Usually `source.<lang>`.
3129 pub(crate) language: Option<Arc<LanguageConfiguration>>,
3230
@@ -36,7 +34,6 @@ pub struct Document {
3634 old_state: Option<State>,
3735 /// Undo tree.
3836 history: History,
39− /// Current document version, incremented at each change.
4037 version: i32, // should be usize?
4138
4239 pub diagnostics: Vec<Diagnostic>,
@@ -284,6 +281,7 @@ impl Document {
284281 }
285282
286283 #[inline]
284+ /// Current document version, incremented at each change.
287285 pub fn version(&self) -> i32 {
288286 self.version
289287 }
@@ -292,7 +290,13 @@ impl Document {
292290 self.language_server.as_deref()
293291 }
294292
293+ /// Tree-sitter AST tree
294+ pub fn syntax(&self) -> Option<&Syntax> {
295+ self.syntax.as_ref()
296+ }
297+
295298 #[inline]
299+ /// File path on disk.
296300 pub fn path(&self) -> Option<&PathBuf> {
297301 self.path.as_ref()
298302 }
helix-view/src/editor.rs+1−1
@@ -95,7 +95,7 @@ impl Editor {
9595 self.tree.remove(id)
9696 }
9797
98− pub fn should_close(&mut self) -> bool {
98+ pub fn should_close(&self) -> bool {
9999 self.tree.is_empty()
100100 }
101101