Skip to content

Commit

Permalink
Add support for user-defined types in type hints, closes Scony#210
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Apr 11, 2023
1 parent 30b09ab commit f49ab6d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [master]

### Added
- Added support for user-defined types in type hints

### Fixed
- Fixed `max-returns` check's message

Expand Down
2 changes: 1 addition & 1 deletion gdtoolkit/parser/gdscript.lark
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ c_dict_element: type_cast ":" type_cast
eq_dict_element: NAME "=" type_cast

TYPE: NAME ([" "] "." [" "] NAME)*
TYPE_HINT: NAME ([" "] "." [" "] NAME)* ["[" NAME "]"]
TYPE_HINT: NAME ([" "] "." [" "] NAME)* ["[" NAME ([" "] "." [" "] NAME)* "]"]
NUMBER: ["+"|"-"] (FLOAT | INT)
FLOAT: INT _EXP | DECIMAL _EXP?
_EXP: ("e"|"E") SIGNED_INT
Expand Down
3 changes: 3 additions & 0 deletions tests/formatter/input-output-pairs/type-hints.in.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class SubClass:
enum NamedEnum { A, B, C }
var a:Array[int]
var b:Array[int] = [1]
const C:Array[int]=[1]
var e:Array[SubClass.NamedEnum]

func foo(d:Array[int])->Array[int]:
return [1]
5 changes: 5 additions & 0 deletions tests/formatter/input-output-pairs/type-hints.out.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class SubClass:
enum NamedEnum { A, B, C }


var a: Array[int]
var b: Array[int] = [1]
const C: Array[int] = [1]
var e: Array[SubClass.NamedEnum]


func foo(d: Array[int]) -> Array[int]:
Expand Down
4 changes: 4 additions & 0 deletions tests/valid-gd-scripts/typed-arrays.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
extends Node

class SubClass:
enum NamedEnum { A, B, C }


func _ready():
var r: Array[int] = [1,2,3]
print([1,2.1] as Array[int])
var r2: Array[SubClass.NamedEnum]
#print([1,2] is Array[int])
#print([1,2.0] is Array[int])
#var rr: Array[Array[int]] = [[1]]
Expand Down

0 comments on commit f49ab6d

Please sign in to comment.