Skip to content
This repository has been archived by the owner on Mar 13, 2020. It is now read-only.

Commit

Permalink
Split long rewritten import lines
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
webknjaz committed Jan 15, 2020
1 parent c769830 commit 2b095f8
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,40 @@ def rewrite_imports_in_fst(mod_fst, import_map, collection, spec, namespace, arg
imp_src[2] = plugin_collection
deps.append((plugin_namespace, plugin_collection))

respect_line_length(imp, max_chars=160)

return deps


def respect_line_length(import_node, *, max_chars=79):
"""Split the import node into multiple if it's too long."""
if import_node.type != 'from_import':
return

parent_node = import_node.parent
replacement_position = slice(
import_node.index_on_parent,
import_node.index_on_parent + 1,
)
node_bounding_box = (
import_node.bounding_box.top_left -
import_node.bounding_box.bottom_right
)

is_too_long = node_bounding_box.column <= max_chars
if not is_too_long:
return

import_targets = import_node.targets
replacement_import_nodes = [
import_node.copy() for _ in import_targets
]
for node, target in zip(replacement_import_nodes, import_targets):
node.targets = target
parent_node[replacement_position] = replacement_import_nodes
# TODO: validate new entries (replacement_import_nodes) too


def rewrite_py(src, dest, collection, spec, namespace, args):
with fst_rewrite_session(src, dest) as mod_fst:
import_deps = rewrite_imports(
Expand Down

0 comments on commit 2b095f8

Please sign in to comment.