@@ -1,6 +1,5 @@
11use helix_core::{
22 comment, coords_at_pos, graphemes,
3− indent::TAB_WIDTH,
43 movement::{self, Direction},
54 object, pos_at_coords,
65 regex::{self, Regex},
@@ -835,7 +834,7 @@ pub fn open_below(cx: &mut Context) {
835834 // TODO: share logic with insert_newline for indentation
836835 let indent_level =
837836 helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
838− let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
837+ let indent = doc.indent_unit().repeat(indent_level);
839838 let mut text = String::with_capacity(1 + indent.len());
840839 text.push('\n');
841840 text.push_str(&indent);
@@ -1035,8 +1034,13 @@ pub mod insert {
10351034 }
10361035
10371036 pub fn insert_tab(cx: &mut Context) {
1038− // TODO: tab should insert either \t or indent width spaces
1039− insert_char(cx, '\t');
1037+ let doc = cx.doc();
1038+ // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1039+ // indent by one to reach 4 spaces).
1040+
1041+ let indent = Tendril::from(doc.indent_unit());
1042+ let transaction = Transaction::insert(doc.text(), doc.selection(), indent);
1043+ doc.apply(&transaction);
10401044 }
10411045
10421046 pub fn insert_newline(cx: &mut Context) {
@@ -1045,7 +1049,7 @@ pub mod insert {
10451049 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
10461050 let indent_level =
10471051 helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
1048− let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
1052+ let indent = doc.indent_unit().repeat(indent_level);
10491053 let mut text = String::with_capacity(1 + indent.len());
10501054 text.push('\n');
10511055 text.push_str(&indent);
@@ -1185,7 +1189,7 @@ pub fn indent(cx: &mut Context) {
11851189 let lines = get_lines(doc);
11861190
11871191 // Indent by one level
1188− let indent = Tendril::from(" ".repeat(TAB_WIDTH));
1192+ let indent = Tendril::from(doc.indent_unit());
11891193
11901194 let transaction = Transaction::change(
11911195 doc.text(),
@@ -1202,6 +1206,7 @@ pub fn unindent(cx: &mut Context) {
12021206 let doc = cx.doc();
12031207 let lines = get_lines(doc);
12041208 let mut changes = Vec::with_capacity(lines.len());
1209+ let tab_width = doc.tab_width();
12051210
12061211 for line_idx in lines {
12071212 let line = doc.text().line(line_idx);
@@ -1210,11 +1215,11 @@ pub fn unindent(cx: &mut Context) {
12101215 for ch in line.chars() {
12111216 match ch {
12121217 ' ' => width += 1,
1213− '\t' => width = (width / TAB_WIDTH + 1) * TAB_WIDTH,
1218+ '\t' => width = (width / tab_width + 1) * tab_width,
12141219 _ => break,
12151220 }
12161221
1217− if width >= TAB_WIDTH {
1222+ if width >= tab_width {
12181223 break;
12191224 }
12201225 }