forked from CRAG666/code_runner.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_runner.lua
More file actions
110 lines (96 loc) · 3.05 KB
/
Copy pathcode_runner.lua
File metadata and controls
110 lines (96 loc) · 3.05 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
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
local commands = require("code_runner.commands")
local o = require("code_runner.options")
local function setup(opt)
-- Load json config and convert to table
local load_json_as_table = require("code_runner.load_json")
-- Convert json filetype as table lua
if vim.tbl_isempty(opt.filetype or {}) then
opt.filetype_path = opt.filetype_path or ""
if opt.filetype_path ~= "" then
local filetype = load_json_as_table(opt.filetype_path)
if not filetype then
vim.notify("Error trying to load filetypes commands", vim.log.levels.ERROR, { title = "Code Runner Error" })
end
opt.filetype = filetype or {}
end
end
-- Convert json project as table lua
if vim.tbl_isempty(opt.project or {}) then
opt.project_path = opt.project_path or ""
if opt.project_path ~= "" then
local project = load_json_as_table(opt.project_path)
if not project then
vim.notify("Error trying to load project commands", vim.log.levels.ERROR, { title = "Code Runner Error" })
end
opt.project = project or {}
end
end
-- set user options
o.set(opt)
-- Message if json file not exist
if vim.tbl_isempty(o.get().filetype) then
vim.notify(
"Not exist command for filetypes or format invalid, if use json please execute :CRFiletype or if use lua edit setup",
vim.log.levels.ERROR,
{ title = "Code Runner Error" }
)
end
end
local function open_json(json_path)
local command = "tabnew " .. json_path
vim.cmd(command)
end
local function completion(ArgLead, options)
local filterd_args = vim.tbl_filter(function(v)
return v:find(ArgLead:lower(), 1, true) == 1
end, options)
if not vim.tbl_isempty(filterd_args) then
return filterd_args
end
return options
end
local M = {}
M.open_filetype_suported = function()
open_json(o.get().filetype_path)
end
M.open_project_manager = function()
open_json(o.get().project_path)
end
M.setup = function(user_options)
setup(user_options or {})
local simple_cmds = {
RunClose = commands.run_close,
CRFiletype = M.open_filetype_suported,
CRProjects = M.open_project_manager,
}
for cmd, func in pairs(simple_cmds) do
vim.api.nvim_create_user_command(cmd, func, { nargs = 0 })
end
-- Commands with autocomplete
local modes = vim.tbl_keys(commands.modes)
-- Format:
-- CoomandName = { function, option_list }
local completion_cmds = {
RunCode = { commands.run_code, vim.tbl_keys(o.get().filetype) },
RunFile = { commands.run_filetype, modes },
RunProject = { commands.run_project, modes },
}
for cmd, cmo in pairs(completion_cmds) do
vim.api.nvim_create_user_command(cmd, function(opts)
cmo[1](unpack(opts.fargs))
end, {
nargs = "*",
complete = function(ArgLead, word, ...)
-- only complete the first argument
if #vim.split(word, "%s+") > 2 then
return
end
return completion(ArgLead, cmo[2])
end,
})
end
M.run_code = commands.run_code
M.run_filetype = commands.run_filetype
M.run_project = commands.run_project
end
return M