Skip to content

Commit e586b4e

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

File tree

15 files changed

+293
-283
lines changed

15 files changed

+293
-283
lines changed

lua/telescope/actions/init.lua

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
a.nvim_feedkeys(a.nvim_replace_termcodes("<c-y>", true, true, true), "n", true)
374374
end
375375
end
376376

@@ -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+
a.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+
a.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+
a.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+
a.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+
a.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+
a.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+
a.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 = a.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(a.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 a.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+
a.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 = a.nvim_create_buf(true, true)
1200+
a.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+
a.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 = a.nvim_win_get_buf(win_id)
12101210
end
12111211
return ok
12121212
end)
@@ -1404,12 +1404,12 @@ actions.which_key = function(prompt_bufnr, opts)
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+
a.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(a.nvim_win_close, km_win_id, true)
1412+
pcall(a.nvim_win_close, km_opts.border.win_id, true)
14131413
require("telescope.utils").buf_delete(km_buf)
14141414
end,
14151415
})
@@ -1432,7 +1432,7 @@ actions.which_key = function(prompt_bufnr, opts)
14321432
table.insert(highlights, { hl = display_hl, row = row, col = #prev_line })
14331433
end
14341434

1435-
-- highlighting only after line setting as vim.api.nvim_buf_set_lines removes hl otherwise
1435+
-- highlighting only after line setting as a.nvim_buf_set_lines removes hl otherwise
14361436
for _, highlight_tbl in pairs(highlights) do
14371437
local highlight = highlight_tbl.hl
14381438
local row_ = highlight_tbl.row
@@ -1458,14 +1458,14 @@ actions.which_key = function(prompt_bufnr, opts)
14581458
end
14591459
-- only set up autocommand after showing preview completed
14601460
vim.schedule(function()
1461-
vim.api.nvim_create_autocmd(close_event, {
1461+
a.nvim_create_autocmd(close_event, {
14621462
pattern = close_pattern,
14631463
buffer = close_buffer,
14641464
once = true,
14651465
callback = function()
14661466
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)
1467+
pcall(a.nvim_win_close, km_win_id, true)
1468+
pcall(a.nvim_win_close, km_opts.border.win_id, true)
14691469
utils.buf_delete(km_buf)
14701470
end)
14711471
end,
@@ -1516,9 +1516,9 @@ actions.delete_mark = function(prompt_bufnr)
15161516

15171517
local success
15181518
if mark:match "%u" then
1519-
success = pcall(vim.api.nvim_del_mark, mark)
1519+
success = pcall(a.nvim_del_mark, mark)
15201520
else
1521-
success = pcall(vim.api.nvim_buf_del_mark, bufnr, mark)
1521+
success = pcall(a.nvim_buf_del_mark, bufnr, mark)
15221522
end
15231523
return success
15241524
end)

lua/telescope/actions/set.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ do
101101
if buf_command ~= "drop" and buf_command ~= "tab drop" then
102102
vim.cmd(string.format("%s %d", buf_command, bufnr))
103103
else
104-
vim.cmd(string.format("%s %s", buf_command, vim.fn.fnameescape(vim.api.nvim_buf_get_name(bufnr))))
104+
vim.cmd(string.format("%s %s", buf_command, vim.fn.fnameescape(a.nvim_buf_get_name(bufnr))))
105105
end
106106
end
107107
end
@@ -170,7 +170,7 @@ action_set.edit = function(prompt_bufnr, command)
170170

171171
local picker = action_state.get_current_picker(prompt_bufnr)
172172
require("telescope.pickers").on_close_prompt(prompt_bufnr)
173-
pcall(vim.api.nvim_set_current_win, picker.original_win_id)
173+
pcall(a.nvim_set_current_win, picker.original_win_id)
174174
local win_id = picker.get_selection_window(picker, entry)
175175

176176
if picker.push_cursor_on_edit then
@@ -184,7 +184,7 @@ action_set.edit = function(prompt_bufnr, command)
184184
end
185185

186186
if win_id ~= 0 and a.nvim_get_current_win() ~= win_id then
187-
vim.api.nvim_set_current_win(win_id)
187+
a.nvim_set_current_win(win_id)
188188
end
189189

190190
if entry_bufnr then
@@ -195,7 +195,7 @@ action_set.edit = function(prompt_bufnr, command)
195195
else
196196
-- check if we didn't pick a different buffer
197197
-- prevents restarting lsp server
198-
if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then
198+
if a.nvim_buf_get_name(0) ~= filename or command ~= "edit" then
199199
filename = Path:new(filename):normalize(vim.uv.cwd())
200200
pcall(vim.cmd, string.format("%s %s", command, vim.fn.fnameescape(filename)))
201201
end
@@ -208,7 +208,7 @@ action_set.edit = function(prompt_bufnr, command)
208208
end)
209209
end
210210

211-
local pos = vim.api.nvim_win_get_cursor(0)
211+
local pos = a.nvim_win_get_cursor(0)
212212
if col == nil then
213213
if row == pos[1] then
214214
col = pos[2] + 1
@@ -220,7 +220,7 @@ action_set.edit = function(prompt_bufnr, command)
220220
end
221221

222222
if row and col then
223-
if vim.api.nvim_buf_get_name(0) == filename then
223+
if a.nvim_buf_get_name(0) == filename then
224224
vim.cmd [[normal! m']]
225225
end
226226
local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, { row, col })
@@ -243,7 +243,7 @@ local __scroll_previewer = function(prompt_bufnr)
243243
return
244244
end
245245

246-
local default_speed = vim.api.nvim_win_get_height(preview_winid) / 2
246+
local default_speed = a.nvim_win_get_height(preview_winid) / 2
247247
local speed = status.picker.layout_config.scroll_speed or default_speed
248248
return previewer, speed
249249
end
@@ -282,12 +282,12 @@ end
282282
-- Valid directions include: "1", "-1"
283283
action_set.scroll_results = function(prompt_bufnr, direction)
284284
local status = state.get_status(prompt_bufnr)
285-
local default_speed = vim.api.nvim_win_get_height(status.layout.results.winid) / 2
285+
local default_speed = a.nvim_win_get_height(status.layout.results.winid) / 2
286286
local speed = status.picker.layout_config.scroll_speed or default_speed
287287

288288
local input = direction > 0 and [[]] or [[]]
289289

290-
vim.api.nvim_win_call(status.layout.results.winid, function()
290+
a.nvim_win_call(status.layout.results.winid, function()
291291
vim.cmd([[normal! ]] .. math.floor(speed) .. input)
292292
end)
293293

@@ -302,12 +302,12 @@ end
302302
-- Valid directions include: "1", "-1"
303303
action_set.scroll_horizontal_results = function(prompt_bufnr, direction)
304304
local status = state.get_status(prompt_bufnr)
305-
local default_speed = vim.api.nvim_win_get_height(status.results_win) / 2
305+
local default_speed = a.nvim_win_get_height(status.results_win) / 2
306306
local speed = status.picker.layout_config.scroll_speed or default_speed
307307

308308
local input = direction > 0 and [[zl]] or [[zh]]
309309

310-
vim.api.nvim_win_call(status.results_win, function()
310+
a.nvim_win_call(status.results_win, function()
311311
vim.cmd([[normal! ]] .. math.floor(speed) .. input)
312312
end)
313313
end

lua/telescope/builtin/__files.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local a = vim.api
2+
13
local action_state = require "telescope.actions.state"
24
local action_set = require "telescope.actions.set"
35
local actions = require "telescope.actions"
@@ -66,14 +68,14 @@ local get_open_filelist = function(grep_open_files, cwd)
6668
return false
6769
end
6870
return true
69-
end, vim.api.nvim_list_bufs())
71+
end, a.nvim_list_bufs())
7072
if not next(bufnrs) then
7173
return
7274
end
7375

7476
local filelist = {}
7577
for _, bufnr in ipairs(bufnrs) do
76-
local file = vim.api.nvim_buf_get_name(bufnr)
78+
local file = a.nvim_buf_get_name(bufnr)
7779
table.insert(filelist, Path:new(file):make_relative(cwd))
7880
end
7981
return filelist
@@ -470,10 +472,10 @@ end
470472

471473
files.current_buffer_fuzzy_find = function(opts)
472474
-- All actions are on the current buffer
473-
local filename = vim.api.nvim_buf_get_name(opts.bufnr)
475+
local filename = a.nvim_buf_get_name(opts.bufnr)
474476
local filetype = vim.bo[opts.bufnr].filetype
475477

476-
local lines = vim.api.nvim_buf_get_lines(opts.bufnr, 0, -1, false)
478+
local lines = a.nvim_buf_get_lines(opts.bufnr, 0, -1, false)
477479
local lines_with_numbers = {}
478480

479481
for lnum, line in ipairs(lines) do
@@ -569,7 +571,7 @@ files.current_buffer_fuzzy_find = function(opts)
569571
actions.close(prompt_bufnr)
570572
vim.schedule(function()
571573
vim.cmd "normal! m'"
572-
vim.api.nvim_win_set_cursor(0, { selection.lnum, first_col })
574+
a.nvim_win_set_cursor(0, { selection.lnum, first_col })
573575
end)
574576
end)
575577

@@ -619,7 +621,7 @@ files.tags = function(opts)
619621
vim.fn.search(scode)
620622
vim.cmd "norm! zz"
621623
else
622-
vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 })
624+
a.nvim_win_set_cursor(0, { selection.lnum, 0 })
623625
end
624626
end,
625627
}

