How to use the isort.comments.add_to_line function in isort

To help you get started, we’ve selected a few isort 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 timothycrosley / isort / isort / wrap_modes.py View on Github external
def vertical_grid_common(need_trailing_char: bool, **interface):
    if not interface["imports"]:
        return ""

    interface["statement"] += (
        comments.add_to_line(
            interface["comments"],
            "(",
            removed=interface["remove_comments"],
            comment_prefix=interface["comment_prefix"],
        )
        + interface["line_separator"]
        + interface["indent"]
        + interface["imports"].pop(0)
    )
    while interface["imports"]:
        next_import = interface["imports"].pop(0)
        next_statement = f"{interface['statement']}, {next_import}"
        current_line_length = len(next_statement.split(interface["line_separator"])[-1])
        if interface["imports"] or need_trailing_char:
            # If we have more interface["imports"] we need to account for a comma after this import
            # We might also need to account for a closing ) we're going to add.
github timothycrosley / isort / isort / wrap_modes.py View on Github external
interface["statement"] += interface["imports"].pop(0)
    while interface["imports"]:
        next_import = interface["imports"].pop(0)
        next_statement = comments.add_to_line(
            interface["comments"],
            interface["statement"] + ", " + next_import,
            removed=interface["remove_comments"],
            comment_prefix=interface["comment_prefix"],
        )
        if (
            len(next_statement.split(interface["line_separator"])[-1]) + 3
            > interface["line_length"]
        ):
            next_statement = (
                comments.add_to_line(
                    interface["comments"],
                    f"{interface['statement']}, \\",
                    removed=interface["remove_comments"],
                    comment_prefix=interface["comment_prefix"],
                )
                + f"{interface['line_separator']}{interface['indent']}{next_import}"
            )
            interface["comments"] = []
        interface["statement"] = next_statement
    return interface["statement"]
github timothycrosley / isort / isort / output.py View on Github external
single_import_line += (
                            f"{comments and ';' or config.comment_prefix} " f"{comment}"
                        )
                    if from_import in as_imports:
                        if (
                            config.keep_direct_and_as_imports
                            and parsed.imports[section]["from"][module][from_import]
                        ):
                            new_section_output.append(
                                wrap.line(single_import_line, parsed.line_separator, config)
                            )
                        from_comments = parsed.categorized_comments["straight"].get(
                            f"{module}.{from_import}"
                        )
                        new_section_output.extend(
                            with_comments(
                                from_comments,
                                wrap.line(import_start + as_import, parsed.line_separator, config),
                                removed=config.ignore_comments,
                                comment_prefix=config.comment_prefix,
                            )
                            for as_import in sorting.naturally(as_imports[from_import])
                        )
                    else:
                        new_section_output.append(
                            wrap.line(single_import_line, parsed.line_separator, config)
                        )
                    comments = None
            else:
                while from_imports and from_imports[0] in as_imports:
                    from_import = from_imports.pop(0)
                    as_imports[from_import] = sorting.naturally(as_imports[from_import])
