diff --git a/lua/laravel/ui_select/actions.lua b/lua/laravel/pickers/common/actions.lua similarity index 70% rename from lua/laravel/ui_select/actions.lua rename to lua/laravel/pickers/common/actions.lua index 79d806e..66784c1 100644 --- a/lua/laravel/ui_select/actions.lua +++ b/lua/laravel/pickers/common/actions.lua @@ -1,4 +1,4 @@ -local ui_run = require("laravel.ui_select.ui_run") +-- local ui_run = require("laravel.pickers.ui_select.ui_run") local lsp = require("laravel._lsp") local app = require("laravel").app @@ -29,7 +29,7 @@ local function go(route) lsp.go_to(action[1], action[2]) end -function M.run(command) +function M.run(command, ui_run) vim.schedule(function() ui_run(command) end) @@ -42,35 +42,25 @@ function M.open_route(route) end function M.open_browser(route) - local app_url = nil - app("configs") - :get("app.url", function(value) - app_url = value - end) - :wait() - if not app_url then - return - end - - local uri = route.uri - for capturedString in uri:gmatch("{(.-)}") do - local val = vim.fn.input(capturedString .. ": ") - uri = uri:gsub("{" .. capturedString .. "}", val) - end + app("configs_repository"):get("app.url"):thenCall(function(app_url) + local uri = route.uri + for capturedString in uri:gmatch("{(.-)}") do + local val = vim.fn.input(capturedString .. ": ") + uri = uri:gsub("{" .. capturedString .. "}", val) + end - local url = string.format("%s/%s", app_url, uri) - local command = nil + local url = string.format("%s/%s", app_url, uri) + local command = nil - if vim.fn.executable("xdg-open") == 1 then - command = "xdg-open" - elseif vim.fn.executable("open") == 1 then - command = "open" - end - if not command then - return - end + if vim.fn.executable("xdg-open") == 1 then + command = "xdg-open" + elseif vim.fn.executable("open") == 1 then + command = "open" + end + if not command then + return + end - vim.schedule(function() vim.fn.system({ command, url }) end) end @@ -114,9 +104,9 @@ function M.open_resource(resource) vim.ui.select(lines, { prompt = "Resources", kind = "resources", - }, function(choice) - if choice ~= nil then - vim.cmd("edit " .. resource.path .. "/" .. choice) + }, function(resource) + if resource ~= nil then + vim.cmd("edit " .. resource.path .. "/" .. resource.name) end end) end diff --git a/lua/laravel/pickers/common/ui_run.lua b/lua/laravel/pickers/common/ui_run.lua new file mode 100644 index 0000000..ce01cf6 --- /dev/null +++ b/lua/laravel/pickers/common/ui_run.lua @@ -0,0 +1,70 @@ +local Layout = require("nui.layout") +local Popup = require("nui.popup") +local app = require("laravel").app + +---@class UIPopups +---@field entry_popup NuiPopup +---@field help_popup ?NuiPopup + +local M = {} + +function M.entry_popup() + return Popup({ + enter = true, + border = { + style = "rounded", + text = { + top = "Artisan", + top_align = "center", + }, + }, + buf_options = { + buftype = "prompt", + }, + win_options = { + winhighlight = "Normal:LaravelPrompt", + }, + }) +end + +---@param popups UIPopups +function M.ui_run(command, popups) + local boxes = { + Layout.Box(popups.entry_popup, { size = 3 }), -- 3 because of borders to be 1 row + } + + if popups.help_popup then + table.insert(boxes, Layout.Box(popups.help_popup, { grow = 1 })) + end + + local layout = Layout({ + position = "50%", + size = { + width = "80%", + height = "90%", + }, + relative = "editor", + }, Layout.Box(boxes, { dir = "col" })) + + popups.entry_popup:map("i", "", function() + layout:unmount() + end) + popups.entry_popup:map("n", "", function() + layout:unmount() + end) + + local prompt = "$ artisan " .. command.name .. " " + vim.fn.prompt_setprompt(popups.entry_popup.bufnr, prompt) + vim.fn.prompt_setcallback(popups.entry_popup.bufnr, function(input) + layout:unmount() + local args = vim.fn.split(input, " ", false) + table.insert(args, 1, command.name) + + app("runner"):run("artisan", args) + end) + + layout:mount() + vim.cmd([[startinsert]]) +end + +return M diff --git a/lua/laravel/pickers/telescope/actions.lua b/lua/laravel/pickers/telescope/actions.lua new file mode 100644 index 0000000..960202b --- /dev/null +++ b/lua/laravel/pickers/telescope/actions.lua @@ -0,0 +1,53 @@ +local common_actions = require("laravel.pickers.common.actions") +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") +local ui_run = require("laravel.pickers.telescope.ui_run") +local app = require("laravel").app + +local M = {} + +function M.run(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + local command = entry.value + + common_actions.run(command, ui_run) +end + +function M.make_run(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + local command = entry.value + + common_actions.make_run(command) +end + +function M.open_route(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + + common_actions.open_route(entry.value) +end + +function M.open_browser(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + + common_actions.open_browser(entry.value) +end + +function M.re_run_command(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + + app("runner"):run(entry.value.name, entry.value.args, entry.value.opts) +end + +function M.open_relation(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + + common_actions.open_relation(entry.value) +end + +return M diff --git a/lua/laravel/telescope/make_entry.lua b/lua/laravel/pickers/telescope/make_entry.lua similarity index 90% rename from lua/laravel/telescope/make_entry.lua rename to lua/laravel/pickers/telescope/make_entry.lua index 8cd4ddd..8ea0675 100644 --- a/lua/laravel/telescope/make_entry.lua +++ b/lua/laravel/pickers/telescope/make_entry.lua @@ -1,4 +1,4 @@ -local entry_display = require "telescope.pickers.entry_display" +local entry_display = require("telescope.pickers.entry_display") local M = {} @@ -37,7 +37,7 @@ end function M.gen_from_laravel_routes(opts) opts = opts or {} - local displayer = entry_display.create { + local displayer = entry_display.create({ separator = " ", hl_chars = { ["["] = "TelescopeBorder", ["]"] = "TelescopeBorder" }, items = { @@ -45,14 +45,14 @@ function M.gen_from_laravel_routes(opts) { width = 40 }, { remaining = true }, }, - } + }) local make_display = function(entry) - return displayer { + return displayer({ { vim.fn.join(entry.value.methods, "|"), "TelescopeResultsConstant" }, { entry.value.uri, "TelescopeResultsIdentifier" }, { entry.value.name or "", "TelescopeResultsFunction" }, - } + }) end return function(route) @@ -68,7 +68,7 @@ end function M.gen_from_model_relations(opts) opts = opts or {} - local displayer = entry_display.create { + local displayer = entry_display.create({ separator = " ", hl_chars = { ["["] = "TelescopeBorder", ["]"] = "TelescopeBorder" }, items = { @@ -76,14 +76,14 @@ function M.gen_from_model_relations(opts) { width = 20 }, { remaining = true }, }, - } + }) local make_display = function(entry) - return displayer { + return displayer({ { entry.value.class_name, "TelescopeResultsConstant" }, { entry.value.type, "TelescopeResultsIdentifier" }, { entry.value.extra_information or "", "TelescopeResultsFunction" }, - } + }) end return function(relation) diff --git a/lua/laravel/pickers/telescope/pickers/artisan.lua b/lua/laravel/pickers/telescope/pickers/artisan.lua new file mode 100644 index 0000000..3fe3f1f --- /dev/null +++ b/lua/laravel/pickers/telescope/pickers/artisan.lua @@ -0,0 +1,68 @@ +local conf = require("telescope.config").values +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local previewers = require("telescope.previewers") +local preview = require("laravel.pickers.telescope.preview") +local actions = require("laravel.pickers.telescope.actions") + +---@class LaravelArtisanPicker +---@field commands_repository CommandsRepository +local artisan_picker = {} + +function artisan_picker:new(cache_commands_repository) + local instance = { + commands_repository = cache_commands_repository, + } + setmetatable(instance, self) + self.__index = self + + return instance +end + +function artisan_picker:run(opts) + opts = opts or {} + + return self.commands_repository:all():thenCall(function(commands) + pickers + .new(opts, { + prompt_title = "Artisan commands", + finder = finders.new_table({ + results = commands, + entry_maker = function(command) + return { + value = command, + display = command.name, + ordinal = command.name, + } + end, + }), + previewer = previewers.new_buffer_previewer({ + title = "Help", + get_buffer_by_name = function(_, entry) + return entry.value.name + end, + define_preview = function(preview_self, entry) + local command_preview = preview.command(entry.value) + + vim.api.nvim_buf_set_lines(preview_self.state.bufnr, 0, -1, false, command_preview.lines) + + local hl = vim.api.nvim_create_namespace("laravel") + for _, value in pairs(command_preview.highlights) do + vim.api.nvim_buf_add_highlight(preview_self.state.bufnr, hl, value[1], value[2], value[3], value[4]) + end + end, + }), + sorter = conf.file_sorter(), + attach_mappings = function(_, map) + map("i", "", actions.run) + + return true + end, + }) + :find() + end, function(error) + vim.api.nvim_err_writeln(error) + end) +end + +return artisan_picker diff --git a/lua/laravel/telescope/pickers/commands.lua b/lua/laravel/pickers/telescope/pickers/commands.lua similarity index 51% rename from lua/laravel/telescope/pickers/commands.lua rename to lua/laravel/pickers/telescope/pickers/commands.lua index 50e5d62..af897f4 100644 --- a/lua/laravel/telescope/pickers/commands.lua +++ b/lua/laravel/pickers/telescope/pickers/commands.lua @@ -42,41 +42,41 @@ function commands_picker:run(opts) end pickers - .new(opts, { - prompt_title = "User Commands", - finder = finders.new_table({ - results = commands, - entry_maker = function(command) - return { - value = command, - display = command.display, - ordinal = command.display, - } - end, - }), - previewer = previewers.new_buffer_previewer({ - title = "Description", - get_buffer_by_name = function(_, entry) - return entry.value.name - end, - define_preview = function(self_preview, entry) - vim.api.nvim_buf_set_lines(self_preview.state.bufnr, 0, -1, false, { entry.value.desc }) - end, - }), - sorter = conf.file_sorter(), - attach_mappings = function(_, map) - map("i", "", function(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - local command = entry.value + .new(opts, { + prompt_title = "User Commands", + finder = finders.new_table({ + results = commands, + entry_maker = function(command) + return { + value = command, + display = command.display, + ordinal = command.display, + } + end, + }), + previewer = previewers.new_buffer_previewer({ + title = "Description", + get_buffer_by_name = function(_, entry) + return entry.value.name + end, + define_preview = function(self_preview, entry) + vim.api.nvim_buf_set_lines(self_preview.state.bufnr, 0, -1, false, { entry.value.desc }) + end, + }), + sorter = conf.file_sorter(), + attach_mappings = function(_, map) + map("i", "", function(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + local command = entry.value - self.runner:run(command.executable, command.cmd, command.opts) - end) + self.runner:run(command.executable, command.cmd, command.opts) + end) - return true - end, - }) - :find() + return true + end, + }) + :find() end return commands_picker diff --git a/lua/laravel/pickers/telescope/pickers/history.lua b/lua/laravel/pickers/telescope/pickers/history.lua new file mode 100644 index 0000000..57a80c5 --- /dev/null +++ b/lua/laravel/pickers/telescope/pickers/history.lua @@ -0,0 +1,46 @@ +local conf = require("telescope.config").values +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local actions = require("laravel.pickers.telescope.actions") + +local history_picker = {} + +function history_picker:new(history) + local instance = { + history_provider = history, + } + setmetatable(instance, self) + self.__index = self + return instance +end + +function history_picker:run(opts) + opts = opts or {} + + pickers + .new(opts, { + prompt_title = "Laravel Command History", + finder = finders.new_table({ + results = self.history_provider:get(), + entry_maker = function(history_entry) + return { + value = history_entry, + display = string.format("%s %s", history_entry.name, table.concat(history_entry.args, " ")), + ordinal = string.format("%s %s", history_entry.name, table.concat(history_entry.args, " ")), + } + end, + }), + previewer = false, + sorter = conf.prefilter_sorter({ + sorter = conf.generic_sorter(opts or {}), + }), + attach_mappings = function(_, map) + map("i", "", actions.re_run_command) + + return true + end, + }) + :find() +end + +return history_picker diff --git a/lua/laravel/pickers/telescope/pickers/make.lua b/lua/laravel/pickers/telescope/pickers/make.lua new file mode 100644 index 0000000..e38c427 --- /dev/null +++ b/lua/laravel/pickers/telescope/pickers/make.lua @@ -0,0 +1,72 @@ +local conf = require("telescope.config").values +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local previewers = require("telescope.previewers") +local preview = require("laravel.pickers.telescope.preview") +local actions = require("laravel.pickers.telescope.actions") +local is_make_command = require("laravel.utils").is_make_command + +---@class LaravelMakePicker +---@field commands_repository CommandsRepository +local make_picker = {} + +function make_picker:new(cache_commands_repository) + local instance = { + commands_repository = cache_commands_repository, + } + setmetatable(instance, self) + self.__index = self + + return instance +end + +function make_picker:run(opts) + opts = opts or {} + + self.commands_repository:all():thenCall(function(commands) + pickers + .new(opts, { + prompt_title = "Make commands", + finder = finders.new_table({ + results = vim + .iter(commands) + :filter(function(command) + return is_make_command(command.name) + end) + :totable(), + entry_maker = function(command) + return { + value = command, + display = command.name, + ordinal = command.name, + } + end, + }), + previewer = previewers.new_buffer_previewer({ + title = "Help", + get_buffer_by_name = function(_, entry) + return entry.value.name + end, + define_preview = function(preview_self, entry) + local command_preview = preview.command(entry.value) + + vim.api.nvim_buf_set_lines(preview_self.state.bufnr, 0, -1, false, command_preview.lines) + + local hl = vim.api.nvim_create_namespace("laravel") + for _, value in pairs(command_preview.highlights) do + vim.api.nvim_buf_add_highlight(preview_self.state.bufnr, hl, value[1], value[2], value[3], value[4]) + end + end, + }), + sorter = conf.file_sorter(), + attach_mappings = function(_, map) + map("i", "", actions.make_run) + + return true + end, + }) + :find() + end) +end + +return make_picker diff --git a/lua/laravel/pickers/telescope/pickers/related.lua b/lua/laravel/pickers/telescope/pickers/related.lua new file mode 100644 index 0000000..5463c9c --- /dev/null +++ b/lua/laravel/pickers/telescope/pickers/related.lua @@ -0,0 +1,103 @@ +local pickers = require("telescope.pickers") +local make_entry = require("laravel.pickers.telescope.make_entry") +local finders = require("telescope.finders") +local conf = require("telescope.config").values +local actions = require("laravel.pickers.telescope.actions") + +---@class LaravelRelatedPicker +---@field class LaravelClassService +---@field api LaravelApi +local related_picker = {} + +local build_relation = function(info, relation_type) + if next(info) == nil then + return nil + end + if relation_type == "observers" and info["observer"][2] ~= nil then + return { + class = info["observer"][2], + type = relation_type, + extra_information = info["event"], + } + elseif relation_type == "relations" then + return { + class = info["related"], + type = relation_type, + extra_information = info["type"] .. " " .. info["name"], + } + elseif relation_type == "policy" then + return { + class = info[1], + type = relation_type, + extra_information = "", + } + end + return nil +end + +local types = { "observers", "relations", "policy" } + +function related_picker:new(class, api) + local instance = { + class = class, + api = api, + } + setmetatable(instance, self) + self.__index = self + + return instance +end + +function related_picker:run(opts) + opts = opts or {} + + local bufnr = vim.api.nvim_get_current_buf() + + return self.class + :get(bufnr) + :thenCall(function(class) + return self.api:send("artisan", { "model:show", class.fqn, "--json" }) + end) + :thenCall(function(response) + local model_info = response:json() + + local relations = {} + for _, relation_type in ipairs(types) do + if model_info[relation_type] and #model_info[relation_type] > 0 then + if type(model_info[relation_type]) == "table" and model_info[relation_type][1] then + for _, info in ipairs(model_info[relation_type]) do + local relation = build_relation(info, relation_type) + if relation ~= nil then + table.insert(relations, relation) + end + end + else + local relation = build_relation({ model_info[relation_type] }, relation_type) + if relation ~= nil then + table.insert(relations, relation) + end + end + end + end + + pickers + .new(opts, { + prompt_title = "Related Files", + finder = finders.new_table({ + results = relations, + entry_maker = make_entry.gen_from_model_relations(opts), + }), + sorter = conf.prefilter_sorter({ + sorter = conf.generic_sorter(opts or {}), + }), + attach_mappings = function(_, map) + map("i", "", actions.open_relation) + + return true + end, + }) + :find() + end) +end + +return related_picker diff --git a/lua/laravel/telescope/pickers/resources.lua b/lua/laravel/pickers/telescope/pickers/resources.lua similarity index 50% rename from lua/laravel/telescope/pickers/resources.lua rename to lua/laravel/pickers/telescope/pickers/resources.lua index d19d09a..66e9a1f 100644 --- a/lua/laravel/telescope/pickers/resources.lua +++ b/lua/laravel/pickers/telescope/pickers/resources.lua @@ -35,39 +35,39 @@ function resources_picker:run(opts) end pickers - .new(opts, { - prompt_title = "User Commands", - finder = finders.new_table({ - results = resources, - entry_maker = function(resource) - return { - value = resource, - display = resource.name, - ordinal = resource.name, - } - end, - }), - previewer = previewers.new_termopen_previewer({ - get_command = function(entry) - return { "ls", "-1", entry.value.path } - end, - }), + .new(opts, { + prompt_title = "User Commands", + finder = finders.new_table({ + results = resources, + entry_maker = function(resource) + return { + value = resource, + display = resource.name, + ordinal = resource.name, + } + end, + }), + previewer = previewers.new_termopen_previewer({ + get_command = function(entry) + return { "ls", "-1", entry.value.path } + end, + }), - sorter = conf.file_sorter(), + sorter = conf.file_sorter(), - attach_mappings = function(_, map) - map("i", "", function(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - local resource = entry.value + attach_mappings = function(_, map) + map("i", "", function(prompt_bufnr) + actions.close(prompt_bufnr) + local entry = action_state.get_selected_entry() + local resource = entry.value - require("telescope.builtin").find_files({ cwd = resource.path }) - end) + require("telescope.builtin").find_files({ cwd = resource.path }) + end) - return true - end, - }) - :find() + return true + end, + }) + :find() end return resources_picker diff --git a/lua/laravel/pickers/telescope/pickers/routes.lua b/lua/laravel/pickers/telescope/pickers/routes.lua new file mode 100644 index 0000000..6fdeaa5 --- /dev/null +++ b/lua/laravel/pickers/telescope/pickers/routes.lua @@ -0,0 +1,65 @@ +local conf = require("telescope.config").values +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local previewers = require("telescope.previewers") +local preview = require("laravel.pickers.telescope.preview") +local make_entry = require("laravel.pickers.telescope.make_entry") +local actions = require("laravel.pickers.telescope.actions") + +---@class LaravelRoutesPicker +---@field routes_repository RoutesRepository +local routes_picker = {} + +function routes_picker:new(cache_routes_repository) + local instance = { + routes_repository = cache_routes_repository, + } + setmetatable(instance, self) + self.__index = self + + return instance +end + +function routes_picker:run(opts) + opts = opts or {} + + self.routes_repository:all():thenCall(function(routes) + pickers + .new(opts, { + prompt_title = "Artisan Routes", + finder = finders.new_table({ + results = routes, + entry_maker = opts.entry_maker or make_entry.gen_from_laravel_routes(opts), + }), + previewer = previewers.new_buffer_previewer({ + title = "Help", + get_buffer_by_name = function(_, entry) + return entry.value.name + end, + define_preview = function(preview_self, entry) + local route_preview = preview.route(entry.value) + + vim.api.nvim_buf_set_lines(preview_self.state.bufnr, 0, -1, false, route_preview.lines) + + local hl = vim.api.nvim_create_namespace("laravel") + for _, value in pairs(route_preview.highlights) do + vim.api.nvim_buf_add_highlight(preview_self.state.bufnr, hl, value[1], value[2], value[3], value[4]) + end + end, + }), + sorter = conf.prefilter_sorter({ + tag = "route_method", + sorter = conf.generic_sorter(opts or {}), + }), + attach_mappings = function(_, map) + map("i", "", actions.open_route) + map("i", "", actions.open_browser) + + return true + end, + }) + :find() + end) +end + +return routes_picker diff --git a/lua/laravel/telescope/preview.lua b/lua/laravel/pickers/telescope/preview.lua similarity index 100% rename from lua/laravel/telescope/preview.lua rename to lua/laravel/pickers/telescope/preview.lua diff --git a/lua/laravel/pickers/telescope/ui_run.lua b/lua/laravel/pickers/telescope/ui_run.lua new file mode 100644 index 0000000..49ef452 --- /dev/null +++ b/lua/laravel/pickers/telescope/ui_run.lua @@ -0,0 +1,47 @@ +local Popup = require("nui.popup") +local preview = require("laravel.pickers.telescope.preview") +local common = require("laravel.pickers.common.ui_run") + +--- function to scroll a window +---@param popup any id of window +---@param direction string j o k for the direction +local function scroll_fn(popup, direction) + return function() + local scroll = vim.api.nvim_get_option_value("scroll", { win = popup.winid }) + vim.api.nvim_win_call(popup.winid, function() + vim.cmd("normal! " .. scroll .. direction) + end) + end +end + +return function(command) + local entry_popup = common.entry_popup() + local help_popup = Popup({ + border = { + style = "rounded", + text = { + top = "Help ( to cancel)", + top_align = "center", + }, + }, + win_options = { + winhighlight = "Normal:LaravelHelp", + }, + }) + + local command_preview = preview.command(command) + + vim.api.nvim_buf_set_lines(help_popup.bufnr, 0, -1, false, command_preview.lines) + + local hl = vim.api.nvim_create_namespace("laravel") + for _, value in pairs(command_preview.highlights) do + vim.api.nvim_buf_add_highlight(help_popup.bufnr, hl, value[1], value[2], value[3], value[4]) + end + + entry_popup:map("i", "", scroll_fn(help_popup, "j")) + entry_popup:map("n", "", scroll_fn(help_popup, "j")) + entry_popup:map("i", "", scroll_fn(help_popup, "k")) + entry_popup:map("n", "", scroll_fn(help_popup, "k")) + + common.ui_run(command, { entry_popup = entry_popup, help_popup = help_popup }) +end diff --git a/lua/laravel/pickers/ui_select/actions.lua b/lua/laravel/pickers/ui_select/actions.lua new file mode 100644 index 0000000..5316c9b --- /dev/null +++ b/lua/laravel/pickers/ui_select/actions.lua @@ -0,0 +1,10 @@ +local common_actions = require("laravel.pickers.common.actions") +local ui_run = require("laravel.pickers.ui_select.ui_run") + +local M = {} + +function M.run(command) + common_actions.run(command, ui_run) +end + +return M diff --git a/lua/laravel/ui_select/pickers/artisan.lua b/lua/laravel/pickers/ui_select/pickers/artisan.lua similarity index 86% rename from lua/laravel/ui_select/pickers/artisan.lua rename to lua/laravel/pickers/ui_select/pickers/artisan.lua index 6d3f508..b0c4bca 100644 --- a/lua/laravel/ui_select/pickers/artisan.lua +++ b/lua/laravel/pickers/ui_select/pickers/artisan.lua @@ -1,4 +1,4 @@ -local actions = require("laravel.ui_select.actions") +local actions = require("laravel.pickers.ui_select.actions") ---@class LaravelUISelectArtisanPicker ---@field commands_repository CommandsRepository @@ -25,11 +25,9 @@ function ui_artisan_picker:run(opts) end, kind = "artisan", }, function(command) - if command == nil then - return + if command ~= nil then + actions.run(command) end - - actions.run(command) end) end, function(error) vim.api.nvim_err_writeln(error) diff --git a/lua/laravel/ui_select/pickers/commands.lua b/lua/laravel/pickers/ui_select/pickers/commands.lua similarity index 95% rename from lua/laravel/ui_select/pickers/commands.lua rename to lua/laravel/pickers/ui_select/pickers/commands.lua index 0505f89..f651a76 100644 --- a/lua/laravel/ui_select/pickers/commands.lua +++ b/lua/laravel/pickers/ui_select/pickers/commands.lua @@ -1,5 +1,3 @@ -local actions = require("laravel.ui_select.actions") - local commands_picker = {} function commands_picker:new(runner, options) diff --git a/lua/laravel/ui_select/pickers/history.lua b/lua/laravel/pickers/ui_select/pickers/history.lua similarity index 100% rename from lua/laravel/ui_select/pickers/history.lua rename to lua/laravel/pickers/ui_select/pickers/history.lua diff --git a/lua/laravel/ui_select/pickers/make.lua b/lua/laravel/pickers/ui_select/pickers/make.lua similarity index 89% rename from lua/laravel/ui_select/pickers/make.lua rename to lua/laravel/pickers/ui_select/pickers/make.lua index 2f83da8..a032ce6 100644 --- a/lua/laravel/ui_select/pickers/make.lua +++ b/lua/laravel/pickers/ui_select/pickers/make.lua @@ -1,4 +1,4 @@ -local actions = require("laravel.ui_select.actions") +local actions = require("laravel.pickers.common.actions") local is_make_command = require("laravel.utils").is_make_command ---@class LaravelUISelectMakePicker @@ -35,10 +35,8 @@ function make_picker:run(opts) }, function(command) if command == nil then - return + actions.make_run(command) end - - actions.make_run(command) end ) end) diff --git a/lua/laravel/ui_select/pickers/related.lua b/lua/laravel/pickers/ui_select/pickers/related.lua similarity index 92% rename from lua/laravel/ui_select/pickers/related.lua rename to lua/laravel/pickers/ui_select/pickers/related.lua index ae259bd..aa40b34 100644 --- a/lua/laravel/ui_select/pickers/related.lua +++ b/lua/laravel/pickers/ui_select/pickers/related.lua @@ -1,4 +1,4 @@ -local actions = require("laravel.ui_select.actions") +local actions = require("laravel.pickers.common.actions") ---@class LaravelUISelectRelatedPicker ---@field class LaravelClassService @@ -76,17 +76,15 @@ function related_picker:run(opts) end end - print(vim.inspect(relations)) - vim.ui.select(relations, { prompt = "Related Files", format_item = function(relation) return relation.class .. " " .. relation.extra_information end, kind = "make", - }, function(choice) - if choice ~= nil then - actions.open_relation(choice) + }, function(resource) + if resource ~= nil then + actions.open_relation(resource) end end) end) diff --git a/lua/laravel/ui_select/pickers/resources.lua b/lua/laravel/pickers/ui_select/pickers/resources.lua similarity index 88% rename from lua/laravel/ui_select/pickers/resources.lua rename to lua/laravel/pickers/ui_select/pickers/resources.lua index 1d2c84e..4b3d47e 100644 --- a/lua/laravel/ui_select/pickers/resources.lua +++ b/lua/laravel/pickers/ui_select/pickers/resources.lua @@ -1,5 +1,4 @@ -local actions = require("laravel.ui_select.actions") --- local action_state = require("telescope.actions.state") +local actions = require("laravel.pickers.common.actions") local resources_picker = {} function resources_picker:new(options) diff --git a/lua/laravel/ui_select/pickers/routes.lua b/lua/laravel/pickers/ui_select/pickers/routes.lua similarity index 87% rename from lua/laravel/ui_select/pickers/routes.lua rename to lua/laravel/pickers/ui_select/pickers/routes.lua index 484bba3..2accba7 100644 --- a/lua/laravel/ui_select/pickers/routes.lua +++ b/lua/laravel/pickers/ui_select/pickers/routes.lua @@ -1,4 +1,4 @@ -local actions = require("laravel.ui_select.actions") +local actions = require("laravel.pickers.ui_select.actions") ---@class LaravelUISelectRoutesPicker ---@field routes_repository RoutesRepository @@ -26,10 +26,8 @@ function routes_picker:run(opts) kind = "route", }, function(route) if route == nil then - return + actions.open_route(route) end - - actions.open_route(route) end) end) end diff --git a/lua/laravel/pickers/ui_select/ui_run.lua b/lua/laravel/pickers/ui_select/ui_run.lua new file mode 100644 index 0000000..8b42cbf --- /dev/null +++ b/lua/laravel/pickers/ui_select/ui_run.lua @@ -0,0 +1,8 @@ +local Layout = require("nui.layout") +local Popup = require("nui.popup") +local app = require("laravel").app +local common = require("laravel.pickers.common.ui_run") + +return function(command) + common.ui_run(command, { entry_popup = common.entry_popup() }) +end diff --git a/lua/laravel/providers/telescope_provider.lua b/lua/laravel/providers/telescope_provider.lua index c4c48f5..ee23eaa 100644 --- a/lua/laravel/providers/telescope_provider.lua +++ b/lua/laravel/providers/telescope_provider.lua @@ -9,15 +9,16 @@ function telescope_provider:register(app) app:singeltonIf("telescope.pickers", function() return { - artisan = "laravel.telescope.pickers.artisan", - routes = "laravel.telescope.pickers.routes", - make = "laravel.telescope.pickers.make", - related = "laravel.telescope.pickers.related", - resources = "laravel.telescope.pickers.resources", - commands = "laravel.telescope.pickers.commands", - history = "laravel.telescope.pickers.history", + artisan = "laravel.pickers.telescope.pickers.artisan", + routes = "laravel.pickers.telescope.pickers.routes", + make = "laravel.pickers.telescope.pickers.make", + related = "laravel.pickers.telescope.pickers.related", + resources = "laravel.pickers.telescope.pickers.resources", + commands = "laravel.pickers.telescope.pickers.commands", + history = "laravel.pickers.telescope.pickers.history", } end) end return telescope_provider + diff --git a/lua/laravel/providers/ui_select_provider.lua b/lua/laravel/providers/ui_select_provider.lua new file mode 100644 index 0000000..ad2d799 --- /dev/null +++ b/lua/laravel/providers/ui_select_provider.lua @@ -0,0 +1,14 @@ +---@class LaravelUISelectProvider: LaravelProvider +local ui_select_provider = {} + +function ui_select_provider:register(app) + app:bindIf("artisan_picker", "laravel.pickers.ui_select.pickers.artisan", { tags = { "picker" } }) + app:bindIf("routes_picker", "laravel.pickers.ui_select.pickers.routes", { tags = { "picker" } }) + app:bindIf("make_picker", "laravel.pickers.ui_select.pickers.make", { tags = { "picker" } }) + app:bindIf("related_picker", "laravel.pickers.ui_select.pickers.related", { tags = { "picker" } }) + app:bindIf("resources_picker", "laravel.pickers.ui_select.pickers.resources", { tags = { "picker" } }) + app:bindIf("commands_picker", "laravel.pickers.ui_select.pickers.commands", { tags = { "picker" } }) + app:bindIf("history_picker", "laravel.pickers.ui_select.pickers.history", { tags = { "picker" } }) +end + +return ui_select_provider diff --git a/lua/laravel/telescope/actions.lua b/lua/laravel/telescope/actions.lua deleted file mode 100644 index 73aa5c2..0000000 --- a/lua/laravel/telescope/actions.lua +++ /dev/null @@ -1,103 +0,0 @@ -local actions = require("telescope.actions") -local action_state = require("telescope.actions.state") -local ui_run = require("laravel.telescope.ui_run") -local lsp = require("laravel._lsp") -local app = require("laravel").app - -local function go(route) - if route.action == "Closure" or route.action == "Illuminate\\Routing\\ViewController" then - if vim.tbl_contains(route.middlewares, "api") then - vim.cmd("edit routes/api.php") - vim.fn.search(route.uri:gsub("api", "") .. "") - elseif vim.tbl_contains(route.middlewares, "web") then - vim.cmd("edit routes/web.php") - if route.uri == "/" then - vim.fn.search("['\"]/['\"]") - else - vim.fn.search("/" .. route.uri) - end - else - vim.notify("Could not open the route location", vim.log.levels.WARN) - return - end - - vim.cmd("normal zt") - return - end - - local action = vim.fn.split(route.action, "@") - lsp.go_to(action[1], action[2]) -end - -local M = {} - -function M.run(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - local command = entry.value - - vim.schedule(function() - ui_run(command) - end) -end - -function M.make_run(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - local command = entry.value - - vim.schedule(function() - app("runner"):run("artisan", { command.name }, { ui = "popup" }) - end) -end - -function M.open_route(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - vim.schedule(function() - go(entry.value) - end) -end - -function M.open_browser(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - app("configs_repository"):get("app.url"):thenCall(function(app_url) - local uri = entry.value.uri - for capturedString in uri:gmatch("{(.-)}") do - local val = vim.fn.input(capturedString .. ": ") - uri = uri:gsub("{" .. capturedString .. "}", val) - end - - local url = string.format("%s/%s", app_url, uri) - local command = nil - - if vim.fn.executable("xdg-open") == 1 then - command = "xdg-open" - elseif vim.fn.executable("open") == 1 then - command = "open" - end - if not command then - return - end - - vim.fn.system({ command, url }) - end) -end - -function M.re_run_command(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - app("runner"):run(entry.value.name, entry.value.args, entry.value.opts) -end - -function M.open_relation(prompt_bufnr) - actions.close(prompt_bufnr) - local entry = action_state.get_selected_entry() - vim.schedule(function() - local action = vim.fn.split(entry.value.class, "@") - lsp.go_to(action[1], action[2]) - end) -end - -return M diff --git a/lua/laravel/telescope/pickers/artisan.lua b/lua/laravel/telescope/pickers/artisan.lua deleted file mode 100644 index 1839054..0000000 --- a/lua/laravel/telescope/pickers/artisan.lua +++ /dev/null @@ -1,68 +0,0 @@ -local conf = require("telescope.config").values -local finders = require("telescope.finders") -local pickers = require("telescope.pickers") -local previewers = require("telescope.previewers") -local preview = require("laravel.telescope.preview") -local actions = require("laravel.telescope.actions") - ----@class LaravelArtisanPicker ----@field commands_repository CommandsRepository -local artisan_picker = {} - -function artisan_picker:new(cache_commands_repository) - local instance = { - commands_repository = cache_commands_repository, - } - setmetatable(instance, self) - self.__index = self - - return instance -end - -function artisan_picker:run(opts) - opts = opts or {} - - return self.commands_repository:all():thenCall(function(commands) - pickers - .new(opts, { - prompt_title = "Artisan commands", - finder = finders.new_table({ - results = commands, - entry_maker = function(command) - return { - value = command, - display = command.name, - ordinal = command.name, - } - end, - }), - previewer = previewers.new_buffer_previewer({ - title = "Help", - get_buffer_by_name = function(_, entry) - return entry.value.name - end, - define_preview = function(preview_self, entry) - local command_preview = preview.command(entry.value) - - vim.api.nvim_buf_set_lines(preview_self.state.bufnr, 0, -1, false, command_preview.lines) - - local hl = vim.api.nvim_create_namespace("laravel") - for _, value in pairs(command_preview.highlights) do - vim.api.nvim_buf_add_highlight(preview_self.state.bufnr, hl, value[1], value[2], value[3], value[4]) - end - end, - }), - sorter = conf.file_sorter(), - attach_mappings = function(_, map) - map("i", "", actions.run) - - return true - end, - }) - :find() - end, function(error) - vim.api.nvim_err_writeln(error) - end) -end - -return artisan_picker diff --git a/lua/laravel/telescope/pickers/history.lua b/lua/laravel/telescope/pickers/history.lua deleted file mode 100644 index 8b35e58..0000000 --- a/lua/laravel/telescope/pickers/history.lua +++ /dev/null @@ -1,46 +0,0 @@ -local conf = require("telescope.config").values -local finders = require("telescope.finders") -local pickers = require("telescope.pickers") -local actions = require("laravel.telescope.actions") - -local history_picker = {} - -function history_picker:new(history) - local instance = { - history_provider = history, - } - setmetatable(instance, self) - self.__index = self - return instance -end - -function history_picker:run(opts) - opts = opts or {} - - pickers - .new(opts, { - prompt_title = "Laravel Command History", - finder = finders.new_table({ - results = self.history_provider:get(), - entry_maker = function(history_entry) - return { - value = history_entry, - display = string.format("%s %s", history_entry.name, table.concat(history_entry.args, " ")), - ordinal = string.format("%s %s", history_entry.name, table.concat(history_entry.args, " ")), - } - end, - }), - previewer = false, - sorter = conf.prefilter_sorter({ - sorter = conf.generic_sorter(opts or {}), - }), - attach_mappings = function(_, map) - map("i", "", actions.re_run_command) - - return true - end, - }) - :find() -end - -return history_picker diff --git a/lua/laravel/telescope/pickers/make.lua b/lua/laravel/telescope/pickers/make.lua deleted file mode 100644 index 92c24cd..0000000 --- a/lua/laravel/telescope/pickers/make.lua +++ /dev/null @@ -1,72 +0,0 @@ -local conf = require("telescope.config").values -local finders = require("telescope.finders") -local pickers = require("telescope.pickers") -local previewers = require("telescope.previewers") -local preview = require("laravel.telescope.preview") -local actions = require("laravel.telescope.actions") -local is_make_command = require("laravel.utils").is_make_command - ----@class LaravelMakePicker ----@field commands_repository CommandsRepository -local make_picker = {} - -function make_picker:new(cache_commands_repository) - local instance = { - commands_repository = cache_commands_repository, - } - setmetatable(instance, self) - self.__index = self - - return instance -end - -function make_picker:run(opts) - opts = opts or {} - - self.commands_repository:all():thenCall(function(commands) - pickers - .new(opts, { - prompt_title = "Make commands", - finder = finders.new_table({ - results = vim - .iter(commands) - :filter(function(command) - return is_make_command(command.name) - end) - :totable(), - entry_maker = function(command) - return { - value = command, - display = command.name, - ordinal = command.name, - } - end, - }), - previewer = previewers.new_buffer_previewer({ - title = "Help", - get_buffer_by_name = function(_, entry) - return entry.value.name - end, - define_preview = function(preview_self, entry) - local command_preview = preview.command(entry.value) - - vim.api.nvim_buf_set_lines(preview_self.state.bufnr, 0, -1, false, command_preview.lines) - - local hl = vim.api.nvim_create_namespace("laravel") - for _, value in pairs(command_preview.highlights) do - vim.api.nvim_buf_add_highlight(preview_self.state.bufnr, hl, value[1], value[2], value[3], value[4]) - end - end, - }), - sorter = conf.file_sorter(), - attach_mappings = function(_, map) - map("i", "", actions.make_run) - - return true - end, - }) - :find() - end) -end - -return make_picker diff --git a/lua/laravel/telescope/pickers/related.lua b/lua/laravel/telescope/pickers/related.lua deleted file mode 100644 index c83bfe1..0000000 --- a/lua/laravel/telescope/pickers/related.lua +++ /dev/null @@ -1,103 +0,0 @@ -local pickers = require("telescope.pickers") -local make_entry = require("laravel.telescope.make_entry") -local finders = require("telescope.finders") -local conf = require("telescope.config").values -local actions = require("laravel.telescope.actions") - ----@class LaravelRelatedPicker ----@field class LaravelClassService ----@field api LaravelApi -local related_picker = {} - -local build_relation = function(info, relation_type) - if next(info) == nil then - return nil - end - if relation_type == "observers" and info["observer"][2] ~= nil then - return { - class = info["observer"][2], - type = relation_type, - extra_information = info["event"], - } - elseif relation_type == "relations" then - return { - class = info["related"], - type = relation_type, - extra_information = info["type"] .. " " .. info["name"], - } - elseif relation_type == "policy" then - return { - class = info[1], - type = relation_type, - extra_information = "", - } - end - return nil -end - -local types = { "observers", "relations", "policy" } - -function related_picker:new(class, api) - local instance = { - class = class, - api = api, - } - setmetatable(instance, self) - self.__index = self - - return instance -end - -function related_picker:run(opts) - opts = opts or {} - - local bufnr = vim.api.nvim_get_current_buf() - - return self.class - :get(bufnr) - :thenCall(function(class) - return self.api:send("artisan", { "model:show", class.fqn, "--json" }) - end) - :thenCall(function(response) - local model_info = response:json() - - local relations = {} - for _, relation_type in ipairs(types) do - if model_info[relation_type] and #model_info[relation_type] > 0 then - if type(model_info[relation_type]) == "table" and model_info[relation_type][1] then - for _, info in ipairs(model_info[relation_type]) do - local relation = build_relation(info, relation_type) - if relation ~= nil then - table.insert(relations, relation) - end - end - else - local relation = build_relation({ model_info[relation_type] }, relation_type) - if relation ~= nil then - table.insert(relations, relation) - end - end - end - end - - pickers - .new(opts, { - prompt_title = "Related Files", - finder = finders.new_table({ - results = relations, - entry_maker = make_entry.gen_from_model_relations(opts), - }), - sorter = conf.prefilter_sorter({ - sorter = conf.generic_sorter(opts or {}), - }), - attach_mappings = function(_, map) - map("i", "", actions.open_relation) - - return true - end, - }) - :find() - end) -end - -return related_picker diff --git a/lua/laravel/telescope/pickers/routes.lua b/lua/laravel/telescope/pickers/routes.lua deleted file mode 100644 index cbe5e2b..0000000 --- a/lua/laravel/telescope/pickers/routes.lua +++ /dev/null @@ -1,65 +0,0 @@ -local conf = require("telescope.config").values -local finders = require("telescope.finders") -local pickers = require("telescope.pickers") -local previewers = require("telescope.previewers") -local preview = require("laravel.telescope.preview") -local make_entry = require("laravel.telescope.make_entry") -local actions = require("laravel.telescope.actions") - ----@class LaravelRoutesPicker ----@field routes_repository RoutesRepository -local routes_picker = {} - -function routes_picker:new(cache_routes_repository) - local instance = { - routes_repository = cache_routes_repository, - } - setmetatable(instance, self) - self.__index = self - - return instance -end - -function routes_picker:run(opts) - opts = opts or {} - - self.routes_repository:all():thenCall(function(routes) - pickers - .new(opts, { - prompt_title = "Artisan Routes", - finder = finders.new_table({ - results = routes, - entry_maker = opts.entry_maker or make_entry.gen_from_laravel_routes(opts), - }), - previewer = previewers.new_buffer_previewer({ - title = "Help", - get_buffer_by_name = function(_, entry) - return entry.value.name - end, - define_preview = function(preview_self, entry) - local route_preview = preview.route(entry.value) - - vim.api.nvim_buf_set_lines(preview_self.state.bufnr, 0, -1, false, route_preview.lines) - - local hl = vim.api.nvim_create_namespace("laravel") - for _, value in pairs(route_preview.highlights) do - vim.api.nvim_buf_add_highlight(preview_self.state.bufnr, hl, value[1], value[2], value[3], value[4]) - end - end, - }), - sorter = conf.prefilter_sorter({ - tag = "route_method", - sorter = conf.generic_sorter(opts or {}), - }), - attach_mappings = function(_, map) - map("i", "", actions.open_route) - map("i", "", actions.open_browser) - - return true - end, - }) - :find() - end) -end - -return routes_picker diff --git a/lua/laravel/telescope/ui_run.lua b/lua/laravel/telescope/ui_run.lua deleted file mode 100644 index 7043a84..0000000 --- a/lua/laravel/telescope/ui_run.lua +++ /dev/null @@ -1,95 +0,0 @@ -local Layout = require("nui.layout") -local Popup = require("nui.popup") -local preview = require("laravel.telescope.preview") -local app = require("laravel").app - ---- function to scroll a window ----@param popup any id of window ----@param direction string j o k for the direction -local function scroll_fn(popup, direction) - return function() - local scroll = vim.api.nvim_get_option_value("scroll", { win = popup.winid }) - vim.api.nvim_win_call(popup.winid, function() - vim.cmd("normal! " .. scroll .. direction) - end) - end -end - -return function(command) - local entry_popup, help_popup = - Popup({ - enter = true, - border = { - style = "rounded", - text = { - top = "Artisan", - top_align = "center", - }, - }, - buf_options = { - buftype = "prompt", - }, - win_options = { - winhighlight = "Normal:LaravelPrompt", - }, - }), Popup({ - border = { - style = "rounded", - text = { - top = "Help ( to cancel)", - top_align = "center", - }, - }, - win_options = { - winhighlight = "Normal:LaravelHelp", - }, - }) - - local layout = Layout( - { - position = "50%", - size = { - width = "80%", - height = "90%", - }, - relative = "editor", - }, - Layout.Box({ - Layout.Box(entry_popup, { size = 3 }), -- 3 because of borders to be 1 row - Layout.Box(help_popup, { grow = 1 }), - }, { dir = "col" }) - ) - - local command_preview = preview.command(command) - - vim.api.nvim_buf_set_lines(help_popup.bufnr, 0, -1, false, command_preview.lines) - - local hl = vim.api.nvim_create_namespace("laravel") - for _, value in pairs(command_preview.highlights) do - vim.api.nvim_buf_add_highlight(help_popup.bufnr, hl, value[1], value[2], value[3], value[4]) - end - - entry_popup:map("i", "", scroll_fn(help_popup, "j")) - entry_popup:map("n", "", scroll_fn(help_popup, "j")) - entry_popup:map("i", "", scroll_fn(help_popup, "k")) - entry_popup:map("n", "", scroll_fn(help_popup, "k")) - entry_popup:map("i", "", function() - layout:unmount() - end) - entry_popup:map("n", "", function() - layout:unmount() - end) - - local prompt = "$ artisan " .. command.name .. " " - vim.fn.prompt_setprompt(entry_popup.bufnr, prompt) - vim.fn.prompt_setcallback(entry_popup.bufnr, function(input) - layout:unmount() - local args = vim.fn.split(input, " ", false) - table.insert(args, 1, command.name) - - app("runner"):run("artisan", args) - end) - - layout:mount() - vim.cmd([[startinsert]]) -end diff --git a/lua/laravel/ui_select/ui_run.lua b/lua/laravel/ui_select/ui_run.lua deleted file mode 100644 index 1603c47..0000000 --- a/lua/laravel/ui_select/ui_run.lua +++ /dev/null @@ -1,68 +0,0 @@ -local Layout = require("nui.layout") -local Popup = require("nui.popup") -local app = require("laravel").app - ---- function to scroll a window ----@param popup any id of window ----@param direction string j o k for the direction -local function scroll_fn(popup, direction) - return function() - local scroll = vim.api.nvim_get_option_value("scroll", { win = popup.winid }) - vim.api.nvim_win_call(popup.winid, function() - vim.cmd("normal! " .. scroll .. direction) - end) - end -end - -return function(command) - local entry_popup = Popup({ - enter = true, - border = { - style = "rounded", - text = { - top = "Artisan", - top_align = "center", - }, - }, - buf_options = { - buftype = "prompt", - }, - win_options = { - winhighlight = "Normal:LaravelPrompt", - }, - }) - - local layout = Layout( - { - position = "50%", - size = { - width = "80%", - height = "90%", - }, - relative = "editor", - }, - Layout.Box({ - Layout.Box(entry_popup, { size = 3 }), -- 3 because of borders to be 1 row - }, { dir = "col" }) - ) - - entry_popup:map("i", "", function() - layout:unmount() - end) - entry_popup:map("n", "", function() - layout:unmount() - end) - - local prompt = "$ artisan " .. command.name .. " " - vim.fn.prompt_setprompt(entry_popup.bufnr, prompt) - vim.fn.prompt_setcallback(entry_popup.bufnr, function(input) - layout:unmount() - local args = vim.fn.split(input, " ", false) - table.insert(args, 1, command.name) - - app("runner"):run("artisan", args) - end) - - layout:mount() - vim.cmd([[startinsert]]) -end