Skip to content

Commit

Permalink
feat: add watch
Browse files Browse the repository at this point in the history
  • Loading branch information
adalessa committed Dec 26, 2023
1 parent 0fbb4d9 commit fb108aa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lua/laravel/user_commands/laravel/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ local commands = {
end,
["routes"] = require("telescope").extensions.laravel.routes,
["artisan"] = require("telescope").extensions.laravel.commands,
-- ["test:watch"] = function()
-- return run("artisan", { "test" }, { runner = "watch" })
-- end,
["test:watch"] = function()
require "laravel.watch"("artisan", { "test" })
end,
["related"] = require("telescope").extensions.laravel.related,
["history"] = require("telescope").extensions.laravel.history,
["recipes"] = require("laravel.recipes").run,
Expand Down
58 changes: 58 additions & 0 deletions lua/laravel/watch.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
local config = require "laravel.config"
local environment = require "laravel.environment"
local Split = require "nui.split"
local event = require("nui.utils.autocmd").event

---@param name string
---@param args string[]
---@param opts table|nil
return function(name, args, opts)
opts = opts or {}
local executable = environment.get_executable(name)
if not executable then
error(string.format("Executable %s not found", name), vim.log.levels.ERROR)
return
end
local cmd = vim.fn.extend(executable, args)

local command_option = config.options.commands_options[args[1]] or {}

opts = vim.tbl_extend("force", command_option, opts)

local nui_opts = opts.nui_opts or config.options.ui.nui_opts.split
nui_opts.enter = false
local instance = Split(nui_opts)

instance:mount()

local bufnr = instance.bufnr

local run = function()
local chan_id = vim.api.nvim_open_term(bufnr, {})
vim.fn.jobstart(table.concat(cmd, " "), {
stdeout_buffered = true,
on_stdout = function(_, data)
vim.fn.chansend(chan_id, data)
local row = vim.api.nvim_buf_line_count(bufnr)
vim.api.nvim_win_set_cursor(instance.winid, { row, 0 })
end,
pty = true,
})
end
run()

local group = vim.api.nvim_create_augroup("laravel.watch", {})

local au_cmd_id = vim.api.nvim_create_autocmd({ "BufWritePost" }, {
pattern = opts.pattern or { "*.php" },
group = group,
callback = function()
run()
end,
})

instance:on(event.BufHidden, function()
vim.api.nvim_del_autocmd(au_cmd_id)
vim.notify("AutoCmd for watch deleted", vim.log.levels.INFO, {})
end)
end

0 comments on commit fb108aa

Please sign in to comment.