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

Formatter tests #92

Merged
merged 26 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
5abd08a
remove typescript ignores
leviathanbeak May 26, 2021
6c532fa
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak May 26, 2021
df93ff3
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak May 26, 2021
2a902f8
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak Jun 1, 2021
aba7c8e
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak Jun 15, 2021
7d04824
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll
leviathanbeak Jun 17, 2021
447ae06
handle equal sign with ! correctly
leviathanbeak Jun 17, 2021
371e505
handle more whitespace cases
leviathanbeak Jun 17, 2021
5ec69ff
handle multiline comments and inline comments
leviathanbeak Jun 21, 2021
9079f32
add newline
leviathanbeak Jun 21, 2021
a4c8431
add space after quote
leviathanbeak Jun 21, 2021
be79950
added tests
leviathanbeak Jun 21, 2021
257a4b1
fixed multiline */
leviathanbeak Jun 21, 2021
374b401
fix adding inline comment
leviathanbeak Jun 21, 2021
8f51779
add "}" before moving to new line or new comment
leviathanbeak Jun 21, 2021
e953be6
Merge branch 'multiline-comments' into formatter-tests
leviathanbeak Jun 21, 2021
5f664d8
improve comments tests
leviathanbeak Jun 21, 2021
0691f1e
add more multiline checks
leviathanbeak Jun 21, 2021
2ef1db1
update tests
leviathanbeak Jun 22, 2021
4abf2c2
Merge branch 'master' of github.com:FuelLabs/fuel-vm-hll into formatt…
leviathanbeak Jun 22, 2021
1eb8238
check for None option when checking whitespace
leviathanbeak Jun 22, 2021
f0f6779
check that last char is not None
leviathanbeak Jun 22, 2021
c902319
add comma to ignore list
leviathanbeak Jun 22, 2021
b32f920
added more extensive testcase dealing with keywords on multiple lines
leviathanbeak Jun 22, 2021
b34458f
handle keywords on different lines case
leviathanbeak Jun 22, 2021
4bb5c18
added code type
leviathanbeak Jun 22, 2021
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
33 changes: 26 additions & 7 deletions formatter/src/code_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl CodeBuilder {
pub fn format_and_add(&mut self, line: &str) {
let mut code_line = self.get_unfinished_code_line_or_new();

let is_string_or_multiline_comment = code_line.is_string || code_line.is_multiline_comment;
let is_string_or_multiline_comment =
code_line.is_string() || code_line.is_multiline_comment();

let line = if !is_string_or_multiline_comment {
line.trim()
Expand All @@ -64,15 +65,21 @@ impl CodeBuilder {
code_line.push_char('\n');
}

if code_line.is_multiline_comment() && line.trim() == "*/" {
code_line.push_str(&self.get_indentation());
code_line.push_str("*/");
return self.complete_and_add_line(code_line);
}

let mut iter = line.chars().enumerate().peekable();

loop {
if let Some((current_index, current_char)) = iter.next() {
if code_line.is_string {
if code_line.is_string() {
handle_string_case(&mut code_line, current_char);
} else if code_line.is_multiline_comment {
} else if code_line.is_multiline_comment() {
handle_multiline_comment_case(&mut code_line, current_char, &mut iter);
if !code_line.is_multiline_comment {
if !code_line.is_multiline_comment() {
self.complete_and_add_line(code_line);
return self.move_rest_to_new_line(line, iter);
}
Expand Down Expand Up @@ -111,8 +118,8 @@ impl CodeBuilder {

// handle beginning of the string
'"' => {
if !code_line.is_string {
code_line.push_char(current_char);
if !code_line.is_string() {
code_line.append_with_whitespace("\"");
code_line.become_string();
}
}
Expand All @@ -132,7 +139,19 @@ impl CodeBuilder {
'}' => return self.handle_close_brace(line, code_line, iter),

// add the rest
_ => code_line.push_char(current_char),
_ => {
// handle case when keywords are on different lines
if current_index == 0 {
// if there are 2 keywords on different lines - add whitespace between them
if let Some(last_char) = code_line.text.chars().last() {
if last_char.is_alphabetic() && current_char.is_alphabetic() {
code_line.append_whitespace()
}
}
}

code_line.push_char(current_char)
}
}
}
} else {
Expand Down
55 changes: 33 additions & 22 deletions formatter/src/code_line.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
#[derive(Debug)]
pub struct CodeLine {
pub text: String,
pub is_string: bool,
pub is_completed: bool,
pub is_multiline_comment: bool,
pub was_previously_stored: bool,
code_type: CodeType,
}

impl CodeLine {
pub fn new(text: String) -> Self {
Self {
text,
is_string: false,
is_completed: false,
is_multiline_comment: false,
was_previously_stored: false,
code_type: CodeType::Default,
}
}

pub fn default() -> Self {
Self {
text: "".into(),
is_string: false,
is_completed: false,
is_multiline_comment: false,
was_previously_stored: false,
code_type: CodeType::Default,
}
}

pub fn empty_line() -> Self {
Self {
text: "".into(),
is_string: false,
is_completed: true,
is_multiline_comment: false,
was_previously_stored: false,
code_type: CodeType::Default,
}
}

pub fn is_string(&self) -> bool {
self.code_type == CodeType::String
}

pub fn is_multiline_comment(&self) -> bool {
self.code_type == CodeType::MultilineComment
}

pub fn become_multiline_comment(&mut self) {
self.code_type = CodeType::MultilineComment;
}

pub fn end_multiline_comment(&mut self) {
self.code_type = CodeType::Default;
}

pub fn end_string(&mut self) {
self.code_type = CodeType::Default;
}

pub fn push_str(&mut self, line: &str) {
self.text.push_str(line);
}
Expand All @@ -51,19 +67,7 @@ impl CodeLine {
}

pub fn become_string(&mut self) {
self.is_string = true;
}

pub fn become_multiline_comment(&mut self) {
self.is_multiline_comment = true;
}

pub fn end_multiline_comment(&mut self) {
self.is_multiline_comment = false;
}

pub fn end_string(&mut self) {
self.is_string = false;
self.code_type = CodeType::String
}

pub fn update_for_storage(&mut self, indentation: String) {
Expand All @@ -75,7 +79,7 @@ impl CodeLine {
let last = self.text.chars().last();
let is_previous_whitespace = Some(' ') == last;

if !is_previous_whitespace {
if !is_previous_whitespace && last != None {
self.push_char(' ');
}

Expand Down Expand Up @@ -105,3 +109,10 @@ impl CodeLine {
self.text.is_empty()
}
}

#[derive(Debug, PartialEq)]
enum CodeType {
Default,
String,
MultilineComment,
}
213 changes: 213 additions & 0 deletions formatter/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,216 @@ pub fn get_formatted_data(file: &str, tab_size: u32) -> (usize, String) {

code_builder.get_final_edits()
}

#[cfg(test)]
mod tests {
use super::get_formatted_data;

#[test]
fn test_indentation() {
let correct_sway_code = r#"script;

fn main() {
// this is a comment
let o = 123;

let p = {
/* this is some
multi line stuff t

*/
123;

};

add(1, 2);
}

pub fn add(a: u32, b: u32) -> u32 {
a + b
}
"#;
let (_, result) = get_formatted_data(correct_sway_code, 4);
assert_eq!(correct_sway_code, result);

let sway_code = r#"script;

fn main() {
// this is a comment
let o = 123;

let
p


=


{
/* this is some
multi line stuff t

*/
123


;


};

add( 1,

2


) ;
}

pub
fn
add
(
a:u32 ,
b: u32) ->u32{
a +b}

"#;

let (_, result) = get_formatted_data(sway_code, 4);
assert_eq!(correct_sway_code, result);
}

#[test]
fn test_multiline_string() {
let correct_sway_code = r#"script;

fn main() {
let multiline_string = " sadsa
sadsad
sadasd sadsdsa
sadasd
sadasd sadasd
";
}
"#;

let (_, result) = get_formatted_data(correct_sway_code, 4);
assert_eq!(correct_sway_code, result);

let sway_code = r#"script;

fn main(){
let multiline_string=" sadsa
sadsad
sadasd sadsdsa
sadasd
sadasd sadasd
"
;
}
"#;

let (_, result) = get_formatted_data(sway_code, 4);
assert_eq!(correct_sway_code, result);
}

#[test]
fn test_whitespace_handling() {
let correct_sway_code = r#"script;

fn main() {
let word = "word";
let num = 12;

let multi = {
let k = 12;
k
};
}
"#;

let (_, result) = get_formatted_data(correct_sway_code, 4);
assert_eq!(correct_sway_code, result);

let sway_code = r#"script;

fn main() {
let word="word";
let num= 12 ;

let multi = {
let k = 12;
k
}


;
}
"#;

let (_, result) = get_formatted_data(sway_code, 4);
assert_eq!(correct_sway_code, result);
}

#[test]
fn test_comments() {
let correct_sway_code = r#"script;

fn main() {
// this is a comment
let o = 123; // this is an inline comment
/*
asdasd
asdasdsad asdasdasd */

/* multiline closed on the same line */
let p = {
/* this is some
multi line stuff t

*/
123;
}; // comment here as well
} // comment here too

// example struct with comments
struct Example { // first comment
prop: bool, // second comment
age: u32 // another comment
} // comment as well
"#;

let (_, result) = get_formatted_data(correct_sway_code, 4);
assert_eq!(correct_sway_code, result);

let sway_code = r#"script;

fn main() {
// this is a comment
let o = 123; // this is an inline comment
/*
asdasd
asdasdsad asdasdasd */

/* multiline closed on the same line */
let p = {
/* this is some
multi line stuff t

*/
123;
}; // comment here as well
} // comment here too

// example struct with comments
struct Example { // first comment
prop: bool,// second comment
age: u32// another comment
} // comment as well
"#;

let (_, result) = get_formatted_data(sway_code, 4);
assert_eq!(correct_sway_code, result);
}
}
Loading