Fix auto-indent when pasting multi-line content that was copied start… (#26246)

Closes https://github.com/zed-industries/zed/issues/24914 (again)

Release Notes:

- Fixed an issue where multi-line pasted content was auto-indented
incorrectly if copied from the middle of an existing line.
This commit is contained in:
Max Brunsfeld 2025-03-06 14:13:34 -08:00 committed by GitHub
parent f373383fc1
commit be75f17429
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 93 additions and 59 deletions

View file

@ -401,17 +401,16 @@ pub enum AutoindentMode {
/// Apply the same indentation adjustment to all of the lines
/// in a given insertion.
Block {
/// The original start column of each insertion, if it was
/// copied from elsewhere.
/// The original indentation column of the first line of each
/// insertion, if it has been copied.
///
/// Knowing this start column makes it possible to preserve the
/// relative indentation of every line in the insertion from
/// when it was copied.
/// Knowing this makes it possible to preserve the relative indentation
/// of every line in the insertion from when it was copied.
///
/// If the start column is `a`, and the first line of insertion
/// If the original indent column is `a`, and the first line of insertion
/// is then auto-indented to column `b`, then every other line of
/// the insertion will be auto-indented to column `b - a`
original_start_columns: Vec<u32>,
original_indent_columns: Vec<Option<u32>>,
},
}
@ -2206,15 +2205,20 @@ impl Buffer {
let mut original_indent_column = None;
if let AutoindentMode::Block {
original_start_columns,
original_indent_columns,
} = &mode
{
original_indent_column = Some(
original_start_columns.get(ix).copied().unwrap_or(0)
+ indent_size_for_text(
new_text[range_of_insertion_to_indent.clone()].chars(),
)
.len,
original_indent_columns
.get(ix)
.copied()
.flatten()
.unwrap_or_else(|| {
indent_size_for_text(
new_text[range_of_insertion_to_indent.clone()].chars(),
)
.len
}),
);
// Avoid auto-indenting the line after the edit.

View file

@ -1643,7 +1643,7 @@ fn test_autoindent_block_mode(cx: &mut App) {
// indent level, but the indentation of the first line was not included in
// the copied text. This information is retained in the
// 'original_indent_columns' vector.
let original_indent_columns = vec![4];
let original_indent_columns = vec![Some(4)];
let inserted_text = r#"
"
c
@ -1658,7 +1658,7 @@ fn test_autoindent_block_mode(cx: &mut App) {
buffer.edit(
[(Point::new(2, 0)..Point::new(2, 0), inserted_text.clone())],
Some(AutoindentMode::Block {
original_start_columns: original_indent_columns.clone(),
original_indent_columns: original_indent_columns.clone(),
}),
cx,
);
@ -1686,7 +1686,7 @@ fn test_autoindent_block_mode(cx: &mut App) {
buffer.edit(
[(Point::new(2, 8)..Point::new(2, 8), inserted_text)],
Some(AutoindentMode::Block {
original_start_columns: original_indent_columns.clone(),
original_indent_columns: original_indent_columns.clone(),
}),
cx,
);
@ -1735,7 +1735,7 @@ fn test_autoindent_block_mode_without_original_indent_columns(cx: &mut App) {
buffer.edit(
[(Point::new(2, 0)..Point::new(2, 0), inserted_text)],
Some(AutoindentMode::Block {
original_start_columns: original_indent_columns.clone(),
original_indent_columns: original_indent_columns.clone(),
}),
cx,
);
@ -1766,7 +1766,7 @@ fn test_autoindent_block_mode_without_original_indent_columns(cx: &mut App) {
buffer.edit(
[(Point::new(2, 12)..Point::new(2, 12), inserted_text)],
Some(AutoindentMode::Block {
original_start_columns: Vec::new(),
original_indent_columns: Vec::new(),
}),
cx,
);
@ -1822,7 +1822,7 @@ fn test_autoindent_block_mode_multiple_adjacent_ranges(cx: &mut App) {
(ranges_to_replace[2].clone(), "fn three() {\n 103\n}\n"),
],
Some(AutoindentMode::Block {
original_start_columns: vec![0, 0, 0],
original_indent_columns: vec![Some(0), Some(0), Some(0)],
}),
cx,
);