diff options
author | sumneko <sumneko@hotmail.com> | 2021-11-27 02:26:55 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2021-11-27 02:26:55 +0800 |
commit | 08b963cfe8ffea36529b58d56708492aad04d254 (patch) | |
tree | 6312934d7e62a851660620791ef73aeb7a8cb273 | |
parent | ed27fb8fac021f77b4824f1a0068a97fe801c7bf (diff) | |
download | lua-language-server-08b963cfe8ffea36529b58d56708492aad04d254.zip |
stash
-rw-r--r-- | script/config/config.lua | 107 | ||||
-rw-r--r-- | script/config/loader.lua | 2 | ||||
-rw-r--r-- | script/provider/provider.lua | 49 | ||||
-rw-r--r-- | script/workspace/scope.lua | 21 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 8 | ||||
-rw-r--r-- | test.lua | 1 |
6 files changed, 72 insertions, 116 deletions
diff --git a/script/config/config.lua b/script/config/config.lua index 3c9a6bba..8dbb4d9b 100644 --- a/script/config/config.lua +++ b/script/config/config.lua @@ -1,6 +1,7 @@ local util = require 'utility' local define = require 'proto.define' local timer = require 'timer' +local scope = require 'workspace.scope' ---@alias config.source '"client"'|'"path"'|'"local"' @@ -214,34 +215,40 @@ local Template = { ['editor.acceptSuggestionOnEnter'] = Type.String >> 'on', } -local config = {} -local rawConfig = {} - ---@class config.api local m = {} m.watchList = {} m.NULL = {} -local function update(uri, key, value, raw) - local oldValue = config[key] - config[key] = value - rawConfig[key] = raw - m.event(key, value, oldValue) +---@param scp scope +---@param key string +---@param nowValue any +---@param rawValue any +local function update(scp, key, nowValue, rawValue) + local now = scp:get 'config.now' + local raw = scp:get 'config.raw' + + now[key] = nowValue + raw[key] = rawValue end -function m.set(uri, key, value) +---@param scp scope +---@param key string +---@param value any +function m.setByScope(scp, key, value) local unit = Template[key] if not unit then return false end - if util.equal(rawConfig[key], value) then + local raw = scp:get 'config.raw' + if util.equal(raw[key], value) then return false end if unit:checker(value) then - update(uri, key, unit:loader(value), value) + update(scp, key, unit:loader(value), value) else - update(uri, key, unit.default, unit.default) + update(scp, key, unit.default, unit.default) end return true end @@ -300,47 +307,6 @@ function m.getRaw(key) return rawConfig[key] end -local function convertValue(v) - if v == m.NULL then - return nil - end - return v -end - ----@param uri uri ----@param key string -function m.get2(uri, key) - if configs.specified[key] ~= nil then - return convertValue(configs.specified[key]) - end - if uri then - for _, folder in ipairs(configs.folder) do - if uri:sub(1, #folder.uri) == folder.uri - and folder.config[key] ~= nil then - return convertValue(folder.config[key]) - end - end - end - return convertValue(configs.global[key]) -end - ----@param uri uri ----@param key string -function m.getRaw2(uri, key) - if rawConfigs.specified[key] ~= nil then - return convertValue(rawConfigs.specified[key]) - end - if uri then - for _, folder in ipairs(rawConfigs.folder) do - if uri:sub(1, #folder.uri) == folder.uri - and folder.config[key] ~= nil then - return convertValue(folder.config[key]) - end - end - end - return convertValue(rawConfigs.global[key]) -end - function m.dump() local dump = {} @@ -363,20 +329,28 @@ function m.dump() return dump end ----@param scope config.scope ----@param uri uri ----@param new table -function m.update(scope, uri, new) +---@param scp scope +---@param new table +---@param null any +function m.update(scp, new, null) + local oldConfig = scp:get 'config.now' + + scp:set('config.now', {}) + scp:set('config.raw', {}) + local function expand(t, left) for key, value in pairs(t) do local fullKey = key if left then fullKey = left .. '.' .. key end + if value == null then + value = m.NULL + end if Template[fullKey] then - m.set(uri, fullKey, value) + m.setByScope(scp, fullKey, value) elseif Template['Lua.' .. fullKey] then - m.set(uri, 'Lua.' .. fullKey, value) + m.setByScope(scp, 'Lua.' .. fullKey, value) elseif type(value) == 'table' then expand(value, fullKey) end @@ -384,6 +358,11 @@ function m.update(scope, uri, new) end expand(new) + + -- compare then fire event + if oldConfig then + + end end ---@param callback fun(key: string, value: any, oldValue: any) @@ -411,14 +390,4 @@ function m.event(key, value, oldValue) } end -function m.init() - if m.inited then - return - end - m.inited = true - for key, unit in pairs(Template) do - m.set(nil, key, unit.default) - end -end - return m diff --git a/script/config/loader.lua b/script/config/loader.lua index 69daf417..58ddec27 100644 --- a/script/config/loader.lua +++ b/script/config/loader.lua @@ -75,6 +75,8 @@ function m.loadLocalConfig(filename) end ---@async +---@param uri? uri +---@return table function m.loadClientConfig(uri) local configs = proto.awaitRequest('workspace/configuration', { items = { diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 7c453315..8d945696 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -16,49 +16,43 @@ local cfgLoader = require 'config.loader' local converter = require 'proto.converter' local filewatch = require 'filewatch' local json = require 'json' - -local function mergeConfig(a, b) - for k, v in pairs(b) do - if a[k] == nil then - a[k] = v - end - end -end +local scope = require 'workspace.scope' ---@async local function updateConfig() - local cfg = cfgLoader.loadLocalConfig(CONFIGPATH) - if cfg then - log.debug('load config from local', CONFIGPATH) + local specified = cfgLoader.loadLocalConfig(CONFIGPATH) + if specified then + log.debug('Load config from specified', CONFIGPATH) + log.debug(util.dump(specified)) -- watch directory filewatch.watch(workspace.getAbsolutePath(CONFIGPATH):gsub('[^/\\]+$', '')) - config.update('specified', nil, cfg) + config.update(scope.override, specified, json.null) + else + config.update(scope.override, {}, json.null) end - for _, folder in ipairs(workspace.folders) do + for _, folder in ipairs(scope.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(uri) if clientConfig then - log.debug('load config from client') - mergeConfig(folderConfig, clientConfig) + log.debug('Load config from client', uri) + log.debug(util.dump(clientConfig)) + config.update(folder, clientConfig, json.null) end - end - for k, v in pairs(baseConfig) do - if v == json.null then - baseConfig[k] = nil + local rc = cfgLoader.loadRCConfig(uri, '.luarc.json') + if rc then + log.debug('Load config from luarc.json', uri) + log.debug(util.dump(rc)) + config.update(folder, rc, json.null) end end - config.update('global', workspace.rootUri,baseConfig) - log.debug('loaded config dump:', util.dump(baseConfig)) + local global = cfgLoader.loadClientConfig() + log.debug('Load config from client', 'fallback') + log.debug(util.dump(global)) + config.update(scope.fallback, global, json.null) end ---@class provider @@ -88,7 +82,6 @@ end) m.register 'initialize' { function (params) client.init(params) - config.init() if params.rootUri then workspace.initRoot(params.rootUri) diff --git a/script/workspace/scope.lua b/script/workspace/scope.lua index 16c9500d..8bf91c71 100644 --- a/script/workspace/scope.lua +++ b/script/workspace/scope.lua @@ -37,6 +37,7 @@ end ---@param v any function mt:set(k, v) self._data[k] = v + return v end ---@param k string @@ -45,16 +46,6 @@ 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) @@ -67,6 +58,14 @@ local function createScope(scopeType) return scope end +---@class scope.manager +local m = {} + +---@type scope[] +m.folders = {} +m.override = createScope 'override' +m.fallback = createScope 'fallback' + ---@param uri uri ---@return scope function m.createFolder(uri) @@ -115,6 +114,4 @@ function m.getLinkedScope(uri) end end -m.fallback = createScope 'fallback' - return m diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index dbe2f346..a1f57a7d 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -15,6 +15,7 @@ local client = require 'client' local plugin = require 'plugin' local util = require 'utility' local fw = require 'filewatch' +local scope = require 'workspace.scope' ---@class workspace local m = {} @@ -29,8 +30,6 @@ m.requireCache = {} m.cache = {} m.watchers = {} m.matchOption = {} ----@type {uri: uri, path: string}[] -m.folders = {} function m.initRoot(uri) m.rootUri = uri @@ -47,11 +46,8 @@ end function m.create(uri) log.info('Workspace create: ', uri) local path = m.normalize(furi.decode(uri)) - m.folders[#m.folders+1] = { - uri = uri, - path = path, - } fw.watch(path) + scope.createFolder(uri) end local globInteferFace = { @@ -82,7 +82,6 @@ end local function main() require 'utility'.enableCloseFunction() - require 'config'.init() require 'core.searcher'.debugMode = true require 'language' 'zh-cn' require 'library'.init() |