From 4ce8ceafb3a260914095350d858012c1441382fd Mon Sep 17 00:00:00 2001 From: tsar-boomba Date: Wed, 10 Apr 2024 19:28:42 -0400 Subject: [PATCH 1/2] add support for using tabs as indentation --- Cargo.toml | 2 ++ src/config.rs | 3 ++ src/context.rs | 6 +++- src/lib.rs | 6 +++- ...pets__on_off_indent_bug_tab__snapshot.snap | 6 ++++ src/tests/snippets.rs | 28 +++++++++++++++++++ 6 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/tests/snapshots/typstfmt__tests__snippets__on_off_indent_bug_tab__snapshot.snap diff --git a/Cargo.toml b/Cargo.toml index 9bb909f..4877bb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ categories = ["command-line-utilities", "development-tools"] [lib] name = "typstfmt" + [dependencies] lexopt = "0.3.0" confy = "0.5.1" @@ -28,5 +29,6 @@ unicode-width = "0.1.11" similar-asserts = "1.4.2" insta = "1.30.0" tracing-subscriber = "0.3.17" + [[bin]] name = "typstfmt" diff --git a/src/config.rs b/src/config.rs index a6adac3..3ba6088 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,8 @@ pub struct Config { /// If enabled, when breaking arguments, it will try to keep more on one line. pub experimental_args_breaking_consecutive: bool, pub line_wrap: bool, + /// If enabled, `indent_space` will be ignored + pub use_tabs: bool, } impl Default for Config { @@ -19,6 +21,7 @@ impl Default for Config { max_line_length: 80, line_wrap: true, experimental_args_breaking_consecutive: false, + use_tabs: false, } } } diff --git a/src/context.rs b/src/context.rs index 78b2442..4a86ce5 100644 --- a/src/context.rs +++ b/src/context.rs @@ -105,6 +105,10 @@ impl Ctx { /// returns an indent using config to get it's length. pub(crate) fn get_indent(&self) -> String { - " ".repeat(self.config.indent_space) + if self.config.use_tabs { + "\t".to_owned() + } else { + " ".repeat(self.config.indent_space) + } } } diff --git a/src/lib.rs b/src/lib.rs index 0acd8d7..2a26447 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,11 @@ mod params; #[must_use] pub fn format(s: &str, config: Config) -> String { //replace tabs - let s = &s.replace('\t', &" ".repeat(config.indent_space)); + let s = &if config.use_tabs { + s.to_owned() + } else { + s.replace('\t', &" ".repeat(config.indent_space)) + }; let init = parse(s); let mut context = Ctx::from_config(config); diff --git a/src/tests/snapshots/typstfmt__tests__snippets__on_off_indent_bug_tab__snapshot.snap b/src/tests/snapshots/typstfmt__tests__snippets__on_off_indent_bug_tab__snapshot.snap new file mode 100644 index 0000000..bcf2f75 --- /dev/null +++ b/src/tests/snapshots/typstfmt__tests__snippets__on_off_indent_bug_tab__snapshot.snap @@ -0,0 +1,6 @@ +--- +source: src/tests/snippets.rs +description: "INPUT\n===\n\"#let template(doc) = {\\n //typstfmt::off\\n let a = \\\"\\\"\\n //typstfmt::on\\n doc\\n}\"\n===\n#let template(doc) = {\n //typstfmt::off\n let a = \"\"\n //typstfmt::on\n doc\n}\n===\nFORMATTED\n===\n#let template(doc) = {\n\t//typstfmt::off\n let a = \"\"\n //typstfmt::on\n doc\n}" +expression: formatted +--- +"#let template(doc) = {\n\t//typstfmt::off\n let a = \"\"\n //typstfmt::on\n doc\n}" diff --git a/src/tests/snippets.rs b/src/tests/snippets.rs index 94058bd..3728316 100644 --- a/src/tests/snippets.rs +++ b/src/tests/snippets.rs @@ -31,6 +31,19 @@ make_test!( doc }"# ); +make_test!( + on_off_indent_bug_tab, + r#"#let template(doc) = { + //typstfmt::off + let a = "" + //typstfmt::on + doc +}"#, + Config { + use_tabs: true, + ..Default::default() + } +); make_test!(official, OFFICIAL); make_test!(raw_text, RAW); make_test!(tabs, TABS); @@ -72,6 +85,21 @@ test_eq!( ]" ); +test_eq!( + stable_raw_indents_tab, + "#focus-slide[ +#fit-to-height(3em)[Introduction] + +#pdfpc.speaker-note(``` + Let's start the introduction with a quote from Foo Bar + ```) +]", + Config { + use_tabs: true, + ..Default::default() + } +); + // TODO: wait for parser fix // $step(&>= ceil(phi.alt (n+1)) / (n+1) >= phi.alt. )$ // vs $step(&>= ceil(phi.alt (n+1)) / (n+1) >= phi.alt.)$ From 52dc2358b2825cf196d865eff29c9d3473a9f04f Mon Sep 17 00:00:00 2001 From: tsar-boomba Date: Thu, 11 Apr 2024 02:54:38 -0400 Subject: [PATCH 2/2] add `use_tabs` config option --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b314afb..e2550f7 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ Basic formatter for the Typst language with a future! max_line_length = 80 experimental_args_breaking_consecutive = false line_wrap = true + use_tabs = false ``` - Disable the formatting by surrounding code with `// typstfmt::off` and `// typstfmt::on`. (Experimental and broken)