summaryrefslogtreecommitdiff
path: root/server/continuedev/libs/util/calculate_diff.py
blob: 99301ae775592ae7cd109c612a88c67c311defbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import difflib
from typing import List

from ...models.filesystem import FileEdit
from ...models.main import Position, Range


def calculate_diff(filepath: str, original: str, updated: str) -> List[FileEdit]:
    s = difflib.SequenceMatcher(None, original, updated)
    offset = 0  # The indices are offset by previous deletions/insertions
    edits = []
    for tag, i1, i2, j1, j2 in s.get_opcodes():
        i1, i2, j1, j2 = i1 + offset, i2 + offset, j1 + offset, j2 + offset
        replacement = updated[j1:j2]
        if tag == "equal":
            pass
        elif tag == "delete":
            edits.append(
                FileEdit.from_deletion(filepath, Range.from_indices(original, i1, i2))
            )
            offset -= i2 - i1
        elif tag == "insert":
            edits.append(
                FileEdit.from_insertion(
                    filepath, Position.from_index(original, i1), replacement
                )
            )
            offset += j2 - j1
        elif tag == "replace":
            edits.append(
                FileEdit(
                    filepath=filepath,
                    range=Range.from_indices(original, i1, i2),
                    replacement=replacement,
                )
            )
            offset += (j2 - j1) - (i2 - i1)
        else:
            raise Exception("Unexpected difflib.SequenceMatcher tag: " + tag)

    return edits


def calculate_diff2(filepath: str, original: str, updated: str) -> List[FileEdit]:
    # original_lines = original.splitlines()
    # updated_lines = updated.splitlines()
    # offset = 0
    # while len(original_lines) and len(updated_lines) and original_lines[0] == updated_lines[0]:
    #     original_lines = original_lines[1:]
    #     updated_lines = updated_lines[1:]

    # while len(original_lines) and len(updated_lines) and original_lines[-1] == updated_lines[-1]:
    #     original_lines = original_lines[:-1]
    #     updated_lines = updated_lines[:-1]

    # original = "\n".join(original_lines)
    # updated = "\n".join(updated_lines)

    edits = []
    max_iterations = 1000
    i = 0
    while not original == updated:
        # TODO - For some reason it can't handle a single newline at the end of the file?
        s = difflib.SequenceMatcher(None, original, updated)
        opcodes = s.get_opcodes()
        for edit_index in range(len(opcodes)):
            tag, i1, i2, j1, j2 = s.get_opcodes()[edit_index]
            replacement = updated[j1:j2]
            if tag == "equal":
                continue  # ;)
            elif tag == "delete":
                edits.append(
                    FileEdit.from_deletion(
                        filepath, Range.from_indices(original, i1, i2)
                    )
                )
            elif tag == "insert":
                edits.append(
                    FileEdit.from_insertion(
                        filepath, Position.from_index(original, i1), replacement
                    )
                )
            elif tag == "replace":
                edits.append(
                    FileEdit(
                        filepath=filepath,
                        range=Range.from_indices(original, i1, i2),
                        replacement=replacement,
                    )
                )
            else:
                raise Exception("Unexpected difflib.SequenceMatcher tag: " + tag)
            break

        original = apply_edit_to_str(original, edits[-1])

        i += 1
        if i > max_iterations:
            raise Exception("Max iterations reached")

    return edits


def read_range_in_str(s: str, r: Range) -> str:
    lines = s.splitlines()[r.start.line : r.end.line + 1]
    if len(lines) == 0:
        return ""

    lines[0] = lines[0][r.start.character :]
    lines[-1] = lines[-1][: r.end.character + 1]
    return "\n".join(lines)


def apply_edit_to_str(s: str, edit: FileEdit) -> str:
    read_range_in_str(s, edit.range)

    # Split lines and deal with some edge cases (could obviously be nicer)
    lines = s.splitlines()
    if s.startswith("\n"):
        lines.insert(0, "")
    if s.endswith("\n"):
        lines.append("")

    if len(lines) == 0:
        lines = [""]

    end = Position(line=edit.range.end.line, character=edit.range.end.character)
    if edit.range.end.line == len(lines) and edit.range.end.character == 0:
        end = Position(
            line=edit.range.end.line - 1,
            character=len(lines[min(len(lines) - 1, edit.range.end.line - 1)]),
        )

    before_lines = lines[: edit.range.start.line]
    after_lines = lines[end.line + 1 :]
    between_str = (
        lines[min(len(lines) - 1, edit.range.start.line)][: edit.range.start.character]
        + edit.replacement
        + lines[min(len(lines) - 1, end.line)][end.character + 1 :]
    )

    Range(
        start=edit.range.start,
        end=Position(
            line=edit.range.start.line + len(edit.replacement.splitlines()) - 1,
            character=edit.range.start.character
            + len(edit.replacement.splitlines()[-1])
            if edit.replacement != ""
            else 0,
        ),
    )

    lines = before_lines + between_str.splitlines() + after_lines
    return "\n".join(lines)