# It operates on the current, potentially unsaved buffer and does not create
# or save any files. To revert a formatting, just undo.
+import difflib
import json
import subprocess
import sys
lines = stdout.split('\n')
output = json.loads(lines[0])
lines = lines[1:]
- if '\n'.join(lines) != text:
- common_length = min(len(buf), len(lines))
- buf[:common_length] = lines[:common_length]
- for line in lines[len(buf):]:
- buf.append(line)
- del buf[len(lines):]
- vim.command('goto %d' % (output['Cursor'] + 1))
+ sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
+ for op in sequence.get_opcodes():
+ if op[0] is not 'equal':
+ vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+ vim.command('goto %d' % (output['Cursor'] + 1))