diff options
| author | Ariel D'Alessandro <ariel.dalessandro@auto1.com> | 2022-09-26 09:17:39 +0200 |
|---|---|---|
| committer | Ariel D'Alessandro <ariel.dalessandro@auto1.com> | 2022-09-26 09:18:14 +0200 |
| commit | b2ea5bb4976ad786a104d5539d47645ddef237a9 (patch) | |
| tree | 63ced30c20f1ca65c23a009d316e5d5cbfdef3c4 /lua/alpha/php/lsp.lua | |
| parent | 4457e657255ba071497b30356676a42f6f184cdf (diff) | |
| download | nvim-conf-b2ea5bb4976ad786a104d5539d47645ddef237a9.tar.gz nvim-conf-b2ea5bb4976ad786a104d5539d47645ddef237a9.zip | |
Add gateway graphql queries search
Diffstat (limited to 'lua/alpha/php/lsp.lua')
| -rw-r--r-- | lua/alpha/php/lsp.lua | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/lua/alpha/php/lsp.lua b/lua/alpha/php/lsp.lua new file mode 100644 index 0000000..dc6fa1c --- /dev/null +++ b/lua/alpha/php/lsp.lua @@ -0,0 +1,113 @@ +local Path = require("plenary.path") + +local lsp = {} + +lsp.get_intelephense_buffer = function () + local clients = vim.lsp.get_active_clients({ name = "intelephense" }) + if #clients == 0 then + return + end + + local intelephense = clients[1] + + local attached_buffers = intelephense.attached_buffers + + for key, val in pairs(attached_buffers) do + if val then + return key + end + end + + return nil +end + + +local get_index_buffer = function () + local uri = vim.uri_from_fname(vim.fn.getcwd() .. "/public/index.php") + local buff = vim.uri_to_bufnr(uri) + vim.fn.bufload(buff) + + return buff +end + + +--- @param search string: fully name to search Class::method +lsp.find = function (search) + search = vim.split(search, "::") + local class = search[1] + local method = search[2] + + local buffer = lsp.get_intelephense_buffer() + + if buffer == nil then + -- TODO I want to not require this. + -- I can load a buffer but does not start the lsp + P("load a php buffer to load the lsp server") + return + end + + 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) + 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: " .. class) + return + end + + local class_location = locations[1] + + vim.lsp.buf_request( + buffer, + "workspace/symbol", + { query = method }, + function(method_err, method_server_result, _, _) + if err then + vim.api.nvim_err_writeln("Error when finding workspace symbols: " .. method_err.message) + return + end + + local method_locations = vim.lsp.util.symbols_to_items(method_server_result or {}, buffer) or {} + if vim.tbl_isempty(method_locations) then + P("empty response looking for method") + return + end + + local method_location = nil + for _, value in ipairs(method_locations) do + if value.filename == class_location.filename then + method_location = value + break + end + end + + if method_location == nil then + return + end + + local command = "edit" + local filename = method_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)) + end + + 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) +end + +return lsp |