Skip to content
Snippets Groups Projects
Commit 8634a138 authored by 最萌小汐's avatar 最萌小汐
Browse files

加载插件

parent b13de09a
Branches
No related tags found
No related merge requests found
function OnRequirePath(literal, raw)
if type(literal) == 'string' then
return literal
end
return raw:match '[^%.]+$'
end
......@@ -18,10 +18,12 @@
"/server/locale/",
"/server/libs/",
"/server/src/3rd",
"/server/meta/"
"/server/meta/",
".vscode"
],
"Lua.workspace.library": {
"E:/Github/test" : true,
"server/src/meta" : true
}
},
"Lua.plugin.enable": true
}
......@@ -44,6 +44,7 @@ local function tryDebugger()
local addr = "127.0.0.1:" .. port
local dbg = loadfile(entry)('windows', root)
dbg:start(addr)
dbg:wait()
log.debug('Debugger startup, listen port:', port)
log.debug('Debugger args:', addr, root, path, cpath)
end
......
......@@ -15,7 +15,7 @@ log.info('Lua Lsp startup, root: ', ROOT)
log.debug('ROOT:', ROOT:string())
ac = {}
--xpcall(dofile, log.debug, rootPath .. 'debugger.lua')
xpcall(dofile, log.debug, rootPath .. '/debugger.lua')
require 'utility'
local service = require 'service'
local session = service()
......
......@@ -125,7 +125,7 @@ local ConfigTemplate = {
},
plugin = {
enable = {false, Boolean},
path = {'${workspaceRoot}/.vscode/lua/', String},
path = {'.vscode/lua-plugin/*.lua', String},
},
}
......
local fs = require 'bee.filesystem'
local rpc = require 'rpc'
local config = require 'config'
local glob = require 'glob'
local platform = require 'bee.platform'
local sandbox = require 'sandbox'
local Plugins
local function showError(msg)
local traceback = log.error(msg)
rpc:notify('window/showMessage', {
type = 3,
message = traceback,
})
return traceback
end
local function showWarn(msg)
log.warn(msg)
rpc:notify('window/showMessage', {
type = 3,
message = msg,
})
return msg
end
local function scan(path, callback)
if fs.is_directory(path) then
for p in path:list_directory() do
scan(p, callback)
end
else
callback(path)
end
end
local function loadPluginFrom(path, root)
log.info('Load plugin from:', path:string())
local env = setmetatable({}, { __index = _G })
sandbox(path:filename():string(), root:string(), io.open, package.loaded, env)
Plugins[#Plugins+1] = env
end
local function load(workspace)
Plugins = {}
if not config.config.plugin.enable then
return
end
local suc, path = xpcall(fs.path, showWarn, config.config.plugin.path)
if not suc then
return
end
local pluginPath
if workspace then
pluginPath = fs.absolute(workspace.root / path)
else
pluginPath = fs.absolute(path)
end
if not fs.is_directory(pluginPath) then
pluginPath = pluginPath:parent_path()
end
local pattern = {config.config.plugin.path}
local options = {
ignoreCase = platform.OS == 'Windows'
}
local parser = glob.glob(pattern, options)
scan(pluginPath:parent_path(), function (filePath)
if parser(filePath:string()) then
loadPluginFrom(filePath, pluginPath)
end
end)
end
return {
load = load,
}
......@@ -67,8 +67,10 @@ local function recieve(self, proto)
BUF[id] = nil
if data.timeout and os.clock() > data.timeout then
log.warn('Recieve timeout: ', table.dump(proto))
if data.callback then
local info = debug.getinfo(data.callback, 'S')
log.warn('Call back info: ', info.source, info.linedefined)
end
return
end
if proto.error then
......
local function standard(loaded)
local r = {}
local function standard(loaded, env)
local r = env or {}
for _, s in ipairs {
--'package',
'coroutine',
......@@ -45,9 +45,9 @@ local function standard(loaded)
return r
end
local function sandbox_env(loadlua, openfile, loaded)
local function sandbox_env(loadlua, openfile, loaded, env)
local _LOADED = loaded or {}
local _E = standard(_LOADED)
local _E = standard(_LOADED, env)
local _PRELOAD = {}
_E.io = {
......@@ -112,7 +112,9 @@ local function sandbox_env(loadlua, openfile, loaded)
return p
end
local init, extra = require_load(name)
if debug.getupvalue(init, 1) == '_ENV' then
debug.setupvalue(init, 1, _E)
end
local res = init(name, extra)
if res ~= nil then
_LOADED[name] = res
......@@ -139,7 +141,7 @@ local function sandbox_env(loadlua, openfile, loaded)
return _E
end
return function(name, root, io_open, loaded)
return function(name, root, io_open, loaded, env)
if not root:sub(-1):find '[/\\]' then
root = root .. '/'
end
......@@ -158,6 +160,8 @@ return function(name, root, io_open, loaded)
if not init then
return
end
debug.setupvalue(init, 1, sandbox_env(loadlua, openfile, loaded))
if debug.getupvalue(init, 1) == '_ENV' then
debug.setupvalue(init, 1, sandbox_env(loadlua, openfile, loaded, env))
end
return init()
end
......@@ -20,6 +20,7 @@ local task = require 'task'
local files = require 'files'
local uric = require 'uri'
local capability = require 'capability'
local plugin = require 'plugin'
local ErrorCodes = {
-- Defined by JSON RPC
......@@ -763,7 +764,11 @@ function mt:onUpdateConfig(updated, other)
else
capability.completion.disable()
end
if not table.equal(oldConfig.plugin, newConfig.plugin) then
plugin.load(self.workspace)
end
if not table.equal(oldConfig.workspace, newConfig.workspace)
or not table.equal(oldConfig.plugin, newConfig.plugin)
or not table.equal(oldOther.associations, newOther.associations)
or not table.equal(oldOther.exclude, newOther.exclude)
then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment