Skip to content

Commit

Permalink
Fix formatting of certain '@warning_ignore' annotations, fixes #267
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Jan 2, 2025
1 parent e9ec1f5 commit 90ebbd1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 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 linter problem reports when global scope is used
- Fixed formatting of certain `@warning_ignore` annotations

## [4.3.3] 2024-11-02

Expand Down
10 changes: 2 additions & 8 deletions gdtoolkit/common/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from lark import Tree

from ..formatter.annotation import STANDALONE_ANNOTATIONS
from ..formatter.annotation import is_non_standalone_annotation

from .utils import find_name_token_among_children, find_tree_among_children
from .exceptions import GDToolkitError
Expand Down Expand Up @@ -124,9 +124,7 @@ def _load_data_from_node_children(self, node: Tree) -> None:
offset = 1 if node.data == "class_def" else 0
annotations = []
for stmt in node.children[offset:]:
if stmt.data == "annotation" and not _is_annotation_standalone(
Annotation(stmt)
):
if stmt.data == "annotation" and is_non_standalone_annotation(stmt):
annotations.append(Annotation(stmt))
continue
if stmt.data == "property_body_def":
Expand Down Expand Up @@ -159,7 +157,3 @@ def __init__(self, parse_tree: Tree):
self.root_class = Class(parse_tree)
self.all_classes = [self.root_class] + self.root_class.all_sub_classes
self.all_functions = self.root_class.all_functions


def _is_annotation_standalone(annotation: Annotation) -> bool:
return annotation.name in STANDALONE_ANNOTATIONS
21 changes: 15 additions & 6 deletions gdtoolkit/formatter/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@
from .expression import format_concrete_expression
from .expression_to_str import expression_to_str

STANDALONE_ANNOTATIONS = [
_STANDALONE_ANNOTATIONS = [
"export_category",
"export_group",
"export_subgroup",
"icon",
"tool",
"warning_ignore",
]
_NON_STANDALONE_WARNING_IGNORES = [
"unused_parameter",
]


def is_non_standalone_annotation(statement: Tree) -> bool:
if statement.data != "annotation":
return False
name = statement.children[0].value
return name not in STANDALONE_ANNOTATIONS
if name in _STANDALONE_ANNOTATIONS:
return False
if name != "warning_ignore":
return True
ignoree = statement.children[1].children[0].children[0].value.strip('"')
if ignoree in _NON_STANDALONE_WARNING_IGNORES:
return True
return False


def prepend_annotations_to_formatted_line(
Expand Down Expand Up @@ -78,8 +87,8 @@ def _annotations_have_standalone_comments(
return any(
comment is not None
for comment in standalone_comments[
get_line(annotations[0]) : last_line
if last_line is not None
else get_end_line(annotations[-1])
get_line(annotations[0]) : (
last_line if last_line is not None else get_end_line(annotations[-1])
)
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ func b():
func d():
@warning_ignore("unused_variable") @warning_ignore("unused_variable") var x: Array[int] = [ 1, 2, ]

@warning_ignore("unused_parameter")
func e():
@warning_ignore("unused_variable")
@warning_ignore("unused_variable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func d():
]


@warning_ignore("unused_parameter")
func e():
@warning_ignore("unused_variable")
@warning_ignore("unused_variable")
Expand Down

0 comments on commit 90ebbd1

Please sign in to comment.