From 5abd08ad3dc9e267320704a4b58ec48fe8a44413 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Wed, 26 May 2021 12:28:15 +0200 Subject: [PATCH 01/19] remove typescript ignores --- .gitignore | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index f4c05b5167b..69eb7bc865f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,9 +8,4 @@ target Cargo.lock # These are backup files generated by rustfmt -**/*.rs.bk - -# Ignore Typescript files -out -node_modules -.vscode-test \ No newline at end of file +**/*.rs.bk \ No newline at end of file From 447ae066b00695a9bd38d4f6a1e9bb56b7c0aa74 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Thu, 17 Jun 2021 17:03:39 +0200 Subject: [PATCH 02/19] handle equal sign with ! correctly --- formatter/src/code_line.rs | 16 +++++++++++----- formatter/src/parse_helpers.rs | 3 +-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/formatter/src/code_line.rs b/formatter/src/code_line.rs index 91d0178a27a..97cc36a70c8 100644 --- a/formatter/src/code_line.rs +++ b/formatter/src/code_line.rs @@ -61,11 +61,7 @@ impl CodeLine { pub fn append_with_whitespace(&mut self, value: &str) { let last = self.text.chars().last(); - let is_previous_whitespace = if last.is_none() { - true - } else { - last.unwrap() == ' ' - }; + let is_previous_whitespace = Some(' ') == last; if !is_previous_whitespace { self.push_char(' '); @@ -74,6 +70,16 @@ impl CodeLine { self.push_str(value); } + pub fn append_equal_sign(&mut self) { + let last = self.text.chars().last(); + + if Some('!') == last { + self.push_char('='); + } else { + self.append_with_whitespace("="); + } + } + pub fn is_empty(&self) -> bool { self.text.is_empty() } diff --git a/formatter/src/parse_helpers.rs b/formatter/src/parse_helpers.rs index f8c5c24bf87..bdde5270d1e 100644 --- a/formatter/src/parse_helpers.rs +++ b/formatter/src/parse_helpers.rs @@ -50,8 +50,7 @@ pub fn handle_assignment_case(code_line: &mut CodeLine, iter: &mut Peekable "); iter.next(); } else { - // it's assignment - code_line.append_with_whitespace("= "); + code_line.append_equal_sign(); } } else { code_line.append_with_whitespace("= "); From 371e5056e868a4f897720a2d0ae317dfcca17907 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Thu, 17 Jun 2021 17:21:23 +0200 Subject: [PATCH 03/19] handle more whitespace cases --- formatter/src/code_line.rs | 9 +++++++++ formatter/src/parse_helpers.rs | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/formatter/src/code_line.rs b/formatter/src/code_line.rs index 97cc36a70c8..3c74b9315a4 100644 --- a/formatter/src/code_line.rs +++ b/formatter/src/code_line.rs @@ -80,6 +80,15 @@ impl CodeLine { } } + pub fn append_whitespace(&mut self) { + let last = self.text.chars().last(); + + match last { + Some('(') => {} // do not add whitespace, + _ => self.append_with_whitespace(""), + } + } + pub fn is_empty(&self) -> bool { self.text.is_empty() } diff --git a/formatter/src/parse_helpers.rs b/formatter/src/parse_helpers.rs index bdde5270d1e..9c3ee61068e 100644 --- a/formatter/src/parse_helpers.rs +++ b/formatter/src/parse_helpers.rs @@ -29,10 +29,10 @@ pub fn handle_whitespace_case(code_line: &mut CodeLine, iter: &mut Peekable {} // do nothing, handle it in next turn + '(' | ';' | ':' | ')' => {} // do nothing, handle it in next turn _ => { // add whitespace if it is not already there - code_line.append_with_whitespace(""); + code_line.append_whitespace(); } } } From 5ec69ffdbb9ae11acfadaba2de806322b687d2c2 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 14:29:59 +0200 Subject: [PATCH 04/19] handle multiline comments and inline comments --- formatter/src/code_builder.rs | 55 ++++++++++++++++++++++++++-------- formatter/src/code_line.rs | 12 ++++++++ formatter/src/parse_helpers.rs | 20 +++++++++++-- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index 19c5e70a10d..70cc93be3a9 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -3,12 +3,12 @@ use std::{ str::Chars, }; -use super::parse_helpers::{clean_all_incoming_whitespace, is_comment}; use super::{ code_line::CodeLine, parse_helpers::{ - handle_ampersand_case, handle_assignment_case, handle_colon_case, handle_dash_case, - handle_pipe_case, handle_string_case, handle_whitespace_case, + clean_all_incoming_whitespace, handle_ampersand_case, handle_assignment_case, + handle_colon_case, handle_dash_case, handle_multiline_comment_case, handle_pipe_case, + handle_string_case, handle_whitespace_case, is_comment, }, }; @@ -45,7 +45,9 @@ impl CodeBuilder { pub fn format_and_add(&mut self, line: &str) { let mut code_line = self.get_unfinished_code_line_or_new(); - let line = if !code_line.is_string { + 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() } else { line @@ -57,8 +59,8 @@ impl CodeBuilder { return self.complete_and_add_line(code_line); } - // handle multiline string - if code_line.is_string { + // add newline if it's multiline string or comment + if is_string_or_multiline_comment { code_line.push_char('\n'); } @@ -68,6 +70,12 @@ impl CodeBuilder { if let Some((_, current_char)) = iter.next() { if code_line.is_string { handle_string_case(&mut code_line, current_char); + } else if code_line.is_multiline_comment { + handle_multiline_comment_case(&mut code_line, current_char, &mut iter); + if !code_line.is_multiline_comment { + self.complete_and_add_line(code_line); + return self.move_rest_to_new_line(line, iter); + } } else { match current_char { ' ' => handle_whitespace_case(&mut code_line, &mut iter), @@ -80,7 +88,22 @@ impl CodeBuilder { ',' => code_line.push_str(", "), '+' => code_line.append_with_whitespace("+ "), '*' => code_line.append_with_whitespace("* "), - '/' => code_line.append_with_whitespace("- "), + '/' => { + match iter.peek() { + Some((_, '*')) => { + // it's a multiline comment + code_line.become_multiline_comment(); + iter.next(); + code_line.push_str("/*"); + } + Some((_, '/')) => { + // it's a comment + code_line.push_str(line); + return self.complete_and_add_line(code_line); + } + _ => code_line.append_with_whitespace("/ "), + } + } '%' => code_line.append_with_whitespace("% "), '^' => code_line.append_with_whitespace("^ "), '!' => code_line.append_with_whitespace("!"), @@ -155,7 +178,7 @@ impl CodeBuilder { // case when '}' was separated from ';' by one or more new lines if previous_code_line.is_completed { // remove empty line first - if !(previous_code_line.text.chars().last().unwrap_or(' ') == '}') { + if !(previous_code_line.text.chars().last() == Some('}')) { self.edits.pop(); } @@ -207,11 +230,19 @@ impl CodeBuilder { fn move_rest_to_new_line(&mut self, line: &str, iter: Peekable>) { let mut iter = iter; - if iter.peek().is_some() { - let (next_index, _) = iter.peek().unwrap(); + if let Some((next_index, _)) = iter.peek() { + let next_line = &line[*next_index..].trim(); - let next_line = &line[*next_index..]; - self.format_and_add(next_line); + // if rest is comment append it to the last existing line + if is_comment(&next_line) { + if let Some(mut code_line) = self.edits.pop() { + code_line.push_char(' '); + code_line.push_str(next_line); + self.add_line(code_line); + } + } else { + self.format_and_add(next_line); + } } } diff --git a/formatter/src/code_line.rs b/formatter/src/code_line.rs index 3c74b9315a4..e0ea76eabe6 100644 --- a/formatter/src/code_line.rs +++ b/formatter/src/code_line.rs @@ -3,6 +3,7 @@ pub struct CodeLine { pub text: String, pub is_string: bool, pub is_completed: bool, + pub is_multiline_comment: bool, pub was_previously_stored: bool, } @@ -12,6 +13,7 @@ impl CodeLine { text, is_string: false, is_completed: false, + is_multiline_comment: false, was_previously_stored: false, } } @@ -21,6 +23,7 @@ impl CodeLine { text: "".into(), is_string: false, is_completed: false, + is_multiline_comment: false, was_previously_stored: false, } } @@ -30,6 +33,7 @@ impl CodeLine { text: "".into(), is_string: false, is_completed: true, + is_multiline_comment: false, was_previously_stored: false, } } @@ -50,6 +54,14 @@ impl CodeLine { 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; } diff --git a/formatter/src/parse_helpers.rs b/formatter/src/parse_helpers.rs index 9c3ee61068e..3db065ab4bc 100644 --- a/formatter/src/parse_helpers.rs +++ b/formatter/src/parse_helpers.rs @@ -10,13 +10,29 @@ pub fn is_comment(line: &str) -> bool { chars.next() == Some('/') && chars.next() == Some('/') } +pub fn handle_multiline_comment_case( + code_line: &mut CodeLine, + current_char: char, + iter: &mut Peekable>, +) { + code_line.push_char(current_char); + + if current_char == '*' { + // end multiline + if let Some((_, '/')) = iter.peek() { + code_line.push_char('/'); + iter.next(); + code_line.end_multiline_comment(); + } + } +} // if it's a string just keep pushing the characters pub fn handle_string_case(code_line: &mut CodeLine, current_char: char) { code_line.push_char(current_char); if current_char == '"' { - let previous_char = code_line.text.chars().last().unwrap_or(' '); + let previous_char = code_line.text.chars().last(); // end of the string - if previous_char != '\\' { + if previous_char != Some('\\') { code_line.end_string(); } } From 9079f324b2aaf448b202674cf499ece81ad6a178 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 14:43:27 +0200 Subject: [PATCH 05/19] add newline --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 69eb7bc865f..eb941c1cec9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ target Cargo.lock # These are backup files generated by rustfmt -**/*.rs.bk \ No newline at end of file +**/*.rs.bk From a4c843162984ebbe9f300413b9e07c9e2f508c9c Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 16:33:57 +0200 Subject: [PATCH 06/19] add space after quote --- formatter/src/code_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index 70cc93be3a9..93784c591a8 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -111,7 +111,7 @@ impl CodeBuilder { // handle beginning of the string '"' => { if !code_line.is_string { - code_line.push_char(current_char); + code_line.append_with_whitespace("\""); code_line.become_string(); } } From be7995066ec302e245a8f53d4af384e4303bfcca Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 18:14:31 +0200 Subject: [PATCH 07/19] added tests --- formatter/src/formatter.rs | 158 +++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) diff --git a/formatter/src/formatter.rs b/formatter/src/formatter.rs index 5ec3456cb29..3c22f50b68c 100644 --- a/formatter/src/formatter.rs +++ b/formatter/src/formatter.rs @@ -12,3 +12,161 @@ 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; + }; +} +"#; + 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; +}; +} +"#; + + 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 + + let p = { + /* this is some + multi line stuff t + + */ + 123; + }; +} +"#; + + 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 + + let p = { + /* this is some + multi line stuff t + + */ + 123; + }; +} +"#; + + let (_, result) = get_formatted_data(sway_code, 4); + assert_eq!(correct_sway_code, result); + } +} From 257a4b11c78284b313a6e068556bbeb1afc596ac Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 18:14:48 +0200 Subject: [PATCH 08/19] fixed multiline */ --- formatter/src/code_builder.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index 93784c591a8..d034cb04103 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -64,6 +64,12 @@ 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 { From 374b401ad551777c72d82bcdf0348d7c309e1b50 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 18:21:30 +0200 Subject: [PATCH 09/19] fix adding inline comment --- formatter/src/code_builder.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index 70cc93be3a9..d36cb642cde 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -67,7 +67,7 @@ impl CodeBuilder { let mut iter = line.chars().enumerate().peekable(); loop { - if let Some((_, current_char)) = iter.next() { + if let Some((current_index, current_char)) = iter.next() { if code_line.is_string { handle_string_case(&mut code_line, current_char); } else if code_line.is_multiline_comment { @@ -98,7 +98,8 @@ impl CodeBuilder { } Some((_, '/')) => { // it's a comment - code_line.push_str(line); + let comment = &line[current_index..]; + code_line.push_str(&comment); return self.complete_and_add_line(code_line); } _ => code_line.append_with_whitespace("/ "), From 8f51779c9abea1785cef82e07cb9d2798396c88e Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 18:37:23 +0200 Subject: [PATCH 10/19] add "}" before moving to new line or new comment --- formatter/src/code_builder.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index d36cb642cde..c64230491d2 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -220,6 +220,7 @@ impl CodeBuilder { } // if there is more - move to new line! Some(_) => { + self.complete_and_add_line(CodeLine::new("}".into())); self.move_rest_to_new_line(line, iter); } None => { From 5f664d8d5e6db43dab86c2de149e8727e8131547 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 18:46:03 +0200 Subject: [PATCH 11/19] improve comments tests --- formatter/src/formatter.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/formatter/src/formatter.rs b/formatter/src/formatter.rs index 3c22f50b68c..d6a259055d3 100644 --- a/formatter/src/formatter.rs +++ b/formatter/src/formatter.rs @@ -143,8 +143,14 @@ fn main() { */ 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); @@ -162,8 +168,14 @@ fn main() { */ 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); From 0691f1eb7885e8947ed931ad21f1e12dc2af2ba8 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Mon, 21 Jun 2021 18:54:47 +0200 Subject: [PATCH 12/19] add more multiline checks --- formatter/src/formatter.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/formatter/src/formatter.rs b/formatter/src/formatter.rs index d6a259055d3..ee1df1b2d1d 100644 --- a/formatter/src/formatter.rs +++ b/formatter/src/formatter.rs @@ -136,7 +136,11 @@ fn main() { 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 @@ -161,12 +165,16 @@ struct Example { // first comment 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 From 2ef1db1e2feb0256939a8e519954022be6e64db6 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Tue, 22 Jun 2021 11:58:11 +0200 Subject: [PATCH 13/19] update tests --- formatter/src/code_builder.rs | 2 +- formatter/src/formatter.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index 29625f6b5aa..06811988681 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -105,7 +105,7 @@ impl CodeBuilder { Some((_, '/')) => { // it's a comment let comment = &line[current_index..]; - code_line.push_str(&comment); + code_line.append_with_whitespace(&comment); return self.complete_and_add_line(code_line); } _ => code_line.append_with_whitespace("/ "), diff --git a/formatter/src/formatter.rs b/formatter/src/formatter.rs index ee1df1b2d1d..68d6b9d6c8f 100644 --- a/formatter/src/formatter.rs +++ b/formatter/src/formatter.rs @@ -153,7 +153,7 @@ fn main() { // example struct with comments struct Example { // first comment prop: bool, // second comment - age: u32, // another comment + age: u32 // another comment } // comment as well "#; @@ -181,8 +181,8 @@ fn main() { // example struct with comments struct Example { // first comment - prop: bool, // second comment - age: u32, // another comment + prop: bool,// second comment + age: u32// another comment } // comment as well "#; From 1eb8238f2ef9f4b1fd79f6672e7a36b52342c0a0 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Tue, 22 Jun 2021 13:30:38 +0200 Subject: [PATCH 14/19] check for None option when checking whitespace --- formatter/src/code_line.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formatter/src/code_line.rs b/formatter/src/code_line.rs index e0ea76eabe6..16e2ca4c197 100644 --- a/formatter/src/code_line.rs +++ b/formatter/src/code_line.rs @@ -73,7 +73,7 @@ impl CodeLine { pub fn append_with_whitespace(&mut self, value: &str) { let last = self.text.chars().last(); - let is_previous_whitespace = Some(' ') == last; + let is_previous_whitespace = last != None && Some(' ') == last; if !is_previous_whitespace { self.push_char(' '); From f0f6779c7366359ec7c49e653a7909717ac93283 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Tue, 22 Jun 2021 14:12:17 +0200 Subject: [PATCH 15/19] check that last char is not None --- formatter/src/code_line.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/formatter/src/code_line.rs b/formatter/src/code_line.rs index 16e2ca4c197..7e101af582f 100644 --- a/formatter/src/code_line.rs +++ b/formatter/src/code_line.rs @@ -73,9 +73,9 @@ impl CodeLine { pub fn append_with_whitespace(&mut self, value: &str) { let last = self.text.chars().last(); - let is_previous_whitespace = last != None && Some(' ') == last; + let is_previous_whitespace = Some(' ') == last; - if !is_previous_whitespace { + if !is_previous_whitespace && last != None { self.push_char(' '); } From c9023193cfb3bd2ca1c643a0a4ce2c2067d41b93 Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Tue, 22 Jun 2021 14:41:19 +0200 Subject: [PATCH 16/19] add comma to ignore list --- formatter/src/parse_helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/formatter/src/parse_helpers.rs b/formatter/src/parse_helpers.rs index 3db065ab4bc..ba1c8067f11 100644 --- a/formatter/src/parse_helpers.rs +++ b/formatter/src/parse_helpers.rs @@ -45,7 +45,7 @@ pub fn handle_whitespace_case(code_line: &mut CodeLine, iter: &mut Peekable {} // do nothing, handle it in next turn + '(' | ';' | ':' | ')' | ',' => {} // do nothing, handle it in next turn _ => { // add whitespace if it is not already there code_line.append_whitespace(); From b32f920ab9d5d85263c6eacf00f312af20332fdd Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Tue, 22 Jun 2021 14:41:56 +0200 Subject: [PATCH 17/19] added more extensive testcase dealing with keywords on multiple lines --- formatter/src/formatter.rs | 49 ++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/formatter/src/formatter.rs b/formatter/src/formatter.rs index 68d6b9d6c8f..d415bbf5778 100644 --- a/formatter/src/formatter.rs +++ b/formatter/src/formatter.rs @@ -31,7 +31,14 @@ fn main() { */ 123; + }; + + add(1, 2); +} + +pub fn add(a: u32, b: u32) -> u32 { + a + b } "#; let (_, result) = get_formatted_data(correct_sway_code, 4); @@ -40,17 +47,45 @@ fn main() { let sway_code = r#"script; fn main() { - // this is a comment - let o = 123; + // this is a comment + let o = 123; - let p = { - /* this is some + let +p + + + = + + + { + /* this is some multi line stuff t - */ - 123; -}; + */ + 123 + + + ; + + + }; + + add( 1, + + 2 + + + ) ; } + +pub +fn +add + ( + a:u32 , + b: u32) ->u32{ + a +b} + "#; let (_, result) = get_formatted_data(sway_code, 4); From b34458f2bccba854dd7c2ff3ff9e2a7350e8044f Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Tue, 22 Jun 2021 14:45:34 +0200 Subject: [PATCH 18/19] handle keywords on different lines case --- formatter/src/code_builder.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index 06811988681..9fd664d71e8 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -138,7 +138,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 { From 4bb5c18708be820332ea5cd2d49693a922d5f10d Mon Sep 17 00:00:00 2001 From: leviathan88 Date: Tue, 22 Jun 2021 15:07:11 +0200 Subject: [PATCH 19/19] added code type --- formatter/src/code_builder.rs | 13 +++++---- formatter/src/code_line.rs | 53 +++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/formatter/src/code_builder.rs b/formatter/src/code_builder.rs index 9fd664d71e8..1f5b5963eac 100644 --- a/formatter/src/code_builder.rs +++ b/formatter/src/code_builder.rs @@ -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() @@ -64,7 +65,7 @@ impl CodeBuilder { code_line.push_char('\n'); } - if code_line.is_multiline_comment && line.trim() == "*/" { + 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); @@ -74,11 +75,11 @@ impl CodeBuilder { 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); } @@ -117,7 +118,7 @@ impl CodeBuilder { // handle beginning of the string '"' => { - if !code_line.is_string { + if !code_line.is_string() { code_line.append_with_whitespace("\""); code_line.become_string(); } diff --git a/formatter/src/code_line.rs b/formatter/src/code_line.rs index 7e101af582f..97052af4bdb 100644 --- a/formatter/src/code_line.rs +++ b/formatter/src/code_line.rs @@ -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); } @@ -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) { @@ -105,3 +109,10 @@ impl CodeLine { self.text.is_empty() } } + +#[derive(Debug, PartialEq)] +enum CodeType { + Default, + String, + MultilineComment, +}