Compute Levenshtein by sub-hunks to speed up a bit
This commit is contained in:
parent
7936f6b3a7
commit
70a55cd728
3 changed files with 31 additions and 3 deletions
|
@ -1,6 +1,9 @@
|
|||
""" Tools to make smart edition to apply a hunk """
|
||||
|
||||
from collections import OrderedDict
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Levenshtein:
|
||||
|
|
|
@ -102,6 +102,7 @@ def configure_log(args):
|
|||
logging.basicConfig(level=logging.CRITICAL)
|
||||
|
||||
logging.getLogger("libtmux").setLevel(logging.INFO)
|
||||
logging.getLogger("tmux").setLevel(logging.INFO)
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
import logging
|
||||
from hunk_changes import InlineLevenshtein, HunkLevenshtein
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LineMovement:
|
||||
""" A movement to a given line, absolute or relative """
|
||||
|
@ -179,21 +182,42 @@ class VimSession:
|
|||
self.set_mode("command")
|
||||
|
||||
def apply_hunk(self, hunk):
|
||||
logger.debug("Applying hunk @{}/{}".format(hunk.startsrc, hunk.starttgt))
|
||||
pre_lines = []
|
||||
post_lines = []
|
||||
cur_subhunk_line = hunk.starttgt
|
||||
cur_target_line = hunk.starttgt
|
||||
for b_line in hunk.text:
|
||||
u_line = b_line.decode("utf8")
|
||||
op = u_line[0]
|
||||
line = u_line[1:]
|
||||
|
||||
if op in ["+", " "]:
|
||||
if op == "+":
|
||||
post_lines.append(line)
|
||||
if op in ["-", " "]:
|
||||
cur_target_line += 1
|
||||
elif op == "-":
|
||||
pre_lines.append(line)
|
||||
elif op == " ":
|
||||
if pre_lines or post_lines:
|
||||
logger.debug(
|
||||
"\tApplying subhunk @{} span {}/{}".format(
|
||||
cur_subhunk_line, len(pre_lines), len(post_lines)
|
||||
)
|
||||
)
|
||||
self.apply_subhunk(pre_lines, post_lines, cur_subhunk_line)
|
||||
cur_target_line += 1
|
||||
cur_subhunk_line = cur_target_line
|
||||
pre_lines = []
|
||||
post_lines = []
|
||||
|
||||
if pre_lines or post_lines:
|
||||
self.apply_subhunk(pre_lines, post_lines, cur_subhunk_line)
|
||||
|
||||
def apply_subhunk(self, pre_lines, post_lines, startline_target):
|
||||
hunk_levenshtein = HunkLevenshtein(pre_lines, post_lines).compute()
|
||||
line_ops = hunk_levenshtein["ops"]
|
||||
|
||||
line_mvt = LineMovement(absolute=hunk.starttgt)
|
||||
line_mvt = LineMovement(absolute=startline_target)
|
||||
|
||||
for op, positions, values, cost in line_ops:
|
||||
if op == "L":
|
||||
|
|
Loading…
Reference in a new issue