Compute Levenshtein by sub-hunks to speed up a bit

This commit is contained in:
Théophile Bastian 2020-05-10 17:15:08 +02:00
parent 7936f6b3a7
commit 70a55cd728
3 changed files with 31 additions and 3 deletions

View File

@ -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:

View File

@ -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():

View File

@ -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":