@@ -1,6 +1,5 @@
1use helix_core::{
1use helix_core::{
2 comment, coords_at_pos, graphemes,
2 comment, coords_at_pos, graphemes,
4 movement::{self, Direction},
3 movement::{self, Direction},
5 object, pos_at_coords,
4 object, pos_at_coords,
6 regex::{self, Regex},
5 regex::{self, Regex},
@@ -835,7 +834,7 @@ pub fn open_below(cx: &mut Context) {
835 // TODO: share logic with insert_newline for indentation
834 // TODO: share logic with insert_newline for indentation
836 let indent_level =
835 let indent_level =
837 helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
836 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);
839 let mut text = String::with_capacity(1 + indent.len());
838 let mut text = String::with_capacity(1 + indent.len());
840 text.push('\n');
839 text.push('\n');
841 text.push_str(&indent);
840 text.push_str(&indent);
@@ -1035,8 +1034,13 @@ pub mod insert {
1037 pub fn insert_tab(cx: &mut Context) {
1036 pub fn insert_tab(cx: &mut Context) {
1038− // TODO: tab should insert either \t or indent width spaces
1037+ let doc = cx.doc();
1039− insert_char(cx, '\t');
1038+ // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1039+ // indent by one to reach 4 spaces).
1041+ let indent = Tendril::from(doc.indent_unit());
1042+ let transaction = Transaction::insert(doc.text(), doc.selection(), indent);
1043+ doc.apply(&transaction);
1042 pub fn insert_newline(cx: &mut Context) {
1046 pub fn insert_newline(cx: &mut Context) {
@@ -1045,7 +1049,7 @@ pub mod insert {
1045 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1049 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1046 let indent_level =
1050 let indent_level =
1047 helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
1051 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);
1049 let mut text = String::with_capacity(1 + indent.len());
1053 let mut text = String::with_capacity(1 + indent.len());
1050 text.push('\n');
1054 text.push('\n');
1051 text.push_str(&indent);
1055 text.push_str(&indent);
@@ -1185,7 +1189,7 @@ pub fn indent(cx: &mut Context) {
1185 let lines = get_lines(doc);
1189 let lines = get_lines(doc);
1187 // Indent by one level
1191 // Indent by one level
1188− let indent = Tendril::from(" ".repeat(TAB_WIDTH));
1192+ let indent = Tendril::from(doc.indent_unit());
1190 let transaction = Transaction::change(
1194 let transaction = Transaction::change(
1191 doc.text(),
1195 doc.text(),
@@ -1202,6 +1206,7 @@ pub fn unindent(cx: &mut Context) {
1202 let doc = cx.doc();
1206 let doc = cx.doc();
1203 let lines = get_lines(doc);
1207 let lines = get_lines(doc);
1204 let mut changes = Vec::with_capacity(lines.len());
1208 let mut changes = Vec::with_capacity(lines.len());
1209+ let tab_width = doc.tab_width();
1206 for line_idx in lines {
1211 for line_idx in lines {
1207 let line = doc.text().line(line_idx);
1212 let line = doc.text().line(line_idx);
@@ -1210,11 +1215,11 @@ pub fn unindent(cx: &mut Context) {
1210 for ch in line.chars() {
1215 for ch in line.chars() {
1211 match ch {
1216 match ch {
1212 ' ' => width += 1,
1217 ' ' => width += 1,
1213− '\t' => width = (width / TAB_WIDTH + 1) * TAB_WIDTH,
1218+ '\t' => width = (width / tab_width + 1) * tab_width,
1214 _ => break,
1219 _ => break,
1217− if width >= TAB_WIDTH {
1222+ if width >= tab_width {