Skip to content

Commit 4e487e7

Browse files
authored
Merge pull request #140 from docwhat/docwhat/trailing-spaces
add a rule to strip trailing whitespace
2 parents 303c075 + 4b71390 commit 4e487e7

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/formatter.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl Formatter {
137137
self.add_newlines_after_extends_statement()
138138
.fix_dangling_semicolons()
139139
.fix_dangling_commas()
140+
.fix_trailing_spaces()
140141
.remove_trailing_commas_from_preload()
141142
.postprocess_tree_sitter()
142143
}
@@ -223,6 +224,17 @@ impl Formatter {
223224
self
224225
}
225226

227+
/// This function removes trailing spaces at the end of lines.
228+
#[inline(always)]
229+
fn fix_trailing_spaces(&mut self) -> &mut Self {
230+
let re = RegexBuilder::new(r"[ \t]+$")
231+
.multi_line(true)
232+
.build()
233+
.expect("trailing spaces regex should compile");
234+
self.regex_replace_all_outside_strings(re, "");
235+
self
236+
}
237+
226238
/// This function removes trailing commas from preload function calls.
227239
/// The GDScript parser doesn't support trailing commas in preload calls,
228240
/// but our formatter might add them for multi-line calls.

tests/expected/trailing_spaces.gd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## This function is the best in the world.
2+
##
3+
## There are no mistakes in it.
4+
func best_function() -> String:
5+
var new_string: String = """
6+
This is a multi-line string with trailing spaces.
7+
"""
8+
9+
return new_string
10+
11+
# EOF

tests/input/trailing_spaces.gd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## This function is the best in the world.
2+
##
3+
## There are no mistakes in it.
4+
func best_function() -> String:
5+
var new_string: String = """
6+
This is a multi-line string with trailing spaces.
7+
"""
8+
9+
return new_string
10+
11+
# EOF

0 commit comments

Comments
 (0)