Skip to content

Commit 4a7c10a

Browse files
Combine @abstract annotation with class/class_name/func line
1 parent d751f3a commit 4a7c10a

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

gdtoolkit/formatter/annotation.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@
111111
]
112112

113113

114+
def is_abstract_annotation_for_statement(statement: Tree, next_statement: Tree) -> bool:
115+
"""Check if this is an @abstract annotation that should be combined with the next statement."""
116+
if statement.data != "annotation":
117+
return False
118+
name = statement.children[0].value
119+
if name != "abstract":
120+
return False
121+
return next_statement.data in ["abstract_func_def", "classname_stmt", "class_def"]
122+
123+
114124
def is_non_standalone_annotation(statement: Tree) -> bool:
115125
if statement.data != "annotation":
116126
return False
@@ -136,9 +146,26 @@ def prepend_annotations_to_formatted_line(
136146
single_line_length = (
137147
context.indent + len(annotations_string) + len(whitelineless_line)
138148
)
139-
standalone_formatting_enforced = whitelineless_line.startswith(
140-
"func"
141-
) or whitelineless_line.startswith("static func")
149+
# Check if this is an abstract function or class_name annotation
150+
is_abstract_func = (
151+
len(context.annotations) == 1 and
152+
context.annotations[0].children[0].value == "abstract" and
153+
whitelineless_line.startswith("func")
154+
)
155+
is_abstract_class_name = (
156+
len(context.annotations) == 1 and
157+
context.annotations[0].children[0].value == "abstract" and
158+
whitelineless_line.startswith("class_name")
159+
)
160+
is_abstract_class = (
161+
len(context.annotations) == 1 and
162+
context.annotations[0].children[0].value == "abstract" and
163+
whitelineless_line.startswith("class ")
164+
)
165+
standalone_formatting_enforced = (
166+
whitelineless_line.startswith("func") or
167+
whitelineless_line.startswith("static func")
168+
) and not is_abstract_func and not is_abstract_class_name
142169
if (
143170
not _annotations_have_standalone_comments(
144171
context.annotations, context.standalone_comments, line_to_prepend_to[0]

gdtoolkit/formatter/block.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .annotation import (
1515
is_non_standalone_annotation,
1616
prepend_annotations_to_formatted_line,
17+
is_abstract_annotation_for_statement,
1718
)
1819

1920

@@ -26,8 +27,15 @@ def format_block(
2627
previous_statement_name = None
2728
formatted_lines = [] # type: FormattedLines
2829
previously_processed_line_number = context.previously_processed_line_number
29-
for statement in statements:
30-
if is_non_standalone_annotation(statement):
30+
for i, statement in enumerate(statements):
31+
# Check if this is an abstract annotation followed by an abstract function or class_name
32+
next_statement = statements[i + 1] if i + 1 < len(statements) else None
33+
is_abstract_for_statement = (
34+
next_statement is not None and
35+
is_abstract_annotation_for_statement(statement, next_statement)
36+
)
37+
38+
if is_non_standalone_annotation(statement) or is_abstract_for_statement:
3139
context.annotations.append(statement)
3240
is_first_annotation = len(context.annotations) == 1
3341
if not is_first_annotation:
@@ -47,7 +55,7 @@ def format_block(
4755
blank_lines, statement.data, surrounding_empty_lines_table
4856
)
4957
is_first_annotation = len(context.annotations) == 1
50-
if is_non_standalone_annotation(statement) and is_first_annotation:
58+
if (is_non_standalone_annotation(statement) or is_abstract_for_statement) and is_first_annotation:
5159
formatted_lines += blank_lines
5260
continue
5361
if len(context.annotations) == 0:
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
@abstract class_name BaseClass
1+
@abstract
2+
class_name BaseClass
23

3-
@abstract func simple_abstract()
4+
@abstract
5+
class TestClass:
6+
@abstract
7+
func test_func()
48

5-
@abstract func abstract_with_params(param1: String, param2: int)
9+
@abstract
10+
func simple_abstract()
611

7-
@abstract func abstract_with_return_type() -> String
12+
@abstract
13+
func abstract_with_params(param1: String, param2: int)
814

9-
@abstract func abstract_with_params_and_return(input: String) -> int
15+
@abstract
16+
func abstract_with_return_type() -> String
17+
18+
@abstract
19+
func abstract_with_params_and_return(input: String) -> int
1020

1121
func concrete_method():
1222
pass

tests/formatter/input-output-pairs/abstract_functions.out.gd

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
@abstract
2-
class_name BaseClass
1+
@abstract class_name BaseClass
32

4-
@abstract
5-
func simple_abstract()
3+
@abstract class TestClass:
4+
@abstract func test_func()
65

7-
@abstract
8-
func abstract_with_params(param1: String, param2: int)
96

10-
@abstract
11-
func abstract_with_return_type() -> String
7+
@abstract func simple_abstract()
128

13-
@abstract
14-
func abstract_with_params_and_return(input: String) -> int
9+
@abstract func abstract_with_params(param1: String, param2: int)
10+
11+
@abstract func abstract_with_return_type() -> String
12+
13+
@abstract func abstract_with_params_and_return(input: String) -> int
1514

1615

1716
func concrete_method():

0 commit comments

Comments
 (0)