editor: Improve rewrap to respect indent and prefix boundaries (#33566)
1. Fixes bug where this would not rewrap: ```rs // This is the first long comment block to be wrapped. fn my_func(a: u32); // This is the second long comment block to be wrapped. ``` 2. Comment prefix boundaries (Notice now they don't merge between different comment prefix): Initial text: ```rs // A regular long long comment to be wrapped. // A second regular long comment to be wrapped. /// A documentation long comment to be wrapped. ``` Upon rewrap: ```rs // A regular long long comment to be // wrapped. A second regular long // comment to be wrapped. /// A documentation long comment to be /// wrapped. ``` 3. Indent boundaries (Notice now they don't merge between different indentation): Initial text: ```rs fn foo() { // This is a long comment at the base indent. // This is a long comment at the base indent. // This is a long comment at the next indent. // This is a long comment at the next indent. // This is a long comment at the base indent. } ``` Upon rewrap: ```rs fn foo() { // This is a long comment at the base // indent. This is a long comment at the // base indent. // This is a long comment at the // next indent. This is a long // comment at the next indent. // This is a long comment at the base // indent. } ``` Release Notes: - Fixed an issue where rewrap would not work with selection when two comment blocks are separated with line of code. - Improved rewrap to respect changes in indentation or comment prefix (e.g. `//` vs `///`) as boundaries so that it doesn't merge them into one mangled text.
This commit is contained in:
parent
c56b8904cc
commit
bbf16bda75
2 changed files with 242 additions and 280 deletions
|
@ -11524,42 +11524,82 @@ impl Editor {
|
|||
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||
let selections = self.selections.all::<Point>(cx);
|
||||
|
||||
// Shrink and split selections to respect paragraph boundaries.
|
||||
let ranges = selections.into_iter().flat_map(|selection| {
|
||||
// Split selections to respect paragraph, indent, and comment prefix boundaries.
|
||||
let wrap_ranges = selections.into_iter().flat_map(|selection| {
|
||||
let mut non_blank_rows_iter = (selection.start.row..=selection.end.row)
|
||||
.filter(|row| !buffer.is_line_blank(MultiBufferRow(*row)))
|
||||
.peekable();
|
||||
|
||||
let first_row = if let Some(&row) = non_blank_rows_iter.peek() {
|
||||
row
|
||||
} else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
let language_settings = buffer.language_settings_at(selection.head(), cx);
|
||||
let language_scope = buffer.language_scope_at(selection.head());
|
||||
|
||||
let Some(start_row) = (selection.start.row..=selection.end.row)
|
||||
.find(|row| !buffer.is_line_blank(MultiBufferRow(*row)))
|
||||
else {
|
||||
return vec![];
|
||||
};
|
||||
let Some(end_row) = (selection.start.row..=selection.end.row)
|
||||
.rev()
|
||||
.find(|row| !buffer.is_line_blank(MultiBufferRow(*row)))
|
||||
else {
|
||||
return vec![];
|
||||
let mut ranges = Vec::new();
|
||||
let mut current_range_start = first_row;
|
||||
let from_empty_selection = selection.is_empty();
|
||||
|
||||
let mut prev_row = first_row;
|
||||
let mut prev_indent = buffer.indent_size_for_line(MultiBufferRow(first_row));
|
||||
let mut prev_comment_prefix = if let Some(language_scope) = &language_scope {
|
||||
let indent = buffer.indent_size_for_line(MultiBufferRow(first_row));
|
||||
let indent_end = Point::new(first_row, indent.len);
|
||||
language_scope
|
||||
.line_comment_prefixes()
|
||||
.iter()
|
||||
.find(|prefix| buffer.contains_str_at(indent_end, prefix))
|
||||
.cloned()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut row = start_row;
|
||||
let mut ranges = Vec::new();
|
||||
while let Some(blank_row) =
|
||||
(row..end_row).find(|row| buffer.is_line_blank(MultiBufferRow(*row)))
|
||||
{
|
||||
let next_paragraph_start = (blank_row + 1..=end_row)
|
||||
.find(|row| !buffer.is_line_blank(MultiBufferRow(*row)))
|
||||
.unwrap();
|
||||
ranges.push((
|
||||
language_settings.clone(),
|
||||
language_scope.clone(),
|
||||
Point::new(row, 0)..Point::new(blank_row - 1, 0),
|
||||
));
|
||||
row = next_paragraph_start;
|
||||
for row in non_blank_rows_iter.skip(1) {
|
||||
let has_paragraph_break = row > prev_row + 1;
|
||||
|
||||
let row_indent = buffer.indent_size_for_line(MultiBufferRow(row));
|
||||
let row_comment_prefix = if let Some(language_scope) = &language_scope {
|
||||
let indent = buffer.indent_size_for_line(MultiBufferRow(row));
|
||||
let indent_end = Point::new(row, indent.len);
|
||||
language_scope
|
||||
.line_comment_prefixes()
|
||||
.iter()
|
||||
.find(|prefix| buffer.contains_str_at(indent_end, prefix))
|
||||
.cloned()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let has_boundary_change =
|
||||
row_indent != prev_indent || row_comment_prefix != prev_comment_prefix;
|
||||
|
||||
if has_paragraph_break || has_boundary_change {
|
||||
ranges.push((
|
||||
language_settings.clone(),
|
||||
Point::new(current_range_start, 0)
|
||||
..Point::new(prev_row, buffer.line_len(MultiBufferRow(prev_row))),
|
||||
prev_indent,
|
||||
prev_comment_prefix.clone(),
|
||||
from_empty_selection,
|
||||
));
|
||||
current_range_start = row;
|
||||
}
|
||||
|
||||
prev_row = row;
|
||||
prev_indent = row_indent;
|
||||
prev_comment_prefix = row_comment_prefix;
|
||||
}
|
||||
|
||||
ranges.push((
|
||||
language_settings.clone(),
|
||||
language_scope.clone(),
|
||||
Point::new(row, 0)..Point::new(end_row, 0),
|
||||
Point::new(current_range_start, 0)
|
||||
..Point::new(prev_row, buffer.line_len(MultiBufferRow(prev_row))),
|
||||
prev_indent,
|
||||
prev_comment_prefix,
|
||||
from_empty_selection,
|
||||
));
|
||||
|
||||
ranges
|
||||
|
@ -11568,9 +11608,11 @@ impl Editor {
|
|||
let mut edits = Vec::new();
|
||||
let mut rewrapped_row_ranges = Vec::<RangeInclusive<u32>>::new();
|
||||
|
||||
for (language_settings, language_scope, range) in ranges {
|
||||
let mut start_row = range.start.row;
|
||||
let mut end_row = range.end.row;
|
||||
for (language_settings, wrap_range, indent_size, comment_prefix, from_empty_selection) in
|
||||
wrap_ranges
|
||||
{
|
||||
let mut start_row = wrap_range.start.row;
|
||||
let mut end_row = wrap_range.end.row;
|
||||
|
||||
// Skip selections that overlap with a range that has already been rewrapped.
|
||||
let selection_range = start_row..end_row;
|
||||
|
@ -11583,49 +11625,16 @@ impl Editor {
|
|||
|
||||
let tab_size = language_settings.tab_size;
|
||||
|
||||
// Since not all lines in the selection may be at the same indent
|
||||
// level, choose the indent size that is the most common between all
|
||||
// of the lines.
|
||||
//
|
||||
// If there is a tie, we use the deepest indent.
|
||||
let (indent_size, indent_end) = {
|
||||
let mut indent_size_occurrences = HashMap::default();
|
||||
let mut rows_by_indent_size = HashMap::<IndentSize, Vec<u32>>::default();
|
||||
|
||||
for row in start_row..=end_row {
|
||||
let indent = buffer.indent_size_for_line(MultiBufferRow(row));
|
||||
rows_by_indent_size.entry(indent).or_default().push(row);
|
||||
*indent_size_occurrences.entry(indent).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let indent_size = indent_size_occurrences
|
||||
.into_iter()
|
||||
.max_by_key(|(indent, count)| (*count, indent.len_with_expanded_tabs(tab_size)))
|
||||
.map(|(indent, _)| indent)
|
||||
.unwrap_or_default();
|
||||
let row = rows_by_indent_size[&indent_size][0];
|
||||
let indent_end = Point::new(row, indent_size.len);
|
||||
|
||||
(indent_size, indent_end)
|
||||
};
|
||||
|
||||
let mut line_prefix = indent_size.chars().collect::<String>();
|
||||
|
||||
let mut inside_comment = false;
|
||||
if let Some(comment_prefix) = language_scope.and_then(|language| {
|
||||
language
|
||||
.line_comment_prefixes()
|
||||
.iter()
|
||||
.find(|prefix| buffer.contains_str_at(indent_end, prefix))
|
||||
.cloned()
|
||||
}) {
|
||||
line_prefix.push_str(&comment_prefix);
|
||||
if let Some(prefix) = &comment_prefix {
|
||||
line_prefix.push_str(prefix);
|
||||
inside_comment = true;
|
||||
}
|
||||
|
||||
let allow_rewrap_based_on_language = match language_settings.allow_rewrap {
|
||||
RewrapBehavior::InComments => inside_comment,
|
||||
RewrapBehavior::InSelections => !range.is_empty(),
|
||||
RewrapBehavior::InSelections => !wrap_range.is_empty(),
|
||||
RewrapBehavior::Anywhere => true,
|
||||
};
|
||||
|
||||
|
@ -11636,7 +11645,7 @@ impl Editor {
|
|||
continue;
|
||||
}
|
||||
|
||||
if range.is_empty() {
|
||||
if from_empty_selection {
|
||||
'expand_upwards: while start_row > 0 {
|
||||
let prev_row = start_row - 1;
|
||||
if buffer.contains_str_at(Point::new(prev_row, 0), &line_prefix)
|
||||
|
|
|
@ -5149,6 +5149,7 @@ async fn test_rewrap(cx: &mut TestAppContext) {
|
|||
"Markdown".into(),
|
||||
LanguageSettingsContent {
|
||||
allow_rewrap: Some(language_settings::RewrapBehavior::Anywhere),
|
||||
preferred_line_length: Some(40),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
|
@ -5156,6 +5157,31 @@ async fn test_rewrap(cx: &mut TestAppContext) {
|
|||
"Plain Text".into(),
|
||||
LanguageSettingsContent {
|
||||
allow_rewrap: Some(language_settings::RewrapBehavior::Anywhere),
|
||||
preferred_line_length: Some(40),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
"C++".into(),
|
||||
LanguageSettingsContent {
|
||||
allow_rewrap: Some(language_settings::RewrapBehavior::InComments),
|
||||
preferred_line_length: Some(40),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
"Python".into(),
|
||||
LanguageSettingsContent {
|
||||
allow_rewrap: Some(language_settings::RewrapBehavior::InComments),
|
||||
preferred_line_length: Some(40),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
"Rust".into(),
|
||||
LanguageSettingsContent {
|
||||
allow_rewrap: Some(language_settings::RewrapBehavior::InComments),
|
||||
preferred_line_length: Some(40),
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
|
@ -5164,15 +5190,17 @@ async fn test_rewrap(cx: &mut TestAppContext) {
|
|||
|
||||
let mut cx = EditorTestContext::new(cx).await;
|
||||
|
||||
let language_with_c_comments = Arc::new(Language::new(
|
||||
let cpp_language = Arc::new(Language::new(
|
||||
LanguageConfig {
|
||||
name: "C++".into(),
|
||||
line_comments: vec!["// ".into()],
|
||||
..LanguageConfig::default()
|
||||
},
|
||||
None,
|
||||
));
|
||||
let language_with_pound_comments = Arc::new(Language::new(
|
||||
let python_language = Arc::new(Language::new(
|
||||
LanguageConfig {
|
||||
name: "Python".into(),
|
||||
line_comments: vec!["# ".into()],
|
||||
..LanguageConfig::default()
|
||||
},
|
||||
|
@ -5185,8 +5213,9 @@ async fn test_rewrap(cx: &mut TestAppContext) {
|
|||
},
|
||||
None,
|
||||
));
|
||||
let language_with_doc_comments = Arc::new(Language::new(
|
||||
let rust_language = Arc::new(Language::new(
|
||||
LanguageConfig {
|
||||
name: "Rust".into(),
|
||||
line_comments: vec!["// ".into(), "/// ".into()],
|
||||
..LanguageConfig::default()
|
||||
},
|
||||
|
@ -5201,296 +5230,220 @@ async fn test_rewrap(cx: &mut TestAppContext) {
|
|||
None,
|
||||
));
|
||||
|
||||
// Test basic rewrapping of a long line with a cursor
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
// ˇLorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor, eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis porttitor id. Aliquam id accumsan eros.
|
||||
// ˇThis is a long comment that needs to be wrapped.
|
||||
"},
|
||||
indoc! {"
|
||||
// ˇLorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit
|
||||
// purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus
|
||||
// auctor, eu lacinia sapien scelerisque. Vivamus sit amet neque et quam
|
||||
// tincidunt hendrerit. Praesent semper egestas tellus id dignissim.
|
||||
// Pellentesque odio lectus, iaculis ac volutpat et, blandit quis urna. Sed
|
||||
// vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam,
|
||||
// et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum
|
||||
// dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu
|
||||
// viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis
|
||||
// porttitor id. Aliquam id accumsan eros.
|
||||
// ˇThis is a long comment that needs to
|
||||
// be wrapped.
|
||||
"},
|
||||
language_with_c_comments.clone(),
|
||||
cpp_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that rewrapping works inside of a selection
|
||||
// Test rewrapping a full selection
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
«// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor, eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis porttitor id. Aliquam id accumsan eros.ˇ»
|
||||
"},
|
||||
«// This selected long comment needs to be wrapped.ˇ»"
|
||||
},
|
||||
indoc! {"
|
||||
«// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit
|
||||
// purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus
|
||||
// auctor, eu lacinia sapien scelerisque. Vivamus sit amet neque et quam
|
||||
// tincidunt hendrerit. Praesent semper egestas tellus id dignissim.
|
||||
// Pellentesque odio lectus, iaculis ac volutpat et, blandit quis urna. Sed
|
||||
// vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam,
|
||||
// et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum
|
||||
// dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu
|
||||
// viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis
|
||||
// porttitor id. Aliquam id accumsan eros.ˇ»
|
||||
"},
|
||||
language_with_c_comments.clone(),
|
||||
«// This selected long comment needs to
|
||||
// be wrapped.ˇ»"
|
||||
},
|
||||
cpp_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that cursors that expand to the same region are collapsed.
|
||||
// Test multiple cursors on different lines within the same paragraph are preserved after rewrapping
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
// ˇLorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
// ˇVivamus mollis elit purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor, eu lacinia sapien scelerisque.
|
||||
// ˇVivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et,
|
||||
// ˇblandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis porttitor id. Aliquam id accumsan eros.
|
||||
"},
|
||||
// ˇThis is the first line.
|
||||
// Thisˇ is the second line.
|
||||
// This is the thirdˇ line, all part of one paragraph.
|
||||
"},
|
||||
indoc! {"
|
||||
// ˇLorem ipsum dolor sit amet, consectetur adipiscing elit. ˇVivamus mollis elit
|
||||
// purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus
|
||||
// auctor, eu lacinia sapien scelerisque. ˇVivamus sit amet neque et quam
|
||||
// tincidunt hendrerit. Praesent semper egestas tellus id dignissim.
|
||||
// Pellentesque odio lectus, iaculis ac volutpat et, ˇblandit quis urna. Sed
|
||||
// vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam,
|
||||
// et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum
|
||||
// dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu
|
||||
// viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis
|
||||
// porttitor id. Aliquam id accumsan eros.
|
||||
"},
|
||||
language_with_c_comments.clone(),
|
||||
// ˇThis is the first line. Thisˇ is the
|
||||
// second line. This is the thirdˇ line,
|
||||
// all part of one paragraph.
|
||||
"},
|
||||
cpp_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that non-contiguous selections are treated separately.
|
||||
// Test multiple cursors in different paragraphs trigger separate rewraps
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
// ˇLorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
// ˇVivamus mollis elit purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor, eu lacinia sapien scelerisque.
|
||||
//
|
||||
// ˇVivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et,
|
||||
// ˇblandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis porttitor id. Aliquam id accumsan eros.
|
||||
// ˇThis is the first paragraph, first line.
|
||||
// ˇThis is the first paragraph, second line.
|
||||
|
||||
// ˇThis is the second paragraph, first line.
|
||||
// ˇThis is the second paragraph, second line.
|
||||
"},
|
||||
indoc! {"
|
||||
// ˇLorem ipsum dolor sit amet, consectetur adipiscing elit. ˇVivamus mollis elit
|
||||
// purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus
|
||||
// auctor, eu lacinia sapien scelerisque.
|
||||
//
|
||||
// ˇVivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas
|
||||
// tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et,
|
||||
// ˇblandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec
|
||||
// molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque
|
||||
// nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras egestas
|
||||
// porta metus, eu viverra ipsum efficitur quis. Donec luctus eros turpis, id
|
||||
// vulputate turpis porttitor id. Aliquam id accumsan eros.
|
||||
// ˇThis is the first paragraph, first
|
||||
// line. ˇThis is the first paragraph,
|
||||
// second line.
|
||||
|
||||
// ˇThis is the second paragraph, first
|
||||
// line. ˇThis is the second paragraph,
|
||||
// second line.
|
||||
"},
|
||||
language_with_c_comments.clone(),
|
||||
cpp_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that different comment prefixes are supported.
|
||||
// Test that change in comment prefix (e.g., `//` to `///`) trigger seperate rewraps
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
# ˇLorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor, eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque nisi. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras egestas porta metus, eu viverra ipsum efficitur quis. Donec luctus eros turpis, id vulputate turpis porttitor id. Aliquam id accumsan eros.
|
||||
"},
|
||||
«// A regular long long comment to be wrapped.
|
||||
/// A documentation long comment to be wrapped.ˇ»
|
||||
"},
|
||||
indoc! {"
|
||||
# ˇLorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit
|
||||
# purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor,
|
||||
# eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt
|
||||
# hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio
|
||||
# lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit
|
||||
# amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet
|
||||
# in. Integer sit amet scelerisque nisi. Lorem ipsum dolor sit amet, consectetur
|
||||
# adipiscing elit. Cras egestas porta metus, eu viverra ipsum efficitur quis.
|
||||
# Donec luctus eros turpis, id vulputate turpis porttitor id. Aliquam id
|
||||
# accumsan eros.
|
||||
"},
|
||||
language_with_pound_comments.clone(),
|
||||
«// A regular long long comment to be
|
||||
// wrapped.
|
||||
/// A documentation long comment to be
|
||||
/// wrapped.ˇ»
|
||||
"},
|
||||
rust_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that rewrapping is ignored outside of comments in most languages.
|
||||
// Test that change in indentation level trigger seperate rewraps
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
/// Adds two numbers.
|
||||
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae.ˇ
|
||||
fn add(a: u32, b: u32) -> u32 {
|
||||
a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + bˇ
|
||||
fn foo() {
|
||||
«// This is a long comment at the base indent.
|
||||
// This is a long comment at the next indent.ˇ»
|
||||
}
|
||||
"},
|
||||
indoc! {"
|
||||
/// Adds two numbers. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
/// Vivamus mollis elit purus, a ornare lacus gravida vitae.ˇ
|
||||
fn add(a: u32, b: u32) -> u32 {
|
||||
a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + b + a + bˇ
|
||||
fn foo() {
|
||||
«// This is a long comment at the
|
||||
// base indent.
|
||||
// This is a long comment at the
|
||||
// next indent.ˇ»
|
||||
}
|
||||
"},
|
||||
language_with_doc_comments.clone(),
|
||||
rust_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that rewrapping works in Markdown and Plain Text languages.
|
||||
// Test that different comment prefix characters (e.g., '#') are handled correctly
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
# Hello
|
||||
|
||||
Lorem ipsum dolor sit amet, ˇconsectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor, eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque nisi.
|
||||
# ˇThis is a long comment using a pound sign.
|
||||
"},
|
||||
indoc! {"
|
||||
# Hello
|
||||
|
||||
Lorem ipsum dolor sit amet, ˇconsectetur adipiscing elit. Vivamus mollis elit
|
||||
purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor,
|
||||
eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt
|
||||
hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio
|
||||
lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit amet
|
||||
nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in.
|
||||
Integer sit amet scelerisque nisi.
|
||||
# ˇThis is a long comment using a pound
|
||||
# sign.
|
||||
"},
|
||||
python_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test rewrapping only affects comments, not code even when selected
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
«/// This doc comment is long and should be wrapped.
|
||||
fn my_func(a: u32, b: u32, c: u32, d: u32, e: u32, f: u32) {}ˇ»
|
||||
"},
|
||||
indoc! {"
|
||||
«/// This doc comment is long and should
|
||||
/// be wrapped.
|
||||
fn my_func(a: u32, b: u32, c: u32, d: u32, e: u32, f: u32) {}ˇ»
|
||||
"},
|
||||
rust_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that rewrapping works in Markdown documents where `allow_rewrap` is `Anywhere`
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
# Header
|
||||
|
||||
A long long long line of markdown text to wrap.ˇ
|
||||
"},
|
||||
indoc! {"
|
||||
# Header
|
||||
|
||||
A long long long line of markdown text
|
||||
to wrap.ˇ
|
||||
"},
|
||||
markdown_language,
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that rewrapping works in plain text where `allow_rewrap` is `Anywhere`
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
Lorem ipsum dolor sit amet, ˇconsectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor, eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit amet nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in. Integer sit amet scelerisque nisi.
|
||||
ˇThis is a very long line of plain text that will be wrapped.
|
||||
"},
|
||||
indoc! {"
|
||||
Lorem ipsum dolor sit amet, ˇconsectetur adipiscing elit. Vivamus mollis elit
|
||||
purus, a ornare lacus gravida vitae. Proin consectetur felis vel purus auctor,
|
||||
eu lacinia sapien scelerisque. Vivamus sit amet neque et quam tincidunt
|
||||
hendrerit. Praesent semper egestas tellus id dignissim. Pellentesque odio
|
||||
lectus, iaculis ac volutpat et, blandit quis urna. Sed vestibulum nisi sit amet
|
||||
nisl venenatis tempus. Donec molestie blandit quam, et porta nunc laoreet in.
|
||||
Integer sit amet scelerisque nisi.
|
||||
ˇThis is a very long line of plain text
|
||||
that will be wrapped.
|
||||
"},
|
||||
plaintext_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test rewrapping unaligned comments in a selection.
|
||||
// Test that non-commented code acts as a paragraph boundary within a selection
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
fn foo() {
|
||||
if true {
|
||||
« // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae.
|
||||
// Praesent semper egestas tellus id dignissim.ˇ»
|
||||
do_something();
|
||||
} else {
|
||||
//
|
||||
}
|
||||
}
|
||||
"},
|
||||
«// This is the first long comment block to be wrapped.
|
||||
fn my_func(a: u32);
|
||||
// This is the second long comment block to be wrapped.ˇ»
|
||||
"},
|
||||
indoc! {"
|
||||
fn foo() {
|
||||
if true {
|
||||
« // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus
|
||||
// mollis elit purus, a ornare lacus gravida vitae. Praesent semper
|
||||
// egestas tellus id dignissim.ˇ»
|
||||
do_something();
|
||||
} else {
|
||||
//
|
||||
}
|
||||
}
|
||||
"},
|
||||
language_with_doc_comments.clone(),
|
||||
«// This is the first long comment block
|
||||
// to be wrapped.
|
||||
fn my_func(a: u32);
|
||||
// This is the second long comment block
|
||||
// to be wrapped.ˇ»
|
||||
"},
|
||||
rust_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test rewrapping multiple selections, including ones with blank lines or tabs
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
fn foo() {
|
||||
if true {
|
||||
«ˇ // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis elit purus, a ornare lacus gravida vitae.
|
||||
// Praesent semper egestas tellus id dignissim.»
|
||||
do_something();
|
||||
} else {
|
||||
//
|
||||
}
|
||||
«ˇThis is a very long line that will be wrapped.
|
||||
|
||||
}
|
||||
"},
|
||||
This is another paragraph in the same selection.»
|
||||
|
||||
«\tThis is a very long indented line that will be wrapped.ˇ»
|
||||
"},
|
||||
indoc! {"
|
||||
fn foo() {
|
||||
if true {
|
||||
«ˇ // Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus
|
||||
// mollis elit purus, a ornare lacus gravida vitae. Praesent semper
|
||||
// egestas tellus id dignissim.»
|
||||
do_something();
|
||||
} else {
|
||||
//
|
||||
}
|
||||
«ˇThis is a very long line that will be
|
||||
wrapped.
|
||||
|
||||
}
|
||||
"},
|
||||
language_with_doc_comments.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
This is another paragraph in the same
|
||||
selection.»
|
||||
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
«ˇone one one one one one one one one one one one one one one one one one one one one one one one one
|
||||
|
||||
two»
|
||||
|
||||
three
|
||||
|
||||
«ˇ\t
|
||||
|
||||
four four four four four four four four four four four four four four four four four four four four»
|
||||
|
||||
«ˇfive five five five five five five five five five five five five five five five five five five five
|
||||
\t»
|
||||
six six six six six six six six six six six six six six six six six six six six six six six six six
|
||||
"},
|
||||
indoc! {"
|
||||
«ˇone one one one one one one one one one one one one one one one one one one one
|
||||
one one one one one
|
||||
|
||||
two»
|
||||
|
||||
three
|
||||
|
||||
«ˇ\t
|
||||
|
||||
four four four four four four four four four four four four four four four four
|
||||
four four four four»
|
||||
|
||||
«ˇfive five five five five five five five five five five five five five five five
|
||||
five five five five
|
||||
\t»
|
||||
six six six six six six six six six six six six six six six six six six six six six six six six six
|
||||
"},
|
||||
«\tThis is a very long indented line
|
||||
\tthat will be wrapped.ˇ»
|
||||
"},
|
||||
plaintext_language.clone(),
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
// Test that an empty comment line acts as a paragraph boundary
|
||||
assert_rewrap(
|
||||
indoc! {"
|
||||
//ˇ long long long long long long long long long long long long long long long long long long long long long long long long long long long long
|
||||
//ˇ
|
||||
//ˇ long long long long long long long long long long long long long long long long long long long long long long long long long long long long
|
||||
//ˇ short short short
|
||||
int main(void) {
|
||||
return 17;
|
||||
}
|
||||
"},
|
||||
// ˇThis is a long comment that will be wrapped.
|
||||
//
|
||||
// And this is another long comment that will also be wrapped.ˇ
|
||||
"},
|
||||
indoc! {"
|
||||
//ˇ long long long long long long long long long long long long long long long
|
||||
// long long long long long long long long long long long long long
|
||||
//ˇ
|
||||
//ˇ long long long long long long long long long long long long long long long
|
||||
//ˇ long long long long long long long long long long long long long short short
|
||||
// short
|
||||
int main(void) {
|
||||
return 17;
|
||||
}
|
||||
"},
|
||||
language_with_c_comments,
|
||||
// ˇThis is a long comment that will be
|
||||
// wrapped.
|
||||
//
|
||||
// And this is another long comment that
|
||||
// will also be wrapped.ˇ
|
||||
"},
|
||||
cpp_language,
|
||||
&mut cx,
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue