Skip to content

Commit d76bf8d

Browse files
authored
Refactor the formatter to use more idiomatic Rust, docstrings, and remove obsolete dependencies and configuration
Add `Formatter` struct
2 parents 609aac9 + 2fa2973 commit d76bf8d

8 files changed

Lines changed: 271 additions & 543 deletions

File tree

Cargo.lock

Lines changed: 14 additions & 295 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ repository = "https://github.com/gdquest/gdscript-formatter"
88
default-run = "gdscript-formatter"
99

1010
[dependencies]
11-
anyhow = "1.0"
1211
clap = { version = "4.0", features = ["derive"] }
1312
topiary-core = "0.6"
1413
tree-sitter-gdscript = { git = "https://github.com/PrestonKnopp/tree-sitter-gdscript.git", rev = "9686853b696db07118ad110e440d6de0ca6498b4" }
1514
regex = "1.11"
1615
tree-sitter = "0.25.9"
1716

1817
[dev-dependencies]
19-
assert_cmd = "2.0"
20-
predicates = "3.0"
21-
tempfile = "3.0"
2218
test_each_file = "0.3.5"
2319
similar = "2.7.0"
2420

@@ -36,6 +32,4 @@ path = "src/scripts/benchmark.rs"
3632

3733
[profile.release]
3834
strip = true
39-
debug = false
40-
opt-level = 3
4135
lto = true

src/formatter.rs

Lines changed: 224 additions & 201 deletions
Large diffs are not rendered by default.

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
pub mod formatter;
22
pub mod reorder;
33

4-
pub use formatter::{format_gdscript, format_gdscript_with_config};
5-
4+
#[derive(Clone)]
65
pub struct FormatterConfig {
76
pub indent_size: usize,
87
pub use_spaces: bool,

src/main.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6969
let args = Args::parse();
7070

7171
let input_content = match &args.input {
72-
Some(file_path) => fs::read_to_string(&file_path)
72+
Some(file_path) => fs::read_to_string(file_path)
7373
.map_err(|e| format!("Failed to read file {}: {}", file_path.display(), e))?,
7474
None => {
7575
let mut buffer = String::new();
@@ -96,20 +96,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
9696
println!("File is formatted");
9797
} else {
9898
match (args.input.as_ref(), args.stdout) {
99-
// If there's no input file, always output to stdout
100-
(None, _) => {
101-
print!("{}", formatted_content);
102-
}
103-
// We're reading from a file and the --stdout flag is on: we output to stdout
104-
(Some(_), true) => {
105-
print!("{}", formatted_content);
106-
}
107-
// We're reading from a file without the --stdout flag: we overwrite the input file
99+
// If we're reading from a file without the --stdout flag: we overwrite the input file
108100
(Some(input_file), false) => {
109101
fs::write(input_file, formatted_content).map_err(|e| {
110102
format!("Failed to write to file {}: {}", input_file.display(), e)
111103
})?;
112104
}
105+
// Otherwise we output to stdout
106+
_ => {
107+
print!("{}", formatted_content);
108+
}
113109
}
114110
}
115111

src/reorder.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
/**
2-
* This module exposes a function that reorders GDScript code according to the
3-
* official GDScript style guide.
4-
*
5-
* It works as a separate processing pass that parses the GDScript code using
6-
* tree-sitter, detects top-level declarations, and reorders them according to
7-
* the style guide.
8-
*
9-
* We assume that you won't run this on every save, but rather manually using
10-
* a code editor command or task when you're met with a messy file.
11-
*
12-
*/
1+
//! This module exposes a function that reorders GDScript code according to the
2+
//! official GDScript style guide.
3+
//!
4+
//! It works as a separate processing pass that parses the GDScript code using
5+
//! tree-sitter, detects top-level declarations, and reorders them according to
6+
//! the style guide.
7+
//!
8+
//! We assume that you won't run this on every save, but rather manually using
9+
//! a code editor command or task when you're met with a messy file.
1310
use tree_sitter::{Node, Query, QueryCursor, StreamingIterator, Tree};
1411

1512
/// This method parses the GDScript content, extracts top-level elements,

src/scripts/benchmark.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
/// This module tests the performance of the GDScript formatter. Use this to quickly test the
2-
/// performance impact of changes to the formatter locally.
3-
///
4-
/// Run cargo run --bin benchmark --release to compile and run the benchmark.
5-
/// You can use it in a shell script to compare performance between two git revisions.
6-
///
7-
/// For example, to compare between this commit and the previous one:
8-
///
9-
/// ```sh
10-
/// cargo run --bin benchmark --release > benchmark_results.txt
11-
/// echo "On previous commit:\n" >> benchmark_results.txt
12-
/// git checkout HEAD^
13-
/// cargo run --bin benchmark --release >> benchmark_results.txt
14-
/// git checkout -
15-
/// ```
16-
use gdscript_formatter::{format_gdscript_with_config, FormatterConfig};
1+
//! This module tests the performance of the GDScript formatter. Use this to quickly test the
2+
//! performance impact of changes to the formatter locally.
3+
//!
4+
//! Run cargo run --bin benchmark --release to compile and run the benchmark.
5+
//! You can use it in a shell script to compare performance between two git revisions.
6+
//!
7+
//! For example, to compare between this commit and the previous one:
8+
//!
9+
//! ```sh
10+
//! cargo run --bin benchmark --release > benchmark_results.txt
11+
//! echo "On previous commit:\n" >> benchmark_results.txt
12+
//! git checkout HEAD^
13+
//! cargo run --bin benchmark --release >> benchmark_results.txt
14+
//! git checkout -
15+
//! ```
16+
use gdscript_formatter::{formatter::format_gdscript_with_config, FormatterConfig};
1717
use std::{fs, time::Instant};
1818

1919
const ITERATIONS: u16 = 100;

tests/integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gdscript_formatter::format_gdscript;
1+
use gdscript_formatter::formatter::format_gdscript;
22
use similar::{ChangeTag, TextDiff};
33
use std::fs;
44
use std::path::Path;

0 commit comments

Comments
 (0)