style: Only call extend_nodes when deepest_preceding is Some

pull/4649/head
Michael Davis 2 years ago committed by Blaž Hrastnik
parent a19eee6450
commit 260ae3a0f4

@ -461,60 +461,58 @@ fn query_indents(
/// so that the indent computation starts with the correct syntax node. /// so that the indent computation starts with the correct syntax node.
fn extend_nodes<'a>( fn extend_nodes<'a>(
node: &mut Node<'a>, node: &mut Node<'a>,
deepest_preceding: Option<Node<'a>>, mut deepest_preceding: Node<'a>,
extend_captures: &HashMap<usize, Vec<ExtendCapture>>, extend_captures: &HashMap<usize, Vec<ExtendCapture>>,
text: RopeSlice, text: RopeSlice,
line: usize, line: usize,
tab_width: usize, tab_width: usize,
) { ) {
if let Some(mut deepest_preceding) = deepest_preceding { let mut stop_extend = false;
let mut stop_extend = false;
while deepest_preceding != *node { while deepest_preceding != *node {
let mut extend_node = false; let mut extend_node = false;
// This will be set to true if this node is captured, regardless of whether // This will be set to true if this node is captured, regardless of whether
// it actually will be extended (e.g. because the cursor isn't indented // it actually will be extended (e.g. because the cursor isn't indented
// more than the node). // more than the node).
let mut node_captured = false; let mut node_captured = false;
if let Some(captures) = extend_captures.get(&deepest_preceding.id()) { if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
for capture in captures { for capture in captures {
match capture { match capture {
ExtendCapture::PreventOnce => { ExtendCapture::PreventOnce => {
stop_extend = true; stop_extend = true;
} }
ExtendCapture::Extend => { ExtendCapture::Extend => {
node_captured = true; node_captured = true;
// We extend the node if // We extend the node if
// - the cursor is on the same line as the end of the node OR // - the cursor is on the same line as the end of the node OR
// - the line that the cursor is on is more indented than the // - the line that the cursor is on is more indented than the
// first line of the node // first line of the node
if deepest_preceding.end_position().row == line { if deepest_preceding.end_position().row == line {
extend_node = true;
} else {
let cursor_indent = indent_level_for_line(text.line(line), tab_width);
let node_indent = indent_level_for_line(
text.line(deepest_preceding.start_position().row),
tab_width,
);
if cursor_indent > node_indent {
extend_node = true; extend_node = true;
} else {
let cursor_indent =
indent_level_for_line(text.line(line), tab_width);
let node_indent = indent_level_for_line(
text.line(deepest_preceding.start_position().row),
tab_width,
);
if cursor_indent > node_indent {
extend_node = true;
}
} }
} }
} }
} }
} }
// If we encountered some `StopExtend` capture before, we don't
// extend the node even if we otherwise would
if node_captured && stop_extend {
stop_extend = false;
} else if extend_node && !stop_extend {
*node = deepest_preceding;
break;
}
// This parent always exists since node is an ancestor of deepest_preceding
deepest_preceding = deepest_preceding.parent().unwrap();
} }
// If we encountered some `StopExtend` capture before, we don't
// extend the node even if we otherwise would
if node_captured && stop_extend {
stop_extend = false;
} else if extend_node && !stop_extend {
*node = deepest_preceding;
break;
}
// This parent always exists since node is an ancestor of deepest_preceding
deepest_preceding = deepest_preceding.parent().unwrap();
} }
} }
@ -612,14 +610,16 @@ pub fn treesitter_indent_for_pos(
let extend_captures = query_result.extend_captures; let extend_captures = query_result.extend_captures;
// Check for extend captures, potentially changing the node that the indent calculation starts with // Check for extend captures, potentially changing the node that the indent calculation starts with
extend_nodes( if let Some(deepest_preceding) = deepest_preceding {
&mut node, extend_nodes(
deepest_preceding, &mut node,
&extend_captures, deepest_preceding,
text, &extend_captures,
line, text,
tab_width, line,
); tab_width,
);
}
let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos)); let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos));
let mut result = Indentation::default(); let mut result = Indentation::default();

Loading…
Cancel
Save