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 """
|
""" Tools to make smart edition to apply a hunk """
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Levenshtein:
|
class Levenshtein:
|
||||||
|
|
|
@ -102,6 +102,7 @@ def configure_log(args):
|
||||||
logging.basicConfig(level=logging.CRITICAL)
|
logging.basicConfig(level=logging.CRITICAL)
|
||||||
|
|
||||||
logging.getLogger("libtmux").setLevel(logging.INFO)
|
logging.getLogger("libtmux").setLevel(logging.INFO)
|
||||||
|
logging.getLogger("tmux").setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
import logging
|
||||||
from hunk_changes import InlineLevenshtein, HunkLevenshtein
|
from hunk_changes import InlineLevenshtein, HunkLevenshtein
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class LineMovement:
|
class LineMovement:
|
||||||
""" A movement to a given line, absolute or relative """
|
""" A movement to a given line, absolute or relative """
|
||||||
|
@ -179,21 +182,42 @@ class VimSession:
|
||||||
self.set_mode("command")
|
self.set_mode("command")
|
||||||
|
|
||||||
def apply_hunk(self, hunk):
|
def apply_hunk(self, hunk):
|
||||||
|
logger.debug("Applying hunk @{}/{}".format(hunk.startsrc, hunk.starttgt))
|
||||||
pre_lines = []
|
pre_lines = []
|
||||||
post_lines = []
|
post_lines = []
|
||||||
|
cur_subhunk_line = hunk.starttgt
|
||||||
|
cur_target_line = hunk.starttgt
|
||||||
for b_line in hunk.text:
|
for b_line in hunk.text:
|
||||||
u_line = b_line.decode("utf8")
|
u_line = b_line.decode("utf8")
|
||||||
op = u_line[0]
|
op = u_line[0]
|
||||||
line = u_line[1:]
|
line = u_line[1:]
|
||||||
|
|
||||||
if op in ["+", " "]:
|
if op == "+":
|
||||||
post_lines.append(line)
|
post_lines.append(line)
|
||||||
if op in ["-", " "]:
|
cur_target_line += 1
|
||||||
|
elif op == "-":
|
||||||
pre_lines.append(line)
|
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()
|
hunk_levenshtein = HunkLevenshtein(pre_lines, post_lines).compute()
|
||||||
line_ops = hunk_levenshtein["ops"]
|
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:
|
for op, positions, values, cost in line_ops:
|
||||||
if op == "L":
|
if op == "L":
|
||||||
|
|
Loading…
Reference in a new issue