lua/telescope/builtin/__git.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local a = vim.api
2+
13
local actions = require "telescope.actions"
24
local action_state = require "telescope.actions.state"
35
local finders = require "telescope.finders"
@@ -111,8 +113,8 @@ git.stash = function(opts)
111113
end
112114

113115
local get_current_buf_line = function(winnr)
114-
local lnum = vim.api.nvim_win_get_cursor(winnr)[1]
115-
return vim.trim(vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(winnr), lnum - 1, lnum, false)[1])
116+
local lnum = a.nvim_win_get_cursor(winnr)[1]
117+
return vim.trim(a.nvim_buf_get_lines(a.nvim_win_get_buf(winnr), lnum - 1, lnum, false)[1])
116118
end
117119

118120
local bcommits_picker = function(opts, title, finder)
@@ -136,9 +138,9 @@ local bcommits_picker = function(opts, title, finder)
136138
local value = selection.value .. ":" .. transfrom_file()
137139
local content = utils.get_os_command_output({ "git", "--no-pager", "show", value }, opts.cwd)
138140

139-
local bufnr = vim.api.nvim_create_buf(false, true)
140-
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, content)
141-
vim.api.nvim_buf_set_name(bufnr, "Original")
141+
local bufnr = a.nvim_create_buf(false, true)
142+
a.nvim_buf_set_lines(bufnr, 0, -1, false, content)
143+
a.nvim_buf_set_name(bufnr, "Original")
142144
return bufnr
143145
end
144146

