Skip to content

Commit

Permalink
more clarity; rename JoinTransformer to CodeEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
ikamensh committed Jun 15, 2023
1 parent 8f835d6 commit 189af1c
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 90 deletions.
10 changes: 8 additions & 2 deletions src/flynt/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import astor

from flynt.cli_messages import farewell_message
from flynt.process import (
from flynt.code_editor import (
fstringify_code_by_line,
fstringify_concats,
fstringify_static_joins,
Expand Down Expand Up @@ -186,6 +185,13 @@ def fstringify_files(
return changed_files


farewell_message = (
"Please run your tests before committing. Did flynt get a perfect conversion? give it a star at: "
"\n~ https://github.com/ikamensh/flynt ~"
"\nThank you for using flynt. Upgrade more projects and recommend it to your colleagues!\n"
)


def _print_report(
state: State,
found_files: int,
Expand Down
5 changes: 0 additions & 5 deletions src/flynt/cli_messages.py

This file was deleted.

27 changes: 18 additions & 9 deletions src/flynt/process.py → src/flynt/code_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
log = logging.getLogger(__name__)


class JoinTransformer:
"""JoinTransformer merges original and edited code by keeping track of last edit location.
class CodeEditor:
"""CodeEditor applies local edits, and keeps most of the original code.
As parsing and unparsing a file risks unintended changes ( escaped chars,
formatting quirks, etc.), we try to keep most characters of the original,
Expand Down Expand Up @@ -60,16 +60,19 @@ def __init__(
self.last_line = 0
self.last_idx = 0
self.used_up = False
self.output: str | None = None

def fstringify_code_by_line(self) -> Tuple[str, int]:
def edit(self) -> Tuple[str, int]:
"""Apply edits to the original code."""
assert not self.used_up, "Tried to use JT twice."
for chunk in self.candidates_iter:
self.fill_up_to(chunk)
self.try_chunk(chunk)

self.add_rest()
self.used_up = True
return "".join(self.results)[:-1], self.count_expressions
self.output = "".join(self.results)[:-1]
return self.output, self.count_expressions

def fill_up_to(self, chunk: Union[Chunk, AstChunk]) -> None:
start_line, start_idx, _ = (chunk.start_line, chunk.start_idx, chunk.end_idx)
Expand All @@ -87,14 +90,17 @@ def fill_up_to(self, chunk: Union[Chunk, AstChunk]) -> None:

self.last_idx = start_idx

def fill_up_to_line(self, start_line: int) -> None:
while self.last_line < start_line:
def fill_up_to_line(self, line: int) -> None:
while self.last_line < line:
self.results.append(self.src_lines[self.last_line] + "\n")
self.last_line += 1

def try_chunk(self, chunk: Union[Chunk, AstChunk]) -> None:
"""Try applying a transform to a chunk of code.
for line in self.src_lines[chunk.start_line : chunk.start_line + chunk.n_lines]:
Transformation function is free to decide to refuse conversion,
e.g. in edge cases that are not supported."""
for line in self.src_lines[chunk.start_line : chunk.end_line+1]:
if noqa_regex.findall(line):
# user does not wish for this line to be converted.
return
Expand Down Expand Up @@ -126,6 +132,9 @@ def maybe_replace(
converted: str,
rest: str,
) -> None:
"""Given a possible edit, see if we want to apply it.
For example, we might not want to change multiple lines. """
if contract_lines:
if get_quote_type(str(chunk)) in (qt.triple_double, qt.triple_single):
lines = converted.split("\\n")
Expand Down Expand Up @@ -222,9 +231,9 @@ def _transform_code(
state: State,
) -> Tuple[str, int]:
"""returns fstringified version of the code and amount of lines edited."""
return JoinTransformer(
return CodeEditor(
code,
state.len_limit,
candidates_iter_factory,
transform_func,
).fstringify_code_by_line()
).edit()
2 changes: 1 addition & 1 deletion test/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import pytest

import flynt
from flynt.api import farewell_message
from flynt.cli import run_flynt_cli
from flynt.cli_messages import farewell_message


def test_cli_no_args(capsys):
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from flynt.process import fstringify_code_by_line, fstringify_concats
from flynt.code_editor import fstringify_code_by_line, fstringify_concats
from flynt.state import State


Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from flynt.process import fstringify_code_by_line
from flynt.code_editor import fstringify_code_by_line
from flynt.state import State


Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_files_len_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from flynt.process import fstringify_concats
from flynt.code_editor import fstringify_concats


@pytest.mark.parametrize("filename", ["multiline_limit.py"])
Expand Down
Loading

0 comments on commit 189af1c

Please sign in to comment.