on master help sign in
Expose doc.syntax() via accessor.
8ba1e15Blaž Hrastnik
helix-term/src/commands.rs+5−13
@@ -770,12 +770,8 @@ pub fn open_below(cx: &mut Context) {
770 let changes: Vec<Change> = positions
770 let changes: Vec<Change> = positions
771 .map(|index| {
771 .map(|index| {
772 // TODO: share logic with insert_newline for indentation
772 // TODO: share logic with insert_newline for indentation
773+ let indent_level =
773− let indent_level = helix_core::indent::suggested_indent_for_pos(
774+ helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
774− doc.syntax.as_ref(),
775− text,
776− index,
777− true,
778− );
779 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
775 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
780 let mut text = String::with_capacity(1 + indent.len());
776 let mut text = String::with_capacity(1 + indent.len());
781 text.push('\n');
777 text.push('\n');
@@ -970,12 +966,8 @@ pub mod insert {
970 let doc = cx.doc();
966 let doc = cx.doc();
971 let text = doc.text().slice(..);
967 let text = doc.text().slice(..);
972 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
968 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
969+ let indent_level =
973− let indent_level = helix_core::indent::suggested_indent_for_pos(
970+ helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
974− doc.syntax.as_ref(),
975− text,
976− range.head,
977− true,
978− );
979 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
971 let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
980 let mut text = String::with_capacity(1 + indent.len());
972 let mut text = String::with_capacity(1 + indent.len());
981 text.push('\n');
973 text.push('\n');
@@ -1434,7 +1426,7 @@ pub fn toggle_comments(cx: &mut Context) {
1434pub fn expand_selection(cx: &mut Context) {
1426pub fn expand_selection(cx: &mut Context) {
1435 let doc = cx.doc();
1427 let doc = cx.doc();
1436
1428
1437− if let Some(syntax) = &doc.syntax {
1429+ if let Some(syntax) = doc.syntax() {
1438 let text = doc.text().slice(..);
1430 let text = doc.text().slice(..);
1439 let selection = object::expand_selection(syntax, text, doc.selection());
1431 let selection = object::expand_selection(syntax, text, doc.selection());
1440 doc.set_selection(selection);
1432 doc.set_selection(selection);
helix-term/src/ui/editor.rs+1−1
@@ -90,7 +90,7 @@ impl EditorView {
90 // TODO: range doesn't actually restrict source, just highlight range
90 // TODO: range doesn't actually restrict source, just highlight range
91 // TODO: cache highlight results
91 // TODO: cache highlight results
92 // TODO: only recalculate when state.doc is actually modified
92 // 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() {
94 Some(syntax) => {
94 Some(syntax) => {
95 syntax
95 syntax
96 .highlight_iter(text.slice(..), Some(range), None, |_| None)
96 .highlight_iter(text.slice(..), Some(range), None, |_| None)
helix-term/src/ui/prompt.rs+4−4
@@ -7,11 +7,11 @@ use std::borrow::Cow;
7use std::string::String;
7use std::string::String;
8
8
9pub struct Prompt {
9pub struct Prompt {
10− pub prompt: String,
10+ prompt: String,
11 pub line: String,
11 pub line: String,
12− pub cursor: usize,
12+ cursor: usize,
13− pub completion: Vec<Cow<'static, str>>,
13+ completion: Vec<Cow<'static, str>>,
14− pub completion_selection_index: Option<usize>,
14+ completion_selection_index: Option<usize>,
15 completion_fn: Box<dyn FnMut(&str) -> Vec<Cow<'static, str>>>,
15 completion_fn: Box<dyn FnMut(&str) -> Vec<Cow<'static, str>>>,
16 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
16 callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
17}
17}
helix-view/src/document.rs+8−4
@@ -18,15 +18,13 @@ pub enum Mode {
18
18
19pub struct Document {
19pub struct Document {
20 pub state: State, // rope + selection
20 pub state: State, // rope + selection
21− /// File path on disk.
22 path: Option<PathBuf>,
21 path: Option<PathBuf>,
23
22
24 /// Current editing mode.
23 /// Current editing mode.
25 pub mode: Mode,
24 pub mode: Mode,
26 pub restore_cursor: bool,
25 pub restore_cursor: bool,
27
26
28− /// Tree-sitter AST tree
29− pub syntax: Option<Syntax>,
27+ syntax: Option<Syntax>,
30 // /// Corresponding language scope name. Usually `source.<lang>`.
28 // /// Corresponding language scope name. Usually `source.<lang>`.
31 pub(crate) language: Option<Arc<LanguageConfiguration>>,
29 pub(crate) language: Option<Arc<LanguageConfiguration>>,
32
30
@@ -36,7 +34,6 @@ pub struct Document {
36 old_state: Option<State>,
34 old_state: Option<State>,
37 /// Undo tree.
35 /// Undo tree.
38 history: History,
36 history: History,
39− /// Current document version, incremented at each change.
40 version: i32, // should be usize?
37 version: i32, // should be usize?
41
38
42 pub diagnostics: Vec<Diagnostic>,
39 pub diagnostics: Vec<Diagnostic>,
@@ -284,6 +281,7 @@ impl Document {
284 }
281 }
285
282
286 #[inline]
283 #[inline]
284+ /// Current document version, incremented at each change.
287 pub fn version(&self) -> i32 {
285 pub fn version(&self) -> i32 {
288 self.version
286 self.version
289 }
287 }
@@ -292,7 +290,13 @@ impl Document {
292 self.language_server.as_deref()
290 self.language_server.as_deref()
293 }
291 }
294
292
293+ /// Tree-sitter AST tree
294+ pub fn syntax(&self) -> Option<&Syntax> {
295+ self.syntax.as_ref()
296+ }
297+
295 #[inline]
298 #[inline]
299+ /// File path on disk.
296 pub fn path(&self) -> Option<&PathBuf> {
300 pub fn path(&self) -> Option<&PathBuf> {
297 self.path.as_ref()
301 self.path.as_ref()
298 }
302 }
helix-view/src/editor.rs+1−1
@@ -95,7 +95,7 @@ impl Editor {
95 self.tree.remove(id)
95 self.tree.remove(id)
96 }
96 }
97
97
98− pub fn should_close(&mut self) -> bool {
98+ pub fn should_close(&self) -> bool {
99 self.tree.is_empty()
99 self.tree.is_empty()
100 }
100 }
101
101