Skip to content

Commit 5b37fe1

Browse files
committed
Supplement support for locally scoped constants #190
1 parent c6373b6 commit 5b37fe1

File tree

6 files changed

+44
-36
lines changed

6 files changed

+44
-36
lines changed

gdtoolkit/formatter/class_statement.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010
from .statement_utils import format_simple_statement
1111
from .var_statement import format_var_statement
1212
from .expression_to_str import expression_to_str
13-
from .expression import (
14-
format_expression,
15-
format_concrete_expression,
16-
)
13+
from .expression import format_concrete_expression
1714
from .annotation import format_standalone_annotation
1815
from .property import (
1916
has_inline_property_body,
2017
append_property_body_to_formatted_line,
2118
format_property_body,
2219
)
20+
from .const_statement import format_const_statement
2321

2422

2523
def format_class_statement(statement: Tree, context: Context) -> Outcome:
@@ -31,7 +29,7 @@ def format_class_statement(statement: Tree, context: Context) -> Outcome:
3129
"classname_stmt": _format_classname_statement,
3230
"classname_extends_stmt": _format_classname_extends_statement,
3331
"class_var_stmt": _format_var_statement,
34-
"const_stmt": _format_const_statement,
32+
"const_stmt": format_const_statement,
3533
"class_def": _format_class_statement,
3634
"func_def": _format_func_statement,
3735
"static_func_def": partial(
@@ -55,21 +53,6 @@ def _format_child_and_prepend_to_outcome(
5553
)
5654

5755

58-
def _format_const_statement(statement: Tree, context: Context) -> Outcome:
59-
if len(statement.children) == 4:
60-
prefix = f"const {statement.children[1].value} = "
61-
elif len(statement.children) == 5:
62-
prefix = f"const {statement.children[1].value} := "
63-
elif len(statement.children) == 6:
64-
prefix = (
65-
f"const {statement.children[1].value}: {statement.children[3].value} = "
66-
)
67-
expression_context = ExpressionContext(
68-
prefix, statement.line, "", statement.end_line
69-
)
70-
return format_expression(statement.children[-1], expression_context, context)
71-
72-
7356
def _format_signal_statement(statement: Tree, context: Context) -> Outcome:
7457
if len(statement.children) == 1 or len(statement.children[1].children) == 0:
7558
return format_simple_statement(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from lark import Tree
2+
3+
from .types import Outcome
4+
from .context import Context, ExpressionContext
5+
from .expression import format_expression
6+
7+
8+
def format_const_statement(statement: Tree, context: Context) -> Outcome:
9+
if len(statement.children) == 4:
10+
prefix = f"const {statement.children[1].value} = "
11+
elif len(statement.children) == 5:
12+
prefix = f"const {statement.children[1].value} := "
13+
elif len(statement.children) == 6:
14+
prefix = (
15+
f"const {statement.children[1].value}: {statement.children[3].value} = "
16+
)
17+
expression_context = ExpressionContext(
18+
prefix, statement.line, "", statement.end_line
19+
)
20+
return format_expression(statement.children[-1], expression_context, context)

gdtoolkit/formatter/function_statement.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
from .block import format_block, reconstruct_blank_lines_in_range
1010
from .statement_utils import format_simple_statement
1111
from .var_statement import format_var_statement
12+
from .const_statement import format_const_statement
1213

1314

1415
def format_func_statement(statement: Tree, context: Context) -> Outcome:
1516
handlers = {
1617
"pass_stmt": partial(format_simple_statement, "pass"),
1718
"func_var_stmt": format_var_statement,
18-
"const_stmt": _format_const_statement,
19+
"const_stmt": format_const_statement,
1920
"expr_stmt": _format_expr_statement,
2021
"return_stmt": _format_return_statement,
2122
"break_stmt": partial(format_simple_statement, "break"),
@@ -30,21 +31,6 @@ def format_func_statement(statement: Tree, context: Context) -> Outcome:
3031
return handlers[statement.data](statement, context)
3132

3233

33-
def _format_const_statement(statement: Tree, context: Context) -> Outcome:
34-
if len(statement.children) == 4:
35-
prefix = f"const {statement.children[1].value} = "
36-
elif len(statement.children) == 5:
37-
prefix = f"const {statement.children[1].value} := "
38-
elif len(statement.children) == 6:
39-
prefix = (
40-
f"const {statement.children[1].value}: {statement.children[3].value} = "
41-
)
42-
expression_context = ExpressionContext(
43-
prefix, statement.line, "", statement.end_line
44-
)
45-
return format_expression(statement.children[-1], expression_context, context)
46-
47-
4834
def _format_expr_statement(statement: Tree, context: Context) -> Outcome:
4935
expr = statement.children[0]
5036
expression_context = ExpressionContext("", statement.line, "", statement.end_line)

tests/formatter/input-output-pairs/const-statements.in.gd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@ const a = 1
22
const b = 1 + 1
33
const c : = 1
44
const d : int = 1
5+
6+
7+
func foo():
8+
const q = 1
9+
const w = 1 + 1
10+
const e : = 1
11+
const r : int = 1

tests/formatter/input-output-pairs/const-statements.out.gd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@ const a = 1
22
const b = 1 + 1
33
const c := 1
44
const d: int = 1
5+
6+
7+
func foo():
8+
const q = 1
9+
const w = 1 + 1
10+
const e := 1
11+
const r: int = 1

tests/valid-gd-scripts/constants.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ const X = 1
22
const Y := 1
33
const Z : int = 1
44
# const Q : int := 1 # illegal
5+
6+
func foo():
7+
const Q = 1
8+
const W := 1
9+
const E : int = 1

0 commit comments

Comments
 (0)