Skip to content

Commit

Permalink
Merge pull request #37 from alvarosevilla95/telescope-picker
Browse files Browse the repository at this point in the history
feat: add support for telescope picker
  • Loading branch information
dlants authored Jan 26, 2025
2 parents cbf61f8 + 88acec3 commit 2097c1f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ TLDR:
- `<leader>mt` is for `:Magenta toggle`, will toggle the sidebar on and off.
- `<leader>mp` is for `:Magenta paste-selection`. In visual mode it will take the current selection and paste it into the input buffer.
- `<leader>mb` is for `:Magenta context-files` with your _current_ file. It will pin the current file to your context.
- `<leader>mf` is for `:Magenta context-files` it allows you to select files via fzf-lua, and will pin those files to your context. This requires that fzf-lua is installed.
- `<leader>mf` is for `:Magenta context-files` it allows you to select files via fzf-lua or telescope, and will pin those files to your context. This requires that fzf-lua or telescope are installed.
- `<leader>mc` is for `:Magenta clear`, which will clear the current chat.
- `<leader>ma` is for `:Magenta abort`, which will abort the current in-flight request.

Expand Down
57 changes: 22 additions & 35 deletions lua/magenta/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ M.defaults = {

M.setup = function(opts)
M.options = vim.tbl_deep_extend("force", M.defaults, opts or {})
if (M.options.picker == nil) then
local success, _ = pcall(require, "fzf-lua")
if success then
M.options.picker = "fzf-lua"
else
success, _ = pcall(require, "telescope")
if success then
M.options.picker = "telescope"
end
end
end

M.start(true)
vim.api.nvim_set_keymap(
Expand Down Expand Up @@ -75,25 +86,13 @@ M.setup = function(opts)
silent = true,
desc = "Select files to add to Magenta context",
callback = function()
local success, fzf = pcall(require, "fzf-lua")
if not success then
Utils.log_job("error", "fzf-lua is not installed")
if M.options.picker == "fzf-lua" then
Utils.fzf_files()
elseif M.options.picker == "telescope" then
Utils.telescope_files()
else
vim.notify("Neither fzf-lua nor telescope are installed!", vim.log.levels.ERROR)
end

fzf.files(
{
raw = true, -- return just the raw path strings
actions = {
["default"] = function(selected)
local escaped_files = {}
for _, entry in ipairs(selected) do
table.insert(escaped_files, vim.fn.shellescape(fzf.path.entry_to_file(entry).path))
end
vim.cmd("Magenta context-files " .. table.concat(escaped_files, " "))
end
}
}
)
end
}
)
Expand All @@ -107,29 +106,17 @@ M.setup = function(opts)
silent = true,
desc = "Select provider and model",
callback = function()
local success, fzf = pcall(require, "fzf-lua")
if not success then
Utils.log_job("error", "fzf-lua is not installed")
end

local items = {
'openai gpt-4o',
'openai o1',
'openai o1-mini',
'anthropic claude-3-5-sonnet-latest'
}

fzf.fzf_exec(items, {
prompt = 'Select Model > ',
actions = {
['default'] = function(selected)
-- selected[1] contains the selected line
-- Your code here to handle the selection
-- For example:
vim.cmd("Magenta provider " .. selected[1] )
end
}
})
vim.ui.select(items, { prompt = "Select Model", }, function (choice)
if choice ~= nil then
vim.cmd("Magenta provider " .. choice )
end
end)
end
}
)
Expand Down
45 changes: 45 additions & 0 deletions lua/magenta/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,49 @@ M.log_job = function(log_level, is_stderr)
end
end

M.fzf_files = function()
local fzf = require("fzf")
fzf.files(
{
raw = true, -- return just the raw path strings
actions = {
["default"] = function(selected)
local escaped_files = {}
for _, entry in ipairs(selected) do
table.insert(escaped_files, vim.fn.shellescape(fzf.path.entry_to_file(entry).path))
end
vim.cmd("Magenta context-files " .. table.concat(escaped_files, " "))
end
}
}
)
end

M.telescope_files = function()
local builtin = require("telescope.builtin")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
builtin.find_files({
prompt_title = "Select context files",
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local picker = action_state.get_current_picker(prompt_bufnr)
local selected_entries = picker:get_multi_selection()
if vim.tbl_isempty(selected_entries) then
selected_entries = { action_state.get_selected_entry() }
end
actions.close(prompt_bufnr)
local escaped_files = {}
for _, entry in ipairs(selected_entries) do
table.insert(escaped_files, vim.fn.shellescape(entry.path))
end
if not vim.tbl_isempty(escaped_files) then
vim.cmd("Magenta context-files " .. table.concat(escaped_files, " "))
end
end)
return true
end,
})
end

return M

0 comments on commit 2097c1f

Please sign in to comment.