Skip to content

Commit 01419c1

Browse files
committed
refactor: prefer local alias for vim.api, vim.lsp
1 parent c41b36d commit 01419c1

File tree

15 files changed

+358
-341
lines changed

15 files changed

+358
-341
lines changed

lua/telescope/actions/init.lua

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
--- file.
5353
---@brief ]]
5454

55-
local a = vim.api
55+
local api = vim.api
5656

5757
local conf = require("telescope.config").values
5858
local state = require "telescope.state"
@@ -370,7 +370,7 @@ end
370370

371371
actions.close_pum = function(_)
372372
if 0 ~= vim.fn.pumvisible() then
373-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<c-y>", true, true, true), "n", true)
373+
api.nvim_feedkeys(api.nvim_replace_termcodes("<c-y>", true, true, true), "n", true)
374374
end
375375
end
376376

@@ -379,14 +379,14 @@ end
379379
actions.close = function(prompt_bufnr)
380380
local picker = action_state.get_current_picker(prompt_bufnr)
381381
local original_win_id = picker.original_win_id
382-
local cursor_valid, original_cursor = pcall(a.nvim_win_get_cursor, original_win_id)
382+
local cursor_valid, original_cursor = pcall(api.nvim_win_get_cursor, original_win_id)
383383

384384
actions.close_pum(prompt_bufnr)
385385

386386
require("telescope.pickers").on_close_prompt(prompt_bufnr)
387-
pcall(a.nvim_set_current_win, original_win_id)
388-
if cursor_valid and a.nvim_get_mode().mode == "i" and picker._original_mode ~= "i" then
389-
pcall(a.nvim_win_set_cursor, original_win_id, { original_cursor[1], original_cursor[2] + 1 })
387+
pcall(api.nvim_set_current_win, original_win_id)
388+
if cursor_valid and api.nvim_get_mode().mode == "i" and picker._original_mode ~= "i" then
389+
pcall(api.nvim_win_set_cursor, original_win_id, { original_cursor[1], original_cursor[2] + 1 })
390390
end
391391
end
392392

@@ -400,14 +400,14 @@ end
400400

401401
local set_edit_line = function(prompt_bufnr, fname, prefix, postfix)
402402
postfix = vim.F.if_nil(postfix, "")
403-
postfix = a.nvim_replace_termcodes(postfix, true, false, true)
403+
postfix = api.nvim_replace_termcodes(postfix, true, false, true)
404404
local selection = action_state.get_selected_entry()
405405
if selection == nil then
406406
utils.__warn_no_selection(fname)
407407
return
408408
end
409409
actions.close(prompt_bufnr)
410-
a.nvim_feedkeys(prefix .. selection.value .. postfix, "n", true)
410+
api.nvim_feedkeys(prefix .. selection.value .. postfix, "n", true)
411411
end
412412

413413
--- Set a value in the command line and don't run it, making it editable.
@@ -479,7 +479,7 @@ actions.paste_register = function(prompt_bufnr)
479479

480480
-- ensure that the buffer can be written to
481481
if vim.bo[0].modifiable then
482-
vim.api.nvim_paste(selection.content, true, -1)
482+
api.nvim_paste(selection.content, true, -1)
483483
end
484484
end
485485

@@ -489,7 +489,7 @@ actions.insert_symbol = function(prompt_bufnr)
489489
local symbol = action_state.get_selected_entry().value[1]
490490
actions.close(prompt_bufnr)
491491
vim.schedule(function()
492-
vim.api.nvim_put({ symbol }, "", true, true)
492+
api.nvim_put({ symbol }, "", true, true)
493493
end)
494494
end
495495

@@ -500,7 +500,7 @@ actions.insert_symbol_i = function(prompt_bufnr)
500500
actions.close(prompt_bufnr)
501501
vim.schedule(function()
502502
vim.cmd [[startinsert]]
503-
vim.api.nvim_put({ symbol }, "", true, true)
503+
api.nvim_put({ symbol }, "", true, true)
504504
end)
505505
end
506506

@@ -928,15 +928,15 @@ local send_selected_to_qf = function(prompt_bufnr, mode, target)
928928
local prompt = picker:_get_prompt()
929929
actions.close(prompt_bufnr)
930930

