Skip to content

Commit

Permalink
Fix panic in forc-fmt with special chars (#5014)
Browse files Browse the repository at this point in the history
## Description

Closes #5013

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
  • Loading branch information
sdankel and JoshuaBatty authored Aug 28, 2023
1 parent 218f0da commit 0c045be
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
21 changes: 11 additions & 10 deletions swayfmt/src/comments.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use ropey::Rope;
use std::{fmt::Write, ops::Range};
use sway_ast::token::{Comment, CommentKind};
use sway_types::{Span, Spanned};

use crate::{
formatter::FormattedCode,
parse::parse_snippet,
Expand All @@ -12,6 +7,10 @@ use crate::{
},
Format, Formatter, FormatterError,
};
use ropey::Rope;
use std::{fmt::Write, ops::Range};
use sway_ast::token::{Comment, CommentKind};
use sway_types::{Span, Spanned};

pub type UnformattedCode = String;

Expand Down Expand Up @@ -151,9 +150,7 @@ pub fn rewrite_with_comments<T: sway_parse::Parse + Format + LeafSpans>(
let mut offset = 0;
let mut to_rewrite = formatted_code[last_formatted..].to_string();

let formatted_leaf_spans = parse_snippet::<T>(&formatted_code[last_formatted..])
.unwrap()
.leaf_spans();
let formatted_leaf_spans = parse_snippet::<T>(&formatted_code[last_formatted..])?.leaf_spans();

let mut previous_unformatted_leaf_span = unformatted_leaf_spans
.first()
Expand Down Expand Up @@ -352,13 +349,17 @@ fn insert_after_span(
};

// Insert the actual comment(s).
src_rope.insert(from.end + offset, &comment_str);
src_rope
.try_insert(from.end + offset, &comment_str)
.map_err(|_| FormatterError::CommentError)?;

formatted_code.clear();
formatted_code.push_str(&src_rope.to_string());
}

Ok(comment_str.len())
// In order to handle special characters, we return the number of characters rather than
// the size of the string.
Ok(comment_str.chars().count())
}

#[cfg(test)]
Expand Down
12 changes: 12 additions & 0 deletions swayfmt/src/items/item_fn/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,15 @@ intermediate_whitespace
}
}"
);

fmt_test_item!(fn_comments_special_chars
"fn comments_special_chars() {
// this ↓↓↓↓↓
let val = 1; // this is a normal comment
}",
intermediate_whitespace
"fn comments_special_chars() {
// this ↓↓↓↓↓
let val = 1; // this is a normal comment
}"
);
5 changes: 2 additions & 3 deletions swayfmt/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ pub fn parse_format<P: sway_parse::Parse + crate::Format>(
let parsed = with_handler(|handler| {
let token_stream = sway_parse::lex(handler, &input.into(), 0, input.len(), None)?;
sway_parse::Parser::new(handler, &token_stream).parse::<P>()
})
.unwrap();
})?;

// Allow test cases that include comments.
let mut formatter = Formatter::default();
formatter.with_comments_context(input)?;

let mut buf = <_>::default();
parsed.format(&mut buf, &mut formatter).unwrap();
parsed.format(&mut buf, &mut formatter)?;
Ok(buf)
}

Expand Down
6 changes: 5 additions & 1 deletion swayfmt/src/utils/map/newline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ fn insert_after_span(
format_newline_sequence(&newline_sequence, threshold)
)?;
let mut src_rope = Rope::from_str(formatted_code);
src_rope.insert(at, &sequence_string);

src_rope
.try_insert(at, &sequence_string)
.map_err(|_| FormatterError::NewlineSequenceError)?;

formatted_code.clear();
formatted_code.push_str(&src_rope.to_string());
Ok(sequence_string.len())
Expand Down

0 comments on commit 0c045be

Please sign in to comment.