Skip to content

Commit

Permalink
Removed use of destructive method
Browse files Browse the repository at this point in the history
  • Loading branch information
cr-fuel committed Oct 11, 2023
1 parent e2d0db4 commit a8b8e03
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
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()
}

#[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)?;
}
Self::close_curly_brace(formatted_code, formatter)?;
}
Expand Down
20 changes: 9 additions & 11 deletions swayfmt/src/items/item_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
comments::{rewrite_with_comments, write_comments},
comments::{has_comments_in_formatter, rewrite_with_comments, write_comments},
config::items::ItemBraceStyle,
constants::NEW_LINE,
formatter::*,
utils::{
map::byte_span::{ByteSpan, LeafSpans},
Expand Down Expand Up @@ -42,17 +41,17 @@ impl Format for ItemImpl {

let contents = self.contents.get();
if contents.is_empty() {
let range = self.span().into();
Self::open_curly_brace(formatted_code, formatter)?;
let comments = write_comments(formatted_code, self.span().into(), formatter)?;
if !comments {
formatter.unindent();
if formatted_code.ends_with(NEW_LINE) {
formatted_code.truncate(formatted_code.len() - NEW_LINE.len());
}
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)?;
Expand Down Expand Up @@ -93,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 @@ -102,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

0 comments on commit a8b8e03

Please sign in to comment.