Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

feat: handle commands on the client side #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lua/code_action_menu/lsp_objects/actions/base_action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ local WorkspaceEdit = require(

local BaseAction = {}

function BaseAction:new(server_data)
vim.validate({ ['server data'] = { server_data, 'table' } })

local instance = { server_data = server_data }
function BaseAction:new(data)
vim.validate({ ['data'] = { data, 'table' } })
local instance = { server_data = data[1], client_id = data[2] }
Comment on lines +7 to +9
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you rename this here. Is it somehow related to the feature? 🤔

setmetatable(instance, self)
self.__index = self
return instance
Expand Down
19 changes: 18 additions & 1 deletion lua/code_action_menu/lsp_objects/actions/code_action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,24 @@ function CodeAction:execute()
if self:is_workspace_edit() then
vim.lsp.util.apply_workspace_edit(self.server_data.edit, 'utf-8')
elseif self:is_command() then
vim.lsp.buf.execute_command(self.server_data.command)
local client = vim.lsp.get_client_by_id(self.client_id)
local fn = client.commands[self.server_data.command.command]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my tests, this is not correct and also not how the native implementation does it. According to the specification this is just self.server_data.command. Do you have a language server which formats that differently?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you share your tests?

The navite implementation:

local fn = client.commands[command.command] or vim.lsp.commands[command.command]

https://github.com/neovim/neovim/blob/75ff156d9b11f23f6156cd6b90fb7bad0a83fd18/runtime/lua/vim/lsp/buf.lua#L512-L513

or vim.lsp.commands[self.server_data.command.command]
if fn then
local context = {}
context.diagnostic = vim.lsp.diagnostic.get_line_diagnostics()
local params = vim.lsp.util.make_range_params()
params.context = context

fn(self.server_data.command, {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the above comment: we just need to pass self.server_data here. The self.server_data.command is just the string.

Copy link
Author

@aspeddro aspeddro Apr 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, self.sever_data.command is a table.

Add the following code at line 91

print(vim.inspect('here --> ' .. type(self.server_data.command)))

See the output #49 (comment)

bufnr = vim.api.nvim_get_current_buf(),
client_id = self.client_id,
method = 'textDocument/codeAction',
params = params,
})
else
vim.lsp.buf.execute_command(self.server_data.command)
end
else
vim.api.nvim_notify(
'Failed to execute code action of unknown kind!',
Expand Down
8 changes: 4 additions & 4 deletions lua/code_action_menu/utility_functions/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ local function resolve_code_action(client_id, code_action_object)
return action_object
end

local function parse_object_as_action(code_action_object)
local function parse_object_as_action(code_action_object, client_id)
if
type(code_action_object) == 'table'
and type(code_action_object.command) == 'string'
then
return Command:new(code_action_object)
return Command:new({ code_action_object, client_id })
elseif
type(code_action_object) == 'table'
and (
type(code_action_object.edit) == 'table'
or type(code_action_object.command) == 'table'
)
then
return CodeAction:new(code_action_object)
return CodeAction:new({ code_action_object, client_id })
else
local error =
'Failed to parse unknown code action or command data structure! Skipped.'
Expand All @@ -74,7 +74,7 @@ local function parse_action_data_objects(client_id, all_code_action_objects)
code_action_object = resolve_code_action(client_id, code_action_object)
end

local action = parse_object_as_action(code_action_object)
local action = parse_object_as_action(code_action_object, client_id)

if action ~= nil then
table.insert(all_actions, action)
Expand Down