|
|
@ -4,6 +4,9 @@ use crate::{
|
|
|
|
Rope, RopeSlice, State,
|
|
|
|
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;
|
|
|
|
pub const TAB_WIDTH: usize = 4;
|
|
|
|
|
|
|
|
|
|
|
|
fn indent_level_for_line(line: RopeSlice) -> usize {
|
|
|
|
fn indent_level_for_line(line: RopeSlice) -> usize {
|
|
|
@ -53,22 +56,51 @@ fn walk(node: Option<Node>) -> usize {
|
|
|
|
|
|
|
|
|
|
|
|
let mut increment = 0;
|
|
|
|
let mut increment = 0;
|
|
|
|
|
|
|
|
|
|
|
|
let not_first_or_last_sibling = node.next_sibling().is_some() && node.prev_sibling().is_some();
|
|
|
|
// Hardcoded for rust for now
|
|
|
|
let is_scope = true;
|
|
|
|
let indent_scopes = &[
|
|
|
|
|
|
|
|
"block",
|
|
|
|
if not_first_or_last_sibling && is_scope {
|
|
|
|
"function_item",
|
|
|
|
increment += 1;
|
|
|
|
"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
|
|
|
|
walk(Some(parent)) + increment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for_line_at_col
|
|
|
|
|
|
|
|
fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
|
|
|
|
fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
|
|
|
|
let line = state.doc.line(line_num);
|
|
|
|
let line = state.doc.line(line_num);
|
|
|
|
let current = indent_level_for_line(line);
|
|
|
|
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
|
|
|
|
// find first non-whitespace char
|
|
|
|
for ch in line.chars() {
|
|
|
|
for ch in line.chars() {
|
|
|
@ -76,18 +108,23 @@ fn suggested_indent_for_line(state: &State, line_num: usize) -> usize {
|
|
|
|
if ch != ' ' && ch != '\t' {
|
|
|
|
if ch != ' ' && ch != '\t' {
|
|
|
|
break;
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
// special case for comments
|
|
|
|
|
|
|
|
|
|
|
|
// if preserve_leading_whitespace
|
|
|
|
// if preserve_leading_whitespace
|
|
|
|
|
|
|
|
|
|
|
|
unimplemented!()
|
|
|
|
indentation
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// TODO: case for non-tree sitter grammars
|
|
|
|
// TODO: case for non-tree sitter grammars
|
|
|
|
0
|
|
|
|
0
|
|
|
@ -99,7 +136,7 @@ mod test {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn indent_level() {
|
|
|
|
fn test_indent_level() {
|
|
|
|
let line = Rope::from(" fn new"); // 8 spaces
|
|
|
|
let line = Rope::from(" fn new"); // 8 spaces
|
|
|
|
assert_eq!(indent_level_for_line(line.slice(..)), 2);
|
|
|
|
assert_eq!(indent_level_for_line(line.slice(..)), 2);
|
|
|
|
let line = Rope::from("\t\t\tfn new"); // 3 tabs
|
|
|
|
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
|
|
|
|
let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
|
|
|
|
assert_eq!(indent_level_for_line(line.slice(..)), 3);
|
|
|
|
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); // }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|