Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements in __main__.py #209

Merged
merged 3 commits into from
Oct 2, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals
src = git_root / path_in_repo
edited_linenums_differ = EditedLinenumsDiffer(git_root, revrange)

# 4. run black
formatted = run_black(rev2_isorted, black_config)
logger.debug("Read %s lines from edited file %s", len(rev2_isorted.lines), src)
logger.debug("Black reformat resulted in %s lines", len(formatted.lines))

# 5. get the diff between the edited and reformatted file
opcodes = diff_and_get_opcodes(rev2_isorted, formatted)

# 6. convert the diff into chunks
black_chunks = list(opcodes_to_chunks(opcodes, rev2_isorted, formatted))

# Exit early if nothing to do
if not black_chunks:
return rev2_isorted

max_context_lines = len(rev2_isorted.lines)
minimum_context_lines = BinarySearch(0, max_context_lines + 1)
last_successful_reformat = None
Expand All @@ -146,17 +161,6 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals
last_successful_reformat = rev2_isorted
break

# 4. run black
formatted = run_black(rev2_isorted, black_config)
logger.debug("Read %s lines from edited file %s", len(rev2_isorted.lines), src)
logger.debug("Black reformat resulted in %s lines", len(formatted.lines))

# 5. get the diff between the edited and reformatted file
opcodes = diff_and_get_opcodes(rev2_isorted, formatted)

# 6. convert the diff into chunks
black_chunks = list(opcodes_to_chunks(opcodes, rev2_isorted, formatted))

# 7. choose reformatted content
chosen = TextDocument.from_lines(
choose_lines(black_chunks, edited_linenums),
Expand Down