Skip to content

Commit f3824cf

Browse files
committed
fix broken multiline statement docstrings
1 parent 57421eb commit f3824cf

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/formatter.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,21 @@ impl Formatter {
365365
if last_comment_is_doc_comment {
366366
let mut comment_node_index = m.captures.len() - 2;
367367

368+
let first_comment_node = m.captures[1].node;
369+
let first_comment_is_inline_comment =
370+
first_comment_node.start_position().row
371+
== first_node.start_position().row;
372+
// ignore n first nodes when searching for the first docstring comment node
373+
// in case if the first comment is an inline comment we ignore
374+
// two nodes: first statement node and inline comment node
375+
// otherwise we ignore only the first statement node
376+
let mut amount_of_nodes_to_ignore = 1;
377+
if first_comment_is_inline_comment {
378+
amount_of_nodes_to_ignore += 1;
379+
}
380+
368381
// find first documentation comment node
369-
while comment_node_index > 2
382+
while comment_node_index > amount_of_nodes_to_ignore
370383
&& m.captures[comment_node_index - 1].node.start_position().row
371384
== m.captures[comment_node_index].node.start_position().row - 1
372385
{

tests/expected/two_lines_spacing_with_comments.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ func test2() -> void:
66
pass
77

88

9+
# Multiline docstring
10+
# another line
11+
func test2() -> void:
12+
pass
13+
14+
915
var a # case 2
1016

1117

tests/input/two_lines_spacing_with_comments.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ var a
44
func test2() -> void:
55
pass
66

7+
# Multiline docstring
8+
# another line
9+
func test2() -> void:
10+
pass
11+
712
var a # case 2
813

914
func test2() -> void:

0 commit comments

Comments
 (0)