Skip to content

Commit 645a52e

Browse files
committed
Fix semicolons being removed within comments
Close #223
1 parent 7750470 commit 645a52e

3 files changed

Lines changed: 38 additions & 8 deletions

File tree

src/formatter.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl Formatter {
229229
.build()
230230
.expect("regex should compile");
231231

232-
self.regex_replace_all_outside_strings(re, |caps: &regex::Captures| {
232+
self.regex_replace_all_outside_strings_or_comments(re, |caps: &regex::Captures| {
233233
let extends_line = caps.name("extends_line").unwrap().as_str();
234234
let extends_name = caps.name("extends_name").unwrap().as_str();
235235
let doc = caps.name("doc").map(|m| m.as_str()).unwrap_or_default();
@@ -257,7 +257,7 @@ impl Formatter {
257257
.build()
258258
.expect("semicolon regex should compile");
259259

260-
self.regex_replace_all_outside_strings(re_trailing, "");
260+
self.regex_replace_all_outside_strings_or_comments(re_trailing, "");
261261
self
262262
}
263263

@@ -275,7 +275,7 @@ impl Formatter {
275275
.build()
276276
.expect("dangling comma with comment regex should compile");
277277

278-
self.regex_replace_all_outside_strings(comment_re, |caps: &regex::Captures| {
278+
self.regex_replace_all_outside_strings_or_comments(comment_re, |caps: &regex::Captures| {
279279
let before = caps.name("before").unwrap().as_str();
280280
let comment = caps.name("comment").unwrap().as_str();
281281

@@ -296,7 +296,7 @@ impl Formatter {
296296
.build()
297297
.expect("dangling comma regex should compile");
298298

299-
self.regex_replace_all_outside_strings(re, |caps: &regex::Captures| {
299+
self.regex_replace_all_outside_strings_or_comments(re, |caps: &regex::Captures| {
300300
let first_part = caps.get(1).unwrap().as_str();
301301
let mut replacement = String::with_capacity(first_part.len() + 1);
302302
replacement.push_str(first_part);
@@ -468,7 +468,7 @@ impl Formatter {
468468
.multi_line(true)
469469
.build()
470470
.expect("trailing spaces regex should compile");
471-
self.regex_replace_all_outside_strings(re, "");
471+
self.regex_replace_all_outside_strings_or_comments(re, "");
472472
self
473473
}
474474

@@ -481,7 +481,7 @@ impl Formatter {
481481
.build()
482482
.expect("preload regex should compile");
483483

484-
self.regex_replace_all_outside_strings(re, "preload($1$2)");
484+
self.regex_replace_all_outside_strings_or_comments(re, "preload($1$2)");
485485
self
486486
}
487487

@@ -507,7 +507,11 @@ impl Formatter {
507507
/// outside of strings (simple or multiline).
508508
/// Use this to make post-processing changes needed for formatting but that
509509
/// shouldn't affect strings in the source code.
510-
fn regex_replace_all_outside_strings<R: Replacer>(&mut self, re: Regex, mut rep: R) {
510+
fn regex_replace_all_outside_strings_or_comments<R: Replacer>(
511+
&mut self,
512+
re: Regex,
513+
mut rep: R,
514+
) {
511515
let mut iter = re.captures_iter(&self.content).peekable();
512516
if iter.peek().is_none() {
513517
return;
@@ -531,7 +535,12 @@ impl Formatter {
531535
.unwrap();
532536
// String nodes may also contain escape_sequence nodes. These are
533537
// found when a backslash is present within a string.
534-
if node.kind() == "string" || node.kind() == "escape_sequence" {
538+
// Comment nodes contain docstrings (##) and regular comments (#).
539+
// We skip all of these to avoid modifying content inside strings or comments.
540+
if node.kind() == "string"
541+
|| node.kind() == "escape_sequence"
542+
|| node.kind() == "comment"
543+
{
535544
continue;
536545
}
537546

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extends Object
2+
3+
## How fancy the punctuation should be;
4+
## this may include trailing semicolons in docstrings.
5+
var fancy_punctuation := true
6+
7+
8+
## Another docstring with a semicolon;
9+
## and another one;
10+
func test():
11+
pass

tests/input/docstring_semicolon.gd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extends Object
2+
3+
## How fancy the punctuation should be;
4+
## this may include trailing semicolons in docstrings.
5+
var fancy_punctuation := true
6+
7+
## Another docstring with a semicolon;
8+
## and another one;
9+
func test():
10+
pass

0 commit comments

Comments
 (0)