Use first line comment prefix when toggling comments (#10335)
This fixed an issue introduced in https://github.com/zed-industries/zed/pull/10126, where, when toggling comments in a language with multiple line comment prefixes (e.g. Gleam, Erlang) Zed would insert the *last* prefix instead of the first. Release Notes: - Fixed an issue where the `toggle comments` command inserted the wrong line comment prefix in some languages (preview only). Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
parent
0331fdebd3
commit
a0ee29a806
3 changed files with 44 additions and 37 deletions
|
@ -7098,22 +7098,13 @@ impl Editor {
|
||||||
.line_comment_prefixes()
|
.line_comment_prefixes()
|
||||||
.filter(|prefixes| !prefixes.is_empty())
|
.filter(|prefixes| !prefixes.is_empty())
|
||||||
{
|
{
|
||||||
// Split the comment prefix's trailing whitespace into a separate string,
|
let first_prefix = full_comment_prefixes
|
||||||
// as that portion won't be used for detecting if a line is a comment.
|
.first()
|
||||||
struct Comment {
|
.expect("prefixes is non-empty");
|
||||||
full_prefix: Arc<str>,
|
let prefix_trimmed_lengths = full_comment_prefixes
|
||||||
trimmed_prefix_len: usize,
|
|
||||||
}
|
|
||||||
let prefixes: SmallVec<[Comment; 4]> = full_comment_prefixes
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|full_prefix| {
|
.map(|p| p.trim_end_matches(' ').len())
|
||||||
let trimmed_prefix_len = full_prefix.trim_end_matches(' ').len();
|
.collect::<SmallVec<[usize; 4]>>();
|
||||||
Comment {
|
|
||||||
trimmed_prefix_len,
|
|
||||||
full_prefix: full_prefix.clone(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut all_selection_lines_are_comments = true;
|
let mut all_selection_lines_are_comments = true;
|
||||||
|
|
||||||
|
@ -7122,28 +7113,25 @@ impl Editor {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some((prefix, prefix_range)) = prefixes
|
let prefix_range = full_comment_prefixes
|
||||||
.iter()
|
.iter()
|
||||||
.map(|prefix| {
|
.zip(prefix_trimmed_lengths.iter().copied())
|
||||||
(
|
.map(|(prefix, trimmed_prefix_len)| {
|
||||||
prefix,
|
comment_prefix_range(
|
||||||
comment_prefix_range(
|
snapshot.deref(),
|
||||||
snapshot.deref(),
|
row,
|
||||||
row,
|
&prefix[..trimmed_prefix_len],
|
||||||
&prefix.full_prefix[..prefix.trimmed_prefix_len],
|
&prefix[trimmed_prefix_len..],
|
||||||
&prefix.full_prefix[prefix.trimmed_prefix_len..],
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.max_by_key(|(_, range)| range.end.column - range.start.column)
|
.max_by_key(|range| range.end.column - range.start.column)
|
||||||
else {
|
.expect("prefixes is non-empty");
|
||||||
// There has to be at least one prefix.
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
if prefix_range.is_empty() {
|
if prefix_range.is_empty() {
|
||||||
all_selection_lines_are_comments = false;
|
all_selection_lines_are_comments = false;
|
||||||
}
|
}
|
||||||
selection_edit_ranges.push((prefix_range, prefix.full_prefix.clone()));
|
|
||||||
|
selection_edit_ranges.push(prefix_range);
|
||||||
}
|
}
|
||||||
|
|
||||||
if all_selection_lines_are_comments {
|
if all_selection_lines_are_comments {
|
||||||
|
@ -7151,17 +7139,17 @@ impl Editor {
|
||||||
selection_edit_ranges
|
selection_edit_ranges
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.cloned()
|
||||||
.map(|(range, _)| (range, empty_str.clone())),
|
.map(|range| (range, empty_str.clone())),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let min_column = selection_edit_ranges
|
let min_column = selection_edit_ranges
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(range, _)| range.start.column)
|
.map(|range| range.start.column)
|
||||||
.min()
|
.min()
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
edits.extend(selection_edit_ranges.iter().map(|(range, prefix)| {
|
edits.extend(selection_edit_ranges.iter().map(|range| {
|
||||||
let position = Point::new(range.start.row, min_column);
|
let position = Point::new(range.start.row, min_column);
|
||||||
(position..position, prefix.clone())
|
(position..position, first_prefix.clone())
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
} else if let Some((full_comment_prefix, comment_suffix)) =
|
} else if let Some((full_comment_prefix, comment_suffix)) =
|
||||||
|
|
|
@ -6566,7 +6566,7 @@ async fn test_toggle_comment(cx: &mut gpui::TestAppContext) {
|
||||||
let mut cx = EditorTestContext::new(cx).await;
|
let mut cx = EditorTestContext::new(cx).await;
|
||||||
let language = Arc::new(Language::new(
|
let language = Arc::new(Language::new(
|
||||||
LanguageConfig {
|
LanguageConfig {
|
||||||
line_comments: vec!["// ".into()],
|
line_comments: vec!["// ".into(), "//! ".into(), "/// ".into()],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
Some(tree_sitter_rust::language()),
|
Some(tree_sitter_rust::language()),
|
||||||
|
@ -6660,6 +6660,25 @@ async fn test_toggle_comment(cx: &mut gpui::TestAppContext) {
|
||||||
// c();ˇ»
|
// c();ˇ»
|
||||||
}
|
}
|
||||||
"});
|
"});
|
||||||
|
|
||||||
|
// If a selection includes multiple comment prefixes, all lines are uncommented.
|
||||||
|
cx.set_state(indoc! {"
|
||||||
|
fn a() {
|
||||||
|
«// a();
|
||||||
|
/// b();
|
||||||
|
//! c();ˇ»
|
||||||
|
}
|
||||||
|
"});
|
||||||
|
|
||||||
|
cx.update_editor(|e, cx| e.toggle_comments(&ToggleComments::default(), cx));
|
||||||
|
|
||||||
|
cx.assert_editor_state(indoc! {"
|
||||||
|
fn a() {
|
||||||
|
«a();
|
||||||
|
b();
|
||||||
|
c();ˇ»
|
||||||
|
}
|
||||||
|
"});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
name = "Rust"
|
name = "Rust"
|
||||||
grammar = "rust"
|
grammar = "rust"
|
||||||
path_suffixes = ["rs"]
|
path_suffixes = ["rs"]
|
||||||
line_comments = [ "/// ", "//! ", "// "]
|
line_comments = ["// ", "/// ", "//! "]
|
||||||
autoclose_before = ";:.,=}])>"
|
autoclose_before = ";:.,=}])>"
|
||||||
brackets = [
|
brackets = [
|
||||||
{ start = "{", end = "}", close = true, newline = true },
|
{ start = "{", end = "}", close = true, newline = true },
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue