diff options
Diffstat (limited to 'lua/alpha')
| -rw-r--r-- | lua/alpha/php/lsp.lua | 195 |
1 files changed, 111 insertions, 84 deletions
diff --git a/lua/alpha/php/lsp.lua b/lua/alpha/php/lsp.lua index 93f18f9..37e0bc0 100644 --- a/lua/alpha/php/lsp.lua +++ b/lua/alpha/php/lsp.lua @@ -2,123 +2,150 @@ local Path = require "plenary.path" local lsp = {} ----Return a buffer id for the given client +local open_filename = function(filename) + if vim.api.nvim_buf_get_name(0) ~= filename then + filename = Path:new(vim.fn.fnameescape(filename)):normalize(vim.loop.cwd()) + pcall(vim.cmd, string.format("%s %s", "edit", filename)) + end +end + ---@param server_name string ----@return integer|nil -lsp.get_lsp_buffer = function(server_name) +---@return table|nil, boolean +local get_client = function(server_name) local clients = vim.lsp.get_active_clients { name = server_name } - if #clients == 0 then - return nil + local client = clients[1] or nil + local new_instance = false + + if not client then + local server = require("lspconfig")[server_name] + local config = server.make_config(vim.fn.getcwd()) + local client_id = vim.lsp.start(config) + client = vim.lsp.get_client_by_id(client_id) + new_instance = true end - local buffers = vim.lsp.get_buffers_by_client_id(clients[1].id) + return client, new_instance +end - if #buffers == 0 then - return nil - end +local function go_to(client, is_new_instance, full_class, method) + local class_parts = vim.split(full_class, "\\") + local class = class_parts[#class_parts] - return buffers[1] -end + local resp = client.request_sync("workspace/symbol", { query = class }, nil) ---- @param fqn string: fully name to search Class::method -lsp.find = function(fqn, client) - local search = vim.split(fqn, "::") - local fqn_class = search[1] - local method = search[2] + local locations = vim.lsp.util.symbols_to_items(resp.result or {}, nil) or {} + if vim.tbl_isempty(locations) then + vim.api.nvim_err_writeln("Empty response looking for class: " .. full_class) + if is_new_instance then + vim.lsp.stop_client(client.id) + end + return + end - local buffer = lsp.get_lsp_buffer(client) + local class_location = nil + for idx, location in pairs(locations) do + if location.text == string.format("[Class] %s", full_class) then + class_location = locations[idx] + break + end + end - if buffer == nil then - -- TODO I want to not require this. - -- I can load a buffer but does not start the lsp - vim.api.nvim_err_writeln(string.format("No buffer available for client %s", client)) + if class_location == nil then + vim.api.nvim_err_writeln("Could not find class for : " .. full_class) + if is_new_instance then + vim.lsp.stop_client(client.id) + end return end - local class_parts = vim.split(fqn_class, "\\") - local class = class_parts[#class_parts] + open_filename(class_location.filename) + + local params = vim.lsp.util.make_position_params(0) + if is_new_instance then + vim.lsp.buf_attach_client(0, client.id) + end - -- TODO phpactor does not support FQN search need to de sepparated - -- can query get the optiosn, match the full name and type class - -- I don't think gives the namespace - -- So I will get the full name, not the - -- the option is to get the class - -- open the buffer, change in the window - -- call the vim.lsp.buf_document_symbol({on_list = function (list) end}) - -- grab the information and move the cursor - -- if method does not exist show error but stay in the class - -- Can have the code extracted to work with both implementations ? - -- Potentially could have a way to create the classes if they don't exist - -- With the file creater action - vim.lsp.buf_request(buffer, "workspace/symbol", { query = class }, function(err, server_result, _, _) - if err then - vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. err.message) + vim.lsp.buf_request(0, "textDocument/documentSymbol", params, function(method_err, method_server_result, _, _) + if method_err then + vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. method_err.message) + if is_new_instance then + vim.lsp.stop_client(client.id) + end return end - local locations = vim.lsp.util.symbols_to_items(server_result or {}, buffer) or {} - if vim.tbl_isempty(locations) then - vim.api.nvim_err_writeln("Empty response looking for class: " .. fqn_class) + local method_locations = vim.lsp.util.symbols_to_items(method_server_result or {}, 0) or {} + if vim.tbl_isempty(method_locations) then + vim.api.nvim_err_writeln(string.format("empty response looking for method: %s", method or "__invoke")) + if is_new_instance then + vim.lsp.stop_client(client.id) + end return end - local class_location = nil - for idx, location in pairs(locations) do - if location.text == string.format("[Class] %s", fqn_class) then - class_location = locations[idx] + + local method_location = nil + for _, value in ipairs(method_locations) do + if value.text == string.format("[Method] %s", method or "__invoke") then + method_location = value break end end - if class_location == nil then - P("Did not found any class: " .. class) + if method_location == nil then + if is_new_instance then + vim.lsp.stop_client(client.id) + end return end - local command = "edit" - local filename = class_location.filename + local row = method_location.lnum + local col = method_location.col - 1 - if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then - filename = Path:new(vim.fn.fnameescape(filename)):normalize(vim.loop.cwd()) - pcall(vim.cmd, string.format("%s %s", command, filename)) + if row and col then + local ok, err_msg = pcall(vim.api.nvim_win_set_cursor, 0, { row, col }) + if not ok then + vim.api.nvim_err_writeln("Erro setting row and col " .. err_msg) + end + vim.cmd "normal zt" + end + if is_new_instance then + vim.lsp.stop_client(client.id) end + end) +end - local params = vim.lsp.util.make_position_params(0) - vim.lsp.buf_request(0, "textDocument/documentSymbol", params, function(method_err, method_server_result, _, _) - if err then - vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. method_err.message) - return - end +---Return a buffer id for the given client +---@param server_name string +---@return integer|nil +lsp.get_lsp_buffer = function(server_name) + local clients = vim.lsp.get_active_clients { name = server_name } + if #clients == 0 then + return nil + end - local method_locations = vim.lsp.util.symbols_to_items(method_server_result or {}, buffer) or {} - if vim.tbl_isempty(method_locations) then - P(string.format("empty response looking for method %s", method)) - return - end + local buffers = vim.lsp.get_buffers_by_client_id(clients[1].id) - local method_location = nil - for _, value in ipairs(method_locations) do - if value.text == string.format("[Method] %s", method) then - method_location = value - break - end - end + if #buffers == 0 then + return nil + end - if method_location == nil then - return - end + return buffers[1] +end + +--- @param fqn string: fully name to search Class::method +lsp.find = function(fqn, server_name) + local search = vim.split(fqn, "::") + local fqn_class = search[1] + local method = search[2] - local row = method_location.lnum - local col = method_location.col - 1 + local client, is_new_instance = get_client(server_name) - if row and col then - local ok, err_msg = pcall(vim.api.nvim_win_set_cursor, 0, { row, col }) - if not ok then - vim.api.nvim_err_writeln("error seting row and col: " .. err_msg) - end - vim.cmd "normal zt" - end - end) - end) + if not client then + vim.api.nvim_err_writeln "Could not get lsp client" + return + end + + go_to(client, is_new_instance, fqn_class, method) end return lsp |