931-
vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
931+
api.nvim_exec_autocmds("QuickFixCmdPre", {})
932932
if target == "loclist" then
933933
vim.fn.setloclist(picker.original_win_id, qf_entries, mode)
934934
else
935935
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
936936
vim.fn.setqflist(qf_entries, mode)
937937
vim.fn.setqflist({}, "a", { title = qf_title })
938938
end
939-
vim.api.nvim_exec_autocmds("QuickFixCmdPost", {})
939+
api.nvim_exec_autocmds("QuickFixCmdPost", {})
940940
end
941941

942942
local send_all_to_qf = function(prompt_bufnr, mode, target)
@@ -951,7 +951,7 @@ local send_all_to_qf = function(prompt_bufnr, mode, target)
951951
local prompt = picker:_get_prompt()
952952
actions.close(prompt_bufnr)
953953

954-
vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
954+
api.nvim_exec_autocmds("QuickFixCmdPre", {})
955955
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
956956
if target == "loclist" then
957957
vim.fn.setloclist(picker.original_win_id, qf_entries, mode)
@@ -960,7 +960,7 @@ local send_all_to_qf = function(prompt_bufnr, mode, target)
960960
vim.fn.setqflist(qf_entries, mode)
961961
vim.fn.setqflist({}, "a", { title = qf_title })
962962
end
963-
vim.api.nvim_exec_autocmds("QuickFixCmdPost", {})
963+
api.nvim_exec_autocmds("QuickFixCmdPost", {})
964964
end
965965

966966
--- Sends the selected entries to the quickfix list, replacing the previous entries.
@@ -1120,7 +1120,7 @@ actions.complete_tag = function(prompt_bufnr)
11201120
end
11211121

