aboutsummaryrefslogtreecommitdiffstats
path: root/lua/alpha
diff options
context:
space:
mode:
authorAriel D'Alessandro <ariel.dalessandro@auto1.com>2022-05-31 11:43:22 +0200
committerAriel D'Alessandro <ariel.dalessandro@auto1.com>2022-05-31 11:43:22 +0200
commite271d3a3ff8234a68030a6aad8bf586f44f70b7e (patch)
tree116f56a23dc155a5779a64da68dc4ee5d6c0c19c /lua/alpha
parent8ae521098fd01524156dac1bd3e1bd1280bfcb48 (diff)
downloadnvim-conf-e271d3a3ff8234a68030a6aad8bf586f44f70b7e.tar.gz
nvim-conf-e271d3a3ff8234a68030a6aad8bf586f44f70b7e.zip
refactor to get the comment types
Diffstat (limited to 'lua/alpha')
-rw-r--r--lua/alpha/php_code_actions.lua58
-rw-r--r--lua/alpha/setter_getter_template.lua58
2 files changed, 83 insertions, 33 deletions
diff --git a/lua/alpha/php_code_actions.lua b/lua/alpha/php_code_actions.lua
index 5bca3c3..f43245b 100644
--- a/lua/alpha/php_code_actions.lua
+++ b/lua/alpha/php_code_actions.lua
@@ -1,9 +1,17 @@
local null_ls = require("null-ls")
local utils = require("alpha.utils")
local ts_utils = require("nvim-treesitter.ts_utils")
+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]
+ end
+ return t1
+end
+
vim.treesitter.set_query(
"php",
"Alpha_refactor_property",
@@ -27,7 +35,12 @@ vim.treesitter.set_query(
"Alpha_refactor_property_phpdoc",
[[
[
- (text) @types
+ (type_list
+ (named_type
+ (name) @class
+ )
+ (primitive_type) @primitive
+ )
]
]]
)
@@ -52,6 +65,7 @@ local php_setter_getter_actions = {
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
@@ -65,48 +79,26 @@ local php_setter_getter_actions = {
end
local comment_node = ts_utils.get_previous_node(property_node)
- P(comment_node:type())
+ -- 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
- P(get_node_text(subNode, 0))
+ local type = get_node_text(subNode, 0)
+ if type ~= "null" then
+ table.insert(property_types, type)
+ else
+ nullable = true
+ end
end
end
- local insert = {}
- table.insert(insert, "")
- table.insert(
- insert,
- string.format(
- " public function get%s(): %s",
- utils.ucfirst(property_name),
- table.concat(property_types, "|")
- )
- )
- table.insert(insert, " {")
- table.insert(insert, string.format(" return $this->%s;", property_name))
- table.insert(insert, " }")
- table.insert(insert, "")
-
- table.insert(
- insert,
- string.format(
- " public function set%s(%s $%s): self",
- utils.ucfirst(property_name),
- table.concat(property_types, "|"),
- property_name
- )
- )
- table.insert(insert, " {")
- table.insert(insert, string.format(" $this->%s = $%s;", property_name, property_name))
- table.insert(insert, "")
- table.insert(insert, string.format(" return $this;"))
- table.insert(insert, " }")
+ local getter = generator.getGetter(property_name, nullable, property_types)
+ local setter = generator.getSetter(property_name, nullable, property_types)
-- this is using the last line of file mines 2
- vim.api.nvim_buf_set_lines(0, #context.content - 2, #context.content - 2, false, insert)
+ vim.api.nvim_buf_set_lines(0, #context.content - 2, #context.content - 2, false, tableConcat(getter, setter))
end,
},
}
diff --git a/lua/alpha/setter_getter_template.lua b/lua/alpha/setter_getter_template.lua
new file mode 100644
index 0000000..f4a6f81
--- /dev/null
+++ b/lua/alpha/setter_getter_template.lua
@@ -0,0 +1,58 @@
+local utils = require("alpha.utils")
+
+local M = {}
+
+local function isNullable(nullable)
+ if nullable then
+ return "?"
+ end
+
+ return ""
+end
+
+local function toTable(fn)
+ local t = {}
+ for s in fn:gmatch("[^\r\n]+") do
+ table.insert(t, s)
+ end
+
+ return t
+end
+
+M.getGetter = function(property, nullable, types)
+ local fn = string.format([[
+ public function get%s(): %s%s
+ {
+ return $this->%s;
+ }
+ ]],
+ utils.ucfirst(property),
+ isNullable(nullable),
+ table.concat(types, "|"),
+ property
+ )
+
+ return toTable(fn)
+end
+
+M.getSetter = function(property, nullable, types)
+ local fn = string.format([[
+ public function set%s(%s%s $%s): self
+ {
+ $this->%s = $%s;
+
+ return $this;
+ }
+ ]],
+ utils.ucfirst(property),
+ isNullable(nullable),
+ table.concat(types, "|"),
+ property,
+ property,
+ property
+ )
+
+ return toTable(fn)
+end
+
+return M