@@ -237,38 +237,58 @@ 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−#[derive(Default)]
240+#[derive(Default, Debug, PartialEq, Eq, Clone)]
241241pub struct Indentation {
242242 /// The total indent (the number of indent levels) is defined as max(0, indent-outdent).
243243 /// The string that this results in depends on the indent style (spaces or tabs, etc.)
244244 indent: usize,
245+ indent_always: usize,
245246 outdent: usize,
247+ outdent_always: usize,
246248}
247249impl Indentation {
248250 /// Add some other [Indentation] to this.
249251 /// The added indent should be the total added indent from one line
250252 fn add_line(&mut self, added: &Indentation) {
251− if added.indent > 0 && added.outdent == 0 {
252− self.indent += 1;
253− } else if added.outdent > 0 && added.indent == 0 {
254− self.outdent += 1;
255− }
253+ self.indent += added.indent;
254+ self.indent_always += added.indent_always;
255+ self.outdent += added.outdent;
256+ self.outdent_always += added.outdent_always;
256257 }
258+
257259 /// Add an indent capture to this indent.
258260 /// All the captures that are added in this way should be on the same line.
259261 fn add_capture(&mut self, added: IndentCaptureType) {
260262 match added {
261263 IndentCaptureType::Indent => {
262− self.indent = 1;
264+ if self.indent_always == 0 {
265+ self.indent = 1;
266+ }
267+ }
268+ IndentCaptureType::IndentAlways => {
269+ // any time we encounter an `indent.always` on the same line, we
270+ // want to cancel out all regular indents
271+ self.indent_always += 1;
272+ self.indent = 0;
263273 }
264274 IndentCaptureType::Outdent => {
265− self.outdent = 1;
275+ if self.outdent_always == 0 {
276+ self.outdent = 1;
277+ }
278+ }
279+ IndentCaptureType::OutdentAlways => {
280+ self.outdent_always += 1;
281+ self.outdent = 0;
266282 }
267283 }
268284 }
285+
269286 fn as_string(&self, indent_style: &IndentStyle) -> String {
270− let indent_level = if self.indent >= self.outdent {
271− self.indent - self.outdent
287+ let indent = self.indent_always + self.indent;
288+ let outdent = self.outdent_always + self.outdent;
289+
290+ let indent_level = if indent >= outdent {
291+ indent - outdent
272292 } else {
273293 log::warn!("Encountered more outdent than indent nodes while calculating indentation: {} outdent, {} indent", self.outdent, self.indent);
274294 0
@@ -278,27 +298,32 @@ impl Indentation {
278298}
279299
280300/// An indent definition which corresponds to a capture from the indent query
301+#[derive(Debug)]
281302struct IndentCapture {
282303 capture_type: IndentCaptureType,
283304 scope: IndentScope,
284305}
285−#[derive(Clone, Copy)]
306+
307+#[derive(Debug, Clone, Copy, PartialEq)]
286308enum IndentCaptureType {
287309 Indent,
310+ IndentAlways,
288311 Outdent,
312+ OutdentAlways,
289313}
314+
290315impl IndentCaptureType {
291316 fn default_scope(&self) -> IndentScope {
292317 match self {
293− IndentCaptureType::Indent => IndentScope::Tail,
294− IndentCaptureType::Outdent => IndentScope::All,
318+ IndentCaptureType::Indent | IndentCaptureType::IndentAlways => IndentScope::Tail,
319+ IndentCaptureType::Outdent | IndentCaptureType::OutdentAlways => IndentScope::All,
295320 }
296321 }
297322}
298323/// This defines which part of a node an [IndentCapture] applies to.
299324/// Each [IndentCaptureType] has a default scope, but the scope can be changed
300325/// with `#set!` property declarations.
301−#[derive(Clone, Copy)]
326+#[derive(Debug, Clone, Copy)]
302327enum IndentScope {
303328 /// The indent applies to the whole node
304329 All,
@@ -308,6 +333,7 @@ enum IndentScope {
308333
309334/// A capture from the indent query which does not define an indent but extends
310335/// the range of a node. This is used before the indent is calculated.
336+#[derive(Debug)]
311337enum ExtendCapture {
312338 Extend,
313339 PreventOnce,
@@ -316,6 +342,7 @@ enum ExtendCapture {
316342/// The result of running a tree-sitter indent query. This stores for
317343/// each node (identified by its ID) the relevant captures (already filtered
318344/// by predicates).
345+#[derive(Debug)]
319346struct IndentQueryResult {
320347 indent_captures: HashMap<usize, Vec<IndentCapture>>,
321348 extend_captures: HashMap<usize, Vec<ExtendCapture>>,
@@ -334,6 +361,33 @@ fn query_indents(
334361 let mut indent_captures: HashMap<usize, Vec<IndentCapture>> = HashMap::new();
335362 let mut extend_captures: HashMap<usize, Vec<ExtendCapture>> = HashMap::new();
336363 cursor.set_byte_range(range);
364+
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+
337391 // Iterate over all captures from the query
338392 for m in cursor.matches(query, syntax.tree().root_node(), RopeProvider(text)) {
339393 // Skip matches where not all custom predicates are fulfilled
@@ -360,21 +414,13 @@ fn query_indents(
360414 Some(QueryPredicateArg::Capture(capt1)),
361415 Some(QueryPredicateArg::Capture(capt2))
362416 ) => {
363− let get_line_num = |node: Node| {
364− let mut node_line = node.start_position().row;
365− // Adjust for the new line that will be inserted
366− if let Some((line, byte)) = new_line_break {
367− if node_line==line && node.start_byte()>=byte {
368− node_line += 1;
369− }
370− }
371− node_line
372− };
373417 let n1 = m.nodes_for_capture_index(*capt1).next();
374418 let n2 = m.nodes_for_capture_index(*capt2).next();
375419 match (n1, n2) {
376420 (Some(n1), Some(n2)) => {
377− let same_line = get_line_num(n1)==get_line_num(n2);
421+ let n1_line = get_node_start_line(n1);
422+ let n2_line = get_node_start_line(n2);
423+ let same_line = n1_line == n2_line;
378424 same_line==(pred.operator.as_ref()=="same-line?")
379425 }
380426 _ => true,
@@ -385,6 +431,23 @@ fn query_indents(
385431 }
386432 }
387433 }
434+ "one-line?" | "not-one-line?" => match pred.args.get(0) {
435+ Some(QueryPredicateArg::Capture(capture_idx)) => {
436+ let node = m.nodes_for_capture_index(*capture_idx).next();
437+
438+ match node {
439+ Some(node) => {
440+ let (start_line, end_line) = (get_node_start_line(node), get_node_end_line(node));
441+ let one_line = end_line == start_line;
442+ one_line != (pred.operator.as_ref() == "not-one-line?")
443+ },
444+ _ => true,
445+ }
446+ }
447+ _ => {
448+ panic!("Invalid indent query: Arguments to \"not-kind-eq?\" must be a capture and a string");
449+ }
450+ },
388451 _ => {
389452 panic!(
390453 "Invalid indent query: Unknown predicate (\"{}\")",
@@ -399,7 +462,9 @@ fn query_indents(
399462 let capture_name = query.capture_names()[capture.index as usize].as_str();
400463 let capture_type = match capture_name {
401464 "indent" => IndentCaptureType::Indent,
465+ "indent.always" => IndentCaptureType::IndentAlways,
402466 "outdent" => IndentCaptureType::Outdent,
467+ "outdent.always" => IndentCaptureType::OutdentAlways,
403468 "extend" => {
404469 extend_captures
405470 .entry(capture.node.id())
@@ -456,10 +521,15 @@ fn query_indents(
456521 .push(indent_capture);
457522 }
458523 }
459− IndentQueryResult {
524+
525+ let result = IndentQueryResult {
460526 indent_captures,
461527 extend_captures,
462− }
528+ };
529+
530+ log::trace!("indent result = {:?}", result);
531+
532+ result
463533}
464534
465535/// Handle extend queries. deepest_preceding is the deepest descendant of node that directly precedes the cursor position.
@@ -584,6 +654,7 @@ pub fn treesitter_indent_for_pos(
584654 .tree()
585655 .root_node()
586656 .descendant_for_byte_range(byte_pos, byte_pos)?;
657+
587658 let (query_result, deepest_preceding) = {
588659 // The query range should intersect with all nodes directly preceding
589660 // the position of the indent query in case one of them is extended.
@@ -642,10 +713,12 @@ pub fn treesitter_indent_for_pos(
642713 // even if there are multiple "indent" nodes on the same line
643714 let mut indent_for_line = Indentation::default();
644715 let mut indent_for_line_below = Indentation::default();
716+
645717 loop {
646718 // This can safely be unwrapped because `first_in_line` contains
647719 // one entry for each ancestor of the node (which is what we iterate over)
648720 let is_first = *first_in_line.last().unwrap();
721+
649722 // Apply all indent definitions for this node
650723 if let Some(definitions) = indent_captures.get(&node.id()) {
651724 for definition in definitions {
@@ -667,6 +740,7 @@ pub fn treesitter_indent_for_pos(
667740 if let Some(parent) = node.parent() {
668741 let mut node_line = node.start_position().row;
669742 let mut parent_line = parent.start_position().row;
743+
670744 if node_line == line && new_line {
671745 // Also consider the line that will be inserted
672746 if node.start_byte() >= byte_pos {
@@ -676,17 +750,20 @@ pub fn treesitter_indent_for_pos(
676750 parent_line += 1;
677751 }
678752 };
753+
679754 if node_line != parent_line {
755+ // Don't add indent for the line below the line of the query
680756 if node_line < line + (new_line as usize) {
681− // Don't add indent for the line below the line of the query
682757 result.add_line(&indent_for_line_below);
683758 }
759+
684760 if node_line == parent_line + 1 {
685761 indent_for_line_below = indent_for_line;
686762 } else {
687763 result.add_line(&indent_for_line);
688764 indent_for_line_below = Indentation::default();
689765 }
766+
690767 indent_for_line = Indentation::default();
691768 }
692769
@@ -700,6 +777,7 @@ pub fn treesitter_indent_for_pos(
700777 {
701778 result.add_line(&indent_for_line_below);
702779 }
780+
703781 result.add_line(&indent_for_line);
704782 break;
705783 }
@@ -810,4 +888,122 @@ mod test {
810888 2
811889 );
812890 }
891+
892+ #[test]
893+ fn add_capture() {
894+ let indent = || Indentation {
895+ indent: 1,
896+ ..Default::default()
897+ };
898+ let indent_always = || Indentation {
899+ indent_always: 1,
900+ ..Default::default()
901+ };
902+ let outdent = || Indentation {
903+ outdent: 1,
904+ ..Default::default()
905+ };
906+ let outdent_always = || Indentation {
907+ outdent_always: 1,
908+ ..Default::default()
909+ };
910+
911+ let add_capture = |mut indent: Indentation, capture| {
912+ indent.add_capture(capture);
913+ indent
914+ };
915+
916+ // adding an indent to no indent makes an indent
917+ assert_eq!(
918+ indent(),
919+ add_capture(Indentation::default(), IndentCaptureType::Indent)
920+ );
921+ assert_eq!(
922+ indent_always(),
923+ add_capture(Indentation::default(), IndentCaptureType::IndentAlways)
924+ );
925+ assert_eq!(
926+ outdent(),
927+ add_capture(Indentation::default(), IndentCaptureType::Outdent)
928+ );
929+ assert_eq!(
930+ outdent_always(),
931+ add_capture(Indentation::default(), IndentCaptureType::OutdentAlways)
932+ );
933+
934+ // adding an indent to an already indented has no effect
935+ assert_eq!(indent(), add_capture(indent(), IndentCaptureType::Indent));
936+ assert_eq!(
937+ outdent(),
938+ add_capture(outdent(), IndentCaptureType::Outdent)
939+ );
940+
941+ // adding an always to a regular makes it always
942+ assert_eq!(
943+ indent_always(),
944+ add_capture(indent(), IndentCaptureType::IndentAlways)
945+ );
946+ assert_eq!(
947+ outdent_always(),
948+ add_capture(outdent(), IndentCaptureType::OutdentAlways)
949+ );
950+
951+ // adding an always to an always is additive
952+ assert_eq!(
953+ Indentation {
954+ indent_always: 2,
955+ ..Default::default()
956+ },
957+ add_capture(indent_always(), IndentCaptureType::IndentAlways)
958+ );
959+ assert_eq!(
960+ Indentation {
961+ outdent_always: 2,
962+ ..Default::default()
963+ },
964+ add_capture(outdent_always(), IndentCaptureType::OutdentAlways)
965+ );
966+
967+ // adding regular to always should be associative
968+ assert_eq!(
969+ Indentation {
970+ indent_always: 1,
971+ ..Default::default()
972+ },
973+ add_capture(
974+ add_capture(indent(), IndentCaptureType::Indent),
975+ IndentCaptureType::IndentAlways
976+ )
977+ );
978+ assert_eq!(
979+ Indentation {
980+ indent_always: 1,
981+ ..Default::default()
982+ },
983+ add_capture(
984+ add_capture(indent(), IndentCaptureType::IndentAlways),
985+ IndentCaptureType::Indent
986+ )
987+ );
988+ assert_eq!(
989+ Indentation {
990+ outdent_always: 1,
991+ ..Default::default()
992+ },
993+ add_capture(
994+ add_capture(outdent(), IndentCaptureType::Outdent),
995+ IndentCaptureType::OutdentAlways
996+ )
997+ );
998+ assert_eq!(
999+ Indentation {
1000+ outdent_always: 1,
1001+ ..Default::default()
1002+ },
1003+ add_capture(
1004+ add_capture(outdent(), IndentCaptureType::OutdentAlways),
1005+ IndentCaptureType::Outdent
1006+ )
1007+ );
1008+ }
8131009}