From 3fc4739c1b7bd6e3361856b655f284dda081502d Mon Sep 17 00:00:00 2001 From: davidon-top Date: Sat, 23 Sep 2023 23:53:31 +0200 Subject: [PATCH] feat: make helper --- lua/config/commands.lua | 42 ++++++++++++++++++++++++++++++++++++++++- lua/config/keymap.lua | 1 + 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lua/config/commands.lua b/lua/config/commands.lua index 86bc4cd..34d83ea 100644 --- a/lua/config/commands.lua +++ b/lua/config/commands.lua @@ -81,6 +81,24 @@ vim.api.nvim_create_user_command("RetabFile", function () vim.cmd(":set ts=2 sts=2 noet | retab! | set ts=4 sts=4 et | retab!") end, {}) +local function get_makefile_targets() + local makefile = "./Makefile" + local targets = {} + + local file = io.open(makefile, "r") + if file then + for line in file:lines() do + local target = line:match("^[^\\s:]+:.*") + if target then + local tg = target:match("^[^:]+") + table.insert(targets, tg) + end + end + file:close() + end + return targets +end + vim.api.nvim_create_user_command("Make", function (args) local vimCmd = "TermExec cmd=\"make" if (args["args"]) then @@ -88,4 +106,26 @@ vim.api.nvim_create_user_command("Make", function (args) end vimCmd = vimCmd .. "\"" vim.cmd(vimCmd) -end, {nargs = "*"}) +end, {nargs = "*", ["complete"] = function() + return get_makefile_targets() +end}) + +vim.api.nvim_create_user_command("Maket", function() + local targets = get_makefile_targets() + pickers.new({}, { + prompt_title = "Run make target", + finder = finders.new_table { + results = targets + }, + sorter = conf.generic_sorter({}), + attach_mappings = function (prompt_bufnr, map) + actions.select_default:replace(function () + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + selection = selection[1] + vim.cmd(":Make " .. selection) + end) + return true + end + }):find() +end, {}) diff --git a/lua/config/keymap.lua b/lua/config/keymap.lua index 4e6c6b2..eda540b 100644 --- a/lua/config/keymap.lua +++ b/lua/config/keymap.lua @@ -57,6 +57,7 @@ local keys = { b = {require("telescope.builtin").buffers, "buffers"}, h = {require("telescope.builtin").help_tags, "help tags"}, s = {function () require("telescope.builtin").grep_string({ search = vim.fn.input("Grep > ")}); end, "grep search through files"}, + m = {"Maket", "Make"}, }, s = { name = "Settings",