summaryrefslogtreecommitdiffstats
path: root/lua/core/harpoon.lua
blob: 4d20828cc12e858d891c0d382874f739a47252e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_set = require("telescope.actions.set")
local action_state = require("telescope.actions.state")
local harpoon = require('harpoon')

local M = {}

function M.switcher()
  local filepaths = {}
  for _, item in ipairs(harpoon:list().items) do
    table.insert(filepaths, item.value)
  end

  pickers.new({
    prompt_title = "Harpoon",
    finder = finders.new_table { results = filepaths },
    sorter = conf.generic_sorter(),
    previewer = conf.file_previewer {},
    attach_mappings = function(prompt_bufnr, map)
      actions.move_selection_previous:replace(function()
        action_set.shift_selection(prompt_bufnr, -1)
      end)
      actions.move_selection_next:replace(function()
        action_set.shift_selection(prompt_bufnr, 1)
      end)

      -- remove harpoon item
      -- TODO:

      -- select items, and open buffer
      vim.keymap.set("n", "<C-v>", function()
        if action_state.get_selected_entry() then
          actions.close(prompt_bufnr)
          vim.cmd("vsplit "..action_state.get_selected_entry()[1])
        end
      end)
      vim.keymap.set({ "n", "i" }, "<C-s>", function()
        if action_state.get_selected_entry() then
          actions.close(prompt_bufnr)
          vim.cmd("split "..action_state.get_selected_entry()[1])
        end
      end)
      actions.select_default:replace(function()
        if action_state.get_selected_entry() then
          actions.close(prompt_bufnr)
          vim.cmd("e "..action_state.get_selected_entry()[1])
        end
      end)
      return true
    end
  }):find()
end

return M