aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorAriel D'Alessandro <dalessandro.ariel@gmail.com>2022-09-27 08:55:49 +0200
committerAriel D'Alessandro <dalessandro.ariel@gmail.com>2022-09-27 08:56:08 +0200
commit7185e12beac1086ee5a4f50b322a4d6a8cc35699 (patch)
tree0d64fedd5946afb2f7090f6dda9fe6cd5bf60f96 /lua
parentfd46c9fa03cf4870b535ec3a7b8a7e87d5586015 (diff)
downloadnvim-conf-7185e12beac1086ee5a4f50b322a4d6a8cc35699.tar.gz
nvim-conf-7185e12beac1086ee5a4f50b322a4d6a8cc35699.zip
Small improvments to telescope config
Diffstat (limited to 'lua')
-rw-r--r--lua/alpha/scratchs/laravel.lua121
-rw-r--r--lua/alpha/telescope/setup.lua26
2 files changed, 124 insertions, 23 deletions
diff --git a/lua/alpha/scratchs/laravel.lua b/lua/alpha/scratchs/laravel.lua
index a98473f..a5eafdd 100644
--- a/lua/alpha/scratchs/laravel.lua
+++ b/lua/alpha/scratchs/laravel.lua
@@ -2,18 +2,119 @@
-- Want to show the uri beside de controller
-- Why just stetics
--
--- Need autocommand on buffenter bufwritepost
--- Read the namespace, class name, and methods using treesitter
--- create a namespace
--- get the routes on json format
--- look routes that matches class + method
--- write extmark with method and URI
--- using the async from the command should work
--- cache should be ideal but not require
-
-
+-- Give a warning if the controller is missing some assigned routes
+--
-- Snippet for creating relationships
-- A select node for the type of relation on the return type
-- base on that node which function should call
-- base on the name and type create the name??
+
+local artisan = require("laravel.artisan")
+
+local get_node_text = vim.treesitter.get_node_text
+
+vim.treesitter.set_query(
+ "php",
+ "laravel_route_info",
+ [[
+ (namespace_definition (namespace_name) @namespace)
+ (class_declaration (name) @class)
+ (method_declaration
+ (visibility_modifier) @visibility
+ (name) @method
+ )
+ ]]
+)
+
+local function is_same_class(action, class)
+ return string.sub(action, 1, string.len(class)) == class
+end
+
+local function set_route_to_methods()
+ local namespace = vim.api.nvim_create_namespace("laravel.routes")
+ vim.api.nvim_buf_clear_namespace(0, namespace, 0, -1)
+
+ -- get the json
+ local data, result = artisan.exec("route:list --json")
+ if result ~= 0 then
+ P("Error getting laravel routes")
+ return
+ end
+ local route_list = vim.fn.json_decode(data)
+
+ local parsers = require("nvim-treesitter.parsers")
+ local parser = parsers.get_parser(0)
+ local tree = unpack(parser:parse())
+
+ local query = vim.treesitter.get_query("php", "laravel_route_info")
+
+ local class, class_namespace, methods, visibilities = "", "", {}, {}
+ local class_pos = 0
+
+ for id, node in query:iter_captures(tree:root(), 0) do
+ if query.captures[id] == "class" then
+ class = get_node_text(node, 0)
+ class_pos = node:start()
+ elseif query.captures[id] == "namespace" then
+ class_namespace = get_node_text(node, 0)
+ elseif query.captures[id] == "method" then
+ table.insert(methods, {
+ pos = node:start(),
+ name = get_node_text(node, 0),
+ })
+ elseif query.captures[id] == "visibility" then
+ table.insert(visibilities, get_node_text(node, 0))
+ end
+ end
+
+ local class_methods = {}
+
+ local full_class = string.format("%s\\%s", class_namespace, class)
+ for idx, method in ipairs(methods) do
+ if visibilities[idx] == "public" then
+ table.insert(class_methods, {
+ full = string.format("%s\\%s@%s", class_namespace, class, method.name),
+ name = method.name,
+ pos = method.pos,
+ })
+ end
+ end
+
+ local errors = {}
+ for _, route in pairs(route_list) do
+ local found = false
+ for _, method in pairs(class_methods) do
+ if route.action == method.full then
+ local nice_route = string.format(
+ "[Method: %s, URI: %s, Middleware: %s]",
+ route.method,
+ route.uri,
+ vim.fn.join(route.middleware, ",")
+ )
+ vim.api.nvim_buf_set_extmark(0, namespace, method.pos, 0, { virt_text = { { nice_route, "comment" } } })
+ found = true
+ end
+ end
+
+ if is_same_class(route.action, full_class) and not found then
+ table.insert(errors, {
+ lnum = class_pos,
+ col = 0,
+ message = string.format("missing %s", route.action),
+ })
+ end
+ end
+
+ if #errors > 0 then
+ vim.diagnostic.set(namespace, 0, errors)
+ end
+end
+
+local group = vim.api.nvim_create_augroup("laravel", {})
+
+vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost" }, {
+ pattern = { "*Controller.php" },
+ group = group,
+ callback = set_route_to_methods, -- Or myvimfun
+})
diff --git a/lua/alpha/telescope/setup.lua b/lua/alpha/telescope/setup.lua
index 09dcd14..cc92175 100644
--- a/lua/alpha/telescope/setup.lua
+++ b/lua/alpha/telescope/setup.lua
@@ -23,7 +23,7 @@ require("telescope").setup({
width = 0.95,
height = 0.85,
-- preview_cutoff = 120,
- prompt_position = "top",
+ prompt_position = "bottom",
horizontal = {
preview_width = function(_, cols, _)
@@ -75,17 +75,17 @@ require("telescope").setup({
["<C-k>"] = actions.cycle_history_next,
["<C-j>"] = actions.cycle_history_prev,
- ["<c-space>"] = function(prompt_bufnr)
- local opts = {
- callback = actions.toggle_selection,
- loop_callback = actions.send_selected_to_qflist,
- }
- require("telescope").extensions.hop._hop_loop(prompt_bufnr, opts)
- end,
-
- ["<C-w>"] = function()
- vim.api.nvim_input("<c-s-w>")
- end,
+ -- ["<c-space>"] = function(prompt_bufnr)
+ -- local opts = {
+ -- callback = actions.toggle_selection,
+ -- loop_callback = actions.send_selected_to_qflist,
+ -- }
+ -- require("telescope").extensions.hop._hop_loop(prompt_bufnr, opts)
+ -- end,
+
+ -- ["<C-w>"] = function()
+ -- vim.api.nvim_input("<c-s-w>")
+ -- end,
},
},
@@ -153,7 +153,7 @@ require("telescope").setup({
},
["ui-select"] = {
- require("telescope.themes").get_dropdown({
+ require("telescope.themes").get_cursor({
-- even more opts
}),
},