pull/118/head
ray-x 3 years ago
parent 941b3fc80f
commit e869cedba3

@ -192,138 +192,3 @@ code_action.code_action_prompt = function()
end
return code_action
--[[
code_action.code_action_handler = util.mk_handler(function(err, results, ctx, cfg)
log(ctx)
if err ~= nil then
log("code action err", err, results, ctx, cfg)
return
end
on_code_action_results(results, ctx)
end)
local function code_action_request(params)
local bufnr = vim.api.nvim_get_current_buf()
local method = 'textDocument/codeAction'
vim.lsp.buf_request_all(bufnr, method, params, function(results)
on_code_action_results(results, {bufnr = bufnr, method = method, params = params})
end)
end
local function on_code_action_results(results, ctx, client)
local trace = log
local action_tuples = {}
local data = {"  Auto Fix <C-o> Apply <C-e> Exit"}
for client_id, result in pairs(results) do
for i, action in pairs(result.result or {}) do
local title = 'apply action'
trace(action)
if action.edit and action.edit.title then
local edit = action.edit
title = edit.title:gsub("\r\n", "")
title = title:gsub("\n", "")
elseif action.title then
title = action.title:gsub("\r\n", "")
title = title:gsub("\n", "")
elseif action.command and action.command.title then
title = action.command.title:gsub("\n", "")
title = title:gsub("\n", "")
end
local edit = action.edit or {}
-- trace(edit.documentChanges)
if edit.documentChanges or edit.changes then
local changes = edit.documentChanges or edit.changes
-- trace(action.edit.documentChanges)
for _, change in pairs(changes or {}) do
-- trace(change)
if change.edits then
title = title .. " [newText:]"
for _, ed in pairs(change.edits) do
-- trace(ed)
if ed.newText and ed.newText ~= "" then
local newText = ed.newText:gsub("\n\t", "")
newText = newText:gsub("\n", "")
title = title .. " (" .. newText
if ed.range then
title = title .. " line: " .. tostring(ed.range.start.line) .. ")"
else
title = title .. ")"
end
end
end
elseif change.newText and change.newText ~= "" then
local newText = change.newText:gsub("\"\n\t\"", "")
newText = newText:gsub("\n", "")
title = title .. " (newText: " .. newText
if change.range then
title = title .. " line: " .. tostring(change.range.start.line) .. ")"
else
title = title .. ")"
end
end
end
end
title = string.format("[%d] %s", i, title)
table.insert(data, title)
table.insert(action_tuples, {client_id, action, title, i})
end
end
log(action_tuples)
log(data)
if #action_tuples == 0 then
vim.notify('No code actions available', vim.log.levels.INFO)
return
end
local width = 42
for _, str in ipairs(data) do
if #str > width then
width = #str
end
end
local divider = string.rep('', width + 2)
table.insert(data, 2, divider)
local listview = gui.new_list_view {
items = data,
width = width + 4,
loc = "top_center",
relative = "cursor",
rawdata = true,
data = data,
on_confirm = function(item)
trace(item)
local action_chosen = nil
for _, value in pairs(action_tuples) do
if value[3] == item then
action_chosen = value
return require('navigator.lspwrapper').on_user_choice(action_chosen, ctx)
end
end
end,
on_move = function(pos)
trace(pos)
return pos
end
}
log("new buffer", listview.bufnr)
vim.api.nvim_buf_add_highlight(listview.bufnr, -1, 'Title', 0, 0, -1)
-- let move down 2 pos
ListViewCtrl:on_next()
ListViewCtrl:on_next()
end
]]

