Skip to content

Instantly share code, notes, and snippets.

@nilsherzig
Created January 27, 2024 20:43
Show Gist options
  • Save nilsherzig/8f9511b0387e94d3d1d076c2410f7e75 to your computer and use it in GitHub Desktop.
Save nilsherzig/8f9511b0387e94d3d1d076c2410f7e75 to your computer and use it in GitHub Desktop.
Makefile target picker for neovim and telescope
local Path = require("plenary.path")
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local action_state = require("telescope.actions.state")
local function extract_makefile_targets()
local targets = {}
local makefile_path = Path:new("Makefile")
for line in makefile_path:iter() do
local target = line:match("^([%w-_]+):")
if target then
table.insert(targets, target)
end
end
return targets
end
local function makefile_targets_picker()
local targets = extract_makefile_targets()
pickers
.new({}, {
prompt_title = "Makefile Targets",
finder = finders.new_table({
results = targets,
entry_maker = function(entry)
return {
value = entry,
display = entry,
ordinal = entry,
}
end,
}),
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()
vim.cmd("make " .. selection.value)
end)
return true
end,
})
:find()
end
vim.keymap.set("n", "<leader>o", makefile_targets_picker, {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment