From fff9f49aa5a456f3b0892575cb5cb1d1241fb40e Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Fri, 27 Dec 2024 18:28:01 +0200 Subject: [PATCH] fix(diff): update `export()` to set `col`, `end_col`, and `text` Resolve #1416 --- lua/mini/diff.lua | 8 +++++++- tests/test_diff.lua | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lua/mini/diff.lua b/lua/mini/diff.lua index 1a959a0d..d54f1e3e 100644 --- a/lua/mini/diff.lua +++ b/lua/mini/diff.lua @@ -1589,12 +1589,18 @@ H.export_qf = function(opts) buffers = vim.tbl_filter(vim.api.nvim_buf_is_valid, buffers) table.sort(buffers) + local type_text = { add = 'Add', change = 'Change', delete = 'Delete' } + local res = {} for _, buf_id in ipairs(buffers) do local filename = vim.api.nvim_buf_get_name(buf_id) + local buf_lines = vim.api.nvim_buf_get_lines(buf_id, 0, -1, false) for _, h in ipairs(H.cache[buf_id].hunks) do - local entry = { bufnr = buf_id, filename = filename, type = h.type:sub(1, 1):upper() } + local text = type_text[h.type] + local entry = { bufnr = buf_id, filename = filename, type = text:sub(1, 1), text = text } entry.lnum, entry.end_lnum = H.get_hunk_buf_range(h) + -- Make 'add' and 'change' hunks represent actual buffer regions + entry.col, entry.end_col = 1, h.type == 'delete' and 1 or buf_lines[entry.end_lnum]:len() + 1 table.insert(res, entry) end end diff --git a/tests/test_diff.lua b/tests/test_diff.lua index d31ec19f..be8e5cb0 100644 --- a/tests/test_diff.lua +++ b/tests/test_diff.lua @@ -601,23 +601,25 @@ T['export()']['works with "qf" format'] = function() child.api.nvim_buf_set_lines(buf_id_2, 0, -1, false, { 'AAA', 'uuu', 'BBB', 'CcC', 'DDD', 'FFF' }) set_ref_text(buf_id_2, { 'AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF' }) + --stylua: ignore local ref = { - { bufnr = buf_id_1, lnum = 2, end_lnum = 2, type = 'A', filename = test_file_path }, - { bufnr = buf_id_2, lnum = 2, end_lnum = 2, type = 'A', filename = '' }, - { bufnr = buf_id_2, lnum = 4, end_lnum = 4, type = 'C', filename = '' }, - { bufnr = buf_id_2, lnum = 5, end_lnum = 5, type = 'D', filename = '' }, + { bufnr = buf_id_1, lnum = 2, col = 1, end_lnum = 2, end_col = 4, type = 'A', text = 'Add', filename = test_file_path }, + { bufnr = buf_id_2, lnum = 2, col = 1, end_lnum = 2, end_col = 4, type = 'A', text = 'Add', filename = '' }, + { bufnr = buf_id_2, lnum = 4, col = 1, end_lnum = 4, end_col = 4, type = 'C', text = 'Change', filename = '' }, + { bufnr = buf_id_2, lnum = 5, col = 1, end_lnum = 5, end_col = 1, type = 'D', text = 'Delete', filename = '' }, } eq(export('qf'), ref) end T['export()']['works with multiline hunks'] = function() local buf_id_1 = new_buf() - child.api.nvim_buf_set_lines(buf_id_1, 0, -1, false, { 'AAA', 'uuu', 'vvv', 'BBB', 'CcC', 'DdD', 'EEE', 'HHH' }) + child.api.nvim_buf_set_lines(buf_id_1, 0, -1, false, { 'AAA', 'uuu', 'vv', 'BBB', 'CcC', 'DdD', 'EEE', 'HHH' }) set_ref_text(buf_id_1, { 'AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH' }) + --stylua: ignore local ref = { - { bufnr = buf_id_1, lnum = 2, end_lnum = 3, type = 'A', filename = '' }, - { bufnr = buf_id_1, lnum = 5, end_lnum = 6, type = 'C', filename = '' }, - { bufnr = buf_id_1, lnum = 7, end_lnum = 7, type = 'D', filename = '' }, + { bufnr = buf_id_1, lnum = 2, col = 1, end_lnum = 3, end_col = 3, type = 'A', text = 'Add', filename = '' }, + { bufnr = buf_id_1, lnum = 5, col = 1, end_lnum = 6, end_col = 4, type = 'C', text = 'Change', filename = '' }, + { bufnr = buf_id_1, lnum = 7, col = 1, end_lnum = 7, end_col = 1, type = 'D', text = 'Delete', filename = '' }, } eq(export('qf'), ref) end @@ -633,7 +635,8 @@ T['export()']['respects `opts.scope`'] = function() eq(export('qf', { scope = 'current' }), {}) set_buf(buf_id_1) - eq(export('qf', { scope = 'current' }), { { bufnr = 2, lnum = 2, end_lnum = 2, type = 'A', filename = '' } }) + local ref_entry = { bufnr = 2, lnum = 2, col = 1, end_lnum = 2, end_col = 4, type = 'A', text = 'Add', filename = '' } + eq(export('qf', { scope = 'current' }), { ref_entry }) end T['export()']['validates arguments'] = function()