def getTextDiff(code1, code2):
differ = difflib.Differ()
changes = []
codeTokens1 = smartSplit(code1)
tokens1 = [t[0] for t in codeTokens1]
codeTokens2 = smartSplit(code2)
tokens2 = [t[0] for t in codeTokens2]
dif = differ.compare(tokens1, tokens2)
j = 0
type = ""
text = ""
line = 0
col = 0
totalCol = 0
for chr in dif:
changeType = chr[0]
changeChr = chr[2:]
if changeType != type or (len(text) > 0 and text[-1] == "\n"):
if text != "":
changes.append(SyntaxEdit(line, col, totalCol, type, text))
text = ""
type = changeType
if changeType in ["-", "+"]:
text = changeChr
if j < len(codeTokens1):
line = codeTokens1[j][1]
col = codeTokens1[j][2]
totalCol = codeTokens1[j][3]
else:
line = codeTokens1[j-1][1]
col = codeTokens1[j-1][2] + len(codeTokens1[j-1][0])
totalCol = codeTokens1[j-1][3] + len(codeTokens1[j-1][0])
else:
if changeType in ["-", "+"]:
text += changeChr
if changeType in ["-", " "]:
j += 1 # move forward in the codeTokens list
if text != "":
changes.append(SyntaxEdit(line, col, totalCol, type, text))
return changes
评论列表
文章目录