Skip to content

Commit

Permalink
Fix erroneous DedentError when using inline lambas, fixes #339
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Nov 1, 2024
1 parent 1f80ba1 commit ce8051d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed
- Fixed false-positive `expression-not-assigned` linter check on lambdas
- Fixed erroneous DedentError raised in case of inline lambas

## [4.3.2] 2024-10-20

Expand Down
2 changes: 1 addition & 1 deletion gdtoolkit/formatter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ def _format_code(
file=sys.stderr,
)
except lark.indenter.DedentError as exception:
success = False
print(
f"{file_path}:\n",
str(exception),
sep="\n",
file=sys.stderr,
)
return 1
except TreeInvariantViolation:
success = False
print(
Expand Down
8 changes: 2 additions & 6 deletions gdtoolkit/parser/gdscript_indenter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Iterator
from collections import defaultdict

from lark.indenter import DedentError, Indenter
from lark.indenter import Indenter
from lark.lexer import Token


Expand Down Expand Up @@ -81,11 +81,7 @@ def _handle_NL_in_parens(self, token: Token):
self.undedented_lambdas_at_paren_level[self.paren_level] -= 1
yield Token.new_borrow_pos(self.DEDENT_type, indent_str, token)

if indent != self.indent_level[-1]:
raise DedentError(
"Unexpected dedent to column %s. Expected dedent to %s"
% (indent, self.indent_level[-1])
)
# never raising DedentError here as it doesn't make sense in parens

def _dedent_lambda_at_token(self, token: Token):
self.indent_level.pop()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func _ready():
var string_array: Array[String]
var result := string_array.map(func(a): return a).filter(
func(has_underscore):
return (has_underscore.begins_with("a") or has_underscore.begins_with("b"))
)
10 changes: 10 additions & 0 deletions tests/formatter/input-output-pairs/bug_339_multiline_lambda.out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func _ready():
var string_array: Array[String]
var result := (
string_array
. map(func(a): return a)
. filter(
func(has_underscore):
return has_underscore.begins_with("a") or has_underscore.begins_with("b")
)
)

0 comments on commit ce8051d

Please sign in to comment.