diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-01-04 19:35:28 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-01-04 19:35:28 +0800 |
commit | 87834511ff48f68a1e972c8e8ec78c8d2dcf71e7 (patch) | |
tree | 3e8c29923720a2a23b4da59e146e29f11bff3118 /script | |
parent | a694873f15841e7138228b92f1fb28d8f4756900 (diff) | |
download | lua-language-server-87834511ff48f68a1e972c8e8ec78c8d2dcf71e7.zip |
update
Diffstat (limited to 'script')
-rw-r--r-- | script/client.lua | 64 | ||||
-rw-r--r-- | script/config/loader.lua | 37 | ||||
-rw-r--r-- | script/core/command/autoRequire.lua | 5 | ||||
-rw-r--r-- | script/library.lua | 20 | ||||
-rw-r--r-- | script/provider/capability.lua | 1 | ||||
-rw-r--r-- | script/provider/diagnostic.lua | 9 | ||||
-rw-r--r-- | script/provider/provider.lua | 33 | ||||
-rw-r--r-- | script/utility.lua | 8 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 18 |
9 files changed, 123 insertions, 72 deletions
diff --git a/script/client.lua b/script/client.lua index a0935df6..3522f631 100644 --- a/script/client.lua +++ b/script/client.lua @@ -200,35 +200,49 @@ end ---@field uri? uri ---@param cfg table +---@param uri uri ---@param changes config.change[] -local function applyConfig(cfg, changes) +---@return boolean +local function applyConfig(cfg, uri, changes) + local ws = require 'workspace' + local scp = ws.getScope(uri) + local ok = false for _, change in ipairs(changes) do - cfg[change.key] = config.getRaw(change.uri, change.key) + if scp:isChildUri(change.uri) + or scp:isLinkedUri(change.uri) then + cfg[change.key] = config.getRaw(change.uri, change.key) + ok = true + end end + return ok end -local function tryModifySpecifiedConfig(finalChanges) +local function tryModifySpecifiedConfig(uri, finalChanges) if #finalChanges == 0 then return false end local workspace = require 'workspace' local loader = require 'config.loader' - if loader.lastLocalType ~= 'json' then + local scp = workspace.getScope(uri) + if scp:get('lastLocalType') ~= 'json' then + return false + end + local suc = applyConfig(scp:get('lastLocalConfig'), uri, finalChanges) + if not suc then return false end - applyConfig(loader.lastLocalConfig, finalChanges) - local path = workspace.getAbsolutePath(CONFIGPATH) - util.saveFile(path, json.beautify(loader.lastLocalConfig, { indent = ' ' })) + local path = workspace.getAbsolutePath(uri, CONFIGPATH) + util.saveFile(path, json.beautify(scp:get('lastLocalConfig'), { indent = ' ' })) return true end -local function tryModifyRC(finalChanges, create) +local function tryModifyRC(uri, finalChanges, create) if #finalChanges == 0 then return false end local workspace = require 'workspace' local loader = require 'config.loader' - local path = workspace.getAbsolutePath '.luarc.json' + local path = workspace.getAbsolutePath(uri, '.luarc.json') if not path then return false end @@ -236,10 +250,14 @@ local function tryModifyRC(finalChanges, create) if not buf and not create then return false end - local rc = loader.lastRCConfig or { + local scp = workspace.getScope(uri) + local rc = scp:get('lastRCConfig') or { ['$schema'] = lang.id == 'zh-cn' and [[https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema-zh-cn.json]] or [[https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json]] } - applyConfig(rc, finalChanges) + local suc = applyConfig(rc, uri, finalChanges) + if not suc then + return false + end util.saveFile(path, json.beautify(rc, { indent = ' ' })) return true end @@ -310,17 +328,25 @@ function m.setConfig(changes, onlyMemory) return end xpcall(function () - tryModifyClientGlobal(finalChanges) - if tryModifySpecifiedConfig(finalChanges) then + local ws = require 'workspace' + if #ws.folders == 0 then + tryModifyClient(finalChanges) return end - if tryModifyRC(finalChanges) then - return - end - if tryModifyClient(finalChanges) then - return + tryModifyClientGlobal(finalChanges) + for _, scp in ipairs(ws.folders) do + if tryModifySpecifiedConfig(scp.uri, finalChanges) then + goto CONTINUE + end + if tryModifyRC(scp.uri, finalChanges, false) then + goto CONTINUE + end + if tryModifyClient(finalChanges) then + goto CONTINUE + end + tryModifyRC(scp.uri, finalChanges, true) + ::CONTINUE:: end - tryModifyRC(finalChanges, true) end, log.error) end diff --git a/script/config/loader.lua b/script/config/loader.lua index 58ddec27..b2cf6fd3 100644 --- a/script/config/loader.lua +++ b/script/config/loader.lua @@ -1,5 +1,3 @@ -local fs = require 'bee.filesystem' -local fsu = require 'fs-utility' local json = require 'json' local proto = require 'proto' local lang = require 'language' @@ -14,40 +12,43 @@ local function errorMessage(msg) log.error(msg) end +---@class config.loader local m = {} function m.loadRCConfig(uri, filename) + local scp = workspace.getScope(uri) local path = workspace.getAbsolutePath(uri, filename) if not path then - m.lastRCConfig = nil + scp:set('lastRCConfig', nil) return nil end local buf = util.loadFile(path) if not buf then - m.lastRCConfig = nil + scp:set('lastRCConfig', nil) return nil end local suc, res = pcall(json.decode, buf) if not suc then errorMessage(lang.script('CONFIG_LOAD_ERROR', res)) - return m.lastRCConfig + return scp:get('lastRCConfig') end - m.lastRCConfig = res + scp:set('lastRCConfig', res) return res end -function m.loadLocalConfig(filename) - local path = workspace.getAbsolutePath(filename) +function m.loadLocalConfig(uri, filename) + local scp = workspace.getScope(uri) + local path = workspace.getAbsolutePath(uri, filename) if not path then - m.lastLocalConfig = nil - m.lastLocalType = nil + scp:set('lastLocalConfig', nil) + scp:set('lastLocalType', nil) return nil end local buf = util.loadFile(path) if not buf then errorMessage(lang.script('CONFIG_LOAD_FAILED', path)) - m.lastLocalConfig = nil - m.lastLocalType = nil + scp:set('lastLocalConfig', nil) + scp:set('lastLocalType', nil) return nil end local firstChar = buf:match '%S' @@ -55,10 +56,10 @@ function m.loadLocalConfig(filename) local suc, res = pcall(json.decode, buf) if not suc then errorMessage(lang.script('CONFIG_LOAD_ERROR', res)) - return m.lastLocalConfig + return scp:get('lastLocalConfig') end - m.lastLocalConfig = res - m.lastLocalType = 'json' + scp:set('lastLocalConfig', res) + scp:set('lastLocalType', 'json') return res else local suc, res = pcall(function () @@ -66,10 +67,10 @@ function m.loadLocalConfig(filename) end) if not suc then errorMessage(lang.script('CONFIG_LOAD_ERROR', res)) - return m.lastLocalConfig + scp:set('lastLocalConfig', res) end - m.lastLocalConfig = res - m.lastLocalType = 'lua' + scp:set('lastLocalConfig', res) + scp:set('lastLocalType', 'lua') return res end end diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua index 2f4dfa21..c0deecfc 100644 --- a/script/core/command/autoRequire.lua +++ b/script/core/command/autoRequire.lua @@ -64,7 +64,7 @@ local function findInsertRow(uri) end ---@async -local function askAutoRequire(visiblePaths) +local function askAutoRequire(uri, visiblePaths) local selects = {} local nameMap = {} for _, visible in ipairs(visiblePaths) do @@ -91,6 +91,7 @@ local function askAutoRequire(visiblePaths) key = 'Lua.completion.autoRequire', action = 'set', value = false, + uri = uri, } } return @@ -145,7 +146,7 @@ return function (data) return #a.expect < #b.expect end) - local result = askAutoRequire(visiblePaths) + local result = askAutoRequire(uri, visiblePaths) if not result then return end diff --git a/script/library.lua b/script/library.lua index baa336f9..3fa202b6 100644 --- a/script/library.lua +++ b/script/library.lua @@ -319,7 +319,7 @@ local function load3rdConfig(uri) return configs end -local function apply3rd(cfg, onlyMemory) +local function apply3rd(uri, cfg, onlyMemory) local changes = {} if cfg.configs then for _, change in ipairs(cfg.configs) do @@ -332,6 +332,7 @@ local function apply3rd(cfg, onlyMemory) key = 'Lua.runtime.plugin', action = 'set', value = ('%s/plugin.lua'):format(cfg.dirname), + uri = uri, } end @@ -339,6 +340,7 @@ local function apply3rd(cfg, onlyMemory) key = 'Lua.workspace.library', action = 'add', value = ('%s/library'):format(cfg.dirname), + uri = uri, } client.setConfig(changes, onlyMemory) @@ -346,7 +348,7 @@ end local hasAsked ---@async -local function askFor3rd(cfg) +local function askFor3rd(uri, cfg) hasAsked = true local yes1 = lang.script.WINDOW_APPLY_WHIT_SETTING local yes2 = lang.script.WINDOW_APPLY_WHITOUT_SETTING @@ -359,21 +361,23 @@ local function askFor3rd(cfg) return nil end if result == yes1 then - apply3rd(cfg, false) + apply3rd(uri, cfg, false) client.setConfig({ { key = 'Lua.workspace.checkThirdParty', action = 'set', value = false, + uri = uri, }, }, false) elseif result == yes2 then - apply3rd(cfg, true) + apply3rd(uri, cfg, true) client.setConfig({ { key = 'Lua.workspace.checkThirdParty', action = 'set', value = false, + uri = uri, }, }, true) end @@ -396,7 +400,7 @@ local function wholeMatch(a, b) return true end -local function check3rdByWords(text, configs) +local function check3rdByWords(uri, text, configs) if hasAsked then return end @@ -406,7 +410,7 @@ local function check3rdByWords(text, configs) for _, word in ipairs(cfg.words) do await.delay() if wholeMatch(text, word) then - askFor3rd(cfg) + askFor3rd(uri, cfg) return end end @@ -429,7 +433,7 @@ local function check3rdByFileName(uri, configs) for _, filename in ipairs(cfg.files) do await.delay() if wholeMatch(path, filename) then - askFor3rd(cfg) + askFor3rd(uri, cfg) return end end @@ -466,7 +470,7 @@ local function check3rd(uri) if files.isLua(uri) then local text = files.getText(uri) if text then - check3rdByWords(text, thirdConfigs) + check3rdByWords(uri, text, thirdConfigs) end end check3rdByFileName(uri, thirdConfigs) diff --git a/script/provider/capability.lua b/script/provider/capability.lua index b712defc..8d75d7fe 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -81,6 +81,7 @@ function m.getIniter() }, executeCommandProvider = { commands = { + -- TODO: 不再需要 get_id 'lua.removeSpace:' .. sp:get_id(), 'lua.solve:' .. sp:get_id(), 'lua.jsonToLua:' .. sp:get_id(), diff --git a/script/provider/diagnostic.lua b/script/provider/diagnostic.lua index a9132e70..34ac1e17 100644 --- a/script/provider/diagnostic.lua +++ b/script/provider/diagnostic.lua @@ -283,7 +283,7 @@ function m.refresh(uri) end ---@async -local function askForDisable() +local function askForDisable(uri) if m.dontAskedForDisable then return end @@ -315,6 +315,7 @@ local function askForDisable() key = 'Lua.diagnostics.workspaceDelay', action = 'set', value = delay * 1000, + uri = uri, } } elseif item.title == lang.script.WINDOW_DISABLE_DIAGNOSTIC then @@ -323,6 +324,7 @@ local function askForDisable() key = 'Lua.diagnostics.workspaceDelay', action = 'set', value = -1, + uri = uri, } } end @@ -351,7 +353,10 @@ function m.diagnosticsScope(uri, force) bar:onCancel(function () log.debug('Cancel workspace diagnostics') cancelled = true - await.call(askForDisable) + ---@async + await.call(function () + askForDisable(uri) + end) end) local uris = files.getAllUris() for i, uri in ipairs(uris) do diff --git a/script/provider/provider.lua b/script/provider/provider.lua index baf7e705..77c45778 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -19,13 +19,13 @@ local json = require 'json' local scope = require 'workspace.scope' ---@async -local function updateConfig() - local specified = cfgLoader.loadLocalConfig(CONFIGPATH) +local function updateConfig(uri) + local specified = cfgLoader.loadLocalConfig(uri, CONFIGPATH) if specified then log.debug('Load config from specified', CONFIGPATH) log.debug(util.dump(specified)) -- watch directory - filewatch.watch(workspace.getAbsolutePath(CONFIGPATH):gsub('[^/\\]+$', '')) + filewatch.watch(workspace.getAbsolutePath(uri, CONFIGPATH):gsub('[^/\\]+$', '')) config.update(scope.override, specified, json.null) end @@ -66,13 +66,22 @@ function m.register(method) end filewatch.event(function (changes) ---@async - local configPath = CONFIGPATH and workspace.getAbsolutePath(CONFIGPATH) - local rcPath = workspace.getAbsolutePath('.luarc.json') for _, change in ipairs(changes) do - if change.path == configPath - or change.path == rcPath then - updateConfig() - return + if (CONFIGPATH and util.stringEndWith(change.path, CONFIGPATH)) then + for _, scp in ipairs(workspace.folders) do + local configPath = workspace.getAbsolutePath(scp.uri, CONFIGPATH) + if change.path == configPath then + updateConfig(scp.uri) + end + end + end + if util.stringEndWith(change.path, '.luarc.json') then + for _, scp in ipairs(workspace.folders) do + local rcPath = workspace.getAbsolutePath(scp.uri, '.luarc.json') + if change.path == rcPath then + updateConfig(scp.uri) + end + end end end end) @@ -903,7 +912,9 @@ m.register '$/status/click' { end if result == titleDiagnostic then local diagnostic = require 'provider.diagnostic' - diagnostic.diagnosticsAll(true) + for _, scp in ipairs(workspace.folders) do + diagnostic.diagnosticsScope(scp.uri, true) + end end end } @@ -1043,7 +1054,7 @@ end) m.register '$/status/refresh' { refreshStatusBar } files.watch(function (ev, uri) - if not workspace.isReady() then + if not workspace.isReady(uri) then return end if ev == 'update' diff --git a/script/utility.lua b/script/utility.lua index f06dd21e..004e8066 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -708,4 +708,12 @@ function m.getUpvalue(f, name) return nil, false end +function m.stringStartWith(str, head) + return str:sub(1, #head) == head +end + +function m.stringEndWith(str, tail) + return str:sub(-#tail) == tail +end + return m diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 2ae5f322..ebffee9d 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -130,7 +130,7 @@ function m.getNativeMatcher(scp) end end for path in pairs(config.get(scp.uri, 'Lua.workspace.library')) do - path = m.getAbsolutePath(path) + path = m.getAbsolutePath(scp.uri, path) if path then log.info('Ignore by library:', path) pattern[#pattern+1] = path @@ -159,7 +159,7 @@ function m.getLibraryMatchers(scp) local librarys = {} for path in pairs(config.get(scp.uri, 'Lua.workspace.library')) do - path = m.getAbsolutePath(path) + path = m.getAbsolutePath(scp.uri, path) if path then librarys[m.normalize(path)] = true end @@ -335,21 +335,16 @@ function m.normalize(path) end ---@return string -function m.getAbsolutePath(folderUriOrPath, path) +function m.getAbsolutePath(folderUri, path) if not path or path == '' then return nil end path = m.normalize(path) if fs.path(path):is_relative() then - if not folderUriOrPath then + if not folderUri then return nil end - local folderPath - if folderUriOrPath:sub(1, 5) == 'file:' then - folderPath = furi.decode(folderUriOrPath) - else - folderPath = folderUriOrPath - end + local folderPath = furi.decode(folderUri) path = m.normalize(folderPath .. '/' .. path) end return path @@ -506,11 +501,10 @@ config.watch(function (uri, key, value, oldValue) end) fw.event(function (changes) ---@async - -- TODO - m.awaitReady() for _, change in ipairs(changes) do local path = change.path local uri = furi.encode(path) + m.awaitReady(uri) if change.type == 'create' then log.debug('FileChangeType.Created', uri) m.awaitLoadFile(uri) |