Skip to content

Commit

Permalink
fix some edge cases with deflayer formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rszyma committed Jan 15, 2024
1 parent 5e07f95 commit 72addc6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
5 changes: 0 additions & 5 deletions kls/src/formatter/ext_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ impl ExtParseTree {

impl Display for ExtParseTree {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let start = helpers::now();
write!(f, "{}", &self.0)?;
log!(
"display for ExtParseTree in {:.3?}",
helpers::now().duration_since(start)
);
Ok(())
}
}
Expand Down
57 changes: 30 additions & 27 deletions kls/src/formatter/use_defsrc_layout_on_deflayers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,22 @@ impl ExtParseTree {
// in previous operations on tree.
for metadata in &defsrc_item.post_metadata {
match metadata {
Metadata::Comment(_) => {
log!("formatting with comments in `defsrc` is unsupported for now");
Metadata::Comment(comment) => {
if let Comment::LineComment(_) = comment {
layout[i].push(0);
line_num += 1;
}
}
Metadata::Whitespace(whitespace) => {
let mut line_num: usize = 0;
for ch in whitespace.chars() {
match ch {
'\n' => {
layout[i].push(0);
line_num += 1;
}
'\t' => layout[i][line_num] += tab_size as usize,
_ => layout[i][line_num] += 1_usize,
' ' => layout[i][line_num] += 1_usize,
_ => unreachable!(),
}
}
}
Expand Down Expand Up @@ -232,19 +235,17 @@ fn collect_comments_into_metadata_vec(

for (i, comment) in comments.iter().enumerate() {
let is_last_comment: bool = i + 1 == comments.len();
result.push(Metadata::Comment(comment.deref().clone()));
result.push(Metadata::Comment(comment.clone().clone()));
match comment {
Comment::LineComment(_) => {
if is_last_comment {
if !is_the_last_expr_in_deflayer {
result.push(Metadata::Whitespace(" ".to_string()));
}
} else {
if !is_the_last_expr_in_deflayer {
result.push(Metadata::Whitespace(" ".repeat(indent)));
}
}
Comment::BlockComment(_) => {
result.push(Metadata::Whitespace(" ".to_string()));
if !is_the_last_expr_in_deflayer {
result.push(Metadata::Whitespace(" ".to_string()));
}
}
};
}
Expand Down Expand Up @@ -310,15 +311,15 @@ mod tests {
formats_correctly(
"(defsrc \n 1 2\n) (deflayer base 1 2 ) ( deflayer\n\t layer2 \n\n3 \t \n \t4\n )",
"(defsrc \n 1 2\n) (deflayer base 1 2\n) ( deflayer\n\t layer2 \n\n3 4\n)",
)
);
}

#[test]
fn only_deflayer_blocks_get_formatted() {
formats_correctly(
"(defsrc \n 1 2\n) (\ndefalias\n\ta b\n) (deflayer base 1 2 ) ( deflayer \n\t layer2 \n\n3 4 )",
"(defsrc \n 1 2\n) (\ndefalias\n\ta b\n) (deflayer base 1 2\n) ( deflayer \n\t layer2 \n\n3 4\n)",
)
);
}

#[test]
Expand All @@ -327,28 +328,28 @@ mod tests {
formats_correctly(
"(defsrc \n 1 2\n) (deflayer wrong 1 2 3) ( deflayer\n\t right \n\n3 4 )",
"(defsrc \n 1 2\n) (deflayer wrong 1 2 3) ( deflayer\n\t right \n\n3 4\n)",
)
);
}

#[test]
fn multi_byte_unicode_chars() {
formats_correctly(
"(defsrc \n 0 1 2\n) (deflayer base 🌍 1 \n 2 \t)",
"(defsrc \n 0 1 2\n) (deflayer base 🌍 1 2\n)",
)
);
}

#[test]
fn multi_cluster_unicode_chars() {
formats_correctly(
"(defsrc \n 0 1 2\n) (deflayer base 👨‍👩‍👧‍👦 \t 1 2 \n\n)",
"(defsrc \n 0 1 2\n) (deflayer base 👨‍👩‍👧‍👦 1 2\n)",
)
);
}

#[test]
fn invalid_item_in_defsrc() {
should_not_format("(defsrc () 1 2) (deflayer base 0 1 2)")
should_not_format("(defsrc () 1 2) (deflayer base 0 1 2)");
}

#[test]
Expand All @@ -357,31 +358,33 @@ mod tests {
formats_correctly(
"(defsrc 1 2) (deflayer base 3 4\n)",
"(defsrc 1 2) (deflayer base 3 4)",
)
);
}

#[test]
fn regression_test_2() {
// fix: wrong spacing in the newline after line comment on last line
should_not_format("(defsrc 1 2 ;;\n 3) (deflayer base 4 5 ;;\n 6)")
should_not_format("(defsrc 1 2 ;;\n 3) (deflayer base 4 5 ;;\n 6)");
}

#[test]
fn regression_test_3() {
// fix: line comments in new line get moved to the end of the previous line
should_not_format("(defsrc 1 2) (deflayer base 4 5\n ;;)")
fn line_comment_at_the_end_of_line_in_deflayer() {
// Both cases seem correct, but only the first one passes as of now.
// idk how to fix this. Probably another arg would need to be
// added to `formatted_deflayer_node_metadata` or something.
should_not_format("(defsrc 1 2\n) (deflayer base 4 5 ;;\n)");
// should_not_format("(defsrc 1 2\n) (deflayer base 4 5\n;;\n)");
}

#[test]
fn regression_test_4() {
// fix: block comments in new line get moved to the end of the previous line
should_not_format("(defsrc 1 2) (deflayer base 4 5\n#||#\n)")
should_not_format("(defsrc 1 2) (deflayer base 4 5 #||#)");
}

#[test]
fn regression_test_5() {
// fix: additional space characters at the end of lines
should_not_format("(defsrc 1 2 ;;\n 3) (deflayer base 4 5\n ;;\n 6)")
fn the_indent_a_line_after_line_comment_is_correct() {
should_not_format("(defsrc 1 2 \n 3) (deflayer base 4 5 \n 6)"); // should pass with just newline
should_not_format("(defsrc 1 2 \n 3) (deflayer base 4 5 ;;\n 6)"); // but with line comment?
}

#[test]
Expand Down

0 comments on commit 72addc6

Please sign in to comment.