-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_manager.lua
More file actions
145 lines (110 loc) · 2.97 KB
/
plugin_manager.lua
File metadata and controls
145 lines (110 loc) · 2.97 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- Imports
local core = require "core"
local common = require "core.common"
local command = require "core.command"
local plugin_manager = {}
-- Change default enabled here
plugin_manager.enabled = {
"autocomplete",
"autoreload",
"exec",
"macro",
"projectsearch",
"quote",
"reflow",
"tabularize",
"treeview",
"trimwhitespace"
}
--------------------------------------------------------------------------
local function _to_table(val)
if type(val) == "string" then
return true, { val }
elseif type(val) == "table" then
return true, val
end
return false
end
function plugin_manager.disable(val)
local ok
ok, val = _to_table(val)
if ok then
for _, plugin in ipairs(val) do
table.remove(plugin_manager.enabled, plugin)
end
return true
end
return false
end
function plugin_manager.enable(val)
local ok
ok, val = _to_table(val)
if ok then
for _, plugin in ipairs(val) do
table.insert(plugin_manager.enabled, plugin)
end
return true
end
return false
end
--------------------------------------------------------------------------
local function _load_plugin(plugin_name)
local plugin_path = "plugins." .. plugin_name
local success = core.try(require, plugin_path)
if not success then
return false
end
core.log_quiet("plugin_manager: Loaded plugin %q", plugin_path)
return true
end
function plugin_manager.load_plugins()
local no_errors = true
for _, plugin_name in ipairs(plugin_manager.enabled) do
if not _load_plugin(plugin_name) then
no_errors = false
end
end
return no_errors
end
--------------------------------------------------------------------------
local function _get_all_plugins()
local plugin_files = system.list_dir(EXEDIR .. "/data/plugins")
local plugin_names = {}
for _, filename in ipairs(plugin_files) do
table.insert(plugin_names, (filename:gsub(".lua$", "")))
end
return plugin_names
end
command.add(nil, {
["plugin-manager:inject-plugin"] = function()
local plugin_names = _get_all_plugins()
core.command_view:enter("Select Plugin", function(_, plugin_name)
if plugin_name then
if _load_plugin(plugin_name.text) then
core.log("Succesfully injected plugin: %q!", plugin_name.text)
else
core.log("Failed to inject plugin: %q", plugin_name)
end
end
end,
function(text)
return common.fuzzy_match(plugin_names, text)
end)
end,
["plugin-manager:inject-all-plugins"] = function()
local no_errors = true
local plugin_names = _get_all_plugins()
for _, plugin in ipairs(plugin_names) do
if not _load_plugin(plugin) then
core.log_quiet("Failed to inject %q during inject-all-plugins", plugin)
no_errors = false
end
end
if no_errors then
core.log("Sucessfully injected all plugins!")
else
core.log("There was an error injecting the plugins. Check the log for details.")
end
end
})
return plugin_manager