@@ -151,12 +153,12 @@ local bcommits_picker = function(opts, title, finder)
151153
vim.bo.filetype = ft
152154
vim.cmd "diffthis"
153155

154-
vim.api.nvim_create_autocmd("WinClosed", {
156+
a.nvim_create_autocmd("WinClosed", {
155157
buffer = bufnr,
156158
nested = true,
157159
once = true,
158160
callback = function()
159-
vim.api.nvim_buf_delete(bufnr, { force = true })
161+
a.nvim_buf_delete(bufnr, { force = true })
160162
end,
161163
})
162164
end
@@ -186,7 +188,7 @@ end
186188

187189
git.bcommits = function(opts)
188190
opts.current_line = (opts.current_file == nil) and get_current_buf_line(opts.winnr) or nil
189-
opts.current_file = vim.F.if_nil(opts.current_file, vim.api.nvim_buf_get_name(opts.bufnr))
191+
opts.current_file = vim.F.if_nil(opts.current_file, a.nvim_buf_get_name(opts.bufnr))
190192
opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_commits(opts))
191193
opts.git_command =
192194
vim.F.if_nil(opts.git_command, git_command({ "log", "--pretty=oneline", "--abbrev-commit", "--follow" }, opts))
@@ -204,7 +206,7 @@ end
204206

205207
git.bcommits_range = function(opts)
206208
opts.current_line = (opts.current_file == nil) and get_current_buf_line(opts.winnr) or nil
207-
opts.current_file = vim.F.if_nil(opts.current_file, vim.api.nvim_buf_get_name(opts.bufnr))
209+
opts.current_file = vim.F.if_nil(opts.current_file, a.nvim_buf_get_name(opts.bufnr))
208210
opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_commits(opts))
209211
opts.git_command = vim.F.if_nil(
210212
opts.git_command,

0 commit comments

Comments
 (0)