@ -179,81 +179,3 @@ M.inline = function()
end
return M
-- function M.run_action()
-- log('run code len action')
--
-- assert(#vim.lsp.buf_get_clients() > 0, 'Must have a client running to use lsp code action')
-- if not lsphelper.check_capabilities('code_lens') then
-- return
-- end
--
-- local line = api.nvim_win_get_cursor(0)[1]
-- local bufnr = api.nvim_get_current_buf()
--
-- local lenses = codelens.get(bufnr)
-- log(lenses)
-- if lenses == nil or #lenses == 0 then
-- return
-- end
-- local width = 40
--
-- local data = {
-- ' ' .. _NgConfigValues.icons.code_lens_action_icon .. ' CodeLens Action <C-o> Apply <C-e> Exit',
-- }
-- local idx = 1
-- for i, lens in pairs(lenses) do
-- if lens.range.start.line == (line - 1) then
-- local title = lens.command.title:gsub('\r\n', '\\r\\n')
-- title = title:gsub('\n', '\\n')
-- title = string.format('[%d] %s', idx, title)
-- table.insert(data, title)
-- lenses[i].display_title = title
-- width = math.max(width, #lens.command.title)
-- idx = idx + 1
-- end
-- end
-- local apply = require('navigator.lspwrapper').apply_action
-- local function apply_action(action)
-- local action_chosen = nil
-- for key, value in pairs(lenses) do
-- if value.display_title == action then
-- action_chosen = value
-- end
-- end
-- if action_chosen == nil then
-- log('no match for ', action, lenses)
-- return
-- end
-- apply(action_chosen, M.codelens_ctx)
-- end
--
-- local divider = string.rep('─', width + 2)
--
-- table.insert(data, 2, divider)
-- if #data > 2 then
-- local lv = gui.new_list_view({
-- items = data,
-- width = width + 4,
-- loc = 'top_center',
-- relative = 'cursor',
-- rawdata = true,
-- data = data,
-- on_confirm = function(pos)
-- log(pos)
-- apply_action(pos)
-- end,
-- on_move = function(pos)
-- log(pos)
-- return pos
-- end,
-- })
--
-- vim.api.nvim_buf_add_highlight(lv.bufnr, -1, 'Title', 0, 0, -1)
-- else
-- print('no codelense in current line')
-- end
-- end
--

@ -460,85 +460,3 @@ function M.request(method, hdlr) -- e.g textDocument/reference
end
return M
-- local function apply_action(action, client)
-- -- local client = vim.lsp.get_client_by_id(ctx.client_id)
-- log(action)
--
-- assert(action ~= nil, 'action must not be nil')
-- if action.edit then
-- require('vim.lsp.util').apply_workspace_edit(action.edit)
-- end
-- if action.command then
-- local command = type(action.command) == 'table' and action.command or action
-- local fn = vim.lsp.commands and vim.lsp.commands[command.command]
-- if fn then
-- local enriched_ctx = vim.deepcopy(ctx)
-- enriched_ctx.client_id = client.id
-- fn(command, ctx)
-- else
-- require('vim.lsp.buf').execute_command(command)
-- end
-- end
-- end
--
-- M.apply_action = apply_action
--
-- function M.on_user_choice(action_tuple, ctx)
-- log(ctx)
-- if not action_tuple then
-- return
-- end
-- log(action_tuple)
-- -- textDocument/codeAction can return either Command[] or CodeAction[]
-- --
-- -- CodeAction
-- -- ...
-- -- edit?: WorkspaceEdit -- <- must be applied before command
-- -- command?: Command
-- --
-- -- Command:
-- -- title: string
-- -- command: string
-- -- arguments?: any[]
-- --
-- local function apply_code_action(action, client)
-- if action.edit then
-- util.apply_workspace_edit(action.edit)
-- end
-- if action.command then
-- local command = type(action.command) == 'table' and action.command or action
-- local fn = client.commands[command.command] or vim.lsp.commands[command.command]
-- if fn then
-- local enriched_ctx = vim.deepcopy(ctx)
-- enriched_ctx.client_id = client.id
-- fn(command, enriched_ctx)
-- else
-- M.execute_command(command)
-- end
-- end
-- end
--
-- if action_tuple[1] ~= nil then
-- ctx.client_id = action_tuple[1]
-- end
-- local client = vim.lsp.get_client_by_id(ctx.client_id)
-- local action = action_tuple[2]
-- if
-- not action.edit
-- and client
-- and type(client.resolved_capabilities.code_action) == 'table'
-- and client.resolved_capabilities.code_action.resolveProvider
-- then
-- client.request('codeAction/resolve', action, function(err, resolved_action, c)
-- log(resolved_action, c)
-- if err then
-- vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR)
-- return
-- end
-- apply_code_action(resolved_action, client)
-- end)
-- else
-- apply_action(action, client)
-- end
-- end

Loading…
Cancel
Save