From f71a3c926017212d86311537a7e970e247ea4cd4 Mon Sep 17 00:00:00 2001 From: Ariel D'Alessandro Date: Mon, 6 Sep 2021 23:21:56 +0200 Subject: Update refactor to do the setter and getter --- lua/alpha/init.lua | 1 + lua/alpha/refactor.lua | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) (limited to 'lua/alpha') diff --git a/lua/alpha/init.lua b/lua/alpha/init.lua index 3d3ce78..bc869ec 100644 --- a/lua/alpha/init.lua +++ b/lua/alpha/init.lua @@ -2,3 +2,4 @@ require("alpha.telescope") require("alpha.refactoring") require("alpha.snippets") require("alpha.harpoon") +require("alpha.refactor") diff --git a/lua/alpha/refactor.lua b/lua/alpha/refactor.lua index dbf4c5f..d4cea4e 100644 --- a/lua/alpha/refactor.lua +++ b/lua/alpha/refactor.lua @@ -43,7 +43,7 @@ M.setter_getter = function() -- visibility modifier -- type_list (optional) could have a ? to be nullable or could have a list -- property_element - print(vim.inspect(p)) + -- print(vim.inspect(p)) local comment = '' @@ -51,21 +51,56 @@ M.setter_getter = function() local prev = ts_utils.get_previous_node(node, false, false) if (node.type(prev) == 'comment') then comment = ts_utils.get_node_text(prev)[1] + comment = string.match(comment, '@var (.*) ') end -- print(vim.inspect(node.type(prev))) -- print(vim.inspect(ts_utils.get_node_text(prev))) end - print(vim.inspect(comment)) + -- print(vim.inspect(comment)) -- if type_list is not present could go up and read the comment -- and base on a regular expression, and get the type -- if the type is array want to also read to see if is a typed array -- - local classNode = node.parent() + local classNode = node:parent() + local classEnd = classNode:end_() + local variable = string.sub(p.property_element, 2) + local function firstToUpper(str) + return (str:gsub("^%l", string.upper)) + end + + local varType = p.type_list - vim.api.nvim_buf_set_lines(bufnr, ) + local insert = {} + table.insert(insert, "") + if (comment ~= nil) then + table.insert(insert, " /**") + table.insert(insert, string.format("\t * @return %s", comment)) + table.insert(insert, " */") + end + table.insert(insert, string.format("\tpublic function get%s(): %s", firstToUpper(variable), varType)) + table.insert(insert, " {") + table.insert(insert, string.format("\t\treturn $this->%s;", variable)) + table.insert(insert, " }") + table.insert(insert, "") + + if (comment ~= nil) then + table.insert(insert, " /**") + table.insert(insert, string.format("\t * @param %s %s", comment, p.property_element)) + table.insert(insert, " *") + table.insert(insert, " * @return self") + table.insert(insert, " */") + end + table.insert(insert, string.format("\tpublic function set%s(%s %s): self", firstToUpper(variable), varType, p.property_element)) + table.insert(insert, " {") + table.insert(insert, string.format("\t\t$this->%s = %s;", variable, p.property_element)) + table.insert(insert, "") + table.insert(insert, string.format("\t\treturn $this;")) + table.insert(insert, " }") + + vim.api.nvim_buf_set_lines(bufnr, classEnd, classEnd, false, insert) end -- cgit v1.2.3 From 7d3824bb003a52eae168e45c616b168a9fd4ecdf Mon Sep 17 00:00:00 2001 From: Ariel D'Alessandro Date: Tue, 7 Sep 2021 07:54:56 +0200 Subject: Try new snippet, try to fix indent problem on yaml files --- lua/alpha/snippets.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lua/alpha') diff --git a/lua/alpha/snippets.lua b/lua/alpha/snippets.lua index 378fa20..9a42b6c 100644 --- a/lua/alpha/snippets.lua +++ b/lua/alpha/snippets.lua @@ -9,10 +9,8 @@ local d = ls.dynamic_node ls.snippets = { php = { - s("getter", { - t("/**"), - t(" *"), - t(" *"), + s("$", { + t("$this->"), }) } } -- cgit v1.2.3