diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-02-15 17:14:52 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-02-15 17:14:52 +0800 |
commit | 92b00f4e0bb805f2a9076cc89f3cedd37cc4b150 (patch) | |
tree | 9b6869390ecb4b7446320931bf3b46b1c9f05070 /script | |
parent | 8521ec04253fb6367260bfa74ad17e8d5a5e77fb (diff) | |
download | lua-language-server-92b00f4e0bb805f2a9076cc89f3cedd37cc4b150.zip |
fix #950
Diffstat (limited to 'script')
-rw-r--r-- | script/core/diagnostics/codestyle-check.lua | 20 | ||||
-rw-r--r-- | script/filewatch.lua | 21 | ||||
-rw-r--r-- | script/provider/capability.lua | 1 | ||||
-rw-r--r-- | script/provider/formatting.lua | 68 | ||||
-rw-r--r-- | script/provider/provider.lua | 63 | ||||
-rw-r--r-- | script/utility.lua | 9 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 66 |
7 files changed, 142 insertions, 106 deletions
diff --git a/script/core/diagnostics/codestyle-check.lua b/script/core/diagnostics/codestyle-check.lua index 16623eca..34d55ee2 100644 --- a/script/core/diagnostics/codestyle-check.lua +++ b/script/core/diagnostics/codestyle-check.lua @@ -1,8 +1,8 @@ -local files = require("files") -local codeFormat = require "code_format" -local converter = require("proto.converter") -local log = require("log") -local config = require("config") +local files = require 'files' +local codeFormat = require 'code_format' +local converter = require 'proto.converter' +local log = require 'log' +local pformatting = require 'provider.formatting' ---@async @@ -12,6 +12,8 @@ return function(uri, callback) return end + pformatting.updateConfig(uri) + local status, diagnosticInfos = codeFormat.diagnose_file(uri, text) if not status then @@ -21,12 +23,12 @@ return function(uri, callback) return end - + if diagnosticInfos then - for _, diagnosticInfo in pairs(diagnosticInfos) do + for _, diagnosticInfo in ipairs(diagnosticInfos) do callback { - start = converter.unpackPosition(uri, diagnosticInfo.range.start), - finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]), + start = converter.unpackPosition(uri, diagnosticInfo.range.start), + finish = converter.unpackPosition(uri, diagnosticInfo.range["end"]), message = diagnosticInfo.message } end diff --git a/script/filewatch.lua b/script/filewatch.lua index 13507654..5e3a0322 100644 --- a/script/filewatch.lua +++ b/script/filewatch.lua @@ -54,10 +54,10 @@ function m.event(callback) m._eventList[#m._eventList+1] = callback end -function m._callEvent(changes) +function m._callEvent(ev, path) for _, callback in ipairs(m._eventList) do await.call(function () - callback(changes) + callback(ev, path) end) end end @@ -83,29 +83,18 @@ function m.update() return end - local changes = {} for path, flag in pairs(collect) do if flag & RENAME ~= 0 then if exists(path) then - changes[#changes+1] = { - type = 'create', - path = path, - } + m._callEvent('create', path) else - changes[#changes+1] = { - type = 'delete', - path = path, - } + m._callEvent('delete', path) end elseif flag & MODIFY ~= 0 then - changes[#changes+1] = { - type = 'change', - path = path, - } + m._callEvent('change', path) end end - m._callEvent(changes) end return m diff --git a/script/provider/capability.lua b/script/provider/capability.lua index e13e11e0..e012f4b4 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -5,6 +5,7 @@ local completion = require 'provider.completion' local define = require 'proto.define' require 'provider.semantic-tokens' +require 'provider.formatting' local function toArray(map) local array = {} diff --git a/script/provider/formatting.lua b/script/provider/formatting.lua new file mode 100644 index 00000000..9392259e --- /dev/null +++ b/script/provider/formatting.lua @@ -0,0 +1,68 @@ +local codeFormat = require 'code_format' +local ws = require 'workspace' +local furi = require 'file-uri' +local fs = require 'bee.filesystem' +local fw = require 'filewatch' +local util = require 'utility' +local diagnostics = require 'provider.diagnostic' + +local loadedUris = {} + +local updateType = { + Created = 1, + Changed = 2, + Deleted = 3, +} + +fw.event(function (ev, path) + if util.stringEndWith(path, '.editorconfig') then + for uri, fsPath in pairs(loadedUris) do + loadedUris[uri] = nil + if fsPath ~= true then + local status, err = codeFormat.update_config(updateType.Deleted, uri, fsPath:string()) + if not status and err then + log.error(err) + end + end + end + for _, scp in ipairs(ws.folders) do + diagnostics.diagnosticsScope(scp.uri) + end + end +end) + +local m = {} + +---@param uri uri +function m.updateConfig(uri) + local currentUri = uri + while true do + currentUri = currentUri:match('^(.+)/[^/]*$') + if not currentUri or loadedUris[currentUri] then + return + end + loadedUris[currentUri] = true + + local currentPath = furi.decode(currentUri) + local editorConfigFSPath = fs.path(currentPath) / '.editorconfig' + if fs.exists(editorConfigFSPath) then + loadedUris[uri] = editorConfigFSPath + local status, err = codeFormat.update_config(updateType.Created, currentUri, editorConfigFSPath:string()) + if not status and err then + log.error(err) + end + end + + if not ws.rootUri then + return + end + + for _, scp in ipairs(ws.folders) do + if scp.uri == currentUri then + return + end + end + end +end + +return m diff --git a/script/provider/provider.lua b/script/provider/provider.lua index c2ab4b7b..df226c3f 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -65,22 +65,20 @@ function m.register(method) end end -filewatch.event(function (changes) ---@async - for _, change in ipairs(changes) do - 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 +filewatch.event(function (ev, path) ---@async + if (CONFIGPATH and util.stringEndWith(path, CONFIGPATH)) then + for _, scp in ipairs(workspace.folders) do + local configPath = workspace.getAbsolutePath(scp.uri, CONFIGPATH) + if path == configPath then + updateConfig(scp.uri) 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 + if util.stringEndWith(path, '.luarc.json') then + for _, scp in ipairs(workspace.folders) do + local rcPath = workspace.getAbsolutePath(scp.uri, '.luarc.json') + if path == rcPath then + updateConfig(scp.uri) end end end @@ -102,19 +100,6 @@ m.register 'initialize' { workspace.create(params.rootUri) end - if params.initializationOptions then - if params.initializationOptions.editorConfigFiles then - local codeFormat = require "code_format" - for _, config in pairs(params.initializationOptions.editorConfigFiles) do - local status, err = codeFormat.update_config(1, config.workspace, config.path) - - if not status and err ~= nil then - log.error(err) - end - end - end - end - return { capabilities = cap.getIniter(), serverInfo = { @@ -938,6 +923,9 @@ m.register 'textDocument/formatting' { return nil end + local pformatting = require 'provider.formatting' + pformatting.updateConfig(uri) + local core = require 'core.formatting' local edits = core(uri) if not edits or #edits == 0 then @@ -965,6 +953,9 @@ m.register 'textDocument/rangeFormatting' { return nil end + local pformatting = require 'provider.formatting' + pformatting.updateConfig(uri) + local core = require 'core.rangeformatting' local edits = core(uri, params.range) if not edits or #edits == 0 then @@ -983,24 +974,6 @@ m.register 'textDocument/rangeFormatting' { end } -m.register 'config/editorconfig/update' { - ---@async - function(params) - local codeFormat = require "code_format" - local status, err = codeFormat.update_config(params.type, params.source.workspace, params.source.path) - - if not status and err ~= nil then - log.error(err) - return - end - - local diagnostic = require 'provider.diagnostic' - for _, scp in ipairs(workspace.folders) do - diagnostic.diagnosticsScope(scp.uri) - end - end -} - m.register 'textDocument/onTypeFormatting' { abortByFileUpdate = true, ---@async diff --git a/script/utility.lua b/script/utility.lua index 66b769f1..6758a47f 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -23,6 +23,7 @@ local inf = 1 / 0 local nan = 0 / 0 local utf8 = utf8 local error = error +local upvalueid = debug.upvalueid _ENV = nil @@ -721,4 +722,12 @@ function m.stringEndWith(str, tail) return str:sub(-#tail) == tail end +function m.defaultTable(default) + return setmetatable({}, { __index = function (t, k) + local v = default(k) + t[k] = v + return v + end }) +end + return m diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index e53ba525..cb8bd68b 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -500,44 +500,38 @@ config.watch(function (uri, key, value, oldValue) end end) -fw.event(function (changes) ---@async - for _, change in ipairs(changes) do - local path = change.path - local uri = furi.encode(path) - - ---@async - await.call(function () - if change.type == 'create' then - log.debug('FileChangeType.Created', uri) - m.awaitLoadFile(uri) - elseif change.type == 'delete' then - log.debug('FileChangeType.Deleted', uri) - files.remove(uri) - m.removeFile(uri) - local childs = files.getChildFiles(uri) - for _, curi in ipairs(childs) do - log.debug('FileChangeType.Deleted.Child', curi) - files.remove(curi) - m.removeFile(uri) - end - elseif change.type == 'change' then - if m.isValidLuaUri(uri) then - -- 如果文件处于关闭状态,则立即更新;否则等待didChange协议来更新 - if not files.isOpen(uri) then - files.setText(uri, pub.awaitTask('loadFile', furi.decode(uri)), false) - end - end +fw.event(function (ev, path) ---@async + local uri = furi.encode(path) + + if ev == 'create' then + log.debug('FileChangeType.Created', uri) + m.awaitLoadFile(uri) + elseif ev == 'delete' then + log.debug('FileChangeType.Deleted', uri) + files.remove(uri) + m.removeFile(uri) + local childs = files.getChildFiles(uri) + for _, curi in ipairs(childs) do + log.debug('FileChangeType.Deleted.Child', curi) + files.remove(curi) + m.removeFile(uri) + end + elseif ev == 'change' then + if m.isValidLuaUri(uri) then + -- 如果文件处于关闭状态,则立即更新;否则等待didChange协议来更新 + if not files.isOpen(uri) then + files.setText(uri, pub.awaitTask('loadFile', furi.decode(uri)), false) end - end) + end + end - local filename = fs.path(path):filename():string() - -- 排除类文件发生更改需要重新扫描 - if filename == '.gitignore' - or filename == '.gitmodules' then - local scp = scope.getScope(uri) - if scp.type ~= 'fallback' then - m.reload(scp) - end + local filename = fs.path(path):filename():string() + -- 排除类文件发生更改需要重新扫描 + if filename == '.gitignore' + or filename == '.gitmodules' then + local scp = scope.getScope(uri) + if scp.type ~= 'fallback' then + m.reload(scp) end end end) |