Skip to content

Commit 86dcb1a

Browse files
committed
link server by plugin
1 parent 7fa6ee1 commit 86dcb1a

File tree

15 files changed

+73
-28
lines changed

15 files changed

+73
-28
lines changed

script/plugin.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local await = require 'await'
66
local scope = require 'workspace.scope'
77
local ws = require 'workspace'
88
local fs = require 'bee.filesystem'
9+
require 'plugins'
910

1011
---@class plugin
1112
local m = {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
local c99 = {}
2929

3030
local re = require("parser.relabel")
31-
local typed = require("LuaJIT.c-parser.typed")
31+
local typed = require("plugins.ffi.c-parser.typed")
3232

3333
local defs = {}
3434

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
local cdefines = {}
33

4-
local c99 = require("LuaJIT.c-parser.c99")
5-
local cpp = require("LuaJIT.c-parser.cpp")
6-
local typed = require("LuaJIT.c-parser.typed")
4+
local c99 = require("plugins.ffi.c-parser.c99")
5+
local cpp = require("plugins.ffi.c-parser.cpp")
6+
local typed = require("plugins.ffi.c-parser.typed")
77

88
local function add_type(lst, name, typ)
99
lst[name] = typ
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
local cdriver = {}
22

3-
local cpp = require("LuaJIT.c-parser.cpp")
4-
local c99 = require("LuaJIT.c-parser.c99")
5-
local ctypes = require("LuaJIT.c-parser.ctypes")
6-
local cdefines = require("LuaJIT.c-parser.cdefines")
3+
local cpp = require("plugins.ffi.c-parser.cpp")
4+
local c99 = require("plugins.ffi.c-parser.c99")
5+
local ctypes = require("plugins.ffi.c-parser.ctypes")
6+
local cdefines = require("plugins.ffi.c-parser.cdefines")
77

88
function cdriver.process_file(filename)
99
local ctx, err = cpp.parse_file(filename)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
local cpp = {}
33

4-
local typed = require("LuaJIT.c-parser.typed")
5-
local c99 = require("LuaJIT.c-parser.c99")
4+
local typed = require("plugins.ffi.c-parser.typed")
5+
local c99 = require("plugins.ffi.c-parser.c99")
66

77
local SEP = package.config:sub(1,1)
88

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local ctypes = { TESTMODE = false }
22

33
local inspect = require("inspect")
44
local utility = require 'utility'
5-
local typed = require("LuaJIT.c-parser.typed")
5+
local typed = require("plugins.ffi.c-parser.typed")
66

77
local equal_declarations
88

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
local searchCode = require 'LuaJIT.searchCode'
2-
local cdefRerence = require 'LuaJIT.cdefRerence'
3-
local cdriver = require 'LuaJIT.c-parser.cdriver'
1+
local searchCode = require 'plugins.ffi.searchCode'
2+
local cdefRerence = require 'plugins.ffi.cdefRerence'
3+
local cdriver = require 'plugins.ffi.c-parser.cdriver'
44
local util = require 'utility'
55
local SDBMHash = require 'SDBMHash'
6+
local ws = require 'workspace'
7+
local files = require 'files'
8+
local await = require 'await'
9+
local config = require 'config'
10+
local fs = require 'bee.filesystem'
11+
local scope = require 'workspace.scope'
612

713
local namespace <const> = 'ffi.namespace*.'
814

@@ -323,9 +329,16 @@ function m.compileCodes(codes)
323329
return lines
324330
end
325331

326-
function m.initBuilder()
327-
local config = require 'config'
328-
local fs = require 'bee.filesystem'
332+
local function createDir(uri)
333+
local dir = scope.getScope(uri).uri or 'default'
334+
local fileDir = fs.path(METAPATH) / ('%08x'):format(SDBMHash():hash(dir))
335+
fs.create_directories(fileDir)
336+
return fileDir
337+
end
338+
339+
local builder
340+
function m.initBuilder(fileDir)
341+
fileDir = fileDir or createDir()
329342
---@async
330343
return function (uri)
331344
local refs = cdefRerence()
@@ -342,14 +355,44 @@ function m.initBuilder()
342355
if not texts then
343356
return
344357
end
345-
346358
local hash = ('%08x'):format(SDBMHash():hash(uri))
347359
local encoding = config.get(nil, 'Lua.runtime.fileEncoding')
348-
local filePath = METAPATH .. '/ffi/' .. table.concat({ hash, encoding }, '_')
360+
local filePath = fileDir / table.concat({ hash, encoding }, '_')
349361

350-
fs.create_directories(fs.path(filePath):parent_path())
351-
util.saveFile(filePath .. '.d.lua', table.concat(texts, '\n'))
362+
util.saveFile(tostring(filePath) .. '.d.lua', table.concat(texts, '\n'))
352363
end
353364
end
354365

366+
files.watch(function (ev, uri)
367+
if ev == 'compiler' or ev == 'update' then
368+
if builder then
369+
await.call(function () ---@async
370+
builder(uri)
371+
end)
372+
end
373+
end
374+
end)
375+
376+
ws.watch(function (ev, uri)
377+
if ev == 'startReload' then
378+
if config.get(uri, 'Lua.runtime.version') ~= 'LuaJIT' then
379+
return
380+
end
381+
await.call(function () ---@async
382+
ws.awaitReady(uri)
383+
local fileDir = createDir(uri)
384+
builder = m.initBuilder(fileDir)
385+
local client = require 'client'
386+
client.setConfig {
387+
{
388+
key = 'Lua.workspace.library',
389+
action = 'add',
390+
value = tostring(fileDir),
391+
uri = uri,
392+
}
393+
}
394+
end)
395+
end
396+
end)
397+
355398
return m

0 commit comments

Comments
 (0)