Skip to content

Commit

Permalink
add common picker module for logic deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Bleksak committed Oct 12, 2024
1 parent 2694f21 commit 61259e6
Show file tree
Hide file tree
Showing 32 changed files with 668 additions and 752 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions lua/laravel/pickers/common/ui_run.lua
Original file line number Diff line number Diff line change
@@ -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", "<c-c>", function()
layout:unmount()
end)
popups.entry_popup:map("n", "<c-c>", 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
53 changes: 53 additions & 0 deletions lua/laravel/pickers/telescope/actions.lua
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local entry_display = require "telescope.pickers.entry_display"
local entry_display = require("telescope.pickers.entry_display")

local M = {}

Expand Down Expand Up @@ -37,22 +37,22 @@ 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 = {
{ width = 16 },
{ 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)
Expand All @@ -68,22 +68,22 @@ 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 = {
{ width = 40 },
{ 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)
Expand Down
68 changes: 68 additions & 0 deletions lua/laravel/pickers/telescope/pickers/artisan.lua
Original file line number Diff line number Diff line change
@@ -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", "<cr>", actions.run)

return true
end,
})
:find()
end, function(error)
vim.api.nvim_err_writeln(error)
end)
end

return artisan_picker
Loading

0 comments on commit 61259e6

Please sign in to comment.