Compare commits

...

2 Commits

Author SHA1 Message Date
Sylvain Terrien 4defdee595
Merge 32013b13e3 into 395a71bf53 2025-07-23 21:37:47 +02:00
Sylvain Terrien 32013b13e3 fix: alignment when indenting with spaces (#13498)
Adjust the number of spaces inserted, so that indentation is aligned
on columns.
2025-07-03 01:19:54 +02:00
3 changed files with 81 additions and 8 deletions

View File

@ -4184,14 +4184,22 @@ pub mod insert {
pub fn insert_tab(cx: &mut Context) { pub fn insert_tab(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
// TODO: round out to nearest indentation level (for example a line with 3 spaces should
// indent by one to reach 4 spaces).
let indent = Tendril::from(doc.indent_style.as_str()); let transaction = Transaction::change(
let transaction = Transaction::insert(
doc.text(), doc.text(),
&doc.selection(view.id).clone().cursors(doc.text().slice(..)), doc.selection(view.id).ranges().iter().map(|range| {
indent, let indent = if let IndentStyle::Spaces(indent_width) = doc.indent_style {
let line = range.cursor_line(doc.text().slice(..));
let line_start = doc.text().line_to_char(line);
let offset = (range.head - line_start) % indent_width as usize;
Tendril::from(doc.indent_style.as_str()).split_off(offset)
} else {
Tendril::from(doc.indent_style.as_str())
};
(range.head, range.head, Some(indent))
}),
); );
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
} }
@ -4877,7 +4885,16 @@ fn indent(cx: &mut Context) {
return None; return None;
} }
let pos = doc.text().line_to_char(line); let pos = doc.text().line_to_char(line);
Some((pos, pos, Some(indent.clone())))
let indent = if let IndentStyle::Spaces(indent_width) = doc.indent_style {
let line = doc.text().line(line);
let offset = line.first_non_whitespace_char().unwrap_or(0) % indent_width as usize;
indent.clone().split_off(offset)
} else {
indent.clone()
};
Some((pos, pos, Some(indent)))
}), }),
); );
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);

View File

@ -583,3 +583,59 @@ async fn test_jump_undo_redo() -> anyhow::Result<()> {
.await?; .await?;
Ok(()) Ok(())
} }
#[tokio::test(flavor = "multi_thread")]
async fn test_indent_with_spaces() -> anyhow::Result<()> {
let tests = vec![
// at start of line
(
indoc! {"\
SELECT *
#[|FROM table]#
#(|WHERE condition)#
"},
"i<tab>",
indoc! {"\
SELECT *
#[|FROM table]#
#(|WHERE condition)#
"},
),
// in the middle of line
(
indoc! {"\
SELECT #[*|]#
FROM #(table|)#
WHERE #(condition|)#
"},
"i<S-tab>",
indoc! {"\
SELECT #[|*]#
FROM #(|table)#
WHERE #(|condition)#
"},
),
// indentation in normal mode
(
indoc! {"\
-- comment
#[|SELECT *
FROM table
WHERE condition]#
"},
"<gt>",
indoc! {"\
-- comment
#[|SELECT *
FROM table
WHERE condition]#
"},
),
];
for test in tests {
test_with_config(AppBuilder::new().with_file("foo.rs", None), test).await?;
}
Ok(())
}

View File

@ -437,7 +437,7 @@ async fn test_smart_tab_move_parent_node_end() -> anyhow::Result<()> {
let result = if true { let result = if true {
#[|\"yes\"\n]# #[|\"yes\"\n]#
} else { } else {
\"no #(|\"\n)# \"no #(|\"\n)#
} }
} }
"}, "},