From 472f3e2fc6ba7c6e028c07d4c20d06441e62ec72 Mon Sep 17 00:00:00 2001 From: Kim Gert Nielsen Date: Mon, 29 Jul 2024 20:36:19 +0200 Subject: [PATCH] feat!: Add a edit option to modify commands --- README.md | 6 +-- lua/greyjoy/init.lua | 87 ++++++++++++++++++++++++++++++++++++++----- lua/greyjoy/utils.lua | 8 ++++ 3 files changed, 89 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c4de27b..105d948 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Integration with [toggleterm](https://github.com/akinsho/toggleterm.nvim) is als ## Requirements -Neovim 0.9+ is required +Neovim 0.10+ is required ## Default settings @@ -51,7 +51,7 @@ Neovim 0.9+ is required } ``` -Per default all plugins use the same terminal but this behaviour (if you are using `toggleterm`) can be overridden by either grouping the plugins to a specific `group_id` or create a function to assign number based on plugin name. +Per default all plugins use the same terminal but this behaviour (if you are using `toggleterm`) can be overridden by either grouping the plugins to a specific `group_id` or create a function to assign number based on plugin name. Notice that the plugin/group name `edit` is reserved. So if you want all plugins to run under id `id` (default) but the `docker_compose` you would like to have another group you can configure it via @@ -198,7 +198,7 @@ use({"desdic/greyjoy.nvim", }) ``` -Once installed and reloaded you can use `:Greyjoy` to run it or `Greyjoy `. +Once installed and reloaded you can use `:Greyjoy` to run it, `Greyjoy ` or `Greyjoy edit` to modify commands. So in the above example its possible to run the generic and makefile plugin by running `:Greyjoy fast` or if you only wanted to run the makefile plugin you could do `:Greyjoy makefile` diff --git a/lua/greyjoy/init.lua b/lua/greyjoy/init.lua index 800fc6b..6cd4d99 100644 --- a/lua/greyjoy/init.lua +++ b/lua/greyjoy/init.lua @@ -16,6 +16,7 @@ greyjoy.setup = function(options) _extensions.set_config(config.defaults["extensions"] or {}) greyjoy.last_element = {} + greyjoy.overrides = {} -- easy index to do lookup later greyjoy.run_group_map = {} @@ -52,15 +53,30 @@ end greyjoy.extensions = require("greyjoy._extensions").manager -greyjoy.menu = function(rootdir, elements) - if next(elements) == nil then - return - end - +local function generate_list(rootdir, elements, overrides) local menuelem = {} local menulookup = {} local commands = {} + + local newelements = {} for _, value in ipairs(elements) do + local commandinput = table.concat(value.command, " ") + if overrides[commandinput] then + local override = overrides[commandinput] + + table.insert(newelements, { + name = override, + command = utils.str_to_array(override), + orig_command = utils.str_to_array(commandinput), + path = value.path, + plugin = value.plugin, + }) + else + table.insert(newelements, value) + end + end + + for _, value in ipairs(newelements) do -- keep track of what elements we have menulookup[value["name"]] = true table.insert(menuelem, value["name"]) @@ -69,6 +85,7 @@ greyjoy.menu = function(rootdir, elements) path = value["path"], group_id = value["group_id"], plugin = value["plugin"], + orig_command = value["orig_command"] or nil, } end @@ -87,7 +104,52 @@ greyjoy.menu = function(rootdir, elements) end end - vim.ui.select(menuelem, { prompt = "Select a command" }, function(label, _) + return menuelem, commands +end + +greyjoy.edit = function(rootdir, elements) + if next(elements) == nil then + return + end + + local menuelem, commands = + generate_list(rootdir, elements, greyjoy.overrides) + vim.ui.select(menuelem, { prompt = "Edit a command" }, function(label) + if label then + greyjoy.last_element[rootdir] = label + local command = commands[label] + + local commandinput = table.concat(command.command, " ") + + vim.ui.input( + { prompt = "Edit", default = commandinput }, + function(input) + if input then + if command.orig_command then + local tmp = table.concat(command.orig_command, " ") + if tmp == input then + greyjoy.overrides[tmp] = nil + else + greyjoy.overrides[tmp] = input + end + else + greyjoy.overrides[commandinput] = input + end + end + end + ) + end + end) +end + +greyjoy.menu = function(rootdir, elements) + if next(elements) == nil then + return + end + + local menuelem, commands = + generate_list(rootdir, elements, greyjoy.overrides) + vim.ui.select(menuelem, { prompt = "Select a command" }, function(label) if label then greyjoy.last_element[rootdir] = label local command = commands[label] @@ -209,7 +271,7 @@ local add_elements = function(elements, output) end end -greyjoy.run = function(arg) +greyjoy.run = function(args) -- just return if disabled if not greyjoy.enable then return @@ -219,7 +281,10 @@ greyjoy.run = function(arg) local fullname = vim.api.nvim_buf_get_name(0) local filename = vim.fs.basename(fullname) local filepath = vim.fs.dirname(fullname) - local pluginname = arg or "" + local pluginname = args or "" + if pluginname == "edit" then + pluginname = "" + end local uv = vim.uv filepath = utils.if_nil(filepath, "") @@ -270,7 +335,11 @@ greyjoy.run = function(arg) end end - greyjoy.menu(rootdir, elements) + if args == "edit" then + greyjoy.edit(rootdir, elements) + else + greyjoy.menu(rootdir, elements) + end end vim.api.nvim_create_user_command("Greyjoy", function(args) diff --git a/lua/greyjoy/utils.lua b/lua/greyjoy/utils.lua index 78c40d8..bc17121 100644 --- a/lua/greyjoy/utils.lua +++ b/lua/greyjoy/utils.lua @@ -33,4 +33,12 @@ M.if_nil = function(x, y) return x end +M.str_to_array = function(str) + local words = {} + for word in str:gmatch("%w+") do + table.insert(words, word) + end + return words +end + return M