Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with formatting imports with comments #5853

Merged
merged 3 commits into from
Aug 31, 2023
Merged
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
6 changes: 4 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl UseTree {

// Normalise foo::{bar} -> foo::bar
if let UseSegmentKind::List(ref list) = last.kind {
if list.len() == 1 && list[0].to_string() != "self" {
if list.len() == 1 && list[0].to_string() != "self" && !list[0].has_comment() {
normalize_sole_list = true;
}
}
Expand Down Expand Up @@ -1032,7 +1032,9 @@ fn rewrite_nested_use_tree(

let list_str = write_list(&list_items, &fmt)?;

let result = if (list_str.contains('\n') || list_str.len() > remaining_width)
let result = if (list_str.contains('\n')
|| list_str.len() > remaining_width
|| tactic == DefinitiveListTactic::Vertical)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you get a chance can you explain why we need to add the tactic == DefinitiveListTactic::Vertical) check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to resolve the second problem shown in the issue. The problem is that it will try to put in on one line because the list doesn't contain a newline (because there is only one actual item, and the newlines are only between items).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. It looks like tactic is somewhat influenced by the user's specified value for imports_layout. Out of an abundance of caution can you make sure that this fix holds for all values of imports_layout. Probably best to create a issue-5852 directory and add a test case for each imports_layout variant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ytmimi sorry for the delayed response, but it should be fixed now

&& context.config.imports_indent() == IndentStyle::Block
{
format!(
Expand Down
2 changes: 1 addition & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ pub(crate) fn extract_post_comment(
post_snippet.trim_matches(white_space)
}
// not comment or over two lines
else if post_snippet.ends_with(',')
else if post_snippet.ends_with(separator)
&& (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n'))
{
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
Expand Down
12 changes: 12 additions & 0 deletions tests/source/issue-3984.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use a::{item /* comment */};
use b::{
a,
// comment
item,
};
use c::item /* comment */;
use d::item; // really long comment (with `use` exactly 100 characters) ____________________________

use std::e::{/* it's a comment! */ bar /* and another */};
use std::f::{/* it's a comment! */ bar};
use std::g::{bar /* and another */};
104 changes: 104 additions & 0 deletions tests/source/issue-5852/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::{
fs,
// (temporarily commented, we'll need this again in a second) io,
};

use foo::{
self // this is important
};

use foo :: bar
;

use foo::{bar};

use foo::{
bar
// abc
};

use foo::{
bar,
// abc
};

use foo::{
// 345
bar
};

use foo::{
self
// abc
};

use foo::{
self,
// abc
};

use foo::{
// 345
self
};

use foo::{
self // a
,
};

use foo::{ self /* a */ };

use foo::{ self /* a */, };

use foo::{
// abc
abc::{
xyz
// 123
}
};

use foo::{
// abc
bar,
abc
};

use foo::{
bar,
// abc
abc
};

use foo::{
bar,
abc
// abc
};

use foo::{
bar,
abc,
// abc
};

use foo::{
self,
// abc
abc::{
xyz
// 123
}
};

use foo::{
self,
// abc
abc::{
// 123
xyz
}
};

use path::{self /*comment*/,};
106 changes: 106 additions & 0 deletions tests/source/issue-5852/horizontal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// rustfmt-imports_layout: Horizontal

use std::{
fs,
// (temporarily commented, we'll need this again in a second) io,
};

use foo::{
self // this is important
};

use foo :: bar
;

use foo::{bar};

use foo::{
bar
// abc
};

use foo::{
bar,
// abc
};

use foo::{
// 345
bar
};

use foo::{
self
// abc
};

use foo::{
self,
// abc
};

use foo::{
// 345
self
};

use foo::{
self // a
,
};

use foo::{ self /* a */ };

use foo::{ self /* a */, };

use foo::{
// abc
abc::{
xyz
// 123
}
};

use foo::{
// abc
bar,
abc
};

use foo::{
bar,
// abc
abc
};

use foo::{
bar,
abc
// abc
};

use foo::{
bar,
abc,
// abc
};

use foo::{
self,
// abc
abc::{
xyz
// 123
}
};

use foo::{
self,
// abc
abc::{
// 123
xyz
}
};

use path::{self /*comment*/,};
106 changes: 106 additions & 0 deletions tests/source/issue-5852/horizontal_vertical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// rustfmt-imports_layout: HorizontalVertical

use std::{
fs,
// (temporarily commented, we'll need this again in a second) io,
};

use foo::{
self // this is important
};

use foo :: bar
;

use foo::{bar};

use foo::{
bar
// abc
};

use foo::{
bar,
// abc
};

use foo::{
// 345
bar
};

use foo::{
self
// abc
};

use foo::{
self,
// abc
};

use foo::{
// 345
self
};

use foo::{
self // a
,
};

use foo::{ self /* a */ };

use foo::{ self /* a */, };

use foo::{
// abc
abc::{
xyz
// 123
}
};

use foo::{
// abc
bar,
abc
};

use foo::{
bar,
// abc
abc
};

use foo::{
bar,
abc
// abc
};

use foo::{
bar,
abc,
// abc
};

use foo::{
self,
// abc
abc::{
xyz
// 123
}
};

use foo::{
self,
// abc
abc::{
// 123
xyz
}
};

use path::{self /*comment*/,};
8 changes: 8 additions & 0 deletions tests/source/issue-5852/issue_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::{
fs,
// (temporarily commented, we'll need this again in a second) io,
};

use foo::{
self // this is important
};
Loading
Loading