diff options
author | sumneko <sumneko@hotmail.com> | 2019-04-16 17:12:40 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-04-16 17:12:40 +0800 |
commit | ca69ac28173674eb80f822161ed6379e6070d272 (patch) | |
tree | 2b4136ddf7be3e1ddb682d2860f2df460b2f8c7c | |
parent | 4cb9c12ca12e3ce6e254897cc1a72f651bd9fdf8 (diff) | |
download | lua-language-server-ca69ac28173674eb80f822161ed6379e6070d272.zip |
根据文件关联来搜索lua文件
-rw-r--r-- | server/src/async/scanfiles.lua | 14 | ||||
-rw-r--r-- | server/src/config.lua | 50 | ||||
-rw-r--r-- | server/src/method/initialized.lua | 11 | ||||
-rw-r--r-- | server/src/method/workspace/didChangeConfiguration.lua | 8 | ||||
-rw-r--r-- | server/src/service.lua | 4 | ||||
-rw-r--r-- | server/src/workspace.lua | 26 |
6 files changed, 91 insertions, 22 deletions
diff --git a/server/src/async/scanfiles.lua b/server/src/async/scanfiles.lua index 13a8f1c8..eb3073df 100644 --- a/server/src/async/scanfiles.lua +++ b/server/src/async/scanfiles.lua @@ -47,14 +47,12 @@ for path in scan(fs.path(args.root), filter) do OUT:push 'stop' return end - if path:extension():string() == '.lua' then - local buf = io.load(path) - if buf then - OUT:push('file', { - path = fs.absolute(path):string(), - buf = buf, - }) - end + local buf = io.load(path) + if buf then + OUT:push('file', { + path = fs.absolute(path):string(), + buf = buf, + }) end end diff --git a/server/src/config.lua b/server/src/config.lua index 125ac655..5272a964 100644 --- a/server/src/config.lua +++ b/server/src/config.lua @@ -57,7 +57,27 @@ local function Array(checker) end end -local Template = { +local function Hash(keyChecker, valueChecker) + return function (tbl) + if type(tbl) ~= 'table' then + return false + end + local t = {} + for k, v in pairs(tbl) do + local ok1, key = keyChecker(k) + local ok2, value = valueChecker(v) + if ok1 and ok2 then + t[key] = value + end + end + if not next(t) then + return false + end + return true, t + end +end + +local ConfigTemplate = { runtime = { version = {'Lua 5.3', String}, library = {{}, Str2Hash ';'}, @@ -80,33 +100,50 @@ local Template = { } } -local Config +local OtherTemplate = { + associations = {{}, Hash(String, String)}, +} + +local Config, Other local function init() if Config then return end + Config = {} - for c, t in pairs(Template) do + for c, t in pairs(ConfigTemplate) do Config[c] = {} for k, info in pairs(t) do Config[c][k] = info[1] end end + + Other = {} + for k, v in pairs(OtherTemplate) do + Other[k] = v + end end -local function setConfig(self, config) +local function setConfig(self, config, other) pcall(function () for c, t in pairs(config) do for k, v in pairs(t) do - local info = Template[c][k] + local info = ConfigTemplate[c][k] local suc, v = info[2](v) if suc then Config[c][k] = v end end end - log.debug('Config update: ', table.dump(Config)) + for k, v in pairs(other) do + local info = OtherTemplate[k] + local suc, v = info[2](v) + if suc then + Other[k] = v + end + end + log.debug('Config update: ', table.dump(Config), table.dump(Other)) end) end @@ -115,4 +152,5 @@ init() return { setConfig = setConfig, config = Config, + other = Other, } diff --git a/server/src/method/initialized.lua b/server/src/method/initialized.lua index 521c1cc6..ab05c2b3 100644 --- a/server/src/method/initialized.lua +++ b/server/src/method/initialized.lua @@ -40,16 +40,23 @@ return function (lsp) if folders then firstScope = folders[1] end + local uri = firstScope and firstScope.uri -- 请求配置 rpc:request('workspace/configuration', { items = { { - scopeUri = firstScope and firstScope.uri, + scopeUri = uri, section = 'Lua', }, + { + scopeUri = uri, + section = 'files.associations', + } }, }, function (configs) - lsp:onUpdateConfig(configs[1]) + lsp:onUpdateConfig(configs[1], { + associations = configs[2], + }) initAfterConfig(lsp, firstScope) end) end) diff --git a/server/src/method/workspace/didChangeConfiguration.lua b/server/src/method/workspace/didChangeConfiguration.lua index 732d28dd..d5008954 100644 --- a/server/src/method/workspace/didChangeConfiguration.lua +++ b/server/src/method/workspace/didChangeConfiguration.lua @@ -9,8 +9,14 @@ return function (lsp) scopeUri = uri, section = 'Lua', }, + { + scopeUri = uri, + section = 'files.associations', + } }, }, function (configs) - lsp:onUpdateConfig(configs[1]) + lsp:onUpdateConfig(configs[1], { + associations = configs[2], + }) end) end diff --git a/server/src/service.lua b/server/src/service.lua index d35256bf..b7290329 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -672,9 +672,9 @@ function mt:restartDueToMemoryLeak() end) end -function mt:onUpdateConfig(updated) +function mt:onUpdateConfig(updated, other) local oldConfig = table.deepCopy(config.config) - config:setConfig(updated) + config:setConfig(updated, other) local newConfig = config.config if not table.equal(oldConfig.runtime, newConfig.runtime) then local library = require 'core.library' diff --git a/server/src/workspace.lua b/server/src/workspace.lua index 9012f721..37c242d6 100644 --- a/server/src/workspace.lua +++ b/server/src/workspace.lua @@ -145,6 +145,9 @@ function mt:scanFiles() elseif mode == 'file' then local file = ... local path = fs.path(file.path) + if not self:isLuaFile(path) then + return + end local name = getFileName(path) local uri = self:uriEncode(path) self.files[name] = uri @@ -175,9 +178,26 @@ function mt:isComplete() return not not self._complete end +function mt:isLuaFile(path) + local ext = path:extension():string() + for k, v in pairs(config.other.associations) do + if fileNameEq(ext, k:match('[^%*]+$')) then + if v == 'lua' then + return true + else + return false + end + end + end + if fileNameEq(ext, '.lua') then + return true + end + return false +end + function mt:addFile(uri) - if uri:sub(-4) == '.lua' then - local path = self:uriDecode(uri) + local path = self:uriDecode(uri) + if self:isLuaFile(path) then local name = getFileName(path) self.files[name] = uri self.lsp:readText(uri, path) @@ -402,7 +422,7 @@ function mt:loadPath(baseUri, str) if not ok then return nil end - str = getFileName(relative:string()) + str = getFileName(relative) if self.loaded[str] then return self.loaded[str] end |