summaryrefslogtreecommitdiff
path: root/helix-core/src/indent.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-core/src/indent.rs')
-rw-r--r--helix-core/src/indent.rs82
1 files changed, 70 insertions, 12 deletions
diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs
index 932eaa24..c834d8e2 100644
--- a/helix-core/src/indent.rs
+++ b/helix-core/src/indent.rs
@@ -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); // }
+ }
}