diff options
| author | Ariel D'Alessandro <ariel.dalessandro@auto1.com> | 2022-07-08 10:57:43 +0200 |
|---|---|---|
| committer | Ariel D'Alessandro <ariel.dalessandro@auto1.com> | 2022-07-08 17:07:57 +0200 |
| commit | afd9b1b96861f1148b34f2007ec33fab9b6e2190 (patch) | |
| tree | 0640f259165657272fb88353dad9ba17d00a1a61 /lua/alpha | |
| parent | bd40e7372cadf3d1dff86e0aaebda9e07281180b (diff) | |
| download | nvim-conf-afd9b1b96861f1148b34f2007ec33fab9b6e2190.tar.gz nvim-conf-afd9b1b96861f1148b34f2007ec33fab9b6e2190.zip | |
Fix some stuffs, code action fix problem with query
Diffstat (limited to 'lua/alpha')
| -rw-r--r-- | lua/alpha/php_code_actions.lua | 178 | ||||
| -rw-r--r-- | lua/alpha/setter_getter_template.lua | 69 | ||||
| -rw-r--r-- | lua/alpha/snips/ft/php.lua | 2 | ||||
| -rw-r--r-- | lua/alpha/telescope/init.lua | 2 |
4 files changed, 159 insertions, 92 deletions
diff --git a/lua/alpha/php_code_actions.lua b/lua/alpha/php_code_actions.lua index 3d6b5e9..32b3ed9 100644 --- a/lua/alpha/php_code_actions.lua +++ b/lua/alpha/php_code_actions.lua @@ -5,17 +5,17 @@ local generator = require("alpha.setter_getter_template") local get_node_text = vim.treesitter.get_node_text -local function tableConcat(t1,t2) - for i=1,#t2 do - t1[#t1+1] = t2[i] +local function tableConcat(t1, t2) + for i = 1, #t2 do + t1[#t1 + 1] = t2[i] end return t1 end vim.treesitter.set_query( - "php", - "Alpha_refactor_property", - [[ + "php", + "Alpha_refactor_property", + [[ [ (union_type (primitive_type) @type @@ -32,14 +32,25 @@ vim.treesitter.set_query( vim.treesitter.set_query( "phpdoc", - "Alpha_refactor_property_phpdoc", + "Alpha_refactor_property_phpdoc_primitives", + [[ + [ + (type_list + (primitive_type) @primitive + ) + ] + ]] +) + +vim.treesitter.set_query( + "phpdoc", + "Alpha_refactor_property_phpdoc_class", [[ [ (type_list (named_type (name) @class ) - (primitive_type) @primitive ) ] ]] @@ -48,87 +59,102 @@ vim.treesitter.set_query( local M = {} M.php_setter_getter_actions = { - method = null_ls.methods.CODE_ACTION, - filetypes = { "php" }, - generator = { - fn = function(context) - local cursor_node = utils.get_master_node() + method = null_ls.methods.CODE_ACTION, + filetypes = { "php" }, + generator = { + fn = function(context) + local cursor_node = utils.get_master_node() if cursor_node == nil then return end - if cursor_node.type(cursor_node) ~= "property_declaration" then - return - end - return { - { - title = "Create Getter and Setter", - action = function() - local property_node = cursor_node + if cursor_node.type(cursor_node) ~= "property_declaration" then + return + end + return { + { + title = "Create Getter and Setter", + action = function() + local property_node = cursor_node - local property_name - local property_types = {} + local property_name + local property_types = {} local nullable = false - local query = vim.treesitter.get_query("php", "Alpha_refactor_property") - for _, match in query:iter_matches(property_node, 0) do - for id, subNode in pairs(match) do - local capture_name = query.captures[id] - if capture_name == "name" then - property_name = string.sub(get_node_text(subNode, 0), 2) - elseif capture_name == "type" then - table.insert(property_types, get_node_text(subNode, 0)) - end - end - end + local query = vim.treesitter.get_query("php", "Alpha_refactor_property") + for _, match in query:iter_matches(property_node, 0) do + for id, subNode in pairs(match) do + local capture_name = query.captures[id] + if capture_name == "name" then + property_name = string.sub(get_node_text(subNode, 0), 2) + elseif capture_name == "type" then + table.insert(property_types, get_node_text(subNode, 0)) + end + end + end local comment_node = ts_utils.get_previous_node(property_node) - -- P(comment_node:type()) - local row, column = comment_node:start() - local test_node = ts_utils.get_root_for_position(row, column) - local comment_query = vim.treesitter.get_query("phpdoc", "Alpha_refactor_property_phpdoc") - for _, match in comment_query:iter_matches(test_node, 0) do - for _, subNode in pairs(match) do - local type = get_node_text(subNode, 0) - P(type) - if type ~= "null" then - table.insert(property_types, type) - else - nullable = true + if comment_node ~= nil then + -- P(comment_node:type()) + local row, column = comment_node:start() + local test_node = ts_utils.get_root_for_position(row, column) + local primitive_query = vim.treesitter.get_query("phpdoc", "Alpha_refactor_property_phpdoc_primitives") + for _, match in primitive_query:iter_matches(test_node, 0) do + for _, subNode in pairs(match) do + local type = get_node_text(subNode, 0) + -- P(type) + if type ~= "null" then + table.insert(property_types, type) + else + nullable = true + end end - end - end + end - P(property_types) - local getter = generator.getGetter(property_name, nullable, property_types) - local setter = generator.getSetter(property_name, nullable, property_types) + local class_query = vim.treesitter.get_query("phpdoc", "Alpha_refactor_property_phpdoc_class") + for _, match in class_query:iter_matches(test_node, 0) do + for _, subNode in pairs(match) do + local type = get_node_text(subNode, 0) + table.insert(property_types, type) + end + end + end + -- P(property_types) + local getter = generator.getGetter(property_name, nullable, property_types, true) + local setter = generator.getSetter(property_name, nullable, property_types, true) -- this is using the last line of file mines 2 - vim.api.nvim_buf_set_lines(0, #context.content - 2, #context.content - 2, false, tableConcat(getter, setter)) - end, - }, - } - -- context dump - -- { - -- bufname = "/home/alpha/.dotfiles/nvim/.config/nvim/test.php", - -- bufnr = 90, - -- client_id = 1, - -- col = 20, - -- content = { "<?php", "", "class Some", "{", " /**", " * @var []int", " */", " private array $test = 1;", "}", "" }, - -- ft = "php", - -- lsp_method = "textDocument/codeAction", - -- method = "NULL_LS_CODE_ACTION", - -- range = { - -- col = 21, - -- end_col = 21, - -- end_row = 8, - -- row = 8 - -- }, - -- row = 8 - -- } + vim.api.nvim_buf_set_lines( + 0, + #context.content - 2, + #context.content - 2, + false, + tableConcat(getter, setter) + ) + end, + }, + } + -- context dump + -- { + -- bufname = "/home/alpha/.dotfiles/nvim/.config/nvim/test.php", + -- bufnr = 90, + -- client_id = 1, + -- col = 20, + -- content = { "<?php", "", "class Some", "{", " /**", " * @var []int", " */", " private array $test = 1;", "}", "" }, + -- ft = "php", + -- lsp_method = "textDocument/codeAction", + -- method = "NULL_LS_CODE_ACTION", + -- range = { + -- col = 21, + -- end_col = 21, + -- end_row = 8, + -- row = 8 + -- }, + -- row = 8 + -- } -- una solucion manar el archivo a algo php para q lo procece - -- P(context) - end, - }, + -- P(context) + end, + }, } return M diff --git a/lua/alpha/setter_getter_template.lua b/lua/alpha/setter_getter_template.lua index f4a6f81..fc2099f 100644 --- a/lua/alpha/setter_getter_template.lua +++ b/lua/alpha/setter_getter_template.lua @@ -10,8 +10,22 @@ local function isNullable(nullable) return "" end +local function getReturnTypes(nullable, types) if #types > 0 then + return string.format(": %s%s", isNullable(nullable), table.concat(types, "|")) + end + return "" +end + +local function getTypeHint(nullable, types) if #types > 0 then + return string.format("%s%s ", isNullable(nullable), table.concat(types, "|")) + end + return "" +end + + local function toTable(fn) local t = {} + -- todo check why removes all lines for s in fn:gmatch("[^\r\n]+") do table.insert(t, s) end @@ -19,40 +33,67 @@ local function toTable(fn) return t end -M.getGetter = function(property, nullable, types) - local fn = string.format([[ - public function get%s(): %s%s +M.getGetter = function(property, nullable, types, have_dock_block) + local comment = "" + if have_dock_block and #types > 0 then + comment = string.format( + [[ + /** + * @return %s + */ +]], + table.concat(types, "|") + ) + end + + local fn = string.format( + [[ + public function get%s()%s { return $this->%s; } - ]], + ]] , utils.ucfirst(property), - isNullable(nullable), - table.concat(types, "|"), + getReturnTypes(nullable, types), property ) - return toTable(fn) + return toTable(comment .. fn) end -M.getSetter = function(property, nullable, types) - local fn = string.format([[ - public function set%s(%s%s $%s): self +M.getSetter = function(property, nullable, types, have_dock_block) + + local comment = "" + if have_dock_block and #types > 0 then + comment = string.format( + [[ + /** + * @param %s $%s + * + * @return self + */ +]], + table.concat(types, "|"), + property + ) + end + local fn = string.format( + [[ + public function set%s(%s$%s): self { $this->%s = $%s; return $this; } - ]], + ]] , utils.ucfirst(property), - isNullable(nullable), - table.concat(types, "|"), + getTypeHint(nullable, types), property, property, property ) - return toTable(fn) + return toTable(comment .. fn) end return M diff --git a/lua/alpha/snips/ft/php.lua b/lua/alpha/snips/ft/php.lua index 2d69abc..5e95eda 100644 --- a/lua/alpha/snips/ft/php.lua +++ b/lua/alpha/snips/ft/php.lua @@ -65,7 +65,7 @@ local M = { ]], { i(1, "type"), - d(2, visibility, {}, "private"), + visibility(2, "private"), i(3, "var"), } ), diff --git a/lua/alpha/telescope/init.lua b/lua/alpha/telescope/init.lua index 8a4742d..1f8a115 100644 --- a/lua/alpha/telescope/init.lua +++ b/lua/alpha/telescope/init.lua @@ -15,7 +15,7 @@ local actions = require("telescope.actions") local M = {} function M.project_files() - local opts = {} -- define here if you want to define something + local opts = {show_untracked = true} -- define here if you want to define something local ok = pcall(require("telescope.builtin").git_files, opts) if not ok then require("telescope.builtin").find_files(opts) |