summaryrefslogtreecommitdiff
path: root/script/plugin.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/plugin.lua')
-rw-r--r--script/plugin.lua32
1 files changed, 28 insertions, 4 deletions
diff --git a/script/plugin.lua b/script/plugin.lua
index 145abe74..870b68b6 100644
--- a/script/plugin.lua
+++ b/script/plugin.lua
@@ -4,6 +4,8 @@ local client = require 'client'
local lang = require 'language'
local await = require 'await'
local scope = require 'workspace.scope'
+local ws = require 'workspace'
+local fs = require 'bee.filesystem'
---@class plugin
local m = {}
@@ -69,20 +71,35 @@ local function checkTrustLoad(scp)
return true
end
----@param scp scope
-function m.init(scp)
+---@param uri uri
+local function initPlugin(uri)
await.call(function () ---@async
- local ws = require 'workspace'
+ local scp = scope.getScope(uri)
local interface = {}
scp:set('pluginInterface', interface)
+ if not scp.uri then
+ return
+ end
+
local pluginPath = ws.getAbsolutePath(scp.uri, config.get(scp.uri, 'Lua.runtime.plugin'))
log.info('plugin path:', pluginPath)
if not pluginPath then
return
end
+
+ --Adding the plugins path to package.path allows for requires in files
+ --to find files relative to itself.
+ local oldPath = package.path
+ local path = fs.path(pluginPath):parent_path() / '?.lua'
+ if not package.path:find(path:string(), 1, true) then
+ package.path = package.path .. ';' .. path:string()
+ end
+
local pluginLua = util.loadFile(pluginPath)
if not pluginLua then
+ log.warn('plugin not found:', pluginPath)
+ package.path = oldPath
return
end
@@ -98,7 +115,8 @@ function m.init(scp)
if not client.isVSCode() and not checkTrustLoad(scp) then
return
end
- local suc, err = xpcall(f, log.error, f)
+ local pluginArgs = config.get(scp.uri, 'Lua.runtime.pluginArgs')
+ local suc, err = xpcall(f, log.error, f, uri, pluginArgs)
if not suc then
m.showError(scp, err)
return
@@ -108,4 +126,10 @@ function m.init(scp)
end)
end
+ws.watch(function (ev, uri)
+ if ev == 'startReload' then
+ initPlugin(uri)
+ end
+end)
+
return m