diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-11-26 21:05:42 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-11-26 21:05:42 +0800 |
commit | ed27fb8fac021f77b4824f1a0068a97fe801c7bf (patch) | |
tree | fa92d03f6db380d46f454d5e0d0bd62f8a3d3a76 /script | |
parent | 999316d43551dabfe8fadafa017332e840099f90 (diff) | |
download | lua-language-server-ed27fb8fac021f77b4824f1a0068a97fe801c7bf.zip |
stash
Diffstat (limited to 'script')
-rw-r--r-- | script/config/config.lua | 16 | ||||
-rw-r--r-- | script/config/loader.lua | 16 | ||||
-rw-r--r-- | script/provider/provider.lua | 26 | ||||
-rw-r--r-- | script/workspace/scope.lua | 120 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 12 |
5 files changed, 151 insertions, 39 deletions
diff --git a/script/config/config.lua b/script/config/config.lua index 614c3029..3c9a6bba 100644 --- a/script/config/config.lua +++ b/script/config/config.lua @@ -214,22 +214,6 @@ local Template = { ['editor.acceptSuggestionOnEnter'] = Type.String >> 'on', } ----@alias config.scope '"specified"'|'"folder"'|'"global"' - -local configs = { - specified = {}, - ---@type {uri: uri, config: table}[] - folder = {}, - global = {}, -} - -local rawConfigs = { - specified = {}, - ---@type {uri: uri, config: table}[] - folder = {}, - global = {}, -} - local config = {} local rawConfig = {} diff --git a/script/config/loader.lua b/script/config/loader.lua index 072a9c71..69daf417 100644 --- a/script/config/loader.lua +++ b/script/config/loader.lua @@ -16,8 +16,8 @@ end local m = {} -function m.loadRCConfig(filename) - local path = workspace.getAbsolutePath(filename) +function m.loadRCConfig(uri, filename) + local path = workspace.getAbsolutePath(uri, filename) if not path then m.lastRCConfig = nil return nil @@ -75,27 +75,27 @@ function m.loadLocalConfig(filename) end ---@async -function m.loadClientConfig() +function m.loadClientConfig(uri) local configs = proto.awaitRequest('workspace/configuration', { items = { { - scopeUri = workspace.rootUri, + scopeUri = uri, section = 'Lua', }, { - scopeUri = workspace.rootUri, + scopeUri = uri, section = 'files.associations', }, { - scopeUri = workspace.rootUri, + scopeUri = uri, section = 'files.exclude', }, { - scopeUri = workspace.rootUri, + scopeUri = uri, section = 'editor.semanticHighlighting.enabled', }, { - scopeUri = workspace.rootUri, + scopeUri = uri, section = 'editor.acceptSuggestionOnEnter', }, }, diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 44b63d00..7c453315 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -27,26 +27,28 @@ end ---@async local function updateConfig() - local baseConfig = {} - local cfg = cfgLoader.loadLocalConfig(CONFIGPATH) if cfg then log.debug('load config from local', CONFIGPATH) -- watch directory filewatch.watch(workspace.getAbsolutePath(CONFIGPATH):gsub('[^/\\]+$', '')) - mergeConfig(baseConfig, cfg) + config.update('specified', nil, cfg) end - local rc = cfgLoader.loadRCConfig('.luarc.json') - if rc then - log.debug('load config from luarc') - mergeConfig(baseConfig, rc) - end + for _, folder in ipairs(workspace.folders) do + local uri = folder.uri + local folderConfig = {} + local rc = cfgLoader.loadRCConfig(uri, '.luarc.json') + if rc then + log.debug('load config from luarc') + mergeConfig(folderConfig, rc) + end - local clientConfig = cfgLoader.loadClientConfig() - if clientConfig then - log.debug('load config from client') - mergeConfig(baseConfig, clientConfig) + local clientConfig = cfgLoader.loadClientConfig(uri) + if clientConfig then + log.debug('load config from client') + mergeConfig(folderConfig, clientConfig) + end end for k, v in pairs(baseConfig) do diff --git a/script/workspace/scope.lua b/script/workspace/scope.lua new file mode 100644 index 00000000..16c9500d --- /dev/null +++ b/script/workspace/scope.lua @@ -0,0 +1,120 @@ +---@alias scope.type '"override"'|'"folder"'|'"fallback"' + +---@class scope +---@field type scope.type +---@field uri? uri +---@field _links table<uri, boolean> +---@field _data table<string, any> +local mt = {} +mt.__index = mt + +---@param uri uri +function mt:addLink(uri) + self._links[uri] = true +end + +---@param uri uri +function mt:removeLink(uri) + self._links[uri] = nil +end + +function mt:removeAllLinks() + self._links = {} +end + +---@param uri uri +---@return boolean +function mt:isLinkedUri(uri) + for linkUri in pairs(self._links) do + if uri:sub(1, #linkUri) == linkUri then + return true + end + end + return false +end + +---@param k string +---@param v any +function mt:set(k, v) + self._data[k] = v +end + +---@param k string +---@return any +function mt:get(k) + return self._data[k] +end + +---@class scope.manager +local m = {} + +---@type scope[] +m.folders = {} +---@type scope +m.override = nil +---@type scope +m.fallback = nil + +---@param scopeType scope.type +---@return scope +local function createScope(scopeType) + local scope = setmetatable({ + type = 'folder', + _links = {}, + _data = {}, + }, mt) + + return scope +end + +---@param uri uri +---@return scope +function m.createFolder(uri) + local scope = createScope 'folder' + scope.uri = uri + + local inserted = false + for i, otherScope in ipairs(m.folders) do + if #uri > #otherScope.uri then + table.insert(m.folders, i, scope) + inserted = true + break + end + end + if not inserted then + table.insert(m.folders, scope) + end + + return scope +end + +---@param uri uri +---@return scope +function m.getFolder(uri) + for _, scope in ipairs(m.folders) do + if not uri or scope.uri:sub(1, #uri) == uri then + return scope + end + end + return nil +end + +---@param uri uri +---@return scope +function m.getLinkedScope(uri) + if m.override and m.override:isLinkedUri(uri) then + return m.override + end + for _, scope in ipairs(m.folders) do + if scope:isLinkedUri(uri) then + return scope + end + end + if m.fallback:isLinkedUri(uri) then + return m.fallback + end +end + +m.fallback = createScope 'fallback' + +return m diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 65b3b1ac..dbe2f346 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -507,16 +507,22 @@ function m.normalize(path) end ---@return string -function m.getAbsolutePath(path) +function m.getAbsolutePath(folderUriOrPath, path) if not path or path == '' then return nil end path = m.normalize(path) if fs.path(path):is_relative() then - if not m.rootPath then + if not folderUriOrPath then return nil end - path = m.normalize(m.rootPath .. '/' .. path) + local folderPath + if folderUriOrPath:sub(1, 5) == 'file:' then + folderPath = furi.decode(folderUriOrPath) + else + folderPath = folderUriOrPath + end + path = m.normalize(folderPath .. '/' .. path) end return path end |