|
|
@ -1,6 +1,5 @@
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
|
chars::{char_is_line_ending, char_is_whitespace},
|
|
|
|
chars::{char_is_line_ending, char_is_whitespace},
|
|
|
|
find_first_non_whitespace_char,
|
|
|
|
|
|
|
|
syntax::{IndentQuery, LanguageConfiguration, Syntax},
|
|
|
|
syntax::{IndentQuery, LanguageConfiguration, Syntax},
|
|
|
|
tree_sitter::Node,
|
|
|
|
tree_sitter::Node,
|
|
|
|
Rope, RopeSlice,
|
|
|
|
Rope, RopeSlice,
|
|
|
@ -174,8 +173,7 @@ pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
|
|
|
|
|
|
|
|
|
|
|
|
/// To determine indentation of a newly inserted line, figure out the indentation at the last col
|
|
|
|
/// To determine indentation of a newly inserted line, figure out the indentation at the last col
|
|
|
|
/// of the previous line.
|
|
|
|
/// of the previous line.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize {
|
|
|
|
fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize {
|
|
|
|
|
|
|
|
let mut len = 0;
|
|
|
|
let mut len = 0;
|
|
|
|
for ch in line.chars() {
|
|
|
|
for ch in line.chars() {
|
|
|
|
match ch {
|
|
|
|
match ch {
|
|
|
@ -210,10 +208,15 @@ fn get_highest_syntax_node_at_bytepos(syntax: &Syntax, pos: usize) -> Option<Nod
|
|
|
|
Some(node)
|
|
|
|
Some(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn calculate_indentation(query: &IndentQuery, node: Option<Node>, newline: bool) -> usize {
|
|
|
|
/// Calculate the indentation at a given treesitter node.
|
|
|
|
// NOTE: can't use contains() on query because of comparing Vec<String> and &str
|
|
|
|
/// If newline is false, then any "indent" nodes on the line are ignored ("outdent" still applies).
|
|
|
|
// https://doc.rust-lang.org/std/vec/struct.Vec.html#method.contains
|
|
|
|
/// This is because the indentation is only increased starting at the second line of the node.
|
|
|
|
|
|
|
|
fn calculate_indentation(
|
|
|
|
|
|
|
|
query: &IndentQuery,
|
|
|
|
|
|
|
|
node: Option<Node>,
|
|
|
|
|
|
|
|
line: usize,
|
|
|
|
|
|
|
|
newline: bool,
|
|
|
|
|
|
|
|
) -> usize {
|
|
|
|
let mut increment: isize = 0;
|
|
|
|
let mut increment: isize = 0;
|
|
|
|
|
|
|
|
|
|
|
|
let mut node = match node {
|
|
|
|
let mut node = match node {
|
|
|
@ -221,70 +224,45 @@ fn calculate_indentation(query: &IndentQuery, node: Option<Node>, newline: bool)
|
|
|
|
None => return 0,
|
|
|
|
None => return 0,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let mut prev_start = node.start_position().row;
|
|
|
|
let mut current_line = line;
|
|
|
|
|
|
|
|
let mut consider_indent = newline;
|
|
|
|
// if we're calculating indentation for a brand new line then the current node will become the
|
|
|
|
let mut increment_from_line: isize = 0;
|
|
|
|
// parent node. We need to take it's indentation level into account too.
|
|
|
|
|
|
|
|
let node_kind = node.kind();
|
|
|
|
loop {
|
|
|
|
if newline && query.indent.contains(node_kind) {
|
|
|
|
let node_kind = node.kind();
|
|
|
|
increment += 1;
|
|
|
|
let start = node.start_position().row;
|
|
|
|
}
|
|
|
|
if current_line != start {
|
|
|
|
|
|
|
|
// Indent/dedent by at most one per line:
|
|
|
|
while let Some(parent) = node.parent() {
|
|
|
|
// .map(|a| { <-- ({ is two scopes
|
|
|
|
let parent_kind = parent.kind();
|
|
|
|
// let len = 1; <-- indents one level
|
|
|
|
let start = parent.start_position().row;
|
|
|
|
// }) <-- }) is two scopes
|
|
|
|
|
|
|
|
if consider_indent || increment_from_line < 0 {
|
|
|
|
// detect deeply nested indents in the same line
|
|
|
|
increment += increment_from_line.signum();
|
|
|
|
// .map(|a| { <-- ({ is two scopes
|
|
|
|
}
|
|
|
|
// let len = 1; <-- indents one level
|
|
|
|
increment_from_line = 0;
|
|
|
|
// }) <-- }) is two scopes
|
|
|
|
current_line = start;
|
|
|
|
let starts_same_line = start == prev_start;
|
|
|
|
consider_indent = true;
|
|
|
|
|
|
|
|
|
|
|
|
if query.outdent.contains(node.kind()) && !starts_same_line {
|
|
|
|
|
|
|
|
// we outdent by skipping the rules for the current level and jumping up
|
|
|
|
|
|
|
|
// node = parent;
|
|
|
|
|
|
|
|
increment -= 1;
|
|
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if query.indent.contains(parent_kind) // && not_first_or_last_sibling
|
|
|
|
if query.outdent.contains(node_kind) {
|
|
|
|
&& !starts_same_line
|
|
|
|
increment_from_line -= 1;
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// println!("is_scope {}", parent_kind);
|
|
|
|
if query.indent.contains(node_kind) {
|
|
|
|
prev_start = start;
|
|
|
|
increment_from_line += 1;
|
|
|
|
increment += 1
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if last_scope && increment > 0 && ...{ ignore }
|
|
|
|
if let Some(parent) = node.parent() {
|
|
|
|
|
|
|
|
node = parent;
|
|
|
|
node = parent;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if consider_indent || increment_from_line < 0 {
|
|
|
|
|
|
|
|
increment += increment_from_line.signum();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
increment.max(0) as usize
|
|
|
|
increment.max(0) as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
|
|
|
fn suggested_indent_for_line(
|
|
|
|
|
|
|
|
language_config: &LanguageConfiguration,
|
|
|
|
|
|
|
|
syntax: Option<&Syntax>,
|
|
|
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
|
|
|
line_num: usize,
|
|
|
|
|
|
|
|
_tab_width: usize,
|
|
|
|
|
|
|
|
) -> usize {
|
|
|
|
|
|
|
|
if let Some(start) = find_first_non_whitespace_char(text.line(line_num)) {
|
|
|
|
|
|
|
|
return suggested_indent_for_pos(
|
|
|
|
|
|
|
|
Some(language_config),
|
|
|
|
|
|
|
|
syntax,
|
|
|
|
|
|
|
|
text,
|
|
|
|
|
|
|
|
start + text.line_to_char(line_num),
|
|
|
|
|
|
|
|
false,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if the line is blank, indent should be zero
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: two usecases: if we are triggering this for a new, blank line:
|
|
|
|
// TODO: two usecases: if we are triggering this for a new, blank line:
|
|
|
|
// - it should return 0 when mass indenting stuff
|
|
|
|
// - it should return 0 when mass indenting stuff
|
|
|
|
// - it should look up the wrapper node and count it too when we press o/O
|
|
|
|
// - it should look up the wrapper node and count it too when we press o/O
|
|
|
@ -293,23 +271,20 @@ pub fn suggested_indent_for_pos(
|
|
|
|
syntax: Option<&Syntax>,
|
|
|
|
syntax: Option<&Syntax>,
|
|
|
|
text: RopeSlice,
|
|
|
|
text: RopeSlice,
|
|
|
|
pos: usize,
|
|
|
|
pos: usize,
|
|
|
|
|
|
|
|
line: usize,
|
|
|
|
new_line: bool,
|
|
|
|
new_line: bool,
|
|
|
|
) -> usize {
|
|
|
|
) -> Option<usize> {
|
|
|
|
if let (Some(query), Some(syntax)) = (
|
|
|
|
if let (Some(query), Some(syntax)) = (
|
|
|
|
language_config.and_then(|config| config.indent_query()),
|
|
|
|
language_config.and_then(|config| config.indent_query()),
|
|
|
|
syntax,
|
|
|
|
syntax,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
let byte_start = text.char_to_byte(pos);
|
|
|
|
let byte_start = text.char_to_byte(pos);
|
|
|
|
let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
|
|
|
|
let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
|
|
|
|
|
|
|
|
|
|
|
|
// let config = load indentation query config from Syntax(should contain language_config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: special case for comments
|
|
|
|
// TODO: special case for comments
|
|
|
|
// TODO: if preserve_leading_whitespace
|
|
|
|
// TODO: if preserve_leading_whitespace
|
|
|
|
calculate_indentation(query, node, new_line)
|
|
|
|
Some(calculate_indentation(query, node, line, new_line))
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// TODO: heuristics for non-tree sitter grammars
|
|
|
|
None
|
|
|
|
0
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -484,14 +459,23 @@ where
|
|
|
|
|
|
|
|
|
|
|
|
for i in 0..doc.len_lines() {
|
|
|
|
for i in 0..doc.len_lines() {
|
|
|
|
let line = text.line(i);
|
|
|
|
let line = text.line(i);
|
|
|
|
let indent = indent_level_for_line(line, tab_width);
|
|
|
|
if let Some(pos) = crate::find_first_non_whitespace_char(line) {
|
|
|
|
assert_eq!(
|
|
|
|
let indent = indent_level_for_line(line, tab_width);
|
|
|
|
suggested_indent_for_line(&language_config, Some(&syntax), text, i, tab_width),
|
|
|
|
assert_eq!(
|
|
|
|
indent,
|
|
|
|
suggested_indent_for_pos(
|
|
|
|
"line {}: {}",
|
|
|
|
Some(&language_config),
|
|
|
|
i,
|
|
|
|
Some(&syntax),
|
|
|
|
line
|
|
|
|
text,
|
|
|
|
);
|
|
|
|
text.line_to_char(i) + pos,
|
|
|
|
|
|
|
|
i,
|
|
|
|
|
|
|
|
false
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
Some(indent),
|
|
|
|
|
|
|
|
"line {}: \"{}\"",
|
|
|
|
|
|
|
|
i,
|
|
|
|
|
|
|
|
line
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|