-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add common picker module for logic deduplication
- Loading branch information
Showing
32 changed files
with
668 additions
and
752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.