@@ -1,13 +1,13 @@
1−use std::collections::HashMap;
1+use std::{borrow::Cow, collections::HashMap};
22
33use tree_sitter::{Query, QueryCursor, QueryPredicateArg};
44
55use crate::{
66 chars::{char_is_line_ending, char_is_whitespace},
7− graphemes::tab_width_at,
7+ graphemes::{grapheme_width, tab_width_at},
88 syntax::{LanguageConfiguration, RopeProvider, Syntax},
99 tree_sitter::Node,
10− Rope, RopeSlice,
10+ Rope, RopeGraphemes, RopeSlice,
1111};
1212
1313/// Enum representing indentation style.
@@ -237,19 +237,33 @@ fn get_first_in_line(mut node: Node, new_line_byte_pos: Option<usize>) -> Vec<bo
237237/// This is usually constructed in one of 2 ways:
238238/// - Successively add indent captures to get the (added) indent from a single line
239239/// - Successively add the indent results for each line
240+/// The string that this indentation defines starts with the string contained in the align field (unless it is None), followed by:
241+/// - max(0, indent - outdent) tabs, if tabs are used for indentation
242+/// - max(0, indent - outdent)*indent_width spaces, if spaces are used for indentation
240243#[derive(Default, Debug, PartialEq, Eq, Clone)]
241244pub struct Indentation {
242− /// The total indent (the number of indent levels) is defined as max(0, indent-outdent).
243− /// The string that this results in depends on the indent style (spaces or tabs, etc.)
244245 indent: usize,
245246 indent_always: usize,
246247 outdent: usize,
247248 outdent_always: usize,
249+ /// The alignment, as a string containing only tabs & spaces. Storing this as a string instead of e.g.
250+ /// the (visual) width ensures that the alignment is preserved even if the tab width changes.
251+ align: Option<String>,
248252}
253+
249254impl Indentation {
250255 /// Add some other [Indentation] to this.
251− /// The added indent should be the total added indent from one line
252− fn add_line(&mut self, added: &Indentation) {
256+ /// The added indent should be the total added indent from one line.
257+ /// Indent should always be added starting from the bottom (or equivalently, the innermost tree-sitter node).
258+ fn add_line(&mut self, added: Indentation) {
259+ // Align overrides the indent from outer scopes.
260+ if self.align.is_some() {
261+ return;
262+ }
263+ if added.align.is_some() {
264+ self.align = added.align;
265+ return;
266+ }
253267 self.indent += added.indent;
254268 self.indent_always += added.indent_always;
255269 self.outdent += added.outdent;
@@ -280,10 +294,12 @@ impl Indentation {
280294 self.outdent_always += 1;
281295 self.outdent = 0;
282296 }
297+ IndentCaptureType::Align(align) => {
298+ self.align = Some(align);
299+ }
283300 }
284301 }
285−
286− fn as_string(&self, indent_style: &IndentStyle) -> String {
302+ fn into_string(self, indent_style: &IndentStyle) -> String {
287303 let indent = self.indent_always + self.indent;
288304 let outdent = self.outdent_always + self.outdent;
289305
@@ -293,7 +309,13 @@ impl Indentation {
293309 log::warn!("Encountered more outdent than indent nodes while calculating indentation: {} outdent, {} indent", self.outdent, self.indent);
294310 0
295311 };
296− indent_style.as_str().repeat(indent_level)
312+ let mut indent_string = if let Some(align) = self.align {
313+ align
314+ } else {
315+ String::new()
316+ };
317+ indent_string.push_str(&indent_style.as_str().repeat(indent_level));
318+ indent_string
297319 }
298320}
299321
@@ -303,13 +325,14 @@ struct IndentCapture {
303325 capture_type: IndentCaptureType,
304326 scope: IndentScope,
305327}
306−
307−#[derive(Debug, Clone, Copy, PartialEq)]
328+#[derive(Debug, Clone, PartialEq)]
308329enum IndentCaptureType {
309330 Indent,
310331 IndentAlways,
311332 Outdent,
312333 OutdentAlways,
334+ /// Alignment given as a string of whitespace
335+ Align(String),
313336}
314337
315338impl IndentCaptureType {
@@ -317,6 +340,7 @@ impl IndentCaptureType {
317340 match self {
318341 IndentCaptureType::Indent | IndentCaptureType::IndentAlways => IndentScope::Tail,
319342 IndentCaptureType::Outdent | IndentCaptureType::OutdentAlways => IndentScope::All,
343+ IndentCaptureType::Align(_) => IndentScope::All,
320344 }
321345 }
322346}
@@ -348,46 +372,35 @@ struct IndentQueryResult {
348372 extend_captures: HashMap<usize, Vec<ExtendCapture>>,
349373}
350374
375+fn get_node_start_line(node: Node, new_line_byte_pos: Option<usize>) -> usize {
376+ let mut node_line = node.start_position().row;
377+ // Adjust for the new line that will be inserted
378+ if new_line_byte_pos.map_or(false, |pos| node.start_byte() >= pos) {
379+ node_line += 1;
380+ }
381+ node_line
382+}
383+fn get_node_end_line(node: Node, new_line_byte_pos: Option<usize>) -> usize {
384+ let mut node_line = node.end_position().row;
385+ // Adjust for the new line that will be inserted (with a strict inequality since end_byte is exclusive)
386+ if new_line_byte_pos.map_or(false, |pos| node.end_byte() > pos) {
387+ node_line += 1;
388+ }
389+ node_line
390+}
391+
351392fn query_indents(
352393 query: &Query,
353394 syntax: &Syntax,
354395 cursor: &mut QueryCursor,
355396 text: RopeSlice,
356397 range: std::ops::Range<usize>,
357− // Position of the (optional) newly inserted line break.
358− // Given as (line, byte_pos)
359− new_line_break: Option<(usize, usize)>,
398+ new_line_byte_pos: Option<usize>,
360399) -> IndentQueryResult {
361400 let mut indent_captures: HashMap<usize, Vec<IndentCapture>> = HashMap::new();
362401 let mut extend_captures: HashMap<usize, Vec<ExtendCapture>> = HashMap::new();
363402 cursor.set_byte_range(range);
364403
365− let get_node_start_line = |node: Node| {
366− let mut node_line = node.start_position().row;
367−
368− // Adjust for the new line that will be inserted
369− if let Some((line, byte)) = new_line_break {
370− if node_line == line && node.start_byte() >= byte {
371− node_line += 1;
372− }
373− }
374−
375− node_line
376− };
377−
378− let get_node_end_line = |node: Node| {
379− let mut node_line = node.end_position().row;
380−
381− // Adjust for the new line that will be inserted
382− if let Some((line, byte)) = new_line_break {
383− if node_line == line && node.end_byte() < byte {
384− node_line += 1;
385− }
386− }
387−
388− node_line
389− };
390−
391404 // Iterate over all captures from the query
392405 for m in cursor.matches(query, syntax.tree().root_node(), RopeProvider(text)) {
393406 // Skip matches where not all custom predicates are fulfilled
@@ -418,8 +431,8 @@ fn query_indents(
418431 let n2 = m.nodes_for_capture_index(*capt2).next();
419432 match (n1, n2) {
420433 (Some(n1), Some(n2)) => {
421− let n1_line = get_node_start_line(n1);
422− let n2_line = get_node_start_line(n2);
434+ let n1_line = get_node_start_line(n1, new_line_byte_pos);
435+ let n2_line = get_node_start_line(n2, new_line_byte_pos);
423436 let same_line = n1_line == n2_line;
424437 same_line==(pred.operator.as_ref()=="same-line?")
425438 }
@@ -437,7 +450,7 @@ fn query_indents(
437450
438451 match node {
439452 Some(node) => {
440− let (start_line, end_line) = (get_node_start_line(node), get_node_end_line(node));
453+ let (start_line, end_line) = (get_node_start_line(node,new_line_byte_pos), get_node_end_line(node, new_line_byte_pos));
441454 let one_line = end_line == start_line;
442455 one_line != (pred.operator.as_ref() == "not-one-line?")
443456 },
@@ -458,6 +471,11 @@ fn query_indents(
458471 }) {
459472 continue;
460473 }
474+ // A list of pairs (node_id, indent_capture) that are added by this match.
475+ // They cannot be added to indent_captures immediately since they may depend on other captures (such as an @anchor).
476+ let mut added_indent_captures: Vec<(usize, IndentCapture)> = Vec::new();
477+ // The row/column position of the optional anchor in this query
478+ let mut anchor: Option<tree_sitter::Node> = None;
461479 for capture in m.captures {
462480 let capture_name = query.capture_names()[capture.index as usize].as_str();
463481 let capture_type = match capture_name {
@@ -465,6 +483,16 @@ fn query_indents(
465483 "indent.always" => IndentCaptureType::IndentAlways,
466484 "outdent" => IndentCaptureType::Outdent,
467485 "outdent.always" => IndentCaptureType::OutdentAlways,
486+ // The alignment will be updated to the correct value at the end, when the anchor is known.
487+ "align" => IndentCaptureType::Align(String::from("")),
488+ "anchor" => {
489+ if anchor.is_some() {
490+ log::error!("Invalid indent query: Encountered more than one @anchor in the same match.")
491+ } else {
492+ anchor = Some(capture.node);
493+ }
494+ continue;
495+ }
468496 "extend" => {
469497 extend_captures
470498 .entry(capture.node.id())
@@ -514,11 +542,41 @@ fn query_indents(
514542 }
515543 }
516544 }
545+ added_indent_captures.push((capture.node.id(), indent_capture))
546+ }
547+ for (node_id, mut capture) in added_indent_captures {
548+ // Set the anchor for all align queries.
549+ if let IndentCaptureType::Align(_) = capture.capture_type {
550+ let anchor = match anchor {
551+ None => {
552+ log::error!(
553+ "Invalid indent query: @align requires an accompanying @anchor."
554+ );
555+ continue;
556+ }
557+ Some(anchor) => anchor,
558+ };
559+ // Create a string of tabs & spaces that should have the same width
560+ // as the string that precedes the anchor (independent of the tab width).
561+ let mut align = String::new();
562+ for grapheme in RopeGraphemes::new(
563+ text.line(anchor.start_position().row)
564+ .byte_slice(0..anchor.start_position().column),
565+ ) {
566+ if grapheme == "\t" {
567+ align.push('\t');
568+ } else {
569+ align.extend(
570+ std::iter::repeat(' ').take(grapheme_width(&Cow::from(grapheme))),
571+ );
572+ }
573+ }
574+ capture.capture_type = IndentCaptureType::Align(align);
575+ }
517576 indent_captures
518− .entry(capture.node.id())
519− // Most entries only need to contain a single IndentCapture
577+ .entry(node_id)
520578 .or_insert_with(|| Vec::with_capacity(1))
521− .push(indent_capture);
579+ .push(capture);
522580 }
523581 }
524582
@@ -648,6 +706,7 @@ pub fn treesitter_indent_for_pos(
648706 new_line: bool,
649707) -> Option<String> {
650708 let byte_pos = text.char_to_byte(pos);
709+ let new_line_byte_pos = new_line.then_some(byte_pos);
651710 // The innermost tree-sitter node which is considered for the indent
652711 // computation. It may change if some predeceding node is extended
653712 let mut node = syntax
@@ -685,13 +744,13 @@ pub fn treesitter_indent_for_pos(
685744 &mut cursor,
686745 text,
687746 query_range,
688− new_line.then_some((line, byte_pos)),
747+ new_line_byte_pos,
689748 );
690749 ts_parser.cursors.push(cursor);
691750 (query_result, deepest_preceding)
692751 })
693752 };
694− let indent_captures = query_result.indent_captures;
753+ let mut indent_captures = query_result.indent_captures;
695754 let extend_captures = query_result.extend_captures;
696755
697756 // Check for extend captures, potentially changing the node that the indent calculation starts with
@@ -719,8 +778,10 @@ pub fn treesitter_indent_for_pos(
719778 // one entry for each ancestor of the node (which is what we iterate over)
720779 let is_first = *first_in_line.last().unwrap();
721780
722− // Apply all indent definitions for this node
723− if let Some(definitions) = indent_captures.get(&node.id()) {
781+ // Apply all indent definitions for this node.
782+ // Since we only iterate over each node once, we can remove the
783+ // corresponding captures from the HashMap to avoid cloning them.
784+ if let Some(definitions) = indent_captures.remove(&node.id()) {
724785 for definition in definitions {
725786 match definition.scope {
726787 IndentScope::All => {
@@ -738,29 +799,19 @@ pub fn treesitter_indent_for_pos(
738799 }
739800
740801 if let Some(parent) = node.parent() {
741− let mut node_line = node.start_position().row;
742− let mut parent_line = parent.start_position().row;
743−
744− if node_line == line && new_line {
745− // Also consider the line that will be inserted
746− if node.start_byte() >= byte_pos {
747− node_line += 1;
748− }
749− if parent.start_byte() >= byte_pos {
750− parent_line += 1;
751− }
752− };
802+ let node_line = get_node_start_line(node, new_line_byte_pos);
803+ let parent_line = get_node_start_line(parent, new_line_byte_pos);
753804
754805 if node_line != parent_line {
755806 // Don't add indent for the line below the line of the query
756807 if node_line < line + (new_line as usize) {
757− result.add_line(&indent_for_line_below);
808+ result.add_line(indent_for_line_below);
758809 }
759810
760811 if node_line == parent_line + 1 {
761812 indent_for_line_below = indent_for_line;
762813 } else {
763− result.add_line(&indent_for_line);
814+ result.add_line(indent_for_line);
764815 indent_for_line_below = Indentation::default();
765816 }
766817
@@ -775,14 +826,13 @@ pub fn treesitter_indent_for_pos(
775826 if (node.start_position().row < line)
776827 || (new_line && node.start_position().row == line && node.start_byte() < byte_pos)
777828 {
778− result.add_line(&indent_for_line_below);
829+ result.add_line(indent_for_line_below);
779830 }
780−
781− result.add_line(&indent_for_line);
831+ result.add_line(indent_for_line);
782832 break;
783833 }
784834 }
785− Some(result.as_string(indent_style))
835+ Some(result.into_string(indent_style))
786836}
787837
788838/// Returns the indentation for a new line.