|
|
@ -6,7 +6,7 @@ use crate::{
|
|
|
|
chars::{char_is_line_ending, char_is_whitespace},
|
|
|
|
chars::{char_is_line_ending, char_is_whitespace},
|
|
|
|
find_first_non_whitespace_char,
|
|
|
|
find_first_non_whitespace_char,
|
|
|
|
graphemes::{grapheme_width, tab_width_at},
|
|
|
|
graphemes::{grapheme_width, tab_width_at},
|
|
|
|
syntax::{LanguageConfiguration, RopeProvider, Syntax},
|
|
|
|
syntax::{IndentationHeuristic, LanguageConfiguration, RopeProvider, Syntax},
|
|
|
|
tree_sitter::Node,
|
|
|
|
tree_sitter::Node,
|
|
|
|
Rope, RopeGraphemes, RopeSlice,
|
|
|
|
Rope, RopeGraphemes, RopeSlice,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -931,6 +931,7 @@ pub fn treesitter_indent_for_pos<'a>(
|
|
|
|
pub fn indent_for_newline(
|
|
|
|
pub fn indent_for_newline(
|
|
|
|
language_config: Option<&LanguageConfiguration>,
|
|
|
|
language_config: Option<&LanguageConfiguration>,
|
|
|
|
syntax: Option<&Syntax>,
|
|
|
|
syntax: Option<&Syntax>,
|
|
|
|
|
|
|
|
indent_heuristic: &IndentationHeuristic,
|
|
|
|
indent_style: &IndentStyle,
|
|
|
|
indent_style: &IndentStyle,
|
|
|
|
tab_width: usize,
|
|
|
|
tab_width: usize,
|
|
|
|
text: RopeSlice,
|
|
|
|
text: RopeSlice,
|
|
|
@ -939,7 +940,12 @@ pub fn indent_for_newline(
|
|
|
|
current_line: usize,
|
|
|
|
current_line: usize,
|
|
|
|
) -> String {
|
|
|
|
) -> String {
|
|
|
|
let indent_width = indent_style.indent_width(tab_width);
|
|
|
|
let indent_width = indent_style.indent_width(tab_width);
|
|
|
|
if let (Some(query), Some(syntax)) = (
|
|
|
|
if let (
|
|
|
|
|
|
|
|
IndentationHeuristic::TreeSitter | IndentationHeuristic::Hybrid,
|
|
|
|
|
|
|
|
Some(query),
|
|
|
|
|
|
|
|
Some(syntax),
|
|
|
|
|
|
|
|
) = (
|
|
|
|
|
|
|
|
indent_heuristic,
|
|
|
|
language_config.and_then(|config| config.indent_query()),
|
|
|
|
language_config.and_then(|config| config.indent_query()),
|
|
|
|
syntax,
|
|
|
|
syntax,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
@ -953,49 +959,51 @@ pub fn indent_for_newline(
|
|
|
|
line_before_end_pos,
|
|
|
|
line_before_end_pos,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
// We want to compute the indentation not only based on the
|
|
|
|
if let IndentationHeuristic::Hybrid = indent_heuristic {
|
|
|
|
// syntax tree but also on the actual indentation of a previous
|
|
|
|
// We want to compute the indentation not only based on the
|
|
|
|
// line. This makes indentation computation more resilient to
|
|
|
|
// syntax tree but also on the actual indentation of a previous
|
|
|
|
// incomplete queries, incomplete source code & differing indentation
|
|
|
|
// line. This makes indentation computation more resilient to
|
|
|
|
// styles for the same language.
|
|
|
|
// incomplete queries, incomplete source code & differing indentation
|
|
|
|
// However, using the indent of a previous line as a baseline may not
|
|
|
|
// styles for the same language.
|
|
|
|
// make sense, e.g. if it has a different alignment than the new line.
|
|
|
|
// However, using the indent of a previous line as a baseline may not
|
|
|
|
// In order to prevent edge cases with long running times, we only try
|
|
|
|
// make sense, e.g. if it has a different alignment than the new line.
|
|
|
|
// a constant number of (non-empty) lines.
|
|
|
|
// In order to prevent edge cases with long running times, we only try
|
|
|
|
const MAX_ATTEMPTS: usize = 2;
|
|
|
|
// a constant number of (non-empty) lines.
|
|
|
|
let mut num_attempts = 0;
|
|
|
|
const MAX_ATTEMPTS: usize = 2;
|
|
|
|
for line_idx in (0..=line_before).rev() {
|
|
|
|
let mut num_attempts = 0;
|
|
|
|
let line = text.line(line_idx);
|
|
|
|
for line_idx in (0..=line_before).rev() {
|
|
|
|
let first_non_whitespace_char = match find_first_non_whitespace_char(line) {
|
|
|
|
let line = text.line(line_idx);
|
|
|
|
Some(i) => i,
|
|
|
|
let first_non_whitespace_char = match find_first_non_whitespace_char(line) {
|
|
|
|
None => {
|
|
|
|
Some(i) => i,
|
|
|
|
continue;
|
|
|
|
None => {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(indent) = (|| {
|
|
|
|
|
|
|
|
let computed_indent = treesitter_indent_for_pos(
|
|
|
|
|
|
|
|
query,
|
|
|
|
|
|
|
|
syntax,
|
|
|
|
|
|
|
|
tab_width,
|
|
|
|
|
|
|
|
indent_width,
|
|
|
|
|
|
|
|
text,
|
|
|
|
|
|
|
|
line_idx,
|
|
|
|
|
|
|
|
text.line_to_char(line_idx) + first_non_whitespace_char,
|
|
|
|
|
|
|
|
false,
|
|
|
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let leading_whitespace = line.slice(0..first_non_whitespace_char);
|
|
|
|
|
|
|
|
indent.relative_indent(
|
|
|
|
|
|
|
|
&computed_indent,
|
|
|
|
|
|
|
|
leading_whitespace,
|
|
|
|
|
|
|
|
indent_style,
|
|
|
|
|
|
|
|
tab_width,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
})() {
|
|
|
|
|
|
|
|
return indent;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
num_attempts += 1;
|
|
|
|
|
|
|
|
if num_attempts == MAX_ATTEMPTS {
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(indent) = (|| {
|
|
|
|
|
|
|
|
let computed_indent = treesitter_indent_for_pos(
|
|
|
|
|
|
|
|
query,
|
|
|
|
|
|
|
|
syntax,
|
|
|
|
|
|
|
|
tab_width,
|
|
|
|
|
|
|
|
indent_width,
|
|
|
|
|
|
|
|
text,
|
|
|
|
|
|
|
|
line_idx,
|
|
|
|
|
|
|
|
text.line_to_char(line_idx) + first_non_whitespace_char,
|
|
|
|
|
|
|
|
false,
|
|
|
|
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let leading_whitespace = line.slice(0..first_non_whitespace_char);
|
|
|
|
|
|
|
|
indent.relative_indent(
|
|
|
|
|
|
|
|
&computed_indent,
|
|
|
|
|
|
|
|
leading_whitespace,
|
|
|
|
|
|
|
|
indent_style,
|
|
|
|
|
|
|
|
tab_width,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
})() {
|
|
|
|
|
|
|
|
return indent;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
num_attempts += 1;
|
|
|
|
|
|
|
|
if num_attempts == MAX_ATTEMPTS {
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return indent.to_string(indent_style, tab_width);
|
|
|
|
return indent.to_string(indent_style, tab_width);
|
|
|
|