Skip to content

Commit 6af969c

Browse files
authored
Add 'indent_str' option to Config to change the indent type (#24)
Two spaces remains the default. To make the builder more intuitive this adds a depedency on the TypedBuilder crate.
1 parent 4f15b0d commit 6af969c

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

Cargo.lock

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ serde = { version = "1.0", features = ["derive"] }
1919
similar = "2.2.1"
2020
toml = "0.7.6"
2121
tree-sitter = "0.20.10"
22+
typed-builder = "0.21.1"
2223

2324
[build-dependencies]
2425
cc="*"

src/config/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ use std::fs;
22
use std::path::{Path, PathBuf};
33

44
use serde::Deserialize;
5+
use typed_builder::TypedBuilder;
56

67
use crate::layouts::KeyboardLayoutType;
78

89
mod constants;
910

10-
#[derive(Deserialize, Default)]
11+
#[derive(Default, Deserialize, TypedBuilder)]
1112
pub struct Config {
13+
#[builder(default)]
1214
#[serde(default)]
1315
pub layout: KeyboardLayoutType,
1416

17+
#[builder(default_code = "Config::default_indent_str()")]
18+
#[serde(default = "Config::default_indent_str")]
19+
pub indent_str: String,
20+
21+
#[builder(default)]
1522
#[serde(default)]
1623
pub warn_on_unhandled_tokens: bool,
1724
}
@@ -26,6 +33,10 @@ impl Config {
2633
Self::default()
2734
}
2835
}
36+
37+
pub fn default_indent_str() -> String {
38+
" ".to_owned()
39+
}
2940
}
3041

3142
fn find_rc_file(path: &Path) -> Option<PathBuf> {

src/printer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ pub fn print(source: &String, config: &Config) -> String {
418418
let tree = parse(source.clone());
419419
let mut cursor = tree.walk();
420420

421-
let ctx = Context { indent: 0, keymap: false, bindings: false, config };
421+
let ctx =
422+
Context { indent: 0, bindings: false, keymap: false, config: config };
422423

423424
// The first node is the root document node, so we have to traverse all it's
424425
// children with the same indentation level.

src/test_utils/spec_helpers.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ pub fn run_specs(directory_path: &Path) {
4949
let specs = get_specs_in_dir(directory_path);
5050
let test_count = specs.len();
5151
let mut failed_tests = Vec::new();
52-
let config = Config {
53-
layout: KeyboardLayoutType::Adv360,
54-
warn_on_unhandled_tokens: false,
55-
};
52+
let config = Config::builder().layout(KeyboardLayoutType::Adv360).build();
5653

5754
for (_, spec) in specs {
5855
let result = print(&spec.file_text, &config);

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn lookahead<'a>(cursor: &'a TreeCursor) -> Option<Node<'a>> {
3131
}
3232

3333
pub fn print_indent(writer: &mut String, ctx: &Context) {
34-
writer.push_str(" ".repeat(ctx.indent).as_str());
34+
writer.push_str(&ctx.config.indent_str.repeat(ctx.indent));
3535
}
3636

3737
pub fn sep(writer: &mut String) {

0 commit comments

Comments
 (0)