From 2b2eea0beef2ce5e9705cdda7f467fd96f8de5bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 22 Nov 2021 14:33:42 +0800 Subject: merge settings --- script/provider/provider.lua | 51 ++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'script/provider') diff --git a/script/provider/provider.lua b/script/provider/provider.lua index d732f3c2..2bda860c 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -15,33 +15,48 @@ local tm = require 'text-merger' 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 ---@async local function updateConfig() - local new + local merged = {} + if CONFIGPATH then - new = cfgLoader.loadLocalConfig(CONFIGPATH) - config.setSource 'path' + local cfg = cfgLoader.loadLocalConfig(CONFIGPATH) log.debug('load config from local', CONFIGPATH) -- watch directory filewatch.watch(workspace.getAbsolutePath(CONFIGPATH):gsub('[^/\\]+$', '')) - else - new = cfgLoader.loadRCConfig('.luarc.json') - if new then - config.setSource 'luarc' - log.debug('load config from luarc') - else - new = cfgLoader.loadClientConfig() - config.setSource 'client' - log.debug('load config from client') - end + mergeConfig(merged, cfg) end - if not new then - log.warn('load config failed!') - return + + local rc = cfgLoader.loadRCConfig('.luarc.json') + if rc then + log.debug('load config from luarc') + mergeConfig(merged, rc) + end + + local clientConfig = cfgLoader.loadClientConfig() + if clientConfig then + log.debug('load config from client') + mergeConfig(merged, clientConfig) end - config.update(new) - log.debug('loaded config dump:', util.dump(new)) + + for k, v in pairs(merged) do + if v == json.null then + merged[k] = nil + end + end + + config.update(merged) + log.debug('loaded config dump:', util.dump(merged)) end ---@class provider -- cgit v1.2.3