-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode-refactor.lua
More file actions
82 lines (71 loc) · 2 KB
/
Copy pathcode-refactor.lua
File metadata and controls
82 lines (71 loc) · 2 KB
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
local utils = require("utils")
local M = {}
function M.setup(cfg)
M.__conf = vim.tbl_deep_extend("keep", cfg or {}, require("code-refactor.config"))
-- Create command to show code actions.
vim.api.nvim_create_user_command("CodeActions", function(opts)
if opts.fargs[1] == "all" then
M.show_code_actions()
return
end
local language = M.get_language_in_current_buffer()
if not language then
print("No actions for current filetype")
return
end
local has_action, action =
pcall(require, "code-refactor.actions." .. language .. "." .. opts.fargs[1])
if has_action then
action.run()
end
end, {
nargs = '+',
complete = function(arglead)
return vim.tbl_filter(function(arg)
return arg:match("^" .. arglead)
end, vim.tbl_extend("keep", { "all" }, M.get_actions_under_cursor().list))
end,
})
end
M.get_language_in_current_buffer = function()
for language, value in pairs(M.__conf.available_actions) do
for _, filetype in ipairs(value.file_types) do
if filetype == vim.bo.filetype then
return language
end
end
end
return nil
end
M.get_actions_under_cursor = function()
local language = M.get_language_in_current_buffer()
if not language then
return { list = {} }
end
return {
type = language,
list = utils.table_keys(
utils.filter_table(require("code-refactor.actions." .. language), function(item)
return item.is_available()
end)
),
}
end
function M.show_code_actions()
local actions = M.get_actions_under_cursor()
if next(actions.list) == nil then
print("No available code actions")
return
end
vim.ui.select(actions.list, {
prompt = "Code actions",
format_item = function(item)
return require("code-refactor.actions." .. actions.type .. "." .. item).title
end,
}, function(selected)
if selected then
require("code-refactor.actions." .. actions.type .. "." .. selected).run(M.__conf)
end
end)
end
return M