|
1 | 1 | local lcs = require("rzls.utils.lcs") |
2 | 2 | local kind = lcs.edit_kind |
3 | 3 | local eq = MiniTest.expect.equality |
4 | | - |
5 | | -describe("lcs", function() |
6 | | - it("calculates diff for saturday -> sunday", function() |
7 | | - local edits = lcs.diff("sunday", "saturday") |
8 | | - |
9 | | - ---@type rzls.lcs.Edit[] |
10 | | - local expected = { |
11 | | - { text = "s", kind = kind.unchanged }, |
12 | | - { text = "a", kind = kind.addition }, |
13 | | - { text = "t", kind = kind.addition }, |
14 | | - { text = "u", kind = kind.unchanged }, |
15 | | - { text = "n", kind = kind.removal }, |
16 | | - { text = "r", kind = kind.addition }, |
17 | | - { text = "d", kind = kind.unchanged }, |
18 | | - { text = "a", kind = kind.unchanged }, |
19 | | - { text = "y", kind = kind.unchanged }, |
20 | | - } |
21 | | - eq(expected, edits) |
22 | | - end) |
23 | | - |
24 | | - ---@return lsp.TextEdit |
25 | | - local function lsp_edit(new_text, start_line, start_char, end_line, end_char) |
26 | | - return { |
27 | | - newText = new_text, |
| 4 | +local T = MiniTest.new_set() |
| 5 | + |
| 6 | +T["LCS"] = MiniTest.new_set() |
| 7 | +T["LCS"]["calculates diff for saturday -> sunday"] = function() |
| 8 | + local edits = lcs.diff("sunday", "saturday") |
| 9 | + |
| 10 | + ---@type rzls.lcs.Edit[] |
| 11 | + local expected = { |
| 12 | + { text = "s", kind = kind.unchanged }, |
| 13 | + { text = "a", kind = kind.addition }, |
| 14 | + { text = "t", kind = kind.addition }, |
| 15 | + { text = "u", kind = kind.unchanged }, |
| 16 | + { text = "n", kind = kind.removal }, |
| 17 | + { text = "r", kind = kind.addition }, |
| 18 | + { text = "d", kind = kind.unchanged }, |
| 19 | + { text = "a", kind = kind.unchanged }, |
| 20 | + { text = "y", kind = kind.unchanged }, |
| 21 | + } |
| 22 | + eq(expected, edits) |
| 23 | +end |
| 24 | + |
| 25 | +T["LCS"]["converts edits to lsp.TextEdit's"] = function() |
| 26 | + local source = '<div\n\nclass="foo">' |
| 27 | + local target = '<div class="foo">' |
| 28 | + |
| 29 | + local edits = lcs.diff(source, target) |
| 30 | + local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
| 31 | + |
| 32 | + ---@type lsp.TextEdit[] |
| 33 | + local expected = { |
| 34 | + -- Replaces "\n\n" with " " |
| 35 | + { |
| 36 | + newText = " ", |
28 | 37 | range = { |
29 | 38 | start = { |
30 | | - line = start_line, |
31 | | - character = start_char, |
| 39 | + line = 0, |
| 40 | + character = 4, |
32 | 41 | }, |
33 | 42 | ["end"] = { |
34 | | - line = end_line, |
35 | | - character = end_char, |
| 43 | + line = 2, |
| 44 | + character = 0, |
36 | 45 | }, |
37 | 46 | }, |
38 | | - } |
39 | | - end |
40 | | - |
41 | | - it("converts edits to lsp.TextEdit's", function() |
42 | | - local source = '<div\n\nclass="foo">' |
43 | | - local target = '<div class="foo">' |
44 | | - |
45 | | - local edits = lcs.diff(source, target) |
46 | | - local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
| 47 | + }, |
| 48 | + } |
47 | 49 |
|
48 | | - local expected = { |
49 | | - -- Replaces "\n\n" with " " |
50 | | - lsp_edit(" ", 0, 4, 2, 0), |
51 | | - } |
| 50 | + eq(expected, text_edits) |
| 51 | +end |
52 | 52 |
|
53 | | - eq(expected, text_edits) |
54 | | - end) |
| 53 | +T["LCS"]["applies converted lsp.TextEdit's to buffer"] = function() |
| 54 | + local source = '<div class="bar">' |
| 55 | + local target = '<div\n\nclass="bar">' |
55 | 56 |
|
56 | | - it("applies converted lsp.TextEdit's to buffer", function() |
57 | | - local source = '<div class="bar">' |
58 | | - local target = '<div\n\nclass="bar">' |
| 57 | + local edits = lcs.diff(source, target) |
| 58 | + local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
59 | 59 |
|
60 | | - local edits = lcs.diff(source, target) |
61 | | - local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
| 60 | + local buf = vim.api.nvim_create_buf(false, true) |
| 61 | + vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n")) |
| 62 | + vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8") |
62 | 63 |
|
63 | | - local buf = vim.api.nvim_create_buf(false, true) |
64 | | - vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n")) |
65 | | - vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8") |
| 64 | + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
| 65 | + eq(target, table.concat(lines, "\n")) |
| 66 | +end |
66 | 67 |
|
67 | | - local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
68 | | - eq(target, table.concat(lines, "\n")) |
69 | | - end) |
| 68 | +T["LCS"]["applies converted lsp.TextEdit's to buffer with CRLF line endings"] = function() |
| 69 | + local source = '<div class="bar">' |
| 70 | + local target = '<div\r\n\r\nclass="bar">' |
70 | 71 |
|
71 | | - it("applies converted lsp.TextEdit's to buffer with CRLF line endings", function() |
72 | | - local source = '<div class="bar">' |
73 | | - local target = '<div\r\n\r\nclass="bar">' |
| 72 | + local edits = lcs.diff(source, target) |
| 73 | + local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
74 | 74 |
|
75 | | - local edits = lcs.diff(source, target) |
76 | | - local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
| 75 | + local buf = vim.api.nvim_create_buf(false, true) |
| 76 | + vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\r\n")) |
| 77 | + vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8") |
77 | 78 |
|
78 | | - local buf = vim.api.nvim_create_buf(false, true) |
79 | | - vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\r\n")) |
80 | | - vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8") |
| 79 | + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
| 80 | + eq(target, table.concat(lines, "\r\n")) |
| 81 | +end |
81 | 82 |
|
82 | | - local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
83 | | - eq(target, table.concat(lines, "\r\n")) |
84 | | - end) |
85 | | - |
86 | | - it("applies converted edits to buffer with multiple errors", function() |
87 | | - local source = [[ |
| 83 | +T["LCS"]["applies converted edits to buffer with multiple errors"] = function() |
| 84 | + local source = [[ |
88 | 85 | <div |
89 | 86 | class = "bar"> |
90 | 87 | <h1> Intentional Leading Space</h1> |
91 | 88 |
|
92 | 89 |
|
93 | 90 | </div> |
94 | 91 | ]] |
95 | | - local target = [[ |
| 92 | + local target = [[ |
96 | 93 | <div class="bar"> |
97 | 94 | <h1> Intentional Leading Space</h1> |
98 | 95 | </div> |
99 | 96 | ]] |
100 | 97 |
|
101 | | - local edits = lcs.diff(source, target) |
102 | | - local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
| 98 | + local edits = lcs.diff(source, target) |
| 99 | + local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
| 100 | + |
| 101 | + local buf = vim.api.nvim_create_buf(false, true) |
| 102 | + vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n")) |
| 103 | + vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8") |
103 | 104 |
|
104 | | - local buf = vim.api.nvim_create_buf(false, true) |
105 | | - vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n")) |
106 | | - vim.lsp.util.apply_text_edits(text_edits, buf, "utf-8") |
| 105 | + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
| 106 | + eq(target, table.concat(lines, "\n")) |
| 107 | +end |
107 | 108 |
|
108 | | - local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
109 | | - eq(target, table.concat(lines, "\n")) |
110 | | - end) |
| 109 | +T["LCS"]["applies edits to unicode characters"] = function() |
| 110 | + local source = " <h1>💩</h1>" |
| 111 | + local target = "<h1>💩</h1>" |
111 | 112 |
|
112 | | - it("applies edits to unicode characters", function() |
113 | | - local source = " <h1>💩</h1>" |
114 | | - local target = "<h1>💩</h1>" |
| 113 | + local edits = lcs.diff(source, target) |
| 114 | + local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
115 | 115 |
|
116 | | - local edits = lcs.diff(source, target) |
117 | | - local text_edits = lcs.to_lsp_edits(edits, 0, 0) |
| 116 | + local buf = vim.api.nvim_create_buf(false, true) |
| 117 | + vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n")) |
| 118 | + vim.lsp.util.apply_text_edits(text_edits, buf, "utf-16") |
118 | 119 |
|
119 | | - local buf = vim.api.nvim_create_buf(false, true) |
120 | | - vim.api.nvim_buf_set_lines(buf, 0, -1, true, vim.split(source, "\n")) |
121 | | - vim.lsp.util.apply_text_edits(text_edits, buf, "utf-16") |
| 120 | + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
| 121 | + eq(target, table.concat(lines, "\n")) |
| 122 | +end |
122 | 123 |
|
123 | | - local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, true) |
124 | | - eq(target, table.concat(lines, "\n")) |
125 | | - end) |
126 | | -end) |
| 124 | +return T |
0 commit comments