From b2ea5bb4976ad786a104d5539d47645ddef237a9 Mon Sep 17 00:00:00 2001 From: Ariel D'Alessandro Date: Mon, 26 Sep 2022 09:17:39 +0200 Subject: Add gateway graphql queries search --- lua/alpha/php/gateway.lua | 137 ++++++++++++++++++++++++++++++++++++++++++++++ lua/alpha/php/lsp.lua | 113 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 250 insertions(+) create mode 100644 lua/alpha/php/gateway.lua create mode 100644 lua/alpha/php/lsp.lua (limited to 'lua/alpha/php') diff --git a/lua/alpha/php/gateway.lua b/lua/alpha/php/gateway.lua new file mode 100644 index 0000000..d196a85 --- /dev/null +++ b/lua/alpha/php/gateway.lua @@ -0,0 +1,137 @@ +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 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) + + 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 + + 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) +end + +--- @param bufnr integer Buff id +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 + value: (block_node (block_mapping (block_mapping_pair + key: (flow_node) @mutation + value: (block_node (block_mapping (block_mapping_pair + key: (flow_node) @resolve (#eq? @resolve "resolve") + value: (flow_node) @resolver + ))) + ))) + ))) + ))) + )))) + ]] + ) + + 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 +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", "", 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 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 -- cgit v1.2.3