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 new lines into empty implementation #5176

Merged
merged 5 commits into from
Oct 12, 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
12 changes: 12 additions & 0 deletions swayfmt/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ impl CommentsContext {
}
}

#[inline]
pub fn has_comments_in_formatter(formatter: &Formatter, range: &Range<usize>) -> bool {
formatter
.comments_context
.map
.comments_between(range)
.peekable()
.peek()
.is_some()
}

eureka-cpu marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn has_comments<I: Iterator>(comments: I) -> bool {
comments.peekable().peek().is_some()
}
Expand Down
10 changes: 5 additions & 5 deletions swayfmt/src/items/item_fn/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
comments::{rewrite_with_comments, write_comments},
comments::{has_comments_in_formatter, rewrite_with_comments, write_comments},
config::items::ItemBraceStyle,
formatter::{
shape::{ExprKind, LineStyle},
Expand Down Expand Up @@ -51,11 +51,11 @@ impl Format for ItemFn {

Self::close_curly_brace(formatted_code, formatter)?;
} else {
let range = self.span().into();
Self::open_curly_brace(formatted_code, formatter)?;
formatter.indent();
let comments = write_comments(formatted_code, self.span().into(), formatter)?;
if !comments {
formatter.unindent();
if has_comments_in_formatter(formatter, &range) {
formatter.indent();
write_comments(formatted_code, range, formatter)?;
eureka-cpu marked this conversation as resolved.
Show resolved Hide resolved
}
Self::close_curly_brace(formatted_code, formatter)?;
}
Expand Down
30 changes: 21 additions & 9 deletions swayfmt/src/items/item_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
comments::rewrite_with_comments,
comments::{has_comments_in_formatter, rewrite_with_comments, write_comments},
config::items::ItemBraceStyle,
formatter::*,
utils::{
Expand Down Expand Up @@ -38,14 +38,27 @@ impl Format for ItemImpl {
where_clause.format(formatted_code, formatter)?;
formatter.shape.code_line.update_where_clause(true);
}
Self::open_curly_brace(formatted_code, formatter)?;

let contents = self.contents.get();
for item in contents.iter() {
write!(formatted_code, "{}", formatter.indent_to_str()?,)?;
item.format(formatted_code, formatter)?;
if contents.is_empty() {
let range = self.span().into();
Self::open_curly_brace(formatted_code, formatter)?;
if has_comments_in_formatter(formatter, &range) {
formatter.indent();
write_comments(formatted_code, range, formatter)?;
}
Self::close_curly_brace(formatted_code, formatter)?;
} else {
Self::open_curly_brace(formatted_code, formatter)?;
formatter.indent();
writeln!(formatted_code)?;
for item in contents.iter() {
write!(formatted_code, "{}", formatter.indent_to_str()?,)?;
item.format(formatted_code, formatter)?;
writeln!(formatted_code)?;
}
Self::close_curly_brace(formatted_code, formatter)?;
}
Self::close_curly_brace(formatted_code, formatter)?;

rewrite_with_comments::<ItemImpl>(
formatter,
Expand Down Expand Up @@ -79,7 +92,6 @@ impl CurlyBrace for ItemImpl {
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
let brace_style = formatter.config.items.item_brace_style;
formatter.indent();
let open_brace = Delimiter::Brace.as_open_char();
match brace_style {
ItemBraceStyle::AlwaysNextLine => {
Expand All @@ -88,11 +100,11 @@ impl CurlyBrace for ItemImpl {
}
ItemBraceStyle::SameLineWhere => match formatter.shape.code_line.has_where_clause {
true => {
writeln!(line, "{open_brace}")?;
write!(line, "{open_brace}")?;
formatter.shape.code_line.update_where_clause(false);
}
false => {
writeln!(line, " {open_brace}")?;
write!(line, " {open_brace}")?;
}
},
_ => {
Expand Down
14 changes: 14 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,3 +1702,17 @@ pub enum Input {
"#,
);
}

#[test]
fn empty_impl() {
check(
r#"
library;
impl OrdEq for u256 {
}
"#,
r#"library;
impl OrdEq for u256 {}
"#,
);
}
Loading