diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-02-02 20:08:26 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-02-02 20:08:26 +0800 |
commit | ba1e8c4ce8c4d128cef76895ae7877889ad34921 (patch) | |
tree | 98395c7a0c32016704335e0f57964ccec50a9d79 | |
parent | 9b45b621342c0205a503c63cc2706c1a8cf9175c (diff) | |
download | lua-language-server-ba1e8c4ce8c4d128cef76895ae7877889ad34921.zip |
improve checking for modifying config
#1859
-rw-r--r-- | locale/en-us/script.lua | 16 | ||||
-rw-r--r-- | locale/pt-br/script.lua | 16 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 18 | ||||
-rw-r--r-- | locale/zh-tw/script.lua | 16 | ||||
-rw-r--r-- | script/client.lua | 73 |
5 files changed, 118 insertions, 21 deletions
diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index 2937d80a..f0f48a5d 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -568,6 +568,22 @@ CONFIG_TYPE_ERROR = 'The setting file must be in lua or json format: {}' CONFIG_MODIFY_FAIL_SYNTAX_ERROR = 'Failed to modify settings, there are syntax errors in the settings file: {}' +CONFIG_MODIFY_FAIL_NO_WORKSPACE = +[[ +Failed to modify settings: +* The current mode is single-file mode, server cannot create `.luarc.json` without workspace. +* The language client dose not support modifying settings from the server side. + +Please modify following settings manually: +{} +]] +CONFIG_MODIFY_FAIL = +[[ +Failed to modify settings + +Please modify following settings manually: +{} +]] PLUGIN_RUNTIME_ERROR = [[ diff --git a/locale/pt-br/script.lua b/locale/pt-br/script.lua index fd25a3d6..145a69b1 100644 --- a/locale/pt-br/script.lua +++ b/locale/pt-br/script.lua @@ -568,6 +568,22 @@ CONFIG_TYPE_ERROR = 'O arquivo de configuração deve estar no formato LUA ou JSON: {}' CONFIG_MODIFY_FAIL_SYNTAX_ERROR = -- TODO: need translate! 'Failed to modify settings, there are syntax errors in the settings file: {}' +CONFIG_MODIFY_FAIL_NO_WORKSPACE = -- TODO: need translate! +[[ +Failed to modify settings: +* The current mode is single-file mode, server cannot create `.luarc.json` without workspace. +* The language client dose not support modifying settings from the server side. + +Please modify following settings manually: +{} +]] +CONFIG_MODIFY_FAIL = -- TODO: need translate! +[[ +Failed to modify settings + +Please modify following settings manually: +{} +]] PLUGIN_RUNTIME_ERROR = [[ diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index 114546dd..4c3af355 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -538,7 +538,7 @@ WINDOW_CLIENT_NOT_SUPPORT_CONFIG = WINDOW_LCONFIG_NOT_SUPPORT_CONFIG= '暂不支持自动修改本地设置,请手动修改如下设置:' WINDOW_MANUAL_CONFIG_ADD = -'为 `{key}` 添加值 `{value:q}`;' +'为 `{key}` 添加元素 `{value:q}`;' WINDOW_MANUAL_CONFIG_SET = '将 `{key}` 的值设置为 `{value:q}`;' WINDOW_MANUAL_CONFIG_PROP = @@ -568,6 +568,22 @@ CONFIG_TYPE_ERROR = '设置文件必须是lua或json格式:{}' CONFIG_MODIFY_FAIL_SYNTAX_ERROR = '修改设置失败,设置文件中有语法错误:{}' +CONFIG_MODIFY_FAIL_NO_WORKSPACE = +[[ +修改设置失败: +* 当前模式为单文件模式,服务器只能在工作区中创建 `.luarc.json` 文件。 +* 语言客户端不支持从服务器侧修改设置。 + +请手动修改以下设置: +{} +]] +CONFIG_MODIFY_FAIL = +[[ +修改设置失败 + +请手动修改以下设置: +{} +]] PLUGIN_RUNTIME_ERROR = [[ diff --git a/locale/zh-tw/script.lua b/locale/zh-tw/script.lua index d738700e..b4e7b13d 100644 --- a/locale/zh-tw/script.lua +++ b/locale/zh-tw/script.lua @@ -568,6 +568,22 @@ CONFIG_TYPE_ERROR = '設定檔案必須是lua或json格式:{}' CONFIG_MODIFY_FAIL_SYNTAX_ERROR = -- TODO: need translate! 'Failed to modify settings, there are syntax errors in the settings file: {}' +CONFIG_MODIFY_FAIL_NO_WORKSPACE = -- TODO: need translate! +[[ +Failed to modify settings: +* The current mode is single-file mode, server cannot create `.luarc.json` without workspace. +* The language client dose not support modifying settings from the server side. + +Please modify following settings manually: +{} +]] +CONFIG_MODIFY_FAIL = -- TODO: need translate! +[[ +Failed to modify settings + +Please modify following settings manually: +{} +]] PLUGIN_RUNTIME_ERROR = [[ diff --git a/script/client.lua b/script/client.lua index e702a78b..7337238a 100644 --- a/script/client.lua +++ b/script/client.lua @@ -332,12 +332,13 @@ end local function editConfigJson(path, changes) local text = util.loadFile(path) if not text then + m.showMessage('Error', lang.script('CONFIG_LOAD_FAILED', path)) return nil end local suc, res = pcall(jsonc.decode_jsonc, text) if not suc then m.showMessage('Error', lang.script('CONFIG_MODIFY_FAIL_SYNTAX_ERROR', path .. res:match 'ERROR(.+)$')) - return text + return nil end if type(res) ~= 'table' then res = {} @@ -352,6 +353,20 @@ local function editConfigJson(path, changes) return text end +---@param changes config.change[] +---@param applied config.change[] +local function removeAppliedChanges(changes, applied) + local appliedMap = {} + for _, change in ipairs(applied) do + appliedMap[change] = true + end + for i = #changes, 1, -1 do + if appliedMap[changes[i]] then + table.remove(changes, i) + end + end +end + local function tryModifySpecifiedConfig(uri, finalChanges) if #finalChanges == 0 then return false @@ -374,6 +389,7 @@ local function tryModifySpecifiedConfig(uri, finalChanges) return false end util.saveFile(path, newJson) + removeAppliedChanges(finalChanges, validChanges) return true end @@ -398,11 +414,15 @@ local function tryModifyRC(uri, finalChanges, create) if #validChanges == 0 then return false end + if not buf then + util.saveFile(path, '') + end local newJson = editConfigJson(path, validChanges) if not newJson then return false end util.saveFile(path, newJson) + removeAppliedChanges(finalChanges, validChanges) return true end @@ -428,6 +448,7 @@ local function tryModifyClient(uri, finalChanges) command = 'lua.config', data = scpChanges, }) + removeAppliedChanges(finalChanges, scpChanges) return true end @@ -440,18 +461,32 @@ local function tryModifyClientGlobal(finalChanges) return end local changes = {} - for i = #finalChanges, 1, -1 do - local change = finalChanges[i] + for _, change in ipairs(finalChanges) do if change.global then changes[#changes+1] = change - finalChanges[i] = finalChanges[#finalChanges] - finalChanges[#finalChanges] = nil end end proto.notify('$/command', { command = 'lua.config', data = changes, }) + removeAppliedChanges(finalChanges, changes) +end + +---@param changes config.change[] +---@return string +local function buildMaunuallyMessage(changes) + local message = {} + for _, change in ipairs(changes) do + if change.action == 'add' then + message[#message+1] = '* ' .. lang.script('WINDOW_MANUAL_CONFIG_ADD', change.key, change.value) + elseif change.action == 'set' then + message[#message+1] = '* ' .. lang.script('WINDOW_MANUAL_CONFIG_SET', change.key, change.value) + elseif change.action == 'prop' then + message[#message+1] = '* ' .. lang.script('WINDOW_MANUAL_CONFIG_PROP', change.key, change.prop, change.value) + end + end + return table.concat(message, '\n') end ---@param changes config.change[] @@ -484,26 +519,24 @@ function m.setConfig(changes, onlyMemory) end xpcall(function () local ws = require 'workspace' + tryModifyClientGlobal(finalChanges) if #ws.folders == 0 then - if tryModifySpecifiedConfig(nil, finalChanges) then - return - end + tryModifySpecifiedConfig(nil, finalChanges) tryModifyClient(nil, finalChanges) - return - end - tryModifyClientGlobal(finalChanges) - for _, scp in ipairs(ws.folders) do - if tryModifySpecifiedConfig(scp.uri, finalChanges) then - goto CONTINUE + if #finalChanges > 0 then + local manuallyModifyConfig = buildMaunuallyMessage(finalChanges) + m.showMessage('Warning', lang.script('CONFIG_MODIFY_FAIL_NO_WORKSPACE', manuallyModifyConfig)) end - if tryModifyRC(scp.uri, finalChanges, false) then - goto CONTINUE + else + for _, scp in ipairs(ws.folders) do + tryModifySpecifiedConfig(scp.uri, finalChanges) + tryModifyRC(scp.uri, finalChanges, false) + tryModifyClient(scp.uri, finalChanges) + tryModifyRC(scp.uri, finalChanges, true) end - if tryModifyClient(scp.uri, finalChanges) then - goto CONTINUE + if #finalChanges > 0 then + m.showMessage('Warning', lang.script('CONFIG_MODIFY_FAIL', buildMaunuallyMessage(finalChanges))) end - tryModifyRC(scp.uri, finalChanges, true) - ::CONTINUE:: end end, log.error) end |