Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep the newline char if file ends with that #20

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub enum TlaError {
}

pub fn rewrite(input: &str, mode: &Mode, force: bool) -> Result<String, TlaError> {
// if the input ends with '\n', we should put the '\n' back to output
s12f marked this conversation as resolved.
Show resolved Hide resolved
let end_of_newline = input.chars().last().map_or(false, |x| x == '\n');
s12f marked this conversation as resolved.
Show resolved Hide resolved

let mut parser = Parser::new();
parser
.set_language(&tree_sitter_tlaplus::LANGUAGE.into())
Expand All @@ -54,6 +57,25 @@ pub fn rewrite(input: &str, mode: &Mode, force: bool) -> Result<String, TlaError
//println!("{:#?}", tla_lines);
replace_symbols(&mut tla_lines);

// push a empty TlaLine,
// tla_lines...join("\n") will add a '\n' into end of the output,
// the reason why we don't push '\n' into output string directly is
// that maybe cause a reallocation.
if end_of_newline {
ahelwer marked this conversation as resolved.
Show resolved Hide resolved
// if tla_lines is empty, we should set the text as "\n"
// because the join will not work in tla_lines.len < 2.
let text = if tla_lines.is_empty() {
"\n".to_string()
} else {
String::new()
};
tla_lines.push(TlaLine {
text,
jlists: Vec::new(),
symbols: Vec::new(),
})
}

// Ensure output parse tree is identical to input parse tree
let output = tla_lines
.iter()
Expand Down Expand Up @@ -432,6 +454,9 @@ fn mark_symbols(tree: &Tree, cursor: &mut QueryCursor, tla_lines: &mut [TlaLine]
}

fn replace_symbols(tla_lines: &mut [TlaLine]) {
if tla_lines.is_empty() {
return;
}
for line_number in 0..tla_lines.len() - 1 {
s12f marked this conversation as resolved.
Show resolved Hide resolved
let (prefix, suffix) = tla_lines.split_at_mut(line_number + 1);
let line = &mut prefix[line_number];
Expand Down Expand Up @@ -850,4 +875,44 @@ op == /\ A
===="#,
);
}

// Tests that file ends with newline(or without newline)
s12f marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn test_empty_input() {
let input = "";
let output = rewrite(&input, &Mode::UnicodeToAscii, true);
assert_eq!(input, output.unwrap());
let output = rewrite(&input, &Mode::AsciiToUnicode, true);
assert_eq!(input, output.unwrap());
}

#[test]
fn test_single_newline() {
let input = "\n";
let output = rewrite(&input, &Mode::UnicodeToAscii, true);
assert_eq!(input, output.unwrap());
let output = rewrite(&input, &Mode::AsciiToUnicode, true);
assert_eq!(input, output.unwrap());
}

#[test]
fn test_normal_input_without_newline() {
run_roundtrip_test(
r#"
---- MODULE Test ----
op == 1
===="#,
);
}

#[test]
fn test_normal_input_with_newline() {
run_roundtrip_test(
r#"
---- MODULE Test ----
op == 1
====
"#,
);
}
}
Loading