summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-07-08 10:03:41 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-07-08 10:03:41 +0800
commit5781f2a991da34c47b0b65b3d94b75f96a92c4ca (patch)
tree196a04e1c5fdc689edac13fdb123e1ec8144956d /server/src
parentaed744d56a684a2b014e6fcc88ecbdde9833c929 (diff)
downloadlua-language-server-5781f2a991da34c47b0b65b3d94b75f96a92c4ca.zip
emmy支持 require 的特殊处理
Diffstat (limited to 'server/src')
-rw-r--r--server/src/core/completion.lua14
-rw-r--r--server/src/vm/source.lua1
-rw-r--r--server/src/vm/special.lua32
3 files changed, 42 insertions, 5 deletions
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