From 68e6d9059b002fd241f71c44d1a2147757f2f9e3 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 24 Oct 2019 01:41:32 -0700 Subject: Separate wrap modes into there own module --- isort/isort.py | 31 +++--- isort/output.py | 292 --------------------------------------------------- tests/test_output.py | 9 -- 3 files changed, 16 insertions(+), 316 deletions(-) diff --git a/isort/isort.py b/isort/isort.py index d39f5c83..4076defb 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -14,7 +14,7 @@ from typing import Any, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple from isort import utils from isort.format import format_simplified -from . import output, parse, settings +from . import output, parse, settings, wrap_modes from .finders import FindersManager from .natural import nsorted from .settings import WrapModes @@ -481,18 +481,19 @@ class _SortImports: def _multi_line_reformat( self, import_start: str, from_imports: List[str], comments: Sequence[str] ) -> str: - output_mode = self.config["multi_line_output"].name.lower() - formatter = getattr(output, output_mode, output.grid) + formatter = getattr( + wrap_modes, self.config["multi_line_output"].name.lower(), wrap_modes.grid + ) dynamic_indent = " " * (len(import_start) + 1) indent = self.config["indent"] line_length = self.config["wrap_length"] or self.config["line_length"] import_statement = formatter( - import_start, - copy.copy(from_imports), - dynamic_indent, - indent, - line_length, - comments, + statement=import_start, + imports=copy.copy(from_imports), + white_space=dynamic_indent, + indent=indent, + line_length=line_length, + comments=comments, line_separator=self.line_separator, comment_prefix=self.config["comment_prefix"], include_trailing_comma=self.config["include_trailing_comma"], @@ -510,12 +511,12 @@ class _SortImports: import_statement = new_import_statement line_length -= 1 new_import_statement = formatter( - import_start, - copy.copy(from_imports), - dynamic_indent, - indent, - line_length, - comments, + statement=import_start, + imports=copy.copy(from_imports), + white_space=dynamic_indent, + indent=indent, + line_length=line_length, + comments=comments, line_separator=self.line_separator, comment_prefix=self.config["comment_prefix"], include_trailing_comma=self.config["include_trailing_comma"], diff --git a/isort/output.py b/isort/output.py index 3db9d0d7..f0b0bb9f 100644 --- a/isort/output.py +++ b/isort/output.py @@ -3,298 +3,6 @@ from typing import List, Optional from . import parse -def grid( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - if not imports: - return "" - - statement += "(" + imports.pop(0) - while imports: - next_import = imports.pop(0) - next_statement = with_comments( - comments, - statement + ", " + next_import, - removed=remove_comments, - comment_prefix=comment_prefix, - ) - if len(next_statement.split(line_separator)[-1]) + 1 > line_length: - lines = ["{}{}".format(white_space, next_import.split(" ")[0])] - for part in next_import.split(" ")[1:]: - new_line = "{} {}".format(lines[-1], part) - if len(new_line) + 1 > line_length: - lines.append("{}{}".format(white_space, part)) - else: - lines[-1] = new_line - next_import = line_separator.join(lines) - statement = with_comments( - comments, - "{},".format(statement), - removed=remove_comments, - comment_prefix=comment_prefix, - ) + "{}{}".format(line_separator, next_import) - comments = [] - else: - statement += ", " + next_import - return statement + ("," if include_trailing_comma else "") + ")" - - -def vertical( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - if not imports: - return "" - - first_import = ( - with_comments( - comments, imports.pop(0) + ",", removed=remove_comments, comment_prefix=comment_prefix - ) - + line_separator - + white_space - ) - return "{}({}{}{})".format( - statement, - first_import, - ("," + line_separator + white_space).join(imports), - "," if include_trailing_comma else "", - ) - - -def hanging_indent( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - if not imports: - return "" - - statement += imports.pop(0) - while imports: - next_import = imports.pop(0) - next_statement = with_comments( - comments, - statement + ", " + next_import, - removed=remove_comments, - comment_prefix=comment_prefix, - ) - if len(next_statement.split(line_separator)[-1]) + 3 > line_length: - next_statement = with_comments( - comments, - "{}, \\".format(statement), - removed=remove_comments, - comment_prefix=comment_prefix, - ) + "{}{}{}".format(line_separator, indent, next_import) - comments = [] - statement = next_statement - return statement - - -def vertical_hanging_indent( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - return "{0}({1}{2}{3}{4}{5}{2})".format( - statement, - with_comments(comments, "", removed=remove_comments, comment_prefix=comment_prefix), - line_separator, - indent, - ("," + line_separator + indent).join(imports), - "," if include_trailing_comma else "", - ) - - -def vertical_grid_common( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, - need_trailing_char: bool, -) -> str: - if not imports: - return "" - - statement += ( - with_comments(comments, "(", removed=remove_comments, comment_prefix=comment_prefix) - + line_separator - + indent - + imports.pop(0) - ) - while imports: - next_import = imports.pop(0) - next_statement = "{}, {}".format(statement, next_import) - current_line_length = len(next_statement.split(line_separator)[-1]) - if imports or need_trailing_char: - # If we have more 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. - current_line_length += 1 - if current_line_length > line_length: - next_statement = "{},{}{}{}".format(statement, line_separator, indent, next_import) - statement = next_statement - if include_trailing_comma: - statement += "," - return statement - - -def vertical_grid( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - return ( - vertical_grid_common( - statement, - imports, - white_space, - indent, - line_length, - comments, - line_separator=line_separator, - comment_prefix=comment_prefix, - include_trailing_comma=include_trailing_comma, - remove_comments=remove_comments, - need_trailing_char=True, - ) - + ")" - ) - - -def vertical_grid_grouped( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - return ( - vertical_grid_common( - statement, - imports, - white_space, - indent, - line_length, - comments, - line_separator=line_separator, - comment_prefix=comment_prefix, - include_trailing_comma=include_trailing_comma, - remove_comments=remove_comments, - need_trailing_char=True, - ) - + line_separator - + ")" - ) - - -def vertical_grid_grouped_no_comma( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - return ( - vertical_grid_common( - statement, - imports, - white_space, - indent, - line_length, - comments, - line_separator=line_separator, - comment_prefix=comment_prefix, - include_trailing_comma=include_trailing_comma, - remove_comments=remove_comments, - need_trailing_char=False, - ) - + line_separator - + ")" - ) - - -def noqa( - statement: str, - imports: List[str], - white_space: str, - indent: str, - line_length: int, - comments: List[str], - line_separator: str, - comment_prefix: str, - include_trailing_comma: bool, - remove_comments: bool, -) -> str: - retval = "{}{}".format(statement, ", ".join(imports)) - comment_str = " ".join(comments) - if comments: - if len(retval) + len(comment_prefix) + 1 + len(comment_str) <= line_length: - return "{}{} {}".format(retval, comment_prefix, comment_str) - else: - if len(retval) <= line_length: - return retval - if comments: - if "NOQA" in comments: - return "{}{} {}".format(retval, comment_prefix, comment_str) - else: - return "{}{} NOQA {}".format(retval, comment_prefix, comment_str) - else: - return "{}{} NOQA".format(retval, comment_prefix) - - def with_comments( comments: Optional[List[str]], original_string: str = "", diff --git a/tests/test_output.py b/tests/test_output.py index c2ab3402..0868a4a3 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -5,13 +5,4 @@ from isort import output if sys.version_info[1] > 5: from hypothesis_auto import auto_pytest_magic - auto_pytest_magic(output.grid, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.vertical, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.hanging_indent, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.vertical_hanging_indent, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.vertical_grid_common, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.vertical_grid, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.vertical_grid_grouped, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.vertical_grid_grouped_no_comma, auto_allow_exceptions_=(ValueError,)) - auto_pytest_magic(output.noqa, auto_allow_exceptions_=(ValueError,)) auto_pytest_magic(output.with_comments, auto_allow_exceptions_=(ValueError,)) -- cgit v1.2.1