summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
Diffstat (limited to 'script')
-rw-r--r--script/core/command/reloadFFIMeta.lua56
-rw-r--r--script/plugins/ffi/cdefRerence.lua2
-rw-r--r--script/plugins/ffi/init.lua75
-rw-r--r--script/provider/provider.lua5
4 files changed, 72 insertions, 66 deletions
diff --git a/script/core/command/reloadFFIMeta.lua b/script/core/command/reloadFFIMeta.lua
new file mode 100644
index 00000000..d00929ad
--- /dev/null
+++ b/script/core/command/reloadFFIMeta.lua
@@ -0,0 +1,56 @@
+local config = require 'config'
+local ws = require 'workspace'
+local fs = require 'bee.filesystem'
+local scope = require 'workspace.scope'
+local SDBMHash = require 'SDBMHash'
+local searchCode = require 'plugins.ffi.searchCode'
+local cdefRerence = require 'plugins.ffi.cdefRerence'
+local ffi = require 'plugins.ffi'
+
+local function createDir(uri)
+ local dir = scope.getScope(uri).uri or 'default'
+ local fileDir = fs.path(METAPATH) / ('%08x'):format(SDBMHash():hash(dir))
+ if fs.exists(fileDir) then
+ return fileDir, true
+ end
+ fs.create_directories(fileDir)
+ return fileDir
+end
+
+---@async
+return function (uri)
+ if config.get(uri, 'Lua.runtime.version') ~= 'LuaJIT' then
+ return
+ end
+
+ ws.awaitReady(uri)
+
+ local fileDir, exists = createDir(uri)
+
+ local refs = cdefRerence()
+ if not refs or #refs == 0 then
+ return
+ end
+
+ for i, v in ipairs(refs) do
+ local target_uri = v.uri
+ local codes = searchCode(refs, target_uri)
+ if not codes then
+ return
+ end
+
+ ffi.build_single(codes, fileDir, target_uri)
+ end
+
+ if not exists then
+ local client = require 'client'
+ client.setConfig {
+ {
+ key = 'Lua.workspace.library',
+ action = 'add',
+ value = tostring(fileDir),
+ uri = uri,
+ }
+ }
+ end
+end
diff --git a/script/plugins/ffi/cdefRerence.lua b/script/plugins/ffi/cdefRerence.lua
index 819a0dd1..14643f0f 100644
--- a/script/plugins/ffi/cdefRerence.lua
+++ b/script/plugins/ffi/cdefRerence.lua
@@ -20,7 +20,7 @@ end
return function ()
local ffi_state
for uri in files.eachFile() do
- if find(uri, "/ffi.lua", 0, true) then
+ if find(uri, "ffi.lua", 0, true) and find(uri, "lua-language-server", 0, true) then
ffi_state = files.getState(uri)
break
end
diff --git a/script/plugins/ffi/init.lua b/script/plugins/ffi/init.lua
index a551973a..e79be759 100644
--- a/script/plugins/ffi/init.lua
+++ b/script/plugins/ffi/init.lua
@@ -4,9 +4,6 @@ local cdriver = require 'plugins.ffi.c-parser.cdriver'
local util = require 'plugins.ffi.c-parser.util'
local utility = require 'utility'
local SDBMHash = require 'SDBMHash'
-local ws = require 'workspace'
-local files = require 'files'
-local await = require 'await'
local config = require 'config'
local fs = require 'bee.filesystem'
local scope = require 'workspace.scope'
@@ -322,70 +319,18 @@ function m.compileCodes(codes)
return lines
end
-local function createDir(uri)
- local dir = scope.getScope(uri).uri or 'default'
- local fileDir = fs.path(METAPATH) / ('%08x'):format(SDBMHash():hash(dir))
- fs.create_directories(fileDir)
- return fileDir
-end
-
-local builder
-function m.initBuilder(fileDir)
- fileDir = fileDir or createDir()
- ---@async
- return function (uri)
- local refs = cdefRerence()
- if not refs or #refs == 0 then
- return
- end
-
- local codes = searchCode(refs, uri)
- if not codes then
- return
- end
-
- local texts = m.compileCodes(codes)
- if not texts then
- return
- end
- local hash = ('%08x'):format(SDBMHash():hash(uri))
- local encoding = config.get(nil, 'Lua.runtime.fileEncoding')
- local filePath = fileDir / table.concat({ hash, encoding }, '_')
-
- utility.saveFile(tostring(filePath) .. '.d.lua', table.concat(texts, '\n'))
+function m.build_single(codes, fileDir, uri)
+ local texts = m.compileCodes(codes)
+ if not texts then
+ return
end
-end
-files.watch(function (ev, uri)
- if ev == 'compiler' or ev == 'update' then
- if builder then
- await.call(function () ---@async
- builder(uri)
- end)
- end
- end
-end)
+ local hash = ('%08x'):format(SDBMHash():hash(uri))
+ local encoding = config.get(nil, 'Lua.runtime.fileEncoding')
+ local filePath = fileDir / table.concat({ hash, encoding }, '_')
-ws.watch(function (ev, uri)
- if ev == 'startReload' then
- if config.get(uri, 'Lua.runtime.version') ~= 'LuaJIT' then
- return
- end
- await.call(function () ---@async
- ws.awaitReady(uri)
- local fileDir = createDir(uri)
- builder = m.initBuilder(fileDir)
- local client = require 'client'
- client.setConfig {
- {
- key = 'Lua.workspace.library',
- action = 'add',
- value = tostring(fileDir),
- uri = uri,
- }
- }
- end)
- end
-end)
+ utility.saveFile(tostring(filePath) .. '.d.lua', table.concat(texts, '\n'))
+ return true
+end
return m
diff --git a/script/provider/provider.lua b/script/provider/provider.lua
index efa627c3..2ec429d0 100644
--- a/script/provider/provider.lua
+++ b/script/provider/provider.lua
@@ -982,6 +982,11 @@ m.register 'workspace/executeCommand' {
elseif command == 'lua.exportDocument' then
local core = require 'core.command.exportDocument'
core(params.arguments)
+ elseif command == 'lua.reloadFFIMeta' then
+ local core = require 'core.command.reloadFFIMeta'
+ for _, scp in ipairs(workspace.folders) do
+ core(scp.uri)
+ end
end
end
}