11221122
-- incremental completion by substituting string starting from col - #line byte offset
1123-
local col = vim.api.nvim_win_get_cursor(0)[2] + 1
1123+
local col = api.nvim_win_get_cursor(0)[2] + 1
11241124
vim.fn.complete(col - #line, filtered_tags)
11251125
end
11261126

@@ -1180,33 +1180,33 @@ actions.delete_buffer = function(prompt_bufnr)
11801180

11811181
current_picker:delete_selection(function(selection)
11821182
local force = vim.bo[selection.bufnr].buftype == "terminal"
1183-
local ok = pcall(vim.api.nvim_buf_delete, selection.bufnr, { force = force })
1183+
local ok = pcall(api.nvim_buf_delete, selection.bufnr, { force = force })
11841184

11851185
-- If the current buffer is deleted, switch to the previous buffer
11861186
-- according to bdelete behavior
11871187
if ok and selection.bufnr == current_picker.original_bufnr then
1188-
if vim.api.nvim_win_is_valid(current_picker.original_win_id) then
1188+
if api.nvim_win_is_valid(current_picker.original_win_id) then
11891189
local jumplist = vim.fn.getjumplist(current_picker.original_win_id)[1]
11901190
for i = #jumplist, 1, -1 do
11911191
if jumplist[i].bufnr ~= selection.bufnr and vim.fn.bufloaded(jumplist[i].bufnr) == 1 then
1192-
vim.api.nvim_win_set_buf(current_picker.original_win_id, jumplist[i].bufnr)
1192+
api.nvim_win_set_buf(current_picker.original_win_id, jumplist[i].bufnr)
11931193
current_picker.original_bufnr = jumplist[i].bufnr
11941194
return ok
11951195
end
11961196
end
11971197

11981198
-- no more valid buffers in jumplist, create an empty buffer
1199-
local empty_buf = vim.api.nvim_create_buf(true, true)
1200-
vim.api.nvim_win_set_buf(current_picker.original_win_id, empty_buf)
1199+
local empty_buf = api.nvim_create_buf(true, true)
1200+
api.nvim_win_set_buf(current_picker.original_win_id, empty_buf)
12011201
current_picker.original_bufnr = empty_buf
1202-
vim.api.nvim_buf_delete(selection.bufnr, { force = true })
1202+
api.nvim_buf_delete(selection.bufnr, { force = true })
12031203
return ok
12041204
end
12051205

12061206
-- window of the selected buffer got wiped, switch to first valid window
12071207
local win_id = vim.fn.win_getid(1, current_picker.original_tabpage)
12081208
current_picker.original_win_id = win_id
1209-
current_picker.original_bufnr = vim.api.nvim_win_get_buf(win_id)
1209+
current_picker.original_bufnr = api.nvim_win_get_buf(win_id)
12101210
end
12111211
return ok
12121212
end)
@@ -1284,10 +1284,10 @@ actions.which_key = function(prompt_bufnr, opts)
12841284
-- close on repeated keypress
12851285
local km_bufs = (function()
12861286
local ret = {}
1287-
local bufs = a.nvim_list_bufs()
1287+
local bufs = api.nvim_list_bufs()
12881288
for _, buf in ipairs(bufs) do
12891289
for _, bufname in ipairs { "_TelescopeWhichKey", "_TelescopeWhichKeyBorder" } do
1290-
if string.find(a.nvim_buf_get_name(buf), bufname) then
1290+
if string.find(api.nvim_buf_get_name(buf), bufname) then
12911291
table.insert(ret, buf)
12921292
end
12931293
end
@@ -1299,7 +1299,7 @@ actions.which_key = function(prompt_bufnr, opts)
12991299
utils.buf_delete(buf)
13001300
local win_ids = vim.fn.win_findbuf(buf)
13011301
for _, win_id in ipairs(win_ids) do
1302-
pcall(a.nvim_win_close, win_id, true)
1302+
pcall(api.nvim_win_close, win_id, true)
13031303
end
13041304
end
13051305
return
@@ -1323,7 +1323,7 @@ actions.which_key = function(prompt_bufnr, opts)
13231323
end
13241324

13251325
local mappings = {}
1326-
local mode = a.nvim_get_mode().mode
1326+
local mode = api.nvim_get_mode().mode
13271327
for _, v in pairs(action_utils.get_registered_mappings(prompt_bufnr)) do
13281328
if v.desc and v.desc ~= "which_key" and v.desc ~= "nop" then
13291329
if not opts.only_show_current_mode or mode == v.mode then
@@ -1367,7 +1367,7 @@ actions.which_key = function(prompt_bufnr, opts)
13671367

13681368
-- place hints at top or bottom relative to prompt
13691369
local win_central_row = function(win_nr)
1370-
return a.nvim_win_get_position(win_nr)[1] + 0.5 * a.nvim_win_get_height(win_nr)
1370+
return api.nvim_win_get_position(win_nr)[1] + 0.5 * api.nvim_win_get_height(win_nr)
13711371
end
13721372
-- TODO(fdschmidt93|l-kershaw): better generalization of where to put which key float
13731373
local picker = action_state.get_current_picker(prompt_bufnr)
@@ -1396,43 +1396,49 @@ actions.which_key = function(prompt_bufnr, opts)
13961396
zindex = opts.zindex,
13971397
}
13981398
local km_win_id, km_opts = popup.create("", popup_opts)
1399-
local km_buf = a.nvim_win_get_buf(km_win_id)
1400-
a.nvim_buf_set_name(km_buf, "_TelescopeWhichKey")
1401-
a.nvim_buf_set_name(km_opts.border.bufnr, "_TelescopeTelescopeWhichKeyBorder")
1399+
local km_buf = api.nvim_win_get_buf(km_win_id)
1400+
api.nvim_buf_set_name(km_buf, "_TelescopeWhichKey")
1401+
api.nvim_buf_set_name(km_opts.border.bufnr, "_TelescopeTelescopeWhichKeyBorder")
14021402
vim.wo[km_win_id].winhl = "Normal:" .. opts.normal_hl
14031403
vim.wo[km_opts.border.win_id].winhl = "Normal:" .. opts.border_hl
14041404
vim.wo[km_win_id].winblend = opts.winblend
14051405
vim.wo[km_win_id].foldenable = false
14061406

1407-
vim.api.nvim_create_autocmd("BufLeave", {
1407+
api.nvim_create_autocmd("BufLeave", {
14081408
buffer = km_buf,
14091409
once = true,
14101410
callback = function()
1411-
pcall(vim.api.nvim_win_close, km_win_id, true)
1412-
pcall(vim.api.nvim_win_close, km_opts.border.win_id, true)
1411+
pcall(api.nvim_win_close, km_win_id, true)
1412+
pcall(api.nvim_win_close, km_opts.border.win_id, true)
14131413
require("telescope.utils").buf_delete(km_buf)
14141414
end,
14151415
})
14161416

1417-
a.nvim_buf_set_lines(km_buf, 0, -1, false, utils.repeated_table(opts.num_rows + 2 * opts.line_padding, column_indent))
1417+
api.nvim_buf_set_lines(
1418+
km_buf,
1419+
0,
1420+
-1,
1421+
false,
1422+
utils.repeated_table(opts.num_rows + 2 * opts.line_padding, column_indent)
1423+
)
14181424

1419-
local keymap_highlights = a.nvim_create_namespace "telescope_whichkey"
1425+
local keymap_highlights = api.nvim_create_namespace "telescope_whichkey"
14201426
local highlights = {}
14211427
for index, mapping in ipairs(mappings) do
14221428
local row = utils.cycle(index, opts.num_rows) - 1 + opts.line_padding
1423-
local prev_line = a.nvim_buf_get_lines(km_buf, row, row + 1, false)[1]
1429+
local prev_line = api.nvim_buf_get_lines(km_buf, row, row + 1, false)[1]
14241430
if index == total_available_entries and total_available_entries > #mappings then
14251431
local new_line = prev_line .. "..."
1426-
a.nvim_buf_set_lines(km_buf, row, row + 1, false, { new_line })
1432+
api.nvim_buf_set_lines(km_buf, row, row + 1, false, { new_line })
14271433
break
14281434
end
14291435
local display, display_hl = make_display(mapping)
14301436
local new_line = prev_line .. display .. opts.column_padding -- incl. padding
1431-
a.nvim_buf_set_lines(km_buf, row, row + 1, false, { new_line })
1437+
api.nvim_buf_set_lines(km_buf, row, row + 1, false, { new_line })
14321438
table.insert(highlights, { hl = display_hl, row = row, col = #prev_line })
14331439
end
14341440

1435-
-- highlighting only after line setting as vim.api.nvim_buf_set_lines removes hl otherwise
1441+
-- highlighting only after line setting as a.nvim_buf_set_lines removes hl otherwise
14361442
for _, highlight_tbl in pairs(highlights) do
14371443
local highlight = highlight_tbl.hl
14381444
local row_ = highlight_tbl.row
@@ -1458,14 +1464,14 @@ actions.which_key = function(prompt_bufnr, opts)
14581464
end
14591465
-- only set up autocommand after showing preview completed
14601466
vim.schedule(function()
1461-
vim.api.nvim_create_autocmd(close_event, {
1467+
api.nvim_create_autocmd(close_event, {
14621468
pattern = close_pattern,
14631469
buffer = close_buffer,
14641470
once = true,
14651471
callback = function()
14661472
vim.schedule(function()
1467-
pcall(vim.api.nvim_win_close, km_win_id, true)
1468-
pcall(vim.api.nvim_win_close, km_opts.border.win_id, true)
1473+
pcall(api.nvim_win_close, km_win_id, true)
1474+
pcall(api.nvim_win_close, km_opts.border.win_id, true)
14691475
utils.buf_delete(km_buf)
14701476
end)
14711477
end,
@@ -1516,9 +1522,9 @@ actions.delete_mark = function(prompt_bufnr)
15161522

15171523
local success
15181524
if mark:match "%u" then
1519-
success = pcall(vim.api.nvim_del_mark, mark)
1525+
success = pcall(api.nvim_del_mark, mark)
15201526
else
1521-
success = pcall(vim.api.nvim_buf_del_mark, bufnr, mark)
1527+
success = pcall(api.nvim_buf_del_mark, bufnr, mark)
15221528
end
15231529
return success
15241530
end)

0 commit comments

Comments
 (0)