Skip to content

Commit

Permalink
fix(fmt): char counting (#4554)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk committed Mar 14, 2023
1 parent 1e0b628 commit bb62e3c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
24 changes: 15 additions & 9 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl<'a, W: Write> Formatter<'a, W> {
self.config.line_length >=
self.total_indent_len()
.saturating_add(self.current_line_len())
.saturating_add(text.len() + space)
.saturating_add(text.chars().count() + space)
}

/// Write empty brackets with respect to `config.bracket_spacing` setting:
Expand Down Expand Up @@ -470,15 +470,21 @@ impl<'a, W: Write> Formatter<'a, W> {

write!(self.buf(), "{}", comment.start_token())?;

let mut lines = comment.contents().lines().peekable();
let mut wrapped = false;
let contents = comment.contents();
let mut lines = contents.lines().peekable();
while let Some(line) = lines.next() {
self.write_comment_line(comment, line)?;
wrapped |= self.write_comment_line(comment, line)?;
if lines.peek().is_some() {
self.write_preserved_line()?;
}
}

if let Some(end) = comment.end_token() {
// Check if the end token in the original comment was on the separate line
if !wrapped && comment.comment.lines().count() > contents.lines().count() {
self.write_preserved_line()?;
}
write!(self.buf(), "{end}")?;
}
if self.find_next_line(comment.loc.end()).is_some() {
Expand Down Expand Up @@ -549,14 +555,14 @@ impl<'a, W: Write> Formatter<'a, W> {
}

/// Write a comment line that might potentially overflow the maximum line length
/// and, if configured, will be wrapped to the next line
fn write_comment_line(&mut self, comment: &CommentWithMetadata, line: &str) -> Result<()> {
/// and, if configured, will be wrapped to the next line.
fn write_comment_line(&mut self, comment: &CommentWithMetadata, line: &str) -> Result<bool> {
if self.will_it_fit(line) || !self.config.wrap_comments {
let start_with_ws =
line.chars().next().map(|ch| ch.is_whitespace()).unwrap_or_default();
if !self.is_beginning_of_line() || !start_with_ws {
write!(self.buf(), "{line}")?;
return Ok(())
return Ok(false)
}

// if this is the beginning of the line,
Expand All @@ -568,7 +574,7 @@ impl<'a, W: Write> Formatter<'a, W> {
.map(|(_, ch)| ch);
let padded = format!("{}{}", " ".repeat(indent), chars.join(""));
self.write_raw(padded)?;
return Ok(())
return Ok(false)
}

let mut words = line.split(' ').peekable();
Expand All @@ -587,13 +593,13 @@ impl<'a, W: Write> Formatter<'a, W> {
// write newline wrap token
write!(self.buf(), "{}", comment.wrap_token())?;
self.write_comment_line(comment, &words.join(" "))?;
return Ok(())
return Ok(true)
}

self.write_whitespace_separator(false)?;
}
}
Ok(())
Ok(false)
}

/// Write a raw comment. This is like [`write_comment`] but won't do any formatting or worry
Expand Down
11 changes: 11 additions & 0 deletions fmt/testdata/SimpleComments/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ contract SimpleComments {
}
}

/*
██████╗ ██████╗ ██████╗ ████████╗███████╗███████╗████████╗
██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝
██████╔╝██████╔╝██████╔╝ ██║ █████╗ ███████╗ ██║
██╔═══╝ ██╔══██╗██╔══██╗ ██║ ██╔══╝ ╚════██║ ██║
██║ ██║ ██║██████╔╝ ██║ ███████╗███████║ ██║
╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝
*/
function asciiArt() {}

/*
* @notice Here is my comment
* - item 1
Expand Down
11 changes: 11 additions & 0 deletions fmt/testdata/SimpleComments/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ contract SimpleComments {
}
}

/*
██████╗ ██████╗ ██████╗ ████████╗███████╗███████╗████████╗
██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝
██████╔╝██████╔╝██████╔╝ ██║ █████╗ ███████╗ ██║
██╔═══╝ ██╔══██╗██╔══██╗ ██║ ██╔══╝ ╚════██║ ██║
██║ ██║ ██║██████╔╝ ██║ ███████╗███████║ ██║
╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝
*/
function asciiArt() {}

/*
* @notice Here is my comment
* - item 1
Expand Down
11 changes: 11 additions & 0 deletions fmt/testdata/SimpleComments/wrap-comments.fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ contract SimpleComments {
}
}

/*
██████╗ ██████╗ ██████╗ ████████╗███████╗███████╗████████╗
██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝
██████╔╝██████╔╝██████╔╝ ██║ █████╗ ███████╗ ██║
██╔═══╝ ██╔══██╗██╔══██╗ ██║ ██╔══╝ ╚════██║ ██║
██║ ██║ ██║██████╔╝ ██║ ███████╗███████║ ██║
╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝
*/
function asciiArt() {}

/*
* @notice Here is my comment
* - item 1
Expand Down

0 comments on commit bb62e3c

Please sign in to comment.