@@ -56,6 +56,14 @@ impl IndentStyle {
61+ pub fn indent_width(&self, tab_width: usize) -> usize {
63+ IndentStyle::Tabs => tab_width,
64+ IndentStyle::Spaces(width) => width as usize,
61/// Attempts to detect the indentation style used in a document.
69/// Attempts to detect the indentation style used in a document.
@@ -177,7 +185,7 @@ pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
178/// To determine indentation of a newly inserted line, figure out the indentation at the last col
186/// To determine indentation of a newly inserted line, figure out the indentation at the last col
179/// of the previous line.
187/// of the previous line.
180−pub fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize {
188+pub fn indent_level_for_line(line: RopeSlice, tab_width: usize, indent_width: usize) -> usize {
181 let mut len = 0;
189 let mut len = 0;
182 for ch in line.chars() {
190 for ch in line.chars() {
183 match ch {
191 match ch {
@@ -187,7 +195,7 @@ pub fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize {
190− len / tab_width
198+ len / indent_width
193/// Computes for node and all ancestors whether they are the first node on their line.
201/// Computes for node and all ancestors whether they are the first node on their line.
@@ -466,6 +474,7 @@ fn extend_nodes<'a>(
466 text: RopeSlice,
474 text: RopeSlice,
467 line: usize,
475 line: usize,
468 tab_width: usize,
476 tab_width: usize,
477+ indent_width: usize,
470 let mut stop_extend = false;
479 let mut stop_extend = false;
@@ -490,10 +499,12 @@ fn extend_nodes<'a>(
490 if deepest_preceding.end_position().row == line {
499 if deepest_preceding.end_position().row == line {
491 extend_node = true;
500 extend_node = true;
493− let cursor_indent = indent_level_for_line(text.line(line), tab_width);
503+ indent_level_for_line(text.line(line), tab_width, indent_width);
494 let node_indent = indent_level_for_line(
504 let node_indent = indent_level_for_line(
495 text.line(deepest_preceding.start_position().row),
505 text.line(deepest_preceding.start_position().row),
496 tab_width,
506 tab_width,
498 if cursor_indent > node_indent {
509 if cursor_indent > node_indent {
499 extend_node = true;
510 extend_node = true;
@@ -562,6 +573,7 @@ pub fn treesitter_indent_for_pos(
562 syntax: &Syntax,
573 syntax: &Syntax,
563 indent_style: &IndentStyle,
574 indent_style: &IndentStyle,
564 tab_width: usize,
575 tab_width: usize,
576+ indent_width: usize,
565 text: RopeSlice,
577 text: RopeSlice,
566 line: usize,
578 line: usize,
567 pos: usize,
579 pos: usize,
@@ -622,6 +634,7 @@ pub fn treesitter_indent_for_pos(
624 tab_width,
636 tab_width,
627 let mut first_in_line = get_first_in_line(node, new_line.then_some(byte_pos));
640 let mut first_in_line = get_first_in_line(node, new_line.then_some(byte_pos));
@@ -709,6 +722,7 @@ pub fn indent_for_newline(
709 line_before_end_pos: usize,
722 line_before_end_pos: usize,
710 current_line: usize,
723 current_line: usize,
711) -> String {
724) -> String {
725+ let indent_width = indent_style.indent_width(tab_width);
712 if let (Some(query), Some(syntax)) = (
726 if let (Some(query), Some(syntax)) = (
713 language_config.and_then(|config| config.indent_query()),
727 language_config.and_then(|config| config.indent_query()),
@@ -718,6 +732,7 @@ pub fn indent_for_newline(
719 indent_style,
733 indent_style,
720 tab_width,
734 tab_width,
722 line_before,
737 line_before,
723 line_before_end_pos,
738 line_before_end_pos,
@@ -726,7 +741,7 @@ pub fn indent_for_newline(
726 return indent;
741 return indent;
729− let indent_level = indent_level_for_line(text.line(current_line), tab_width);
744+ let indent_level = indent_level_for_line(text.line(current_line), tab_width, indent_width);
730 indent_style.as_str().repeat(indent_level)
745 indent_style.as_str().repeat(indent_level)
@@ -763,12 +778,22 @@ mod test {
764 fn test_indent_level() {
779 fn test_indent_level() {
765 let tab_width = 4;
780 let tab_width = 4;
781+ let indent_width = 4;
766 let line = Rope::from(" fn new"); // 8 spaces
782 let line = Rope::from(" fn new"); // 8 spaces
767− assert_eq!(indent_level_for_line(line.slice(..), tab_width), 2);
784+ indent_level_for_line(line.slice(..), tab_width, indent_width),
768 let line = Rope::from("\t\t\tfn new"); // 3 tabs
787 let line = Rope::from("\t\t\tfn new"); // 3 tabs
769− assert_eq!(indent_level_for_line(line.slice(..), tab_width), 3);
789+ indent_level_for_line(line.slice(..), tab_width, indent_width),
770 // mixed indentation
792 // mixed indentation
771 let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
793 let line = Rope::from("\t \tfn new"); // 1 tab, 4 spaces, tab
772− assert_eq!(indent_level_for_line(line.slice(..), tab_width), 3);
795+ indent_level_for_line(line.slice(..), tab_width, indent_width),