2020-10-09 09:58:43 +02:00
|
|
|
use crate::{
|
|
|
|
syntax::Syntax,
|
|
|
|
tree_sitter::{Node, Tree},
|
|
|
|
Rope, RopeSlice, State,
|
|
|
|
};
|
|
|
|
|
2020-10-14 11:07:42 +02:00
|
|
|
/// To determine indentation of a newly inserted line, figure out the indentation at the last col
|
|
|
|
/// of the previous line.
|
|
|
|
|
2020-10-14 05:01:41 +02:00
|
|
|
pub const TAB_WIDTH: usize = 4;
|
2020-10-09 09:58:43 +02:00
|
|
|
|
|
|
|
fn indent_level_for_line(line: RopeSlice) -> usize {
|
|
|
|
let mut len = 0;
|
|
|
|
for ch in line.chars() {
|
|
|
|
match ch {
|
|
|
|
'\t' => len += TAB_WIDTH,
|
|
|
|
' ' => len += 1,
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len / TAB_WIDTH
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find the highest syntax node at position.
|
|
|
|
/// This is to identify the column where this node (e.g., an HTML closing tag) ends.
|
|
|
|
fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Node> {
|
|
|
|
let tree = syntax.root_layer.tree.as_ref().unwrap();
|
|
|
|
|
|
|
|
let mut node = match tree.root_node().named_descendant_for_byte_range(pos, pos) {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
while let Some(parent) = node.parent() {
|
|
|
|
if parent.start_byte() == node.start_byte() {
|
|
|
|
node = parent
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn walk(node: Option<Node>) -> usize {
|
|
|
|
let node = match node {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let parent = match node.parent() {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut increment = 0;
|
|
|
|
|
2020-10-14 11:07:42 +02:00
|
|
|
// 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
|
2020-10-09 09:58:43 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 11:07:42 +02:00
|
|
|
// if last_scope && increment > 0 && ...{ ignore }
|
|
|
|
|
2020-10-09 09:58:43 +02:00
|
|
|
walk(Some(parent)) + increment
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:31:37 +02:00
|
|
|
fn find_first_non_whitespace_char(state: &State, line_num: usize) -> usize {
|
2020-10-09 09:58:43 +02:00
|
|
|
let line = state.doc.line(line_num);
|
2020-10-14 11:07:42 +02:00
|
|
|
let mut start = state.doc.line_to_char(line_num);
|
2020-10-09 09:58:43 +02:00
|
|
|
|
|
|
|
// find first non-whitespace char
|
|
|
|
for ch in line.chars() {
|
|
|
|
// TODO: could use memchr with chunks?
|
|
|
|
if ch != ' ' && ch != '\t' {
|
|
|
|
break;
|
|
|
|
}
|
2020-10-14 11:07:42 +02:00
|
|
|
start += 1;
|
2020-10-09 09:58:43 +02:00
|
|
|
}
|
2020-10-15 16:31:37 +02:00
|
|
|
start
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
fn suggested_indent_for_line(syntax: Option<&Syntax>, state: &State, line_num: usize) -> usize {
|
2020-10-15 16:31:37 +02:00
|
|
|
let line = state.doc.line(line_num);
|
|
|
|
let current = indent_level_for_line(line);
|
|
|
|
|
|
|
|
let start = find_first_non_whitespace_char(state, line_num);
|
2020-10-09 09:58:43 +02:00
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
suggested_indent_for_pos(syntax, state, start)
|
2020-10-14 11:07:42 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
pub fn suggested_indent_for_pos(syntax: Option<&Syntax>, state: &State, pos: usize) -> usize {
|
|
|
|
if let Some(syntax) = syntax {
|
2020-10-14 11:07:42 +02:00
|
|
|
let byte_start = state.doc.char_to_byte(pos);
|
|
|
|
let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
|
2020-10-09 09:58:43 +02:00
|
|
|
|
2020-10-14 11:07:42 +02:00
|
|
|
let indentation = walk(node);
|
2020-10-09 09:58:43 +02:00
|
|
|
// special case for comments
|
|
|
|
|
|
|
|
// if preserve_leading_whitespace
|
|
|
|
|
2020-10-14 11:07:42 +02:00
|
|
|
indentation
|
2020-10-09 09:58:43 +02:00
|
|
|
} else {
|
2020-10-15 16:31:37 +02:00
|
|
|
// TODO: heuristics for non-tree sitter grammars
|
2020-10-09 09:58:43 +02:00
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-10-14 11:07:42 +02:00
|
|
|
fn test_indent_level() {
|
2020-10-09 09:58:43 +02:00
|
|
|
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
|
|
|
|
assert_eq!(indent_level_for_line(line.slice(..)), 3);
|
|
|
|
// mixed indentation
|
|
|
|
let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
|
|
|
|
assert_eq!(indent_level_for_line(line.slice(..)), 3);
|
|
|
|
}
|
2020-10-14 11:07:42 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_suggested_indent_for_line() {
|
|
|
|
let doc = Rope::from(
|
|
|
|
"mod test {
|
|
|
|
fn hello_world() {
|
|
|
|
1 + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
2020-10-22 07:35:07 +02:00
|
|
|
let state = State::new(doc);
|
|
|
|
// TODO: set_language
|
|
|
|
let language_config = crate::syntax::LOADER
|
|
|
|
.language_config_for_scope("source.rust")
|
|
|
|
.unwrap();
|
|
|
|
let highlight_config = language_config.highlight_config(&[]).unwrap().unwrap();
|
|
|
|
let syntax = Syntax::new(&state.doc, highlight_config.clone());
|
|
|
|
|
|
|
|
assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 0), 0); // mod
|
|
|
|
assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 1), 1); // fn
|
|
|
|
assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 2), 2); // 1 + 1
|
|
|
|
assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 4), 1); // }
|
|
|
|
assert_eq!(suggested_indent_for_line(Some(&syntax), &state, 5), 0); // }
|
2020-10-14 11:07:42 +02:00
|
|
|
}
|
2020-10-09 09:58:43 +02:00
|
|
|
}
|