summaryrefslogtreecommitdiff
path: root/lua/dropship.lua
blob: 7b5eb40b08e77f1ce03ea75feebf6f622fb0ae9f (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local M = {}

local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

M.setup = function(opts)
  opts = opts or {}
  if type(opts.drop_locations) == "table" then
    M.drop_locations = opts.drop_locations
  elseif type(opts.drop_locations) == "string" then
    local success, loaded_sites = pcall(function()
      return dofile(vim.fn.expand(opts.drop_locations))
    end)
    if success and type(loaded_sites) == "table" then
      M.drop_locations = loaded_sites
    else
      vim.notify("Failed to load drop_locations from file: " .. opts.drop_locations, vim.log.levels.ERROR)
    end
  else
    vim.notify("Invalid drop_locations configuration", vim.log.levels.ERROR)
  end
  M.use_exp = opts.new_tab_explorer or false
end

local function get_target(opts, callback)
  opts = opts or {}
  pickers
    .new(opts, {
      prompt_title = opts.prompt_title or "Drop into which project?",
      prompt_prefix = opts.prompt_icon or " ", -- Added prompt_icon option
      finder = finders.new_table({
        results = opts.drop_locations or M.drop_locations,
        entry_maker = function(entry)
          return {
            value = entry,
            display = (entry.name or "Unknown") .. ": " .. (entry.dir or "Unknown"),
            ordinal = entry.name or ".",
          }
        end,
      }),
      sorter = conf.generic_sorter(opts),
      attach_mappings = function(bufnr)
        actions.select_default:replace(function()
          actions.close(bufnr)
          local selection = action_state.get_selected_entry()
          callback(selection.value.dir)
        end)
        return true
      end,
    })
    :find()
end

M.current_tab = function(opts)
  opts = opts or {}
  get_target(opts, function(target)
    vim.cmd("tcd " .. target)
  end)
end

M.new_tab = function(opts)
  opts = opts or {}
  get_target(opts, function(target)
    if M.use_exp then
      vim.cmd("tabnew " .. target)
    else
      vim.cmd("tabnew")
    end
    vim.cmd("tcd " .. target)
  end)
end

M.globally = function(opts)
  opts = opts or {}
  get_target(opts, function(target)
    vim.cmd("cd " .. target)
  end)
end

vim.api.nvim_create_user_command("DropshipCurrentTab", function(opts)
  M.current_tab(opts)
end, { nargs = "?" })

vim.api.nvim_create_user_command("DropshipNewTab", function(opts)
  M.new_tab(opts)
end, { nargs = "?" })

vim.api.nvim_create_user_command("DropshipGlobalDir", function(opts)
  M.globally(opts)
end, { nargs = "?" })

return M