fix: fmt crashing with comment at end of file

closes #568
This commit is contained in:
rvcas 2023-06-07 15:59:54 -04:00
parent feb2698e5e
commit 1747090931
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 18 additions and 1 deletions

View File

@ -24,7 +24,7 @@ pub struct Comment<'a> {
impl<'a> From<(&Span, &'a str)> for Comment<'a> {
fn from(src: (&Span, &'a str)) -> Comment<'a> {
fn char_indice(s: &str, i: usize) -> usize {
s.char_indices().nth(i).expect("char at given indice").0
s.char_indices().nth(i).unwrap_or((i, ' ')).0
}
let start = char_indice(src.1, src.0.start);

View File

@ -17,6 +17,23 @@ fn assert_fmt(src: &str, expected: &str) {
assert_eq!(out, out2, "formatting isn't idempotent");
}
#[test]
fn comment_at_end_of_file() {
let input = indoc! { r#"
type Foo =
Int
//"#};
let output = indoc! { r#"
type Foo =
Int
//
"#};
assert_fmt(input, output);
}
#[test]
fn test_format_if() {
let src = indoc! {r#"