Skip to content

Commit

Permalink
Format line endings in multiline strings and comments (#666)
Browse files Browse the repository at this point in the history
* Add test case

* Convert line endings

* Update changelog
  • Loading branch information
JohnnyMorganz committed Mar 30, 2023
1 parent c371572 commit b8c65f3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Bumped internal parser dependency which should fix parsing problems for comments with Chinese characters, and multiline string escapes
- Fixed comments in punctuated lists for return statements or assignments being incorrectly formatted leading to syntax errors ([#662](https://github.com/JohnnyMorganz/StyLua/issues/662))
- Fixed line endings not being correctly formatted in multiline string literals and comments ([#665](https://github.com/JohnnyMorganz/StyLua/issues/665))

## [0.17.0] - 2023-03-11

Expand Down
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Context {
}

/// Returns the relevant line ending string from the [`LineEndings`] enum
fn line_ending_character(line_endings: LineEndings) -> String {
pub fn line_ending_character(line_endings: LineEndings) -> String {
match line_endings {
LineEndings::Unix => String::from("\n"),
LineEndings::Windows => String::from("\r\n"),
Expand Down
20 changes: 17 additions & 3 deletions src/formatters/general.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{
context::{create_indent_trivia, create_newline_trivia, Context, FormatNode},
context::{
create_indent_trivia, create_newline_trivia, line_ending_character, Context, FormatNode,
},
formatters::{
trivia::{FormatTriviaType, UpdateLeadingTrivia, UpdateTrailingTrivia, UpdateTrivia},
trivia_util::{
Expand Down Expand Up @@ -121,8 +123,14 @@ pub fn format_token(
} => {
// If we have a brackets string, don't mess with it
if let StringLiteralQuoteType::Brackets = quote_type {
// Convert the string to the correct line endings, by first normalising to LF
// then converting LF to output
let literal = literal
.replace("\r\n", "\n")
.replace('\n', &line_ending_character(ctx.config().line_endings));

TokenType::StringLiteral {
literal: literal.to_owned(),
literal: literal.into(),
multi_line: *multi_line,
quote_type: StringLiteralQuoteType::Brackets,
}
Expand Down Expand Up @@ -209,9 +217,15 @@ pub fn format_token(
trailing_trivia = Some(vec![create_newline_trivia(ctx)]);
}

// Convert the comment to the correct line endings, by first normalising to LF
// then converting LF to output
let comment = comment
.replace("\r\n", "\n")
.replace('\n', &line_ending_character(ctx.config().line_endings));

TokenType::MultiLineComment {
blocks: *blocks,
comment: comment.to_owned(),
comment: comment.into(),
}
}
TokenType::Whitespace { characters } => TokenType::Whitespace {
Expand Down
37 changes: 37 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,40 @@ fn test_sort_requires() {
.unwrap());
})
}

#[test]
fn test_crlf_in_multiline_comments() {
// We need to do this outside of insta since it normalises line endings to LF
let code = r###"
local a = "testing"
--[[
This comment
is multiline
and we want to ensure the line endings
convert to CRLF
]]
local x = 1
"###;

let code_crlf = code.lines().collect::<Vec<_>>().join("\r\n");
let output = format(&code_crlf);
assert_eq!(output.find("\r\n"), None);
}

#[test]
fn test_crlf_in_multiline_strings() {
// We need to do this outside of insta since it normalises line endings to LF
let code = r###"
local a = [[
This string
is multiline
and we want to ensure the line endings
convert to CRLF
]]
local x = 1
"###;

let code_crlf = code.lines().collect::<Vec<_>>().join("\r\n");
let output = format(&code_crlf);
assert_eq!(output.find("\r\n"), None);
}

0 comments on commit b8c65f3

Please sign in to comment.