Auto-indent on enter based on tree-sitter scopes.

pull/4/head
Blaž Hrastnik 4 years ago
parent 6ae3c26def
commit 6e658aae1c

@ -4,6 +4,9 @@ use crate::{
Rope, RopeSlice, State,
};
/// To determine indentation of a newly inserted line, figure out the indentation at the last col
/// of the previous line.
pub const TAB_WIDTH: usize = 4;
fn indent_level_for_line(line: RopeSlice) -> usize {
@ -53,22 +56,51 @@ fn walk(node: Option<Node>) -> usize {
let mut increment = 0;
let not_first_or_last_sibling = node.next_sibling().is_some() && node.prev_sibling().is_some();
let is_scope = true;
if not_first_or_last_sibling && is_scope {
increment += 1;
// Hardcoded for rust for now
let indent_scopes = &[
"block",
"function_item",
"closure_expression",
"while_expression",
"for_expression",
"loop_expression",
"if_expression",
"if_let_expression",
"binary_expression",
"match_expression",
"match_arm",
//
"struct_item",
"enum_item",
"impl_item",
//
"mod_item",
];
// let indent_except_first_scopes = &[];
let not_first_sibling = node.next_sibling().is_some();
let not_last_sibling = node.prev_sibling().is_some();
let not_first_or_last_sibling = not_first_sibling && not_last_sibling;
let parent_kind = parent.kind();
let is_scope = indent_scopes.iter().any(|scope| scope == &parent_kind);
// && not_first_or_last_sibling
if is_scope {
increment += 1
}
// if last_scope && increment > 0 && ...{ ignore }
walk(Some(parent)) + increment
}
// for_line_at_col
fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
let line = state.doc.line(line_num);
let current = indent_level_for_line(line);
let mut byte_start = state.doc.line_to_byte(line_num);
let mut start = state.doc.line_to_char(line_num);
// find first non-whitespace char
for ch in line.chars() {
@ -76,18 +108,23 @@ fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
if ch != ' ' && ch != '\t' {
break;
}
byte_start += 1;
start += 1;
}
suggested_indent_for_pos(state, start)
}
pub fn suggested_indent_for_pos(state: &State, pos: usize) -> usize {
if let Some(syntax) = &state.syntax {
let node = get_highest_syntax_node_at_bytepos(state.syntax.as_ref().unwrap(), byte_start);
let byte_start = state.doc.char_to_byte(pos);
let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
// let indentation = walk()
let indentation = walk(node);
// special case for comments
// if preserve_leading_whitespace
unimplemented!()
indentation
} else {
// TODO: case for non-tree sitter grammars
0
@ -99,7 +136,7 @@ mod test {
use super::*;
#[test]
fn indent_level() {
fn test_indent_level() {
let line = Rope::from(" fn new"); // 8 spaces
assert_eq!(indent_level_for_line(line.slice(..)), 2);
let line = Rope::from("\t\t\tfn new"); // 3 tabs
@ -108,4 +145,25 @@ mod test {
let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
assert_eq!(indent_level_for_line(line.slice(..)), 3);
}
#[test]
fn test_suggested_indent_for_line() {
let doc = Rope::from(
"mod test {
fn hello_world() {
1 + 1
}
}
",
);
let mut state = State::new(doc);
state.set_language("source.rust", &[]);
assert_eq!(suggested_indent_for_line(&state, 0), 0); // mod
assert_eq!(suggested_indent_for_line(&state, 1), 1); // fn
assert_eq!(suggested_indent_for_line(&state, 2), 2); // 1 + 1
assert_eq!(suggested_indent_for_line(&state, 4), 1); // }
assert_eq!(suggested_indent_for_line(&state, 5), 0); // }
}
}

@ -86,6 +86,17 @@ impl State {
Ok(state)
}
pub fn set_language(&mut self, scope: &str, scopes: &[String]) {
if let Some(language_config) = LOADER.language_config_for_scope(scope) {
let highlight_config = language_config.highlight_config(scopes).unwrap().unwrap();
// TODO: config.configure(scopes) is now delayed, is that ok?
let syntax = Syntax::new(&self.doc, highlight_config.clone());
self.syntax = Some(syntax);
};
}
// TODO: doc/selection accessors
// TODO: be able to take either Rope or RopeSlice

@ -135,6 +135,13 @@ impl Loader {
// TODO: content_regex handling conflict resolution
}
pub fn language_config_for_scope(&self, scope: &str) -> Option<Arc<LanguageConfiguration>> {
self.language_configs
.iter()
.find(|config| config.scope == scope)
.cloned()
}
}
//

@ -435,7 +435,16 @@ pub mod insert {
}
pub fn insert_newline(view: &mut View, _count: usize) {
insert_char(view, '\n');
let transaction = Transaction::change_by_selection(&view.state, |range| {
let indent_level =
helix_core::indent::suggested_indent_for_pos(&view.state, range.head);
let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
let mut text = String::with_capacity(1 + indent.len());
text.push('\n');
text.push_str(&indent);
(range.head, range.head, Some(text.into()))
});
transaction.apply(&mut view.state);
}
// TODO: handle indent-aware delete
@ -605,3 +614,8 @@ pub fn unindent(view: &mut View, _count: usize) {
transaction.apply(&mut view.state);
append_changes_to_history(view);
}
pub fn indent_selection(view: &mut View, _count: usize) {
// loop over each line and recompute proper indentation
unimplemented!()
}

Loading…
Cancel
Save