How to use the gdtoolkit.formatter.format_code function in gdtoolkit

To help you get started, we’ve selected a few gdtoolkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Scony / godot-gdscript-toolkit / tests / formatter / test_real_world_scripts.py View on Github external
def test_real_world_script_e2e(test_name):
    this_dir = os.path.dirname(os.path.abspath(__file__))
    input_file_path = os.path.join(this_dir, DATA_DIR, test_name)
    with open(input_file_path, "r") as input_fh:
        input_code = input_fh.read()
        formatted_code = format_code(input_code, 100)
        check_formatting_safety(input_code, formatted_code, 100)
github Scony / godot-gdscript-toolkit / tests / formatter / common.py View on Github external
def format_and_compare(input_code, expected_output_code):
    formatted_code = format_code(input_code, max_line_length=MAX_LINE_LENGTH)
    _compare(formatted_code, expected_output_code)

    code_formatted_again = format_code(formatted_code, max_line_length=MAX_LINE_LENGTH)
    _compare_again(code_formatted_again, formatted_code)

    _tree_invariant_check(input_code, formatted_code)

    input_code_comment_stats = _gather_comment_statistics_from_code(input_code)
    formatted_code_comments = _gather_comments_from_code(formatted_code)
    _comment_preservation_check(input_code_comment_stats, formatted_code_comments)
github Scony / godot-gdscript-toolkit / tests / formatter / common.py View on Github external
check_comment_persistence=False,
    check_tree_invariant=False,
    check_formatting_stability=False,
):
    formatted_code = format_code(input_code, max_line_length=MAX_LINE_LENGTH)

    if check_comment_persistence:
        input_code_comment_stats = _gather_comment_statistics_from_code(input_code)
        formatted_code_comments = _gather_comments_from_code(formatted_code)
        _comment_preservation_check(input_code_comment_stats, formatted_code_comments)

    if check_tree_invariant:
        _tree_invariant_check(input_code, formatted_code)

    if check_formatting_stability:
        code_formatted_again = format_code(
            formatted_code, max_line_length=MAX_LINE_LENGTH
        )
        _compare_again(code_formatted_again, formatted_code)
github Scony / godot-gdscript-toolkit / tests / formatter / common.py View on Github external
def format_and_compare(input_code, expected_output_code):
    formatted_code = format_code(input_code, max_line_length=MAX_LINE_LENGTH)
    _compare(formatted_code, expected_output_code)

    code_formatted_again = format_code(formatted_code, max_line_length=MAX_LINE_LENGTH)
    _compare_again(code_formatted_again, formatted_code)

    _tree_invariant_check(input_code, formatted_code)

    input_code_comment_stats = _gather_comment_statistics_from_code(input_code)
    formatted_code_comments = _gather_comments_from_code(formatted_code)
    _comment_preservation_check(input_code_comment_stats, formatted_code_comments)
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / __main__.py View on Github external
"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,
                            formatted_code,
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / __main__.py View on Github external
def main():
    arguments = docopt(
        __doc__,
        version="gdformat {}".format(
            pkg_resources.get_distribution("gdtoolkit").version
        ),
    )
    files: List[str] = find_files_from(arguments["
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / __main__.py View on Github external
code,
            formatted_code,
            max_line_length=line_length,
            given_code_parse_tree=code_parse_tree,
            given_code_comment_parse_tree=comment_parse_tree,
        )
        print(formatted_code, end="")
    elif arguments["--check"]:
        formattable_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:
                    print("would reformat {}".format(file_path), file=sys.stderr)
                    try:
                        check_formatting_safety(
                            code,