diff options
| author | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2023-01-31 09:44:13 +0100 |
|---|---|---|
| committer | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2023-01-31 09:44:13 +0100 |
| commit | 669561e21073e6efafddda25ba3ace69fb43819e (patch) | |
| tree | f94ebd60ad4cab480b5cbcb7e65dd2060696c339 /lua/alpha/php | |
| parent | 5044c70e9f62cdd06ee91e754423b07c1c18c12d (diff) | |
| download | nvim-conf-669561e21073e6efafddda25ba3ace69fb43819e.tar.gz nvim-conf-669561e21073e6efafddda25ba3ace69fb43819e.zip | |
feat: apply stylua
Diffstat (limited to 'lua/alpha/php')
| -rw-r--r-- | lua/alpha/php/gateway.lua | 198 | ||||
| -rw-r--r-- | lua/alpha/php/lsp.lua | 197 | ||||
| -rw-r--r-- | lua/alpha/php/servers.lua | 42 |
3 files changed, 214 insertions, 223 deletions
diff --git a/lua/alpha/php/gateway.lua b/lua/alpha/php/gateway.lua index 9d78bf1..51f230b 100644 --- a/lua/alpha/php/gateway.lua +++ b/lua/alpha/php/gateway.lua @@ -1,58 +1,58 @@ -local pickers = require("telescope.pickers") -local finders = require("telescope.finders") +local pickers = require "telescope.pickers" +local finders = require "telescope.finders" local conf = require("telescope.config").values -local action_state = require("telescope.actions.state") -local action_set = require("telescope.actions.set") -local actions = require("telescope.actions") +local action_state = require "telescope.actions.state" +local action_set = require "telescope.actions.set" +local actions = require "telescope.actions" local get_node_text = vim.treesitter.get_node_text local gateway = {} -local get_definition_buffer = function (type) - local uri = vim.uri_from_fname(vim.fn.getcwd() .. "/config/graphql/_".. type ..".yaml") - local buff = vim.uri_to_bufnr(uri) - vim.fn.bufload(buff) +local get_definition_buffer = function(type) + local uri = vim.uri_from_fname(vim.fn.getcwd() .. "/config/graphql/_" .. type .. ".yaml") + local buff = vim.uri_to_bufnr(uri) + vim.fn.bufload(buff) - return buff + return buff end -local get_all_definitions = function () - local mutations_definitions = gateway.get_graphql_definitions(get_definition_buffer("mutation")) - local queries_definitions = gateway.get_graphql_definitions(get_definition_buffer("query")) - for _, definition in ipairs(queries_definitions) do - table.insert(mutations_definitions, definition) - end +local get_all_definitions = function() + local mutations_definitions = gateway.get_graphql_definitions(get_definition_buffer "mutation") + local queries_definitions = gateway.get_graphql_definitions(get_definition_buffer "query") + for _, definition in ipairs(queries_definitions) do + table.insert(mutations_definitions, definition) + end - return mutations_definitions + return mutations_definitions end -- "'@=mutation(\"App\\\\GraphQL\\\\Mutation\\\\ImageMutation::deleteVehicleImage\", args[\"inspectionId\"], args[\"url\"], info)'", -local open_resolver = function (resolver) - -- need to parse to get this values - local args = string.match(resolver,"'@=mutation%((.+)%)'") - if args == nil then - args = string.match(resolver,"'@=query%((.+)%)'") - end - if args == nil then - P("error resolver") - P(resolver) - return - end - - local args_tbl = vim.split(args, ', ') - local fqn = string.match(args_tbl[1], "\"(.+)\"") - fqn = string.gsub(fqn, "\\\\", "\\") - -- P(fqn) - - R("alpha.php.lsp").find(fqn, 'phpactor') +local open_resolver = function(resolver) + -- need to parse to get this values + local args = string.match(resolver, "'@=mutation%((.+)%)'") + if args == nil then + args = string.match(resolver, "'@=query%((.+)%)'") + end + if args == nil then + P "error resolver" + P(resolver) + return + end + + local args_tbl = vim.split(args, ", ") + local fqn = string.match(args_tbl[1], '"(.+)"') + fqn = string.gsub(fqn, "\\\\", "\\") + -- P(fqn) + + R("alpha.php.lsp").find(fqn, "phpactor") end --- @param bufnr integer Buff id -gateway.get_graphql_definitions = function (bufnr) - vim.treesitter.set_query( - "yaml", - "GraphQL_endpoints", - [[ +gateway.get_graphql_definitions = function(bufnr) + vim.treesitter.set_query( + "yaml", + "GraphQL_endpoints", + [[ (document (block_node (block_mapping (block_mapping_pair value: (block_node (block_mapping (block_mapping_pair value: (block_node (block_mapping (block_mapping_pair @@ -67,71 +67,73 @@ gateway.get_graphql_definitions = function (bufnr) ))) )))) ]] - ) - - local query = vim.treesitter.get_query("yaml", "GraphQL_endpoints") - - local elements = {} - - local parsers = require("nvim-treesitter.parsers") - local parser = parsers.get_parser(bufnr) - local tree = unpack(parser:parse()) - - for id, node in query:iter_captures(tree:root(), bufnr) do - if query.captures[id] == "mutation" then - table.insert(elements, { - value = get_node_text(node, bufnr), - lnum = node:start() + 1, - buffer = bufnr, - }) - elseif query.captures[id] == "resolver" then - elements[#elements].resolver = get_node_text(node, bufnr) - end + ) + + local query = vim.treesitter.get_query("yaml", "GraphQL_endpoints") + + local elements = {} + + local parsers = require "nvim-treesitter.parsers" + local parser = parsers.get_parser(bufnr) + local tree = unpack(parser:parse()) + + for id, node in query:iter_captures(tree:root(), bufnr) do + if query.captures[id] == "mutation" then + table.insert(elements, { + value = get_node_text(node, bufnr), + lnum = node:start() + 1, + buffer = bufnr, + }) + elseif query.captures[id] == "resolver" then + elements[#elements].resolver = get_node_text(node, bufnr) end + end - return elements + return elements end --- gets the graphql function defined on a given buffer --- @param opts table: options to pass to the picker -gateway.graphql_definitions = function (opts) - opts = opts or {} - pickers.new(opts, { - promt_title = "Queries", - finder = finders.new_table({ - results = get_all_definitions(), - entry_maker = function(entry) - -- buffer = 43, - -- lnum = 82, - -- resolver = "'@=mutation(\"App\\\\GraphQL\\\\Mutation\\\\ImageMutation::deleteVehicleImage\", args[\"inspectionId\"], args[\"url\"], info)'", - -- value = "deleteVehicleImage" - return { - value = entry, - display = entry.value, - ordinal = entry.value, - path = vim.api.nvim_buf_get_name(entry.buffer), - lnum = entry.lnum, - resolver = entry.resolver, - } - end, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - attach_mappings = function(prompt_bufnr, map) - action_set.select:enhance({ - post = function() - local selection = action_state.get_selected_entry() - vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 }) - end, - }) - map("i", "<c-o>", function () - actions.close(prompt_bufnr) - local selection = action_state.get_selected_entry() - open_resolver(selection.resolver) - end) - return true +gateway.graphql_definitions = function(opts) + opts = opts or {} + pickers + .new(opts, { + promt_title = "Queries", + finder = finders.new_table { + results = get_all_definitions(), + entry_maker = function(entry) + -- buffer = 43, + -- lnum = 82, + -- resolver = "'@=mutation(\"App\\\\GraphQL\\\\Mutation\\\\ImageMutation::deleteVehicleImage\", args[\"inspectionId\"], args[\"url\"], info)'", + -- value = "deleteVehicleImage" + return { + value = entry, + display = entry.value, + ordinal = entry.value, + path = vim.api.nvim_buf_get_name(entry.buffer), + lnum = entry.lnum, + resolver = entry.resolver, + } end, - }):find() + }, + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + attach_mappings = function(prompt_bufnr, map) + action_set.select:enhance { + post = function() + local selection = action_state.get_selected_entry() + vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 }) + end, + } + map("i", "<c-o>", function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + open_resolver(selection.resolver) + end) + return true + end, + }) + :find() end return gateway diff --git a/lua/alpha/php/lsp.lua b/lua/alpha/php/lsp.lua index 506ed40..93f18f9 100644 --- a/lua/alpha/php/lsp.lua +++ b/lua/alpha/php/lsp.lua @@ -1,4 +1,4 @@ -local Path = require("plenary.path") +local Path = require "plenary.path" local lsp = {} @@ -6,124 +6,119 @@ local lsp = {} ---@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 clients = vim.lsp.get_active_clients { name = server_name } + if #clients == 0 then + return nil + end - local buffers = vim.lsp.get_buffers_by_client_id(clients[1].id) + local buffers = vim.lsp.get_buffers_by_client_id(clients[1].id) - if #buffers == 0 then - return nil - end + if #buffers == 0 then + return nil + end - return buffers[1] + return buffers[1] end --- @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 search = vim.split(fqn, "::") + local fqn_class = search[1] + local method = search[2] + + local buffer = lsp.get_lsp_buffer(client) + + 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)) + return + end + + local class_parts = vim.split(fqn_class, "\\") + local class = class_parts[#class_parts] + + -- 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) + return + end - local buffer = lsp.get_lsp_buffer(client) + 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) + 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] + 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)) - return + if class_location == nil then + P("Did not found any class: " .. class) + return end - local class_parts = vim.split(fqn_class, "\\") - local class = class_parts[#class_parts] - - -- 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) - return - end + local command = "edit" + local filename = class_location.filename - 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) - 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] - break - end - end + 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 class_location == nil then - P("Did not found any class: " .. class) - return - 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 - local command = "edit" - local filename = class_location.filename + 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 - 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)) + 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 - 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 - - 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 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 method_location == nil then - return - end - - local row = method_location.lnum - local col = method_location.col - 1 - - 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 - ) + if method_location == nil then + return + end + + local row = method_location.lnum + local col = method_location.col - 1 + + 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 diff --git a/lua/alpha/php/servers.lua b/lua/alpha/php/servers.lua index caab4a8..ba3680c 100644 --- a/lua/alpha/php/servers.lua +++ b/lua/alpha/php/servers.lua @@ -6,40 +6,34 @@ local phpactor = {} -- vim.lsp.start() should return the client function phpactor.findclass(buffer, fqn, callback) - local class_parts = vim.split(fqn, "\\") - local class = class_parts[#class_parts] - - vim.lsp.buf_request(buffer, "workspace/symbol", { query = class }, function(err, server_result, _, _) - if err then - callback( - nil, - "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: " .. fqn) - return - end - - end) + local class_parts = vim.split(fqn, "\\") + local class = class_parts[#class_parts] + + vim.lsp.buf_request(buffer, "workspace/symbol", { query = class }, function(err, server_result, _, _) + if err then + callback(nil, "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: " .. fqn) + return + end + end) end -- getting the config I can re use the client -- the next thing is to make the request with the client local config = require("lspconfig").phpactor config.root_dir = vim.fn.getcwd() -local client_id = vim.lsp.start( - config -); +local client_id = vim.lsp.start(config) local client = vim.lsp.get_client_by_id(client_id) P(client) -- use client.request or request_sync -- look for the direct of the symbol return { - phpactor = phpactor, + phpactor = phpactor, } |