|
| 1 | +---@diagnostic disable: unused-local |
| 2 | +local MiniTest = require("mini.test") |
| 3 | +local helpers = require("fzf-lua.test.helpers") |
| 4 | +local eq = helpers.expect.equality |
| 5 | +local new_set = MiniTest.new_set |
| 6 | + |
| 7 | +local T = new_set() |
| 8 | + |
| 9 | +T["codeaction"] = new_set() |
| 10 | + |
| 11 | +T["codeaction"]["preview does not mutate same-position text edits"] = function() |
| 12 | + local utils = require("fzf-lua.utils") |
| 13 | + local codeaction = require("fzf-lua.previewer.codeaction") |
| 14 | + local original_lsp_get_clients = utils.lsp_get_clients |
| 15 | + local bufnr = vim.api.nvim_create_buf(true, true) |
| 16 | + local text_edits = { |
| 17 | + { |
| 18 | + range = { |
| 19 | + start = { line = 0, character = 7 }, |
| 20 | + ["end"] = { line = 0, character = 24 }, |
| 21 | + }, |
| 22 | + newText = "strings.CutSuffix", |
| 23 | + }, |
| 24 | + { |
| 25 | + range = { |
| 26 | + start = { line = 0, character = 7 }, |
| 27 | + ["end"] = { line = 0, character = 24 }, |
| 28 | + }, |
| 29 | + newText = "strings.TrimSuffix", |
| 30 | + }, |
| 31 | + } |
| 32 | + local before = vim.deepcopy(text_edits) |
| 33 | + |
| 34 | + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { |
| 35 | + "if strings.HasSuffix(name, suffix) {", |
| 36 | + "\treturn strings.TrimSuffix(name, suffix)", |
| 37 | + "}", |
| 38 | + }) |
| 39 | + |
| 40 | + ---duplication is caused by the stubbign logic. |
| 41 | + ---@diagnostic disable-next-line: duplicate-set-field |
| 42 | + utils.lsp_get_clients = function(opts) |
| 43 | + if opts.id == 1 then |
| 44 | + return { |
| 45 | + { |
| 46 | + offset_encoding = "utf-16", |
| 47 | + dynamic_capabilities = { get = function() end }, |
| 48 | + supports_method = function() return false end, |
| 49 | + server_capabilities = {}, |
| 50 | + }, |
| 51 | + } |
| 52 | + end |
| 53 | + return original_lsp_get_clients(opts) |
| 54 | + end |
| 55 | + |
| 56 | + local ok, err = pcall(function() |
| 57 | + local uri = vim.uri_from_bufnr(bufnr) |
| 58 | + codeaction.builtin.preview_action_tuple({ |
| 59 | + opts = { |
| 60 | + _items = { |
| 61 | + { |
| 62 | + ctx = { client_id = 1, bufnr = bufnr }, |
| 63 | + action = { edit = { changes = { [uri] = text_edits } } }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + diff_opts = { ctxlen = 3 }, |
| 68 | + _resolved_actions = { false }, |
| 69 | + }, 1) |
| 70 | + end) |
| 71 | + utils.lsp_get_clients = original_lsp_get_clients |
| 72 | + vim.api.nvim_buf_delete(bufnr, { force = true }) |
| 73 | + |
| 74 | + if not ok then error(err) end |
| 75 | + eq(text_edits, before) |
| 76 | +end |
| 77 | + |
| 78 | +return T |
0 commit comments