Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
formattable_num,
"s" if formattable_num != 1 else "",
left_unchanged_num,
"s" if left_unchanged_num != 1 else "",
),
file=sys.stderr,
)
sys.exit(1)
else:
formatted_files = set()
for file_path in files:
with open(file_path, "r+") as fh:
code = fh.read()
try:
code_parse_tree = parser.parse(code, gather_metadata=True)
comment_parse_tree = parser.parse_comments(code)
formatted_code = format_code(
gdscript_code=code,
max_line_length=line_length,
parse_tree=code_parse_tree,
comment_parse_tree=comment_parse_tree,
)
except Exception as e:
print(
"exception during formatting of {}".format(file_path),
file=sys.stderr,
)
raise e
if code != formatted_code:
try:
check_formatting_safety(
code,
def check_formatting_safety(
given_code: str,
formatted_code: str,
max_line_length: int,
given_code_parse_tree: Optional[Tree] = None,
given_code_comment_parse_tree: Optional[Tree] = None,
) -> None:
if given_code == formatted_code:
return
formatted_code_parse_tree = parser.parse(formatted_code, gather_metadata=True)
formatted_code_comment_parse_tree = parser.parse_comments(formatted_code)
check_comment_persistence(
given_code,
formatted_code,
given_code_comment_parse_tree=given_code_comment_parse_tree,
formatted_code_comment_parse_tree=formatted_code_comment_parse_tree,
)
check_tree_invariant(
given_code,
formatted_code,
given_code_parse_tree=given_code_parse_tree,
formatted_code_parse_tree=formatted_code_parse_tree,
)
check_formatting_stability(
formatted_code,
max_line_length,
parse_tree=formatted_code_parse_tree,
def gather_comments_from_code(
gdscript_code: str, comment_tree: Optional[Tree] = None
) -> List[str]:
comment_tree = (
comment_tree
if comment_tree is not None
else parser.parse_comments(gdscript_code)
)
comment_line_numbers = [comment.line for comment in comment_tree.children]
lines = gdscript_code.splitlines()
comments = [] # type: List[str]
for line_number, line in enumerate(lines):
normalized_line_number = line_number + 1
comment_start = line.find("#")
if comment_start >= 0 and normalized_line_number in comment_line_numbers:
comments.append(line[comment_start:])
return comments
def format_code(
gdscript_code: str,
max_line_length: int,
parse_tree: Optional[Tree] = None,
comment_parse_tree: Optional[Tree] = None,
) -> str:
parse_tree = (
parse_tree
if parse_tree is not None
else parser.parse(gdscript_code, gather_metadata=True)
)
comment_parse_tree = (
comment_parse_tree
if comment_parse_tree is not None
else parser.parse_comments(gdscript_code)
)
gdscript_code_lines = [
"",
*gdscript_code.splitlines(),
] # type: List[str]
formatted_lines = [] # type: FormattedLines
context = Context(
indent=0,
previously_processed_line_number=0,
max_line_length=max_line_length,
gdscript_code_lines=gdscript_code_lines,
standalone_comments=gather_standalone_comments(
gdscript_code, comment_parse_tree
),
inline_comments=gather_inline_comments(gdscript_code, comment_parse_tree),
)
def main():
arguments = docopt(
__doc__,
version="gdformat {}".format(
pkg_resources.get_distribution("gdtoolkit").version
),
)
files: List[str] = find_files_from(arguments["