Skip to content

Commit

Permalink
Don't touch comments apart from whitespace trim
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Dec 21, 2020
1 parent 7f78f0d commit 0906b64
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 207 deletions.
89 changes: 53 additions & 36 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,43 +113,60 @@ impl CodeFormatter {
}

fn format_single_line_comment_string(&self, comment: String) -> String {
let comment = comment.trim();
let mut formatted_comment = String::from(" "); // Add space before comment begins
formatted_comment += comment;
// Trim any trailing whitespace
comment.trim_end().to_string()

formatted_comment
}

fn format_multi_line_comment_string(&self, comment: String) -> String {
let comment = comment.trim();
let mut formatted_comment = get_line_ending_character(&self.config.line_endings); // Put starting braces seperately on its own line

// Split multiline comment into its individual lines
// We want to take each line, trim any whitespace, and indent it one greater than the current indent level
let comment = comment
.lines()
.map(|line| {
// Check to see if the line is just whitespace
match line.split_whitespace().collect::<String>().is_empty() {
true => String::from(line.trim()), // If its just whitespace, return an empty line
false => {
get_indent_string(
&self.config.indent_type,
&self.indent_level + 1,
self.config.indent_width,
) + line.trim()
} // If not, trim the line and indent it
}
})
.collect::<Vec<String>>()
.join(&get_line_ending_character(&self.config.line_endings));

formatted_comment += &comment; // Add in the multiline comment
formatted_comment += &get_line_ending_character(&self.config.line_endings); // Put closing braces on a new line
// TODO: Do we want to touch comments? If not, then lets ditch this
// let mut formatted_comment = String::from(" "); // Add space before comment begins
// formatted_comment += comment;

formatted_comment
// formatted_comment
}

// fn format_multi_line_comment_string(&self, comment: String) -> String {
// comment
// // TODO: Do we want to touch comments? If not, lets ditch this
// // comment
// // .lines()
// // .map(|line| {
// // // Check to see if the line is just whitespace
// // match line.split_whitespace().collect::<String>().is_empty() {
// // true => line.trim().to_string(), // If its just whitespace, return an empty line
// // false => line.trim_end().to_string() // If not, trim any trailing whitespace and indent it
// // }
// // })
// // .collect::<Vec<String>>()
// // .join(&get_line_ending_character(&self.config.line_endings))

// // let comment = comment.trim();
// // let mut formatted_comment = get_line_ending_character(&self.config.line_endings); // Put starting braces seperately on its own line

// // // Split multiline comment into its individual lines
// // // We want to take each line, trim any whitespace, and indent it one greater than the current indent level
// // let comment = comment
// // .lines()
// // .map(|line| {
// // // Check to see if the line is just whitespace
// // match line.split_whitespace().collect::<String>().is_empty() {
// // true => String::from(line.trim()), // If its just whitespace, return an empty line
// // false => {
// // get_indent_string(
// // &self.config.indent_type,
// // &self.indent_level + 1,
// // self.config.indent_width,
// // ) + line.trim()
// // } // If not, trim the line and indent it
// // }
// // })
// // .collect::<Vec<String>>()
// // .join(&get_line_ending_character(&self.config.line_endings));

// // formatted_comment += &comment; // Add in the multiline comment
// // formatted_comment += &get_line_ending_character(&self.config.line_endings); // Put closing braces on a new line

// // formatted_comment
// }

/// Formats a Token Node
/// Also returns any extra leading or trailing trivia to add for the Token node
/// This should only ever be called from format_token_reference
Expand Down Expand Up @@ -214,8 +231,8 @@ impl CodeFormatter {
}
}
TokenType::MultiLineComment { blocks, comment } => {
let comment =
self.format_multi_line_comment_string(comment.to_owned().into_owned());
// let comment =
// self.format_multi_line_comment_string(comment.to_owned().into_owned());

if let FormatTokenType::LeadingTrivia = format_type {
// Add a new line once the comment is completed
Expand All @@ -224,7 +241,7 @@ impl CodeFormatter {

TokenType::MultiLineComment {
blocks: *blocks,
comment: Cow::Owned(comment),
comment: comment.to_owned(),
}
}
TokenType::Whitespace { characters } => TokenType::Whitespace {
Expand Down
8 changes: 3 additions & 5 deletions tests/files/comments/output.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
--[[
Testing this
]]
--[[Testing this]]
local function foo(bar, baz)
print(bar, baz)
end -- this is a nice function
local test = {} -- this comment should stay
end --this is a nice function
local test = {} --this comment should stay

local y = foo
-- comment line 1
Expand Down
Loading

0 comments on commit 0906b64

Please sign in to comment.