Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Add use_tabs option to config for indenting with tab characters #166

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ categories = ["command-line-utilities", "development-tools"]

[lib]
name = "typstfmt"

[dependencies]
lexopt = "0.3.0"
confy = "0.5.1"
Expand All @@ -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"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,6 +21,7 @@ impl Default for Config {
max_line_length: 80,
line_wrap: true,
experimental_args_breaking_consecutive: false,
use_tabs: false,
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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}"
28 changes: 28 additions & 0 deletions src/tests/snippets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.)$
Expand Down
Loading