From 57b7cd4dba1bc6ad5becad616ffd776ea39f5bd6 Mon Sep 17 00:00:00 2001 From: azjezz Date: Wed, 18 Dec 2024 23:04:50 +0100 Subject: [PATCH] fix(formatter): fix formatting literal strings Signed-off-by: azjezz --- Cargo.lock | 9 +++ Cargo.toml | 1 + crates/formatter/Cargo.toml | 5 ++ crates/formatter/src/format/expression.rs | 2 +- crates/formatter/src/format/string.rs | 9 ++- crates/formatter/tests/format/mod.rs | 1 + crates/formatter/tests/format/string.rs | 68 +++++++++++++++++++++++ crates/formatter/tests/mod.rs | 20 +++++++ 8 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 crates/formatter/tests/format/mod.rs create mode 100644 crates/formatter/tests/format/string.rs create mode 100644 crates/formatter/tests/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 179a5e6a..bd4808d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -835,6 +835,12 @@ dependencies = [ "web-time", ] +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "is-terminal" version = "0.4.13" @@ -1032,11 +1038,14 @@ version = "0.0.10" dependencies = [ "ahash", "bitflags", + "indoc", "mago-ast", "mago-interner", + "mago-parser", "mago-source", "mago-span", "mago-token", + "pretty_assertions", "serde", "strum", "tracing", diff --git a/Cargo.toml b/Cargo.toml index 954fc101..1be987fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,6 +90,7 @@ wasm-bindgen = "0.2.97" serde-wasm-bindgen = "0.4" diffy = "0.4.0" termcolor = "1.4.1" +indoc = "2" [dependencies] mago-cli = { workspace = true } diff --git a/crates/formatter/Cargo.toml b/crates/formatter/Cargo.toml index 8c8eb543..5614944c 100644 --- a/crates/formatter/Cargo.toml +++ b/crates/formatter/Cargo.toml @@ -23,3 +23,8 @@ strum = { workspace = true } serde = { workspace = true } tracing = { workspace = true } bitflags = { workspace = true } + +[dev-dependencies] +mago-parser = { workspace = true } +indoc = { workspace = true } +pretty_assertions = { workspace = true } diff --git a/crates/formatter/src/format/expression.rs b/crates/formatter/src/format/expression.rs index c5f557d3..54324902 100644 --- a/crates/formatter/src/format/expression.rs +++ b/crates/formatter/src/format/expression.rs @@ -148,7 +148,7 @@ impl<'a> Format<'a> for Literal { impl<'a> Format<'a> for LiteralString { fn format(&'a self, f: &mut Formatter<'a>) -> Document<'a> { - wrap!(f, self, LiteralString, { Document::String(print_string(f, &self.value)) }) + wrap!(f, self, LiteralString, { Document::String(print_string(f, &self.kind, &self.value)) }) } } diff --git a/crates/formatter/src/format/string.rs b/crates/formatter/src/format/string.rs index f1f90fbe..baa32ff5 100644 --- a/crates/formatter/src/format/string.rs +++ b/crates/formatter/src/format/string.rs @@ -1,3 +1,4 @@ +use mago_ast::LiteralStringKind; use mago_interner::StringIdentifier; use crate::Formatter; @@ -58,12 +59,16 @@ fn make_string(raw_text: &str, enclosing_quote: char) -> String { result } -pub(super) fn print_string<'a>(f: &Formatter<'a>, value: &StringIdentifier) -> &'a str { +pub(super) fn print_string<'a>(f: &Formatter<'a>, kind: &LiteralStringKind, value: &StringIdentifier) -> &'a str { let text = f.lookup(value); let quote = unsafe { text.chars().next().unwrap_unchecked() }; let raw_text = &text[1..text.len() - 1]; let enclosing_quote = get_preferred_quote(raw_text, quote, f.settings.single_quote); - f.as_str(make_string(raw_text, enclosing_quote)) + match kind { + LiteralStringKind::SingleQuoted if enclosing_quote == '\'' => text, + LiteralStringKind::DoubleQuoted if enclosing_quote == '"' => text, + _ => f.as_str(make_string(raw_text, enclosing_quote)), + } } diff --git a/crates/formatter/tests/format/mod.rs b/crates/formatter/tests/format/mod.rs new file mode 100644 index 00000000..d245b852 --- /dev/null +++ b/crates/formatter/tests/format/mod.rs @@ -0,0 +1 @@ +pub mod string; diff --git a/crates/formatter/tests/format/string.rs b/crates/formatter/tests/format/string.rs new file mode 100644 index 00000000..3c6d9c7c --- /dev/null +++ b/crates/formatter/tests/format/string.rs @@ -0,0 +1,68 @@ +use indoc::indoc; + +use mago_formatter::settings::FormatSettings; +use mago_source::error::SourceError; + +use crate::test_format; + +#[test] +pub fn test_single_quote() -> Result<(), SourceError> { + let code = indoc! {r#" + Result<(), SourceError> { + let code = indoc! {r#" + , expected: &str, settings: FormatSettings) -> Result<(), SourceError> { + let interner = ThreadedInterner::new(); + let mut manager = SourceManager::new(interner.clone()); + let source_id = manager.insert_content("code.php".to_string(), code.as_ref().to_string(), true); + let source = manager.load(&source_id)?; + let (program, _) = parse_source(&interner, &source); + let formatted = mago_formatter::format(settings, &interner, &source, &program); + + pretty_assertions::assert_eq!(expected, formatted, "Formatted code does not match expected"); + + Ok(()) +}