Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lua/laravel/options/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

---@class PickersFeature
---@field enable boolean
---@field provider 'telescope'|'ui.select'|'fzf-lua'
---@field provider 'telescope'|'ui.select'|'fzf-lua'|'snacks'

---@class LaravelFeatures
---@field route_info RouteInfoFeature
Expand Down Expand Up @@ -59,6 +59,7 @@ return {
require("laravel.providers.telescope_provider"),
require("laravel.providers.fzf_lua_provider"),
require("laravel.providers.ui_select_provider"),
require("laravel.providers.snacks_provider"),
require("laravel.providers.user_command_provider"),
require("laravel.providers.status_provider"),
require("laravel.providers.diagnostics_provider"),
Expand Down
5 changes: 5 additions & 0 deletions lua/laravel/pickers/common/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ local function go(route)
vim.fn.search(route.uri:gsub("api", "") .. "")
elseif vim.tbl_contains(route.middlewares, "web") then
vim.cmd("edit routes/web.php")
-- TODO: use the get or post to find the right route or view
-- get or view can be for GET
-- post if not we need the resources
-- can improce this with a treesitter query ?
-- should be able to look for specif methods
if route.uri == "/" then
vim.fn.search("['\"]/['\"]")
else
Expand Down
32 changes: 13 additions & 19 deletions lua/laravel/pickers/fzf_lua/format_entry.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local M = {}

local function split_string(input)
local group, command = input:match("([^:]+):([^:]+)")
if not group then
command = input
end
return group, command
local group, command = input:match("([^:]+):([^:]+)")
if not group then
command = input
end
return group, command
end

local function formatCommandText(command)
Expand All @@ -20,10 +20,7 @@ local function formatCommandText(command)
end

function M.gen_from_artisan(commands)
local string_names = vim
.iter(commands)
:map(formatCommandText)
:totable()
local string_names = vim.iter(commands):map(formatCommandText):totable()

local command_hash = {}
for _, command in ipairs(commands) do
Expand All @@ -34,10 +31,7 @@ function M.gen_from_artisan(commands)
end

function M.gen_from_composer(commands)
local string_names = vim
.iter(commands)
:map(formatCommandText)
:totable()
local string_names = vim.iter(commands):map(formatCommandText):totable()

local command_hash = {}
for _, command in ipairs(commands) do
Expand Down Expand Up @@ -96,12 +90,12 @@ function M.gen_from_related(relations)
end

local function formatRouteText(route)
local uri = require("fzf-lua").utils.ansi_codes.magenta(route.uri)
if route.name ~= nil and route.name ~= "" then
return string.format("[%s] %s", require("fzf-lua").utils.ansi_codes.blue(route.name), uri)
end

return uri
return string.format(
"%s [%s] %s",
vim.iter(route.methods):join("|"),
require("fzf-lua").utils.ansi_codes.blue(route.name or ""),
require("fzf-lua").utils.ansi_codes.magenta(route.uri)
)
end

function M.gen_from_routes(routes)
Expand Down
31 changes: 16 additions & 15 deletions lua/laravel/pickers/fzf_lua/pickers/artisan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ function ui_artisan_picker:new(cache_commands_repository)
end

function ui_artisan_picker:run(opts)
opts = opts or {}

return self.commands_repository:all():thenCall(function(commands)
local command_names, command_table = format_entry(commands)

fzf_exec(command_names, {
actions = {
["default"] = function(selected)
local command = command_table[selected[1]]
actions.artisan_run(command)
end,
},
prompt = "Artisan > ",
previewer = CommandPreviewer(command_table),
fzf_opts = {
["--preview-window"] = "nohidden,70%",
},
})
fzf_exec(
command_names,
vim.tbl_extends("force", {
actions = {
["default"] = function(selected)
local command = command_table[selected[1]]
actions.artisan_run(command)
end,
},
prompt = "Artisan > ",
previewer = CommandPreviewer(command_table),
fzf_opts = {
["--preview-window"] = "nohidden,70%",
},
}, opts or {})
)
end, function(error)
vim.api.nvim_err_writeln(error)
end)
Expand Down
69 changes: 26 additions & 43 deletions lua/laravel/pickers/fzf_lua/pickers/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ local fzf_exec = require("fzf-lua").fzf_exec

local commands_picker = {}

function commands_picker:new(runner, options)
function commands_picker:new(runner, user_commands_repository)
local instance = {
runner = runner,
options = options,
user_commands_repository = user_commands_repository,
}
setmetatable(instance, self)
self.__index = self
Expand All @@ -15,47 +15,30 @@ function commands_picker:new(runner, options)
end

function commands_picker:run(opts)
opts = opts or {}

local commands = {}

for command_name, group_commands in pairs(self.options:get().user_commands) do
for name, details in pairs(group_commands) do
table.insert(commands, {
executable = command_name,
name = name,
display = string.format("[%s] %s", command_name, name),
cmd = details.cmd,
desc = details.desc,
opts = details.opts or {},
})
end
end

if vim.tbl_isempty(commands) then
vim.notify("No user command defined in the config", vim.log.levels.WARN, {})
return
end

local command_names, command_table = format_entry(commands)

fzf_exec(command_names, {
actions = {
["default"] = function(selected)
local command = command_table[selected[1]]
self.runner:run(command.executable, command.cmd, command.opts)
end,
},
prompt = "User Commands > ",
fzf_opts = {
["--preview-window"] = "nohidden,70%",
["--preview"] = function(selected)
local command = command_table[selected[1]]

return command.desc
end
},
})
self.user_commands_repository:all():thenCall(function(commands)
local command_names, command_table = format_entry(commands)

fzf_exec(
command_names,
vim.tbl_extend("force", {
actions = {
["default"] = function(selected)
local command = command_table[selected[1]]
self.runner:run(command.executable, command.cmd, command.opts)
end,
},
prompt = "User Commands > ",
fzf_opts = {
["--preview-window"] = "nohidden,70%",
["--preview"] = function(selected)
local command = command_table[selected[1]]

return command.desc
end,
},
}, opts or {})
)
end)
end

return commands_picker
65 changes: 65 additions & 0 deletions lua/laravel/pickers/snacks/format_entry.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local function split_string(input)
local group, command = input:match("([^:]+):([^:]+)")
if not group then
command = input
end
return group, command
end

local M = {}

local function format_command_text(command)
local group, cmd = split_string(command)
local out = {}
if group then
table.insert(out, { group, "@string" })
table.insert(out, { ":", "@string" })
end
table.insert(out, { cmd, "@keyword" })

return out
end

M.command = function(item, _)
return format_command_text(item.value.name)
end

M.composer_command = function(item, _)
return format_command_text(item.value.name)
end

M.user_command = function(item, _)
return {
{ string.format("[%s]", item.value.executable), "@string" },
{ " ", "@string" },
{ item.value.name, "@keyword" },
}
end

M.history = function(item, _)
return {
{ item.value.name, "@keyword" },
{ " ", "@string" },
{ table.concat(item.value.args, " "), "@string" },
}
end

M.related = function(item, _)
return {
{ item.value.class_name, "@string" },
{ item.value.type, "@keyword" },
{ item.value.extra_information or "", "@string" },
}
end

M.route = function(item, _)
return {
{ vim.iter(item.value.methods):join("|"), "@string" },
{ " ", "@string" },
{ item.value.name, "@keyword" },
{ " ", "@string" },
{ item.value.uri, "@string" },
}
end

return M
45 changes: 45 additions & 0 deletions lua/laravel/pickers/snacks/pickers/artisan.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local snacks = require("snacks").picker
local common_actions = require("laravel.pickers.common.actions")
local preview = require("laravel.pickers.snacks.preview")
local format_entry = require("laravel.pickers.snacks.format_entry")

---@class LaravelPickersSnacksArtisan
---@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)
return self.commands_repository:all():thenCall(function(commands)
snacks.pick(vim.tbl_extend("force", {
title = "Artisan Commands",
items = vim
.iter(commands)
:map(function(command)
return {
value = command,
text = command.name,
}
end)
:totable(),
format = format_entry.command,
preview = preview.command,
confirm = function(picker, item)
picker:close()
if item then
common_actions.artisan_run(item.value)
end
end,
}, opts or {}))
end)
end

return artisan_picker
43 changes: 43 additions & 0 deletions lua/laravel/pickers/snacks/pickers/commands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local snacks = require("snacks").picker
local format_entry = require("laravel.pickers.snacks.format_entry")
local preview = require("laravel.pickers.snacks.preview")

local commands_picker = {}

function commands_picker:new(runner, user_commands_repository)
local instance = {
runner = runner,
user_commands_repository = user_commands_repository,
}
setmetatable(instance, self)
self.__index = self

return instance
end

function commands_picker:run(opts)
self.user_commands_repository:all():thenCall(function(commands)
snacks.pick(vim.tbl_extend("force", {
title = "User Commands",
items = vim
.iter(commands)
:map(function(command)
return {
value = command,
text = command.display,
}
end)
:totable(),
format = format_entry.user_command,
preview = preview.user_command,
confirm = function(picker, item)
picker:close()
if item then
self.runner:run(item.value.executable, item.value.cmd, item.value.opts)
end
end,
}, opts or {}))
end)
end

return commands_picker
Loading