|
1 | 1 | local native = require('codemp.loader').load() |
2 | 2 |
|
3 | | -local function split_without_trim(str, sep) |
4 | | - local res = vim.fn.split(str, sep) |
5 | | - if str:sub(1,1) == "\n" then |
6 | | - table.insert(res, 1, '') |
7 | | - end |
8 | | - if str:sub(-1) == "\n" then |
9 | | - table.insert(res, '') |
10 | | - end |
11 | | - return res |
12 | | -end |
13 | | - |
14 | 3 | local function order_tuples(x) -- TODO send help... |
15 | 4 | if x[1][1] < x[2][1] then |
16 | 5 | return { { x[1][1], x[1][2] }, { x[2][1], x[2][2] } } |
@@ -54,31 +43,60 @@ local function buffer_get_content(buf) |
54 | 43 | return table.concat(lines, '\n') |
55 | 44 | end |
56 | 45 |
|
| 46 | +-- TODO this seems to work but i lost my sanity over it. if you want |
| 47 | +-- to try and make it better be warned api is madness but i will |
| 48 | +-- thank you a lot because this is an ugly mess... |
| 49 | +-- |
| 50 | +-- edge cases to test: |
| 51 | +-- - [x] add newline in buffer |
| 52 | +-- - [x] append newline to buffer |
| 53 | +-- - [x] delete multiline |
| 54 | +-- - [x] append at end of buffer |
| 55 | +-- - [x] delete at end of buffer |
| 56 | +-- - [x] delete line at end of buffer |
| 57 | +-- - [x] delete multiline at end of buffer |
| 58 | +-- - [x] autocomplete |
57 | 59 | local function buffer_set_content(buf, content, first, last) |
58 | 60 | if first == nil and last == nil then |
59 | | - local lines = split_without_trim(content, "\n") |
| 61 | + local lines = vim.split(content, "\n", {trimempty=false}) |
60 | 62 | vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) |
61 | 63 | else |
62 | 64 | local first_row, first_col, last_row, last_col |
63 | 65 | vim.api.nvim_buf_call(buf, function() |
64 | 66 | first_row = vim.fn.byte2line(first + 1) - 1 |
65 | 67 | if first_row == -2 then |
| 68 | + -- print("?? clamping start_row to start") |
66 | 69 | first_row = vim.fn.line('$') - 1 |
67 | 70 | end |
68 | | - first_col = first - (vim.fn.line2byte(first_row + 1) - 1) |
69 | | - last_row = vim.fn.byte2line(last + 1) - 1 |
70 | | - if last_row == -2 then |
71 | | - local sp = vim.split(content, "\n", {trimempty=false}) |
72 | | - last_row = first_row + (#sp - 1) |
73 | | - last_col = string.len(sp[#sp]) |
| 71 | + local first_col_byte = vim.fn.line2byte(first_row + 1) - 1 |
| 72 | + if first_col_byte == -2 then |
| 73 | + -- print("?? clamping start_col to 0") |
| 74 | + first_col = 0 |
74 | 75 | else |
75 | | - last_col = last - (vim.fn.line2byte(last_row + 1) - 1) |
| 76 | + first_col = first - first_col_byte |
| 77 | + end |
| 78 | + if first == last then |
| 79 | + last_row = first_row |
| 80 | + last_col = first_col |
| 81 | + else |
| 82 | + last_row = vim.fn.byte2line(last + 1) - 1 |
| 83 | + if last_row == -2 then |
| 84 | + print("?? clamping end_row to end") |
| 85 | + last_row = vim.fn.line('$') - 1 |
| 86 | + last_col = last - vim.fn.line2byte(last_row + 1) |
| 87 | + else |
| 88 | + last_col = last - (vim.fn.line2byte(last_row + 1) - 1) |
| 89 | + end |
76 | 90 | end |
77 | 91 | end) |
78 | | - vim.api.nvim_buf_set_text( |
79 | | - buf, first_row, first_col, last_row, last_col, |
80 | | - split_without_trim(content, "\n") |
81 | | - ) |
| 92 | + local content_array |
| 93 | + if content == "" then |
| 94 | + content_array = {} |
| 95 | + else |
| 96 | + content_array = vim.split(content, "\n", {trimempty=false}) |
| 97 | + end |
| 98 | + -- print(string.format("set [%s..%s::'%s'] -> start(row:%s col:%s) end(row:%s, col:%s)", first, last, content, first_row, first_col, last_row, last_col)) |
| 99 | + vim.api.nvim_buf_set_text(buf, first_row, first_col, last_row, last_col, content_array) |
82 | 100 | end |
83 | 101 | end |
84 | 102 |
|
|
120 | 138 |
|
121 | 139 |
|
122 | 140 | return { |
123 | | - split_without_trim = split_without_trim, |
124 | 141 | order_tuples = order_tuples, |
125 | 142 | multiline_highlight = multiline_highlight, |
126 | 143 | cursor = { |
|
0 commit comments