github timothycrosley / isort / isort / wrap_modes.py View on Github external
def vertical_hanging_indent(**interface):
    _line_with_comments = comments.add_to_line(
        interface["comments"],
        "",
        removed=interface["remove_comments"],
        comment_prefix=interface["comment_prefix"],
    )
    _imports = ("," + interface["line_separator"] + interface["indent"]).join(interface["imports"])
    _comma_maybe = "," if interface["include_trailing_comma"] else ""
    return (
        f"{interface['statement']}({_line_with_comments}{interface['line_separator']}"
        f"{interface['indent']}{_imports}{_comma_maybe}{interface['line_separator']})"
github timothycrosley / isort / isort / output.py View on Github external
if module in parsed.as_map:
            if config.keep_direct_and_as_imports and parsed.imports[section]["straight"][module]:
                import_definition.append(f"import {module}")
            import_definition.extend(
                f"import {module} as {as_import}" for as_import in parsed.as_map[module]
            )
        else:
            import_definition.append(f"import {module}")

        comments_above = parsed.categorized_comments["above"]["straight"].pop(module, None)
        if comments_above:
            if new_section_output and config.ensure_newline_before_comments:
                new_section_output.append("")
            new_section_output.extend(comments_above)
        new_section_output.extend(
            with_comments(
                parsed.categorized_comments["straight"].get(module),
                idef,
                removed=config.ignore_comments,
                comment_prefix=config.comment_prefix,
            )
            for idef in import_definition
        )

    return new_section_output
github timothycrosley / isort / isort / output.py View on Github external
as_imports[from_import] = sorting.naturally(as_imports[from_import])
                    from_comments = parsed.categorized_comments["straight"].get(
                        f"{module}.{from_import}"
                    )
                    above_comments = parsed.categorized_comments["above"]["from"].pop(module, None)
                    if above_comments:
                        if new_section_output and config.ensure_newline_before_comments:
                            new_section_output.append("")
                        new_section_output.extend(above_comments)

                    if (
                        config.keep_direct_and_as_imports
                        and parsed.imports[section]["from"][module][from_import]
                    ):
                        new_section_output.append(
                            with_comments(
                                from_comments,
                                wrap.line(
                                    import_start + from_import, parsed.line_separator, config
                                ),
                                removed=config.ignore_comments,
                                comment_prefix=config.comment_prefix,
                            )
                        )
                    new_section_output.extend(
                        with_comments(
                            from_comments,
                            wrap.line(import_start + as_import, parsed.line_separator, config),
                            removed=config.ignore_comments,
                            comment_prefix=config.comment_prefix,
                        )
                        for as_import in as_imports[from_import]
github timothycrosley / isort / isort / output.py View on Github external
comments = None

                from_import_section = []
                while from_imports and (
                    from_imports[0] not in as_imports
                    or (
                        config.keep_direct_and_as_imports
                        and config.combine_as_imports
                        and parsed.imports[section]["from"][module][from_import]
                    )
                ):
                    from_import_section.append(from_imports.pop(0))
                if star_import:
                    import_statement = import_start + (", ").join(from_import_section)
                else:
                    import_statement = with_comments(
                        comments,
                        import_start + (", ").join(from_import_section),
                        removed=config.ignore_comments,
                        comment_prefix=config.comment_prefix,
                    )
                if not from_import_section:
                    import_statement = ""

                do_multiline_reformat = False

                force_grid_wrap = config.force_grid_wrap
                if force_grid_wrap and len(from_import_section) >= force_grid_wrap:
                    do_multiline_reformat = True

                if len(import_statement) > config.line_length and len(from_import_section) > 1:
                    do_multiline_reformat = True
github timothycrosley / isort / isort / output.py View on Github external
if (
                        config.keep_direct_and_as_imports
                        and parsed.imports[section]["from"][module][from_import]
                    ):
                        new_section_output.append(
                            with_comments(
                                from_comments,
                                wrap.line(
                                    import_start + from_import, parsed.line_separator, config
                                ),
                                removed=config.ignore_comments,
                                comment_prefix=config.comment_prefix,
                            )
                        )
                    new_section_output.extend(
                        with_comments(
                            from_comments,
                            wrap.line(import_start + as_import, parsed.line_separator, config),
                            removed=config.ignore_comments,
                            comment_prefix=config.comment_prefix,
                        )
                        for as_import in as_imports[from_import]
                    )

                star_import = False
                if "*" in from_imports:
                    new_section_output.append(
                        with_comments(
                            comments,
                            f"{import_start}*",
                            removed=config.ignore_comments,
                            comment_prefix=config.comment_prefix,
github timothycrosley / isort / isort / output.py View on Github external
)
                        )
                    new_section_output.extend(
                        with_comments(
                            from_comments,
                            wrap.line(import_start + as_import, parsed.line_separator, config),
                            removed=config.ignore_comments,
                            comment_prefix=config.comment_prefix,
                        )
                        for as_import in as_imports[from_import]
                    )

                star_import = False
                if "*" in from_imports:
                    new_section_output.append(
                        with_comments(
                            comments,
                            f"{import_start}*",
                            removed=config.ignore_comments,
                            comment_prefix=config.comment_prefix,
                        )
                    )
                    from_imports.remove("*")
                    star_import = True
                    comments = None

                for from_import in copy.copy(from_imports):
                    if from_import in as_imports and not config.keep_direct_and_as_imports:
                        continue
                    comment = (
                        parsed.categorized_comments["nested"].get(module, {}).pop(from_import, None)
                    )