Expose doc.syntax() via accessor.

pull/11/head
Blaž Hrastnik 3 years ago
parent 2b64f49f2c
commit 8ba1e15d29

@ -770,12 +770,8 @@ pub fn open_below(cx: &mut Context) {
let changes: Vec<Change> = positions let changes: Vec<Change> = positions
.map(|index| { .map(|index| {
// TODO: share logic with insert_newline for indentation // TODO: share logic with insert_newline for indentation
let indent_level = helix_core::indent::suggested_indent_for_pos( let indent_level =
doc.syntax.as_ref(), helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
text,
index,
true,
);
let indent = " ".repeat(TAB_WIDTH).repeat(indent_level); let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len()); let mut text = String::with_capacity(1 + indent.len());
text.push('\n'); text.push('\n');
@ -970,12 +966,8 @@ pub mod insert {
let doc = cx.doc(); let doc = cx.doc();
let text = doc.text().slice(..); let text = doc.text().slice(..);
let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| { let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
let indent_level = helix_core::indent::suggested_indent_for_pos( let indent_level =
doc.syntax.as_ref(), helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
text,
range.head,
true,
);
let indent = " ".repeat(TAB_WIDTH).repeat(indent_level); let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len()); let mut text = String::with_capacity(1 + indent.len());
text.push('\n'); text.push('\n');
@ -1434,7 +1426,7 @@ pub fn toggle_comments(cx: &mut Context) {
pub fn expand_selection(cx: &mut Context) { pub fn expand_selection(cx: &mut Context) {
let doc = cx.doc(); let doc = cx.doc();
if let Some(syntax) = &doc.syntax { if let Some(syntax) = doc.syntax() {
let text = doc.text().slice(..); let text = doc.text().slice(..);
let selection = object::expand_selection(syntax, text, doc.selection()); let selection = object::expand_selection(syntax, text, doc.selection());
doc.set_selection(selection); doc.set_selection(selection);

@ -90,7 +90,7 @@ impl EditorView {
// TODO: range doesn't actually restrict source, just highlight range // TODO: range doesn't actually restrict source, just highlight range
// TODO: cache highlight results // TODO: cache highlight results
// TODO: only recalculate when state.doc is actually modified // 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) => { Some(syntax) => {
syntax syntax
.highlight_iter(text.slice(..), Some(range), None, |_| None) .highlight_iter(text.slice(..), Some(range), None, |_| None)

@ -7,11 +7,11 @@ use std::borrow::Cow;
use std::string::String; use std::string::String;
pub struct Prompt { pub struct Prompt {
pub prompt: String, prompt: String,
pub line: String, pub line: String,
pub cursor: usize, cursor: usize,
pub completion: Vec<Cow<'static, str>>, completion: Vec<Cow<'static, str>>,
pub completion_selection_index: Option<usize>, completion_selection_index: Option<usize>,
completion_fn: Box<dyn FnMut(&str) -> Vec<Cow<'static, str>>>, completion_fn: Box<dyn FnMut(&str) -> Vec<Cow<'static, str>>>,
callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>, callback_fn: Box<dyn FnMut(&mut Editor, &str, PromptEvent)>,
} }

@ -18,15 +18,13 @@ pub enum Mode {
pub struct Document { pub struct Document {
pub state: State, // rope + selection pub state: State, // rope + selection
/// File path on disk.
path: Option<PathBuf>, path: Option<PathBuf>,
/// Current editing mode. /// Current editing mode.
pub mode: Mode, pub mode: Mode,
pub restore_cursor: bool, pub restore_cursor: bool,
/// Tree-sitter AST tree syntax: Option<Syntax>,
pub syntax: Option<Syntax>,
// /// Corresponding language scope name. Usually `source.<lang>`. // /// Corresponding language scope name. Usually `source.<lang>`.
pub(crate) language: Option<Arc<LanguageConfiguration>>, pub(crate) language: Option<Arc<LanguageConfiguration>>,
@ -36,7 +34,6 @@ pub struct Document {
old_state: Option<State>, old_state: Option<State>,
/// Undo tree. /// Undo tree.
history: History, history: History,
/// Current document version, incremented at each change.
version: i32, // should be usize? version: i32, // should be usize?
pub diagnostics: Vec<Diagnostic>, pub diagnostics: Vec<Diagnostic>,
@ -284,6 +281,7 @@ impl Document {
} }
#[inline] #[inline]
/// Current document version, incremented at each change.
pub fn version(&self) -> i32 { pub fn version(&self) -> i32 {
self.version self.version
} }
@ -292,7 +290,13 @@ impl Document {
self.language_server.as_deref() self.language_server.as_deref()
} }
/// Tree-sitter AST tree
pub fn syntax(&self) -> Option<&Syntax> {
self.syntax.as_ref()
}
#[inline] #[inline]
/// File path on disk.
pub fn path(&self) -> Option<&PathBuf> { pub fn path(&self) -> Option<&PathBuf> {
self.path.as_ref() self.path.as_ref()
} }

@ -95,7 +95,7 @@ impl Editor {
self.tree.remove(id) self.tree.remove(id)
} }
pub fn should_close(&mut self) -> bool { pub fn should_close(&self) -> bool {
self.tree.is_empty() self.tree.is_empty()
} }

Loading…
Cancel
Save