@@ -456,6 +456,67 @@ fn query_indents(
459+/// Handle extend queries. deepest_preceding is the deepest descendant of node that directly precedes the cursor position.
460+/// Any ancestor of deepest_preceding which is also a descendant of node may be "extended". In that case, node will be updated,
461+/// so that the indent computation starts with the correct syntax node.
463+ node: &mut Node<'a>,
464+ deepest_preceding: Option<Node<'a>>,
465+ extend_captures: &HashMap<usize, Vec<ExtendCapture>>,
470+ if let Some(mut deepest_preceding) = deepest_preceding {
471+ let mut stop_extend = false;
472+ while deepest_preceding != *node {
473+ let mut extend_node = false;
474+ if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
475+ for capture in captures {
477+ ExtendCapture::StopExtend => {
480+ ExtendCapture::ExtendIndented => {
481+ // We extend the node if
482+ // - the cursor is on the same line as the end of the node OR
483+ // - the line that the cursor is on is more indented than the
484+ // first line of the node
485+ if deepest_preceding.end_position().row == line {
489+ indent_level_for_line(text.line(line), tab_width);
490+ let node_indent = indent_level_for_line(
491+ text.line(deepest_preceding.start_position().row),
494+ if cursor_indent > node_indent {
502+ // If we encountered some `StopExtend` capture before, we don't
503+ // extend the node even if we otherwise would
504+ match (extend_node, stop_extend) {
506+ stop_extend = false;
509+ *node = deepest_preceding;
514+ // This parent always exists since node is an ancestor of deepest_preceding
515+ deepest_preceding = deepest_preceding.parent().unwrap();
459/// Use the syntax tree to determine the indentation for a given position.
520/// Use the syntax tree to determine the indentation for a given position.
460/// This can be used in 2 ways:
521/// This can be used in 2 ways:
@@ -510,100 +571,54 @@ pub fn treesitter_indent_for_pos(
511 .root_node()
572 .root_node()
512 .descendant_for_byte_range(byte_pos, byte_pos)?;
573 .descendant_for_byte_range(byte_pos, byte_pos)?;
513− let (query_result, prev_child) = crate::syntax::PARSER.with(|ts_parser| {
574+ let (query_result, deepest_preceding) = {
514− let mut ts_parser = ts_parser.borrow_mut();
515− let mut cursor = ts_parser.cursors.pop().unwrap_or_else(QueryCursor::new);
516 // The query range should intersect with all nodes directly preceding
575 // The query range should intersect with all nodes directly preceding
517 // the cursor in case one of them is extended.
576 // the cursor in case one of them is extended.
518− // prev_child is the deepest such node.
577+ let mut deepest_preceding = None; // The deepest node preceding the cursor
519− let (query_range, prev_child) = {
520− // TODO Is there some way we can reuse this cursor?
521− let mut tree_cursor = node.walk();
578+ let mut tree_cursor = node.walk();
522− let mut prev_child = None;
523− for child in node.children(&mut tree_cursor) {
579+ for child in node.children(&mut tree_cursor) {
524− if child.byte_range().end <= byte_pos {
580+ if child.byte_range().end <= byte_pos {
525− prev_child = Some(child);
581+ deepest_preceding = Some(child);
528− match prev_child {
583+ }
529− Some(mut prev_child) => {
584+ deepest_preceding = deepest_preceding.map(|mut prec| {
530− // Get the deepest directly preceding node
585+ // Get the deepest directly preceding node
531− while prev_child.child_count() > 0 {
586+ while prec.child_count() > 0 {
532− prev_child = prev_child.child(prev_child.child_count() - 1).unwrap();
587+ prec = prec.child(prec.child_count() - 1).unwrap();
535− prev_child.byte_range().end - 1..byte_pos + 1,
539− None => (byte_pos..byte_pos + 1, None),
591+ let query_range = deepest_preceding
592+ .map(|prec| prec.byte_range().end - 1..byte_pos + 1)
593+ .unwrap_or(byte_pos..byte_pos + 1);
595+ crate::syntax::PARSER.with(|ts_parser| {
596+ let mut ts_parser = ts_parser.borrow_mut();
597+ let mut cursor = ts_parser.cursors.pop().unwrap_or_else(QueryCursor::new);
542− let query_result = query_indents(
598+ let query_result = query_indents(
545− &mut cursor,
601+ &mut cursor,
547− query_range,
603+ query_range,
548− new_line.then(|| (line, byte_pos)),
604+ new_line.then(|| (line, byte_pos)),
550− ts_parser.cursors.push(cursor);
606+ ts_parser.cursors.push(cursor);
551− (query_result, prev_child)
607+ (query_result, deepest_preceding)
553 let indent_captures = query_result.indent_captures;
610 let indent_captures = query_result.indent_captures;
554 let extend_captures = query_result.extend_captures;
611 let extend_captures = query_result.extend_captures;
556− // Check for extend captures (starting with the deepest
613+ // Check for extend captures, potentially changing the node that the indent calculation starts with
557− // candidate node and then going up the syntax tree).
614+ extend_nodes(
558− if let Some(mut prev_child) = prev_child {
615+ &mut node,
559− let mut stop_extend = false;
616+ deepest_preceding,
560− while prev_child != node {
561− let mut extend_node = false;
562− if let Some(captures) = extend_captures.get(&prev_child.id()) {
617+ &extend_captures,
563− for capture in captures {
618+ text,
564− match capture {
619+ line,
565− ExtendCapture::StopExtend => {
568− ExtendCapture::ExtendIndented => {
569− // We extend the node if
570− // - the cursor is on the same line as the end of the node OR
571− // - the line that the cursor is on is more indented than the
572− // first line of the node
573− if prev_child.end_position().row == line {
577− indent_level_for_line(text.line(line), tab_width);
578− let node_indent = indent_level_for_line(
579− text.line(prev_child.start_position().row),
580− tab_width,
620+ tab_width,
582− if cursor_indent > node_indent {
590− // If we encountered some `StopExtend` capture before, we don't
591− // extend the node even if we otherwise would
592− match (extend_node, stop_extend) {
594− stop_extend = false;
602− // This parent always exists since node is an ancestor of prev_child
603− prev_child = prev_child.parent().unwrap();
607 let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos));
622 let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos));
609 let mut result = Indentation::default();
624 let mut result = Indentation::default();