Skip to content

Commit b93a47f

Browse files
authored
Merge pull request #70 from fstxz/cleanup
Misc fixes
2 parents 3629fd2 + 1eff025 commit b93a47f

4 files changed

Lines changed: 110 additions & 119 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "gdscript-formatter"
33
version = "0.8.1"
4-
edition = "2021"
4+
edition = "2024"
55
description = "A GDScript code formatter using Topiary and Tree-sitter"
66
license = "MIT"
77
repository = "https://github.com/gdquest/gdscript-formatter"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ To add new formatting rules to the GDScript formatter, you can follow these step
193193

194194
Here are the most important directories and files in the project:
195195

196-
- `src/`: Contains the Rust code to compile and run the formatter using the CLI. It's currently a simple wrapper around the Topiary command line program, but later it could use Topiary as a library instead to pack everything into a simple binary.
196+
- `src/`: Contains the Rust code to compile and run the formatter using the CLI.
197197
- `tests/`: Contains test files for the formatter. It has input files with unformatted GDScript code and expected output files that the formatter should produce when run on the input files.
198198
- `queries/`: Contains the Topiary formatting rules for GDScript. The `gdscript.scm` file is where you define how GDScript code should be formatted based on Tree Sitter queries and Topiary features to mark nodes/patterns for formatting.
199199
- `config/`: Contains configuration files for Topiary - basically a small file that tells Topiary how to run the formatter for GDScript.

src/formatter.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
//!
1313
//! Some of the post-processing is outside of Topiary's capabilities, while other
1414
//! rules have too much performance overhead when applied through Topiary.
15-
use std::io::BufWriter;
15+
use std::{borrow::Cow, io::BufWriter};
1616

1717
use regex::RegexBuilder;
18-
use topiary_core::{formatter_tree, Language, Operation, TopiaryQuery};
18+
use topiary_core::{Language, Operation, TopiaryQuery, formatter_tree};
1919
use tree_sitter::{Parser, Point, Query, QueryCursor, StreamingIterator, Tree};
2020

2121
use crate::FormatterConfig;
@@ -217,7 +217,9 @@ impl Formatter {
217217
.multi_line(true)
218218
.build()
219219
.expect("empty line regex should compile");
220-
self.content = re.replace_all(&self.content, "\n").to_string();
220+
if let Cow::Owned(replaced) = re.replace_all(&self.content, "\n") {
221+
self.content = replaced;
222+
}
221223
self
222224
}
223225

@@ -232,7 +234,9 @@ impl Formatter {
232234
.multi_line(true)
233235
.build()
234236
.expect("semicolon regex should compile");
235-
self.content = re_trailing.replace_all(&self.content, "").to_string();
237+
if let Cow::Owned(replaced) = re_trailing.replace_all(&self.content, "") {
238+
self.content = replaced;
239+
}
236240
self
237241
}
238242

@@ -245,7 +249,9 @@ impl Formatter {
245249
.build()
246250
.expect("preload regex should compile");
247251

248-
self.content = re.replace_all(&self.content, "preload($1$2)").to_string();
252+
if let Cow::Owned(replaced) = re.replace_all(&self.content, "preload($1$2)") {
253+
self.content = replaced;
254+
}
249255
self
250256
}
251257

@@ -254,18 +260,16 @@ impl Formatter {
254260
fn postprocess_tree_sitter(&mut self) -> &mut Self {
255261
self.tree = self.parser.parse(&self.content, None).unwrap();
256262

257-
Self::handle_two_blank_line(&mut self.tree, &mut self.content);
258-
259-
self
263+
self.handle_two_blank_line()
260264
}
261265

262266
/// This function makes sure we have the correct vertical spacing between important definitions:
263267
/// Two blank lines between function definitions, inner classes, etc. Taking any
264268
/// comments or docstrings into account.
265269
///
266270
/// This uses tree-sitter to find the relevant nodes and their positions.
267-
fn handle_two_blank_line(tree: &mut Tree, content: &mut String) {
268-
let root = tree.root_node();
271+
fn handle_two_blank_line(&mut self) -> &mut Self {
272+
let root = self.tree.root_node();
269273
let queries = [
270274
// We need two queries to catch all cases because variables can be placed above or below functions
271275
// First query: variable, function, class, signal, const, enum followed by function, constructor, class, or variable
@@ -294,7 +298,7 @@ impl Formatter {
294298
};
295299

296300
let mut cursor = QueryCursor::new();
297-
let mut matches = cursor.matches(&query, root, content.as_bytes());
301+
let mut matches = cursor.matches(&query, root, self.content.as_bytes());
298302
while let Some(m) = matches.next() {
299303
let first_node = m.captures[0].node;
300304
if m.captures.len() == 3 {
@@ -307,7 +311,7 @@ impl Formatter {
307311
let mut byte_idx = second_node.start_byte();
308312
let mut position = second_node.start_position();
309313
position.column = 0;
310-
while content.as_bytes()[byte_idx] != b'\n' {
314+
while self.content.as_bytes()[byte_idx] != b'\n' {
311315
byte_idx -= 1;
312316
}
313317
new_lines_at.push((byte_idx, position));
@@ -339,19 +343,19 @@ impl Formatter {
339343
let mut new_end_position = position;
340344
let mut new_end_byte_idx = byte_idx;
341345
// Only add a second blank line if there isn't already one
342-
if content.as_bytes()[byte_idx + 1] != b'\n' {
346+
if self.content.as_bytes()[byte_idx + 1] != b'\n' {
343347
new_end_position.row += 1;
344348
new_end_byte_idx += 1;
345-
content.insert(byte_idx, '\n');
349+
self.content.insert(byte_idx, '\n');
346350
}
347351
// Add the first blank line
348352
new_end_position.row += 1;
349353
new_end_byte_idx += 1;
350-
content.insert(byte_idx, '\n');
354+
self.content.insert(byte_idx, '\n');
351355

352356
// Update the tree sitter parse tree to reflect our changes so that any
353357
// future processing will work with the correct positions
354-
tree.edit(&tree_sitter::InputEdit {
358+
self.tree.edit(&tree_sitter::InputEdit {
355359
start_byte: byte_idx,
356360
old_end_byte: byte_idx,
357361
new_end_byte: new_end_byte_idx,
@@ -360,6 +364,7 @@ impl Formatter {
360364
new_end_position,
361365
});
362366
}
367+
self
363368
}
364369
}
365370

0 commit comments

Comments
 (0)