How to use the gdtoolkit.formatter.context.ExpressionContext 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 / gdtoolkit / formatter / var_statement.py View on Github external
def _format_var_inferred_statement(
    statement: Node, context: Context, prefix: str = ""
) -> Outcome:
    name = statement.children[0].value
    expr = statement.children[1]
    expression_context = ExpressionContext(
        "{}var {} := ".format(prefix, name), statement.line, ""
    )
    return format_expression(expr, expression_context, context)
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / expression.py View on Github external
def _format_dict_to_multiple_lines(
    a_dict: Tree, expression_context: ExpressionContext, context: Context
) -> Outcome:
    new_expression_context = ExpressionContext(
        "{}{{".format(expression_context.prefix_string),
        expression_context.prefix_line,
        "}}{}".format(expression_context.suffix_string),
        a_dict.end_line,
    )
    return (
        format_comma_separated_list(a_dict.children, new_expression_context, context),
        a_dict.end_line,
    )
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / function_statement.py View on Github external
def _format_branch(
    prefix: str,
    suffix: str,
    expr_position: Optional[int],
    statement: Node,
    context: Context,
) -> Outcome:
    if expr_position is not None:
        expr = statement.children[expr_position]
        expression_context = ExpressionContext(prefix, statement.line, suffix)
        header_lines, last_processed_line_no = format_expression(
            expr, expression_context, context
        )
        offset = expr_position + 1
    else:
        header_lines = [
            (statement.line, "{}{}{}".format(context.indent_string, prefix, suffix))
        ]
        last_processed_line_no = statement.line
        offset = 0
    body_lines, last_processed_line_no = format_block(
        statement.children[offset:],
        format_func_statement,
        context.create_child_context(last_processed_line_no),
    )
    return (header_lines + body_lines, last_processed_line_no)
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / expression.py View on Github external
def _format_assignment_expression_to_multiline_line(
    expression: Tree, expression_context: ExpressionContext, context: Context,
) -> Outcome:
    new_expression_context = ExpressionContext(
        "{}{} {} ".format(
            expression_context.prefix_string,
            expression_to_str(expression.children[0]),
            expression_to_str(expression.children[1]),
        ),
        expression_context.prefix_line,
        expression_context.suffix_string,
    )
    return _format_concrete_expression(
        expression.children[2], new_expression_context, context
    )
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / expression.py View on Github external
a_list[-1].end_line,
                    "{}{}".format(
                        context.indent_string, expression_context.suffix_string
                    ),
                ),
            ]
    formatted_lines = [
        (
            expression_context.prefix_line,
            "{}{}".format(context.indent_string, expression_context.prefix_string),
        )
    ]  # type: FormattedLines
    trailing_comma_present = is_trailing_comma(a_list[-1])
    for i, element in enumerate(elements):
        suffix = "," if i != len(elements) - 1 or trailing_comma_present else ""
        child_expression_context = ExpressionContext("", element.line, suffix)
        lines, _ = _format_standalone_expression(
            element, child_expression_context, child_context
        )
        formatted_lines += lines
    formatted_lines.append(
        (
            expression_context.suffix_line
            if expression_context.suffix_line is not None
            else a_list[-1].end_line,
            "{}{}".format(context.indent_string, expression_context.suffix_string),
        )
    )
    return formatted_lines
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / expression.py View on Github external
def _format_kv_pair_to_multiple_lines(
    expression: Tree, expression_context: ExpressionContext, context: Context
) -> Outcome:
    concrete_expression = expression.children[0]
    suffix = ":" if concrete_expression.data == "c_dict_element" else ""
    key_expression_context = ExpressionContext(
        expression_context.prefix_string, expression_context.prefix_line, suffix
    )
    key_lines, _ = _format_standalone_expression(
        concrete_expression.children[0], key_expression_context, context
    )
    if concrete_expression.data == "c_dict_element":
        value_expression_context = ExpressionContext(
            "", -1, expression_context.suffix_string,
        )
        value_lines, _ = _format_standalone_expression(
            concrete_expression.children[1], value_expression_context, context
        )
        return (key_lines + value_lines, concrete_expression.end_line)
    last_key_line_no, last_key_line = key_lines[-1]
    value_expression_context = ExpressionContext(
        "{} = ".format(last_key_line.strip()),  # to overcome godot bug #35416
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / expression.py View on Github external
def _format_subscription_to_multiple_lines(
    expression: Tree, expression_context: ExpressionContext, context: Context,
) -> Outcome:
    subscriptee_expression_context = ExpressionContext(
        expression_context.prefix_string, expression_context.prefix_line, ""
    )
    subscriptee = expression.children[0]
    subscriptee_lines, last_line = _format_concrete_expression(
        subscriptee, subscriptee_expression_context, context
    )
    subscript_expression_context = ExpressionContext(
        "{}[".format(subscriptee_lines[-1][1].strip()),
        last_line,
        "]{}".format(expression_context.suffix_string),
    )
    subscript = expression.children[1]
    subscript_lines, _ = _format_standalone_expression(
        subscript, subscript_expression_context, context
    )
    return (subscriptee_lines[:-1] + subscript_lines, expression.end_line)
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / expression.py View on Github external
def _format_parentheses_to_multiple_lines(
    par_expr: Tree, expression_context: ExpressionContext, context: Context
) -> Outcome:
    new_expression_context = ExpressionContext(
        "{}(".format(expression_context.prefix_string),
        expression_context.prefix_line,
        "){}".format(expression_context.suffix_string),
        par_expr.end_line,
    )
    return (
        _format_standalone_expression(
            par_expr.children[0], new_expression_context, context,
        )[0],
        par_expr.end_line,
    )