|
| 1 | +local M = {} |
| 2 | + |
| 3 | +---@class copilotlsp.lcs.Edit |
| 4 | +---@field kind copilotlsp.lcs.EditKind |
| 5 | +---@field text string |
| 6 | + |
| 7 | +---@enum copilotlsp.lcs.EditKind |
| 8 | +M.edit_kind = { |
| 9 | + addition = "addition", |
| 10 | + removal = "removal", |
| 11 | + unchanged = "unchanged", |
| 12 | +} |
| 13 | + |
| 14 | +--- Computes the Long Common Subequence table. |
| 15 | +--- Reference: [https://en.wikipedia.org/wiki/Longest_common_subsequence#Computing_the_length_of_the_LCS] |
| 16 | +---@param source string |
| 17 | +---@param target string |
| 18 | +function M.generate_table(source, target) |
| 19 | + local n = #source + 1 |
| 20 | + local m = #target + 1 |
| 21 | + |
| 22 | + ---@type integer[][] |
| 23 | + local lcs = {} |
| 24 | + for i = 1, n do |
| 25 | + lcs[i] = {} |
| 26 | + for j = 1, m do |
| 27 | + lcs[i][j] = 0 |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + for i = 2, n do |
| 32 | + for j = 2, m do |
| 33 | + if source:byte(i - 1) == target:byte(j - 1) then |
| 34 | + lcs[i][j] = 1 + lcs[i - 1][j - 1] |
| 35 | + else |
| 36 | + lcs[i][j] = math.max(lcs[i - 1][j], lcs[i][j - 1]) |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + return lcs |
| 41 | +end |
| 42 | + |
| 43 | +---@generic T |
| 44 | +---@param tbl T[] |
| 45 | +---@return T[] |
| 46 | +local function reverse_table(tbl) |
| 47 | + local ret = {} |
| 48 | + for i = #tbl, 1, -1 do |
| 49 | + table.insert(ret, tbl[i]) |
| 50 | + end |
| 51 | + return ret |
| 52 | +end |
| 53 | + |
| 54 | +--- Calculates a diff between two strings using LCS |
| 55 | +---@param source string |
| 56 | +---@param target string |
| 57 | +---@return copilotlsp.lcs.Edit[] |
| 58 | +function M.diff(source, target) |
| 59 | + local src_idx, trt_idx = #source + 1, #target + 1 |
| 60 | + local lcs |
| 61 | + |
| 62 | + ---@type copilotlsp.lcs.Edit[] |
| 63 | + local edits = {} |
| 64 | + local edit_idx = 1 |
| 65 | + |
| 66 | + local edit_kind = M.edit_kind |
| 67 | + |
| 68 | + while src_idx > 1 or trt_idx > 1 do |
| 69 | + if src_idx == 1 then |
| 70 | + trt_idx = trt_idx - 1 |
| 71 | + edits[edit_idx] = { |
| 72 | + kind = edit_kind.addition, |
| 73 | + text = string.char(string.byte(target, trt_idx)), |
| 74 | + } |
| 75 | + elseif trt_idx == 1 then |
| 76 | + src_idx = src_idx - 1 |
| 77 | + edits[edit_idx] = { |
| 78 | + kind = edit_kind.removal, |
| 79 | + text = string.char(string.byte(source, src_idx)), |
| 80 | + } |
| 81 | + else |
| 82 | + local src_char = string.byte(source, src_idx - 1) |
| 83 | + local trt_char = string.byte(target, trt_idx - 1) |
| 84 | + |
| 85 | + if src_char == trt_char then |
| 86 | + src_idx, trt_idx = src_idx - 1, trt_idx - 1 |
| 87 | + edits[edit_idx] = { |
| 88 | + kind = edit_kind.unchanged, |
| 89 | + text = string.char(src_char), |
| 90 | + } |
| 91 | + else |
| 92 | + lcs = lcs or M.generate_table(source, target) |
| 93 | + if lcs[src_idx - 1][trt_idx] <= lcs[src_idx][trt_idx - 1] then |
| 94 | + trt_idx = trt_idx - 1 |
| 95 | + edits[edit_idx] = { |
| 96 | + kind = edit_kind.addition, |
| 97 | + text = string.char(trt_char), |
| 98 | + } |
| 99 | + else |
| 100 | + src_idx = src_idx - 1 |
| 101 | + edits[edit_idx] = { |
| 102 | + kind = edit_kind.removal, |
| 103 | + text = string.char(src_char), |
| 104 | + } |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + edit_idx = edit_idx + 1 |
| 109 | + end |
| 110 | + |
| 111 | + return reverse_table(edits) |
| 112 | +end |
| 113 | + |
| 114 | +---@param edits copilotlsp.lcs.Edit[] |
| 115 | +---@param line integer |
| 116 | +---@param character integer |
| 117 | +---@return lsp.TextEdit[] |
| 118 | +function M.to_lsp_edits(edits, line, character) |
| 119 | + local function advance_cursor(edit) |
| 120 | + if edit.text == "\n" then |
| 121 | + line = line + 1 |
| 122 | + character = 0 |
| 123 | + else |
| 124 | + character = character + 1 |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + ---@type lsp.TextEdit[] |
| 129 | + local lsp_edits = {} |
| 130 | + local i = 1 |
| 131 | + while i < #edits do |
| 132 | + -- Skip all unchanged edits and advance cursor |
| 133 | + while i < #edits and edits[i].kind == M.edit_kind.unchanged do |
| 134 | + advance_cursor(edits[i]) |
| 135 | + i = i + 1 |
| 136 | + end |
| 137 | + |
| 138 | + -- No more edits to compute |
| 139 | + if i >= #edits then |
| 140 | + break |
| 141 | + end |
| 142 | + |
| 143 | + local new_text = "" |
| 144 | + local start_line, start_character = line, character |
| 145 | + |
| 146 | + -- Collect consecutive additions and removals |
| 147 | + while i < #edits and edits[i].kind ~= M.edit_kind.unchanged do |
| 148 | + if edits[i].kind == M.edit_kind.addition then |
| 149 | + new_text = new_text .. edits[i].text |
| 150 | + elseif edits[i].kind == M.edit_kind.removal then |
| 151 | + advance_cursor(edits[i]) |
| 152 | + else |
| 153 | + error("unexcepted edit kind " .. edits[i].kind) |
| 154 | + end |
| 155 | + i = i + 1 |
| 156 | + end |
| 157 | + |
| 158 | + ---@type lsp.TextEdit |
| 159 | + local lsp_edit = { |
| 160 | + newText = new_text, |
| 161 | + range = { |
| 162 | + start = { |
| 163 | + line = start_line, |
| 164 | + character = start_character, |
| 165 | + }, |
| 166 | + ["end"] = { |
| 167 | + line = line, |
| 168 | + character = character, |
| 169 | + }, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + table.insert(lsp_edits, lsp_edit) |
| 174 | + end |
| 175 | + |
| 176 | + return lsp_edits |
| 177 | +end |
| 178 | + |
| 179 | +return M |
0 commit comments