Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::config::{Config, IndentStyle};
use crate::rewrite::{ExceedsMaxWidthError, RewriteContext, RewriteError, RewriteResult};
use crate::shape::{Indent, Shape};
use crate::utils::{
count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline,
unicode_str_width,
count_newlines, first_line_width, last_line_unindented_width, last_line_width, mk_sp,
starts_with_newline, unicode_str_width,
};
use crate::visitor::SnippetProvider;

Expand Down Expand Up @@ -468,8 +468,13 @@ where

if !starts_with_newline(comment) {
if formatting.align_comments {
// If it's a multiline inner item, the length of indentation shouldn't
// be counted when evaluating line length with respect to comments.
let inner_item_width = last_line_unindented_width(&inner_item)
.unwrap_or_else(|| unicode_str_width(inner_item));

let mut comment_alignment =
post_comment_alignment(item_max_width, unicode_str_width(inner_item));
post_comment_alignment(item_max_width, inner_item_width);
if first_line_width(&formatted_comment)
+ last_line_width(&result)
+ comment_alignment
Expand Down
8 changes: 8 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ pub(crate) fn last_line_width(s: &str) -> usize {
unicode_str_width(s.rsplitn(2, '\n').next().unwrap_or(""))
}

#[inline]
pub(crate) fn last_line_unindented_width(s: &str) -> Option<usize> {
s.rsplitn(2, '\n').next().map(|s| {
let trimmed = s.trim_start();
unicode_str_width(trimmed)
})
}

/// The total used width of the last line.
#[inline]
pub(crate) fn last_line_used_width(s: &str, offset: usize) -> usize {
Expand Down
9 changes: 9 additions & 0 deletions tests/source/issue-6060/line_split.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn dummy() {
let error_code = match err {
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
msgs::DecodeError::UnknownRequiredFeature
| msgs::DecodeError::InvalidValue
| msgs::DecodeError::ShortReadNowExceedesMaxLineWidthAndTriggersAnotherBranch => 0x4000 | 22, // invalid_onion
_ => 0x2000 | 2, // Should never happen
};
}
11 changes: 11 additions & 0 deletions tests/target/issue-6060/line_split.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn dummy() {
let error_code = match err {
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
msgs::DecodeError::UnknownRequiredFeature
| msgs::DecodeError::InvalidValue
| msgs::DecodeError::ShortReadNowExceedesMaxLineWidthAndTriggersAnotherBranch => {
0x4000 | 22
} // invalid_onion
_ => 0x2000 | 2, // Should never happen
};
}
9 changes: 9 additions & 0 deletions tests/target/issue-6060/no_line_split.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn dummy() {
let error_code = match err {
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
msgs::DecodeError::UnknownRequiredFeature
| msgs::DecodeError::InvalidValue
| msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion
_ => 0x2000 | 2, // Should never happen
};
}
Loading