summaryrefslogtreecommitdiff
path: root/lua/telescope/actions.lua
blob: ae20eb471df8dee89270eb68defeb55eea9765a7 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
-- Actions functions that are useful for people creating their own mappings.

local a = vim.api

local log = require('telescope.log')
local state = require('telescope.state')

local actions = setmetatable({}, {
  __index = function(_, k)
    error("Actions does not have a value: " .. tostring(k))
  end
})

--- Get the current picker object for the prompt
function actions.get_current_picker(prompt_bufnr)
  return state.get_status(prompt_bufnr).picker
end

--- Move the current selection of a picker {change} rows.
--- Handles not overflowing / underflowing the list.
function actions.shift_current_selection(prompt_bufnr, change)
  actions.get_current_picker(prompt_bufnr):move_selection(change)
end

function actions.move_selection_next(prompt_bufnr)
  actions.shift_current_selection(prompt_bufnr, 1)
end

function actions.move_selection_previous(prompt_bufnr)
  actions.shift_current_selection(prompt_bufnr, -1)
end

--- Get the current entry
function actions.get_selected_entry(prompt_bufnr)
  return actions.get_current_picker(prompt_bufnr):get_selection()
end

function actions.preview_scrolling_up(prompt_bufnr)
  actions.get_current_picker(prompt_bufnr).previewer:scroll_fn(-30)
end

function actions.preview_scrolling_down(prompt_bufnr)
  actions.get_current_picker(prompt_bufnr).previewer:scroll_fn(30)
end

-- TODO: It seems sometimes we get bad styling.
local function goto_file_selection(prompt_bufnr, command)
  local picker = actions.get_current_picker(prompt_bufnr)
  local entry = actions.get_selected_entry(prompt_bufnr)

  if not entry then
    print("[telescope] Nothing currently selected")
    return
  else
    local filename, row, col
    if entry.filename then
      filename = entry.path or entry.filename

      -- TODO: Check for off-by-one
      row = entry.row or entry.lnum
      col = entry.col
    else
      -- TODO: Might want to remove this and force people
      -- to put stuff into `filename`
      local value = entry.value
      if not value then
        print("Could not do anything with blank line...")
        return
      end

      if type(value) == "table" then
        value = entry.display
      end

      local sections = vim.split(value, ":")

      filename = sections[1]
      row = tonumber(sections[2])
      col = tonumber(sections[3])
    end

    local preview_win = state.get_status(prompt_bufnr).preview_win
    if preview_win then
      a.nvim_win_set_config(preview_win, {style = ''})
    end

    local original_win_id = picker.original_win_id or 0
    local entry_bufnr = entry.bufnr

    actions.close(prompt_bufnr)

    -- TODO: Sometimes we open something with missing line numbers and stuff...
    if entry_bufnr then
      if command == "e" then
        a.nvim_win_set_buf(original_win_id, entry_bufnr)
        vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename))
      else
        vim.cmd(string.format(":%s #%d", command, entry_bufnr))
      end
    else
      vim.cmd(string.format(":%s %s", command, filename))

      local bufnr = vim.api.nvim_get_current_buf()
      a.nvim_buf_set_option(bufnr, 'buflisted', true)
      if row and col then
        local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, {row, col})
        if not ok then
          log.debug("Failed to move to cursor:", err_msg)
        end
      end
    end
  end
end

function actions.goto_file_selection_edit(prompt_bufnr)
  goto_file_selection(prompt_bufnr, "e")
end

function actions.goto_file_selection_split(prompt_bufnr)
  goto_file_selection(prompt_bufnr, "sp")
end

function actions.goto_file_selection_vsplit(prompt_bufnr)
  goto_file_selection(prompt_bufnr, "vsp")
end

function actions.goto_file_selection_tabedit(prompt_bufnr)
  goto_file_selection(prompt_bufnr, "tabe")
end

function actions.close_pum(_)
  if 0 ~= vim.fn.pumvisible() then
    vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<c-y>", true, true, true), 'n', true)
  end
end

function actions.close(prompt_bufnr)
  local picker = actions.get_current_picker(prompt_bufnr)
  local prompt_win = state.get_status(prompt_bufnr).prompt_win
  local original_win_id = picker.original_win_id

  if picker.previewer then
    picker.previewer:teardown()
  end

  actions.close_pum(prompt_bufnr)
  vim.cmd [[stopinsert]]

  vim.api.nvim_win_close(prompt_win, true)
  pcall(vim.cmd, string.format([[bdelete! %s]], prompt_bufnr))

  a.nvim_set_current_win(original_win_id)
end

actions.set_command_line = function(prompt_bufnr)
  local entry = actions.get_selected_entry(prompt_bufnr)

  actions.close(prompt_bufnr)

  vim.cmd(entry.value)
end

-- TODO: Think about how to do this.
actions.insert_value = function(prompt_bufnr)
  local entry = actions.get_selected_entry(prompt_bufnr)

  vim.schedule(function()
    actions.close(prompt_bufnr)
  end)

  return entry.value
end

return actions