-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcode_runner.lua
More file actions
112 lines (99 loc) · 3.33 KB
/
Copy pathcode_runner.lua
File metadata and controls
112 lines (99 loc) · 3.33 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
111
112
local o = require("code_runner.options")
local M = {}
local function setup(opt)
-- Store user options. JSON config (filetype_path/project_path) is loaded
-- lazily on first use, not here, to keep setup off the startup hot path.
o.set(opt)
end
local function open_json(json_path)
vim.cmd("tabnew " .. json_path)
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
M.open_filetype_suported = function()
open_json(o.get().filetype_path)
end
M.open_project_manager = function()
open_json(o.get().project_path)
end
-- The mode list is static; building it pulls in the command chain, so we cache
-- it and only build it the first time :RunFile/:RunProject completion is used.
local modes_cache = nil
local function get_modes()
if not modes_cache then
modes_cache = vim.tbl_keys(require("code_runner.commands").get_modes())
end
return modes_cache
end
M.setup = function(user_options)
setup(user_options or {})
-- Simple commands lazily pull in the command module only when invoked.
vim.api.nvim_create_user_command("RunClose", function()
require("code_runner.commands").run_close()
end, { nargs = 0 })
vim.api.nvim_create_user_command("CRFiletype", M.open_filetype_suported, { nargs = 0 })
vim.api.nvim_create_user_command("CRProjects", M.open_project_manager, { nargs = 0 })
-- Commands with autocomplete.
-- Format: CommandName = { command_fn_name, options_provider }
local completion_cmds = {
RunCode = {
"run_code",
function()
return vim.tbl_keys(o.get().filetype)
end,
},
RunFile = { "run_filetype", get_modes },
RunProject = { "run_project", get_modes },
}
for cmd, cmo in pairs(completion_cmds) do
vim.api.nvim_create_user_command(cmd, function(opts)
require("code_runner.commands")[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
-- Public API as thin lazy wrappers, so requiring this module never pulls in
-- the heavier command/filetype/project/utils chain on its own.
M.run_code = function(...)
return require("code_runner.commands").run_code(...)
end
M.run_from_fn = function(...)
return require("code_runner.commands").run_from_fn(...)
end
M.run_filetype = function(...)
return require("code_runner.commands").run_filetype(...)
end
M.run_project = function(...)
return require("code_runner.commands").run_project(...)
end
M.run_close = function(...)
return require("code_runner.commands").run_close(...)
end
M.get_filetype_command = function(...)
return require("code_runner.commands").get_filetype_command(...)
end
M.get_project_command = function(...)
return require("code_runner.commands").get_project_command(...)
end
if o.get_raw().hot_reload then
local id = require("code_runner.hooks.autocmd").create_on_write(function(...)
require("code_runner.commands").run_code()
end)
require("code_runner.hooks.utils").create_stop_hot_reload(id)
end
end
return M