Skip to content

Commit 3629fd2

Browse files
authored
Merge pull request #69 from shadr/fix-modifying-input-tree
Fix: do not modify original tree for the safety check
2 parents ab58e46 + 6bc604d commit 3629fd2

1 file changed

Lines changed: 13 additions & 21 deletions

File tree

src/formatter.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn format_gdscript_with_config(
3232
) -> Result<String, Box<dyn std::error::Error>> {
3333
let mut formatter = Formatter::new(content.to_owned(), config.clone());
3434

35-
formatter.preprocess().format()?.postprocess();
35+
formatter.preprocess().format()?.postprocess().reorder();
3636
formatter.finish()
3737
}
3838

@@ -41,7 +41,7 @@ struct Formatter {
4141
config: FormatterConfig,
4242
parser: Parser,
4343
input_tree: Tree,
44-
output_tree: Option<Tree>,
44+
tree: Tree,
4545
}
4646

4747
impl Formatter {
@@ -56,9 +56,9 @@ impl Formatter {
5656
Self {
5757
content,
5858
config,
59+
tree: input_tree.clone(),
5960
input_tree,
6061
parser,
61-
output_tree: None,
6262
}
6363
}
6464

@@ -81,7 +81,7 @@ impl Formatter {
8181
let mut writer = BufWriter::new(&mut output);
8282

8383
formatter_tree(
84-
self.input_tree.clone().into(),
84+
self.tree.clone().into(),
8585
&self.content,
8686
&mut writer,
8787
&language,
@@ -101,13 +101,13 @@ impl Formatter {
101101
}
102102

103103
#[inline(always)]
104-
fn reorder(&mut self, tree: &mut Tree) -> &mut Self {
104+
fn reorder(&mut self) -> &mut Self {
105105
if !self.config.reorder_code {
106106
return self;
107107
}
108108

109-
*tree = self.parser.parse(&self.content, Some(&tree)).unwrap();
110-
match crate::reorder::reorder_gdscript_elements(&tree, &self.content) {
109+
self.tree = self.parser.parse(&self.content, Some(&self.tree)).unwrap();
110+
match crate::reorder::reorder_gdscript_elements(&self.tree, &self.content) {
111111
Ok(reordered) => {
112112
self.content = reordered;
113113
}
@@ -142,10 +142,9 @@ impl Formatter {
142142
#[inline(always)]
143143
fn finish(mut self) -> Result<String, Box<dyn std::error::Error>> {
144144
if self.config.safe {
145-
let mut tree = self.output_tree.unwrap();
146-
tree = self.parser.parse(&self.content, Some(&tree)).unwrap();
145+
self.tree = self.parser.parse(&self.content, Some(&self.tree)).unwrap();
147146

148-
if !compare_trees(self.input_tree, tree) {
147+
if !compare_trees(self.input_tree, self.tree) {
149148
return Err("Trees are different".into());
150149
}
151150
}
@@ -195,7 +194,7 @@ impl Formatter {
195194

196195
self.content.replace_range(start_byte..end_byte, "");
197196

198-
self.input_tree.edit(&tree_sitter::InputEdit {
197+
self.tree.edit(&tree_sitter::InputEdit {
199198
start_byte,
200199
old_end_byte: end_byte,
201200
new_end_byte: start_byte,
@@ -204,10 +203,7 @@ impl Formatter {
204203
new_end_position: start_position,
205204
});
206205

207-
self.input_tree = self
208-
.parser
209-
.parse(&self.content, Some(&self.input_tree))
210-
.unwrap();
206+
self.tree = self.parser.parse(&self.content, Some(&self.tree)).unwrap();
211207
}
212208
self
213209
}
@@ -256,13 +252,9 @@ impl Formatter {
256252
/// This function runs postprocess passes that uses tree-sitter.
257253
#[inline(always)]
258254
fn postprocess_tree_sitter(&mut self) -> &mut Self {
259-
let mut tree = self.parser.parse(&self.content, None).unwrap();
255+
self.tree = self.parser.parse(&self.content, None).unwrap();
260256

261-
Self::handle_two_blank_line(&mut tree, &mut self.content);
262-
263-
self.reorder(&mut tree);
264-
265-
self.output_tree = Some(tree);
257+
Self::handle_two_blank_line(&mut self.tree, &mut self.content);
266258

267259
self
268260
}

0 commit comments

Comments
 (0)