diff options
| author | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2022-08-05 09:02:11 +0200 |
|---|---|---|
| committer | Ariel D'Alessandro <dalessandro.ariel@gmail.com> | 2022-08-05 09:02:55 +0200 |
| commit | 67f55ef388b18310c06219eb3207ba513673127a (patch) | |
| tree | e1dc45f9b52ed39369f451ab5ac4ddcf30e5aa3e /lua | |
| parent | 0c6c7768eb0fe0fd496c936ecb348c694180fc1a (diff) | |
| download | nvim-conf-67f55ef388b18310c06219eb3207ba513673127a.tar.gz nvim-conf-67f55ef388b18310c06219eb3207ba513673127a.zip | |
add temporary laravel plugin scratch
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/alpha/laravel/actions.lua | 55 | ||||
| -rw-r--r-- | lua/alpha/laravel/artisan_config.lua | 29 | ||||
| -rw-r--r-- | lua/alpha/laravel/init.lua | 42 | ||||
| -rw-r--r-- | lua/alpha/laravel/make_command_directory_map.lua | 26 | ||||
| -rw-r--r-- | lua/alpha/laravel/telescope.lua | 73 | ||||
| -rw-r--r-- | lua/alpha/laravel/utils.lua | 8 | ||||
| -rw-r--r-- | lua/alpha/scratchs/laravel.lua | 223 | ||||
| -rw-r--r-- | lua/alpha/scratchs/sample.lua | 22 |
8 files changed, 478 insertions, 0 deletions
diff --git a/lua/alpha/laravel/actions.lua b/lua/alpha/laravel/actions.lua new file mode 100644 index 0000000..b7eca6f --- /dev/null +++ b/lua/alpha/laravel/actions.lua @@ -0,0 +1,55 @@ +local artisan_config = require("alpha.laravel.artisan_config") +local make_command_directory_map = require("alpha.laravel.make_command_directory_map") + +local M = {} +M.make_command = function(command) + -- change prompt from "name" to args + -- parse args from help + -- parse options from help + -- can parse as json + -- how to present the information + vim.ui.input({ prompt = "Name: " }, function(name) + vim.fn.jobstart({ artisan_config.artisan_cmd, "artisan", command, name }, { + on_exit = function() + local directory = make_command_directory_map[vim.split(command, ":")[2]] + if directory == "" then + return + end + local filename = string.format("%s/%s.php", directory, name) + if vim.fn.findfile(filename) then + local uri = vim.uri_from_fname(string.format("%s/%s", vim.fn.getcwd(), filename)) + local buffer = vim.uri_to_bufnr(uri) + vim.api.nvim_win_set_buf(0, buffer) + end + end, + }) + end) +end + +M.output_command = function(command) + vim.cmd('vertical new') + local new_window = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_width(new_window, 125) + local new_buffer = vim.api.nvim_create_buf(false, true) + vim.api.nvim_win_set_buf(new_window, new_buffer) + local channel_id = vim.api.nvim_open_term(new_buffer, {}) + + local function handle_output(_, data) + vim.fn.chansend(channel_id, data) + end + + vim.fn.jobstart({ artisan_config.artisan_cmd, "artisan", command}, { + stdeout_buffered = true, + on_stdout = handle_output, + on_exit = function () + vim.fn.chanclose(channel_id) + vim.cmd("startinsert") + end, + pty = true, + width = 120, + }) +end + +M.output_command("route:list") + +return M diff --git a/lua/alpha/laravel/artisan_config.lua b/lua/alpha/laravel/artisan_config.lua new file mode 100644 index 0000000..6d804e8 --- /dev/null +++ b/lua/alpha/laravel/artisan_config.lua @@ -0,0 +1,29 @@ +local config = { + has_composer = false, + is_laravel = false, + is_sail = false, + artisan_cmd = nil +} + +if vim.fn.findfile("composer.json") == nil then + return config +end + +config.has_composer = true; + + +local composer_json = vim.fn.json_decode(vim.fn.readfile(vim.fn.getcwd() .. "/composer.json")) +if composer_json["require"]["laravel/framework"] == nil then + return config +end +config.is_laravel = true + +local artisan_command +if composer_json["require-dev"]["laravel/sail"] ~= nil then + config.is_sail = true + config.artisan_cmd = "sail" +else + config.artisan_cmd = "php" +end + +return config diff --git a/lua/alpha/laravel/init.lua b/lua/alpha/laravel/init.lua new file mode 100644 index 0000000..c8ee711 --- /dev/null +++ b/lua/alpha/laravel/init.lua @@ -0,0 +1,42 @@ +vim.api.nvim_create_user_command("Artisan", function () + local artisan_config = require("alpha.laravel.artisan_config") + if artisan_config.is_laravel then + require("alpha.laravel.telescope").picker({}) + end +end, {}) + +-- TODO +-- move it as plugin folder +-- Add setup fuction and set the global function like "Artisan" or something like that +-- add code actions to create a model +-- or create controller +-- or create any class +-- hover a class the actions could be call the make for that +-- trigger the completition +-- +-- +-- more searches on telescope or somthing +-- package laravel.nvim +-- +-- require("laravel").setup() || initializes the global variable + +-- uses the global variable so not to recheck the composer and more +-- +-- require("laravel.artisan").run("route:list", {<args>}) +-- require("laravel.artisan").make("controller", {<args>}) +-- require("laravel.artisan").list() || return the list of commands, mostly to be use as telescope entries +-- require("laravel.artisan").help(<cmd>) || return the help for the command +-- require("laravel.artisan").watch_test("filename") || this will run auto cmd for php files and write. +-- +-- can open a new buffer, replace the buffer on the window, delete the previous buffer, put response on the new buffer +-- +-- require("laravel.sail").run("<cmd>") +-- +-- require("laravel.composer").udpate("library") || optional field +-- require("laravel.composer").install() +-- require("laravel.composer").require() +-- require("laravel.composer").remove() +-- +-- +-- User commands +-- Could register the user commands for Artisan Sail & Composer diff --git a/lua/alpha/laravel/make_command_directory_map.lua b/lua/alpha/laravel/make_command_directory_map.lua new file mode 100644 index 0000000..683e001 --- /dev/null +++ b/lua/alpha/laravel/make_command_directory_map.lua @@ -0,0 +1,26 @@ +return { + cast = "app/Casts", + channel = "app/Broadcasting", + command = "app/Console/Commands", + component = "app/View/Components", + controller = "app/Http/Controllers", + event = "app/Events", + exception = "app/Exceptions", + factory = "", + job = "app/Jobs", + listener = "app/Listeners", + mail = "app/Mail", + middleware = "app/Http/Middleware", + migration = "", + model = "app/Models", + notification = "app/Notifications", + observer = "app/Observers", + policy = "app/Policies", + provider = "app/Providers", + request = "app/Http/Requests", + resource = "app/Http/Resources", + rule = "app/Rules", + scope = "app/Models/Scopes", + seeder = "", + test = "tests/Feature", +} diff --git a/lua/alpha/laravel/telescope.lua b/lua/alpha/laravel/telescope.lua new file mode 100644 index 0000000..db9cacf --- /dev/null +++ b/lua/alpha/laravel/telescope.lua @@ -0,0 +1,73 @@ +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") +local utils = require("telescope.utils") +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local previewers = require("telescope.previewers") +local putils = require("telescope.previewers.utils") +local conf = require("telescope.config").values + +local artisan_config = require("alpha.laravel.artisan_config") +local laravel_actions = require("alpha.laravel.actions") +local laravel_utils = require("alpha.laravel.utils") + +local M = {} + +M.picker = function(opts) + -- TODO check if cmd == sail check if sail is running + opts = opts or {} + + local results = utils.get_os_command_output({ artisan_config.artisan_cmd, "artisan", "list", "--raw" }) + -- do I want to change from raw to json and decode it? + -- what advantages will I get + -- I have group by the id like "make" the command is the same + + pickers + .new({}, { + prompt_title = "Artisan commands", + finder = finders.new_table({ + results = results, + entry_maker = function(line) + local artisan_cmd = vim.split(line, " ")[1] + return { + value = artisan_cmd, + display = artisan_cmd, + ordinal = artisan_cmd, + } + end, + }), + previewer = previewers.new_buffer_previewer({ + title = "Help", + get_buffer_by_name = function(_, entry) + return entry.value + end, + + define_preview = function(self, entry) + local cmd = { artisan_config.artisan_cmd, "artisan", entry.value, "-h" } + putils.job_maker(cmd, self.state.bufnr, { + value = entry.value, + bufname = self.state.bufname, + }) + end, + }), + sorter = conf.file_sorter(), + attach_mappings = function(_, map) + map("i", "<cr>", function(prompt_bufnr) + local entry = action_state.get_selected_entry() + -- check if entry.value is a make + actions.close(prompt_bufnr) + -- local cmd = string.format(":!%s %s ", get_artisan_command(), entry.value) + -- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(cmd, true, false, true), "t", true) + if laravel_utils.is_make_command(entry.value) then + laravel_actions.make_command(entry.value) + else + laravel_actions.output_command(entry.value) + end + end) + return true + end, + }) + :find() +end + +return M diff --git a/lua/alpha/laravel/utils.lua b/lua/alpha/laravel/utils.lua new file mode 100644 index 0000000..d1e8885 --- /dev/null +++ b/lua/alpha/laravel/utils.lua @@ -0,0 +1,8 @@ +local M = {} +M.is_make_command = function (command) + local command_split = vim.split(command, ":") + + return command_split[1] == "make" +end + +return M diff --git a/lua/alpha/scratchs/laravel.lua b/lua/alpha/scratchs/laravel.lua new file mode 100644 index 0000000..703e88f --- /dev/null +++ b/lua/alpha/scratchs/laravel.lua @@ -0,0 +1,223 @@ +local actions = require("telescope.actions") +local action_state = require("telescope.actions.state") +local utils = require("telescope.utils") +local finders = require("telescope.finders") +local pickers = require("telescope.pickers") +local previewers = require("telescope.previewers") +local putils = require("telescope.previewers.utils") +local conf = require("telescope.config").values + +if vim.fn.findfile("composer.json") == nil then + return +end + +local composer_json = vim.fn.json_decode(vim.fn.readfile(vim.fn.getcwd() .. "/composer.json")) + +if composer_json["require"]["laravel/framework"] == nil then + return +end + +local artisan_command +if composer_json["require-dev"]["laravel/sail"] ~= nil then + artisan_command = "sail" +else + artisan_command = "php" +end + +local get_artisan_command = function() + return artisan_command +end + +-- TODO review why do I have this +-- local lookup_keys = { +-- display = 1, +-- ordinal = 1, +-- value = 1, +-- } +-- local mt_string_entry = { +-- __index = function(t, k) +-- return rawget(t, rawget(lookup_keys, k)) +-- end, +-- } + +local function is_make_command(command) + local command_split = vim.split(command, ":") + + return command_split[1] == "make" +end + +local make_commands_map = { + cast = "app/Casts", + channel = "app/Broadcasting", + command = "app/Console/Commands", + component = "app/View/Components", + controller = "app/Http/Controllers", + event = "app/Events", + exception = "app/Exceptions", + factory = "", + job = "app/Jobs", + listener = "app/Listeners", + mail = "app/Mail", + middleware = "app/Http/Middleware", + migration = "", + model = "app/Models", + notification = "app/Notifications", + observer = "app/Observers", + policy = "app/Policies", + provider = "app/Providers", + request = "app/Http/Requests", + resource = "app/Http/Resources", + rule = "app/Rules", + scope = "app/Models/Scopes", + seeder = "", + test = "tests/Feature", +} + +local function get_directory_by_command(name) + return make_commands_map[name] +end + +local function handle_make_command(command) + -- change prompt from "name" to args + -- parse args from help + -- parse options from help + -- can parse as json + -- how to present the information + vim.ui.input({ prompt = "Name: " }, function(name) + vim.fn.jobstart({ get_artisan_command(), "artisan", command, name }, { + on_exit = function() + local directory = get_directory_by_command(vim.split(command, ":")[2]) + if directory == "" then + return + end + local filename = string.format("%s/%s.php", directory, name) + if vim.fn.findfile(filename) then + local uri = vim.uri_from_fname(string.format("%s/%s", vim.fn.getcwd(), filename)) + local buffer = vim.uri_to_bufnr(uri) + vim.api.nvim_win_set_buf(0, buffer) + end + end, + }) + end) +end + +local laravel = function(opts) + -- TODO check if cmd == sail check if sail is running + opts = opts or {} + + local results = utils.get_os_command_output({ get_artisan_command(), "artisan", "list", "--raw" }) + -- do I want to change from raw to json and decode it? + -- what advantages will I get + -- I have group by the id like "make" the command is the same + + pickers + .new({}, { + prompt_title = "Artisan commands", + finder = finders.new_table({ + results = results, + entry_maker = function(line) + local artisan_cmd = vim.split(line, " ")[1] + return { + value = artisan_cmd, + display = artisan_cmd, + ordinal = artisan_cmd, + } + end, + }), + previewer = previewers.new_buffer_previewer({ + title = "Help", + get_buffer_by_name = function(_, entry) + return entry.value + end, + + define_preview = function(self, entry) + local cmd = { get_artisan_command(), "artisan", entry.value, "-h" } + putils.job_maker(cmd, self.state.bufnr, { + value = entry.value, + bufname = self.state.bufname, + }) + end, + }), + sorter = conf.file_sorter(), + attach_mappings = function(_, map) + map("i", "<cr>", function(prompt_bufnr) + local entry = action_state.get_selected_entry() + -- check if entry.value is a make + actions.close(prompt_bufnr) + -- local cmd = string.format(":!%s %s ", get_artisan_command(), entry.value) + -- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(cmd, true, false, true), "t", true) + if is_make_command(entry.value) then + handle_make_command(entry.value) + end + end) + return true + end, + }) + :find() +end + +laravel({}) + +-- TODO list +-- open output buffer to show result of not make commands +-- Add option to add arguments for command +-- set a key when choosing the command or config, to ask for properties as well as arguments +-- take arguments in the same input +-- +-- Create Sail user command +-- Create Artisan user command + +-- +-- local uri = vim.uri_from_fname(vim.fn.getcwd() .. "/composer.json") +-- local buffer = vim.uri_to_bufnr(uri) +-- if not vim.api.nvim_buf_is_loaded(buffer) then +-- vim.api.nvim_buf_attach(buffer, false, {}) +-- end + +-- local content = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) +-- local json = vim.fn.join(content) +-- local decoded_json = vim.fn.json_decode(json) +-- P(decoded_json["require-dev"]["laravel/sail"]) +-- + +-- P(vim.fn.fnamemodify(vim.fn.findfile("composer.json"), ":p:h")) + +-- local decoded_json = vim.fn.json_decode(vim.fn.readfile(vim.fn.getcwd() .. "/composer.json")) +-- P(decoded_json["require-dev"]["laravel/sail"]) +-- P(decoded_json["require"]["laravel/sail"]) + +-- find file composer.json +-- generate base path with fnamemodify or with getcwd +-- to run the commands can use jobstart. but think this response line by line, but have on exit as well +-- I can use plenary job but not sure if makes sence + +-- vim.fn.jobstart({"sail", "artisan", "make:controller", "MyController"}, { +-- stdout_buffered = true, +-- stderr_buffered = true, +-- on_exit = function () +-- P("job ended") +-- if vim.fn.findfile("app/Http/Controllers/MyController.php") then +-- local uri = vim.uri_from_fname(vim.fn.getcwd() .. "/app/Http/Controllers/MyController.php") +-- local buffer = vim.uri_to_bufnr(uri) +-- vim.api.nvim_win_set_buf(0, buffer) +-- end +-- end, +-- -- on_stdout = function (_, data) +-- -- P("stdout") +-- -- P(data) +-- -- end, +-- -- on_stderr = function (_, data) +-- -- P("stderr") +-- -- P(data) +-- -- end, +-- }) +-- local Job = require("plenary.job") +-- Job:new({ +-- command = 'sail', +-- args = {'artisan', 'make:controller', 'MyController'}, +-- on_exit = function (j, return_val) +-- P(return_val) +-- P(j:result()) +-- P(j) +-- end +-- }):sync() diff --git a/lua/alpha/scratchs/sample.lua b/lua/alpha/scratchs/sample.lua new file mode 100644 index 0000000..052f9dd --- /dev/null +++ b/lua/alpha/scratchs/sample.lua @@ -0,0 +1,22 @@ +vim.api.nvim_create_user_command("Sailup", function () + vim.fn.jobstart( + {"sail", "up", "-d"}, + { + stdout_buffered = true, + stderr_buffered = true, + on_exit = function () + require("notify").notify("Sail up is done", "info", {}) + end, + on_stdout = function (_, data) + if data then + P(data) + end + end, + on_stderr = function (_, data) + if data then + P(data) + end + end, + } + ) +end, {}) |