diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-11-22 16:54:05 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-11-22 16:54:05 +0800 |
commit | 1b3602f1e936c0e4964f87975bbc3aae5a1cbe09 (patch) | |
tree | 9ba224a4bc97b4a9ad699286271217095808fb82 | |
parent | 85732594a954187774e44dd914ca5566c0e526b8 (diff) | |
download | lua-language-server-1b3602f1e936c0e4964f87975bbc3aae5a1cbe09.zip |
supports quickfix for `.luarc.json`
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/client.lua | 115 | ||||
-rw-r--r-- | script/config/config.lua | 15 | ||||
-rw-r--r-- | script/config/loader.lua | 4 |
4 files changed, 102 insertions, 33 deletions
diff --git a/changelog.md b/changelog.md index 55a7dfda..0ee1eddd 100644 --- a/changelog.md +++ b/changelog.md @@ -15,6 +15,7 @@ + `discard-returns`: check whether the return value is discarded. * `NEW` locale `pt-br`, thanks [Jeferson Ferreira](https://github.com/jefersonf) * `NEW` supports [utf-8-offsets](https://clangd.llvm.org/extensions#utf-8-offsets) +* `NEW` supports quickfix for `.luarc.json` * `CHG` `LuaDoc`: + `---@class` can be re-declared + supports unicode diff --git a/script/client.lua b/script/client.lua index 4d39cd0d..0409b742 100644 --- a/script/client.lua +++ b/script/client.lua @@ -5,6 +5,7 @@ local proto = require 'proto' local define = require 'proto.define' local config = require 'config' local converter = require 'proto.converter' +local json = require 'json-beautify' local m = {} @@ -181,6 +182,86 @@ end ---@field isGlobal? boolean ---@field uri? uri +---@param cfg table +---@param changes config.change[] +local function applyConfig(cfg, changes) + for _, change in ipairs(changes) do + cfg[change.key] = config.getRaw(change.key) + end +end + +local function tryModifySpecifiedConfig(finalChanges) + if #finalChanges == 0 then + return false + end + local workspace = require 'workspace' + local loader = require 'config.loader' + if loader.lastLocalType ~= 'json' then + return false + end + applyConfig(loader.lastLocalConfig, finalChanges) + local path = workspace.getAbsolutePath(CONFIGPATH) + util.saveFile(path, json.beautify(loader.lastLocalConfig, { indent = ' ' })) + return true +end + +local function tryModifyRC(finalChanges, create) + if #finalChanges == 0 then + return + end + local workspace = require 'workspace' + local loader = require 'config.loader' + local path = workspace.getAbsolutePath '.luarc.json' + if not path then + return false + end + local buf = util.loadFile(path) + if not buf and not create then + return false + end + local rc = loader.lastRCConfig or {} + applyConfig(rc, finalChanges) + util.saveFile(path, json.beautify(rc, { indent = ' ' })) + return true +end + +local function tryModifyClient(finalChanges) + if #finalChanges == 0 then + return + end + if not m.getOption 'changeConfiguration' then + return false + end + proto.notify('$/command', { + command = 'lua.config', + data = finalChanges, + }) + return true +end + +---@param finalChanges config.change[] +local function tryModifyClientGlobal(finalChanges) + if #finalChanges == 0 then + return + end + if not m.getOption 'changeConfiguration' then + return + end + local changes = {} + for i = #finalChanges, 1, -1 do + local change = finalChanges[i] + if change.isGlobal then + changes[#changes+1] = change + finalChanges[i] = finalChanges[#finalChanges] + finalChanges[#finalChanges] = nil + end + end + proto.notify('$/command', { + command = 'lua.config', + data = changes, + }) +end + ---@param changes config.change[] ---@param onlyMemory boolean function m.setConfig(changes, onlyMemory) @@ -210,31 +291,19 @@ function m.setConfig(changes, onlyMemory) if #finalChanges == 0 then return end - if m.getOption 'changeConfiguration' - and config.getSource() == 'client' then - proto.notify('$/command', { - command = 'lua.config', - data = finalChanges, - }) - else - local messages = {} - if not m.getOption 'changeConfiguration' then - messages[1] = lang.script('WINDOW_CLIENT_NOT_SUPPORT_CONFIG') - elseif config.getSource() ~= 'client' then - messages[1] = lang.script('WINDOW_LCONFIG_NOT_SUPPORT_CONFIG') + xpcall(function () + tryModifyClientGlobal(finalChanges) + if tryModifySpecifiedConfig(finalChanges) then + return end - for _, change in ipairs(finalChanges) do - if change.action == 'add' then - messages[#messages+1] = lang.script('WINDOW_MANUAL_CONFIG_ADD', change) - elseif change.action == 'set' then - messages[#messages+1] = lang.script('WINDOW_MANUAL_CONFIG_SET', change) - elseif change.action == 'prop' then - messages[#messages+1] = lang.script('WINDOW_MANUAL_CONFIG_PROP', change) - end + if tryModifyRC(finalChanges) then + return end - local message = table.concat(messages, '\n') - m.showMessage('Info', message) - end + if tryModifyClient(finalChanges) then + return + end + tryModifyRC(finalChanges, true) + end, log.error) end ---@alias textEditor {start: integer, finish: integer, text: string} diff --git a/script/config/config.lua b/script/config/config.lua index 0e18428f..2788cda7 100644 --- a/script/config/config.lua +++ b/script/config/config.lua @@ -216,6 +216,7 @@ local Template = { local config = {} local rawConfig = {} +---@class config.api local m = {} m.watchList = {} @@ -292,6 +293,10 @@ function m.get(key) return config[key] end +function m.getRaw(key) + return rawConfig[key] +end + function m.dump() local dump = {} @@ -359,16 +364,6 @@ function m.event(key, value, oldValue) } end ----@param source config.source -function m.setSource(source) - m._source = source -end - ----@return config.source -function m.getSource() - return m._source -end - function m.init() if m.inited then return diff --git a/script/config/loader.lua b/script/config/loader.lua index 85193eb6..daa370cd 100644 --- a/script/config/loader.lua +++ b/script/config/loader.lua @@ -40,12 +40,14 @@ function m.loadLocalConfig(filename) local path = workspace.getAbsolutePath(filename) if not path then m.lastLocalConfig = nil + m.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 return nil end local firstChar = buf:match '%S' @@ -56,6 +58,7 @@ function m.loadLocalConfig(filename) return m.lastLocalConfig end m.lastLocalConfig = res + m.lastLocalType = 'json' return res else local suc, res = pcall(function () @@ -66,6 +69,7 @@ function m.loadLocalConfig(filename) return m.lastLocalConfig end m.lastLocalConfig = res + m.lastLocalType = 'lua' return res end end |