Skip to content

Commit 007b666

Browse files
dmlaryNathanLovato
andauthored
feat: Indent backslash continuation lines according to style guide (#218)
Close #180 * Indent backslash continuation lines according to style guide From gdscript style guide[^1]: > Use 2 indent levels to distinguish continuation lines from regular code blocks. Currently the formatter indents continuation lines to the same level as the first line: ```gdscript func test(thing): thing.calling(1) \ .a(12314) \ .long() \ .chain("a string breaks things") \ .of() \ .functions() ``` With this change, continuation lines (joined with a backslash) are formatted with double indention: ```gdscript func test(thing): thing.calling(1) \ .a(12314) \ .long() \ .chain("a string breaks things") \ .of() \ .functions() ``` fixes #180 [^1]: https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_styleguide.html#indentation * Use queries to narrow and address the indent backslash continuation line issue --------- Co-authored-by: Nathan Lovato <12694995+NathanLovato@users.noreply.github.com>
1 parent 2cb6a30 commit 007b666

File tree

5 files changed

+82
-10
lines changed

5 files changed

+82
-10
lines changed

queries/gdscript.scm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,10 @@
246246
(get_node) @leaf
247247

248248
(line_continuation) @prepend_space @append_antispace @append_hardline
249+
; Indent nodes that immediately follows a line continuation ("\") by two levels,
250+
; following the GDScript style guide. The line_continuation token only appears
251+
; as a direct child of these three node types. We're leaving arrays, dicts, and
252+
; enums out because they should have a single indent level inside their bodies
253+
(attribute (line_continuation) @append_indent_start @append_indent_start . (_) @append_indent_end @append_indent_end)
254+
(binary_operator (line_continuation) @append_indent_start @append_indent_start . (_) @append_indent_end @append_indent_end)
255+
(variable_statement (line_continuation) @append_indent_start @append_indent_start . (_) @append_indent_end @append_indent_end)

src/formatter.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct Formatter {
5353
input_tree: GdTree,
5454
tree: Tree,
5555
original_source: Option<String>,
56+
indent_string: String,
5657
}
5758

5859
impl Formatter {
@@ -72,6 +73,11 @@ impl Formatter {
7273
} else {
7374
None
7475
};
76+
let indent_string = if config.use_spaces {
77+
" ".repeat(config.indent_size)
78+
} else {
79+
"\t".to_string()
80+
};
7581

7682
Self {
7783
original_source,
@@ -80,22 +86,17 @@ impl Formatter {
8086
tree,
8187
input_tree,
8288
parser,
89+
indent_string,
8390
}
8491
}
8592

8693
#[inline(always)]
8794
fn format(&mut self) -> Result<&mut Self, Box<dyn std::error::Error>> {
88-
let indent_string = if self.config.use_spaces {
89-
" ".repeat(self.config.indent_size)
90-
} else {
91-
"\t".to_string()
92-
};
93-
9495
let language = Language {
9596
name: "gdscript".to_owned(),
9697
query: TopiaryQuery::new(&tree_sitter_gdscript::LANGUAGE.into(), QUERY).unwrap(),
9798
grammar: tree_sitter_gdscript::LANGUAGE.into(),
98-
indent: Some(indent_string),
99+
indent: Some(self.indent_string.clone()),
99100
};
100101

101102
let mut output = Vec::new();
@@ -528,7 +529,9 @@ impl Formatter {
528529
.root_node()
529530
.descendant_for_byte_range(start_byte, start_byte)
530531
.unwrap();
531-
if node.kind() == "string" {
532+
// String nodes may also contain escape_sequence nodes. These are
533+
// found when a backslash is present within a string.
534+
if node.kind() == "string" || node.kind() == "escape_sequence" {
532535
continue;
533536
}
534537

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
func _handles(resource: Resource) -> bool:
22
return resource is NoiseTexture2D \
3-
or resource is GradientTexture1D \
4-
or resource is GradientTexture2D
3+
or resource is GradientTexture1D \
4+
or resource is GradientTexture2D
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var is_valid: bool = some_condition \
2+
and another_condition
3+
4+
5+
func _handles(resource: Resource) -> bool:
6+
return resource is NoiseTexture2D \
7+
or resource is GradientTexture1D
8+
9+
10+
func _process(delta: float) -> void:
11+
if is_on_floor() \
12+
and not is_jumping:
13+
apply_gravity(delta)
14+
15+
16+
func _calculate() -> float:
17+
var x: float = some_long_value \
18+
+ another_value
19+
return x
20+
21+
22+
func _ready() -> void:
23+
node.set_position(Vector2.ZERO) \
24+
.rotated(PI)
25+
26+
27+
# string with \ must not change indentation
28+
func _get_message() -> String:
29+
var greeting: String = "\
30+
Hello world"
31+
return greeting
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var is_valid: bool = some_condition \
2+
and another_condition
3+
4+
5+
func _handles(resource: Resource) -> bool:
6+
return resource is NoiseTexture2D \
7+
or resource is GradientTexture1D
8+
9+
10+
func _process(delta: float) -> void:
11+
if is_on_floor() \
12+
and not is_jumping:
13+
apply_gravity(delta)
14+
15+
16+
func _calculate() -> float:
17+
var x: float = some_long_value \
18+
+ another_value
19+
return x
20+
21+
22+
func _ready() -> void:
23+
node.set_position(Vector2.ZERO) \
24+
.rotated(PI)
25+
26+
27+
# string with \ must not change indentation
28+
func _get_message() -> String:
29+
var greeting: String = "\
30+
Hello world"
31+
return greeting

0 commit comments

Comments
 (0)