diff options
-rw-r--r-- | server/meta/Lua 5.4/basic.lua | 7 | ||||
-rw-r--r-- | server/src/core/completion.lua | 14 | ||||
-rw-r--r-- | server/src/vm/source.lua | 1 | ||||
-rw-r--r-- | server/src/vm/special.lua | 32 |
4 files changed, 49 insertions, 5 deletions
diff --git a/server/meta/Lua 5.4/basic.lua b/server/meta/Lua 5.4/basic.lua index 06a5c810..54f74a5c 100644 --- a/server/meta/Lua 5.4/basic.lua +++ b/server/meta/Lua 5.4/basic.lua @@ -134,5 +134,12 @@ end function rawequal(v1, v2) end +--- 加载一个模块,返回该模块的返回值(`nil`时为`true`)。 +---@param modname string {special = 'require:1'} +---@return any +---@return {name = 'loaderdata'} +function require(modname) +end + --- 当前解释器版本号。 _VERSION = 'Lua 5.4' diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 6ab3c565..45e1ab4f 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -599,10 +599,7 @@ local function buildTextEdit(start, finish, str, quo) } end -local function searchInRequire(vm, select, source, callback) - if select ~= 1 then - return - end +local function searchInRequire(vm, source, callback) if not vm.lsp or not vm.lsp.workspace then return end @@ -659,7 +656,9 @@ local function searchEnumAsLib(vm, source, word, callback, pos, args, lib) -- 搜索特殊函数 if lib.special == 'require' then - searchInRequire(vm, select, source, callback) + if select == 1 then + searchInRequire(vm, source, callback) + end end end @@ -704,6 +703,11 @@ local function searchEnumAsEmmyParams(vm, source, word, callback, pos, args, fun end end end) + + local option = param:getOption() + if option and option.special == 'require:1' then + searchInRequire(vm, source, callback) + end end local function getSelect(args, pos) diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua index 3884af20..7a10a38e 100644 --- a/server/src/vm/source.lua +++ b/server/src/vm/source.lua @@ -1,5 +1,6 @@ local listMgr = require 'vm.list' +---@class source local mt = {} mt.__index = mt mt.type = 'source' diff --git a/server/src/vm/special.lua b/server/src/vm/special.lua index f8a03a34..bf02f620 100644 --- a/server/src/vm/special.lua +++ b/server/src/vm/special.lua @@ -1,5 +1,7 @@ local mt = require 'vm.manager' local multi = require 'vm.multi' +local library = require 'core.library' +local libraryBuilder = require 'vm.library' ---@param func emmyFunction ---@param values table @@ -24,6 +26,8 @@ function mt:checkEmmyParam(func, values, index, special, source) self:callEmmyLoadFile(func, values, index) elseif special == 'pcall:1' then self:callEmmyPCall(func, values, index, source) + elseif special == 'require:1' then + self:callEmmyRequire(func, values, index, source) end end @@ -68,6 +72,7 @@ end ---@param func emmyFunction ---@param values table ---@param index integer +---@param source source function mt:callEmmyPCall(func, values, index, source) local funcValue = values[index] if not funcValue then @@ -91,3 +96,30 @@ function mt:callEmmyPCall(func, values, index, source) end) end end + +---@param func emmyFunction +---@param values table +---@param index integer +function mt:callEmmyRequire(func, values, index) + if not values[index] then + values[index] = self:createValue('any', self:getDefaultSource()) + end + local str = values[index]:getLiteral() + if type(str) ~= 'string' then + return + end + local lib = library.library[str] + if lib then + local value = libraryBuilder.value(lib) + value:markGlobal() + func:setReturn(1, value) + return + else + local requireValue = self:tryRequireOne(values[1], 'require') + if not requireValue then + requireValue = self:createValue('any', self:getDefaultSource()) + requireValue:set('cross file', true) + end + func:setReturn(1, requireValue) + end +end |