From cc6236c6803103212b17dadb3014739509a14d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sat, 25 Jul 2020 15:32:53 +0800 Subject: =?UTF-8?q?=E6=9A=82=E6=97=B6=E7=AE=80=E5=8C=96=E8=AF=8A=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-beta/files.lua | 45 ++++++++++++++++++++++++------------- script-beta/provider/diagnostic.lua | 34 +++++++++++++++------------- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/script-beta/files.lua b/script-beta/files.lua index bbcabb0f..1d2b8952 100644 --- a/script-beta/files.lua +++ b/script-beta/files.lua @@ -10,6 +10,7 @@ local m = {} m.openMap = {} m.fileMap = {} +m.watchList = {} m.notifyCache = {} m.assocVersion = -1 m.assocMatcher = nil @@ -18,19 +19,23 @@ m.globalVersion = 0 --- 打开文件 ---@param uri string function m.open(uri) + local originUri = uri if platform.OS == 'Windows' then uri = uri:lower() end m.openMap[uri] = true + m.onWatch('open', originUri) end --- 关闭文件 ---@param uri string function m.close(uri) + local originUri = uri if platform.OS == 'Windows' then uri = uri:lower() end m.openMap[uri] = nil + m.onWatch('close', originUri) end --- 是否打开 @@ -70,24 +75,11 @@ function m.setText(uri, text) return end file.text = text + file.ast = nil file.lines = nil file.cache = {} m.globalVersion = m.globalVersion + 1 -end - ---- 监听编译完成 -function m.onCompiled(uri, callback) - if platform.OS == 'Windows' then - uri = uri:lower() - end - local file = m.fileMap[uri] - if not file then - return - end - if not file.onCompiledList then - file.onCompiledList = {} - end - file.onCompiledList[#file.onCompiledList+1] = callback + m.onWatch('update', originUri) end --- 获取文件文本 @@ -107,6 +99,7 @@ end --- 移除文件 ---@param uri string function m.remove(uri) + local originUri = uri if platform.OS == 'Windows' then uri = uri:lower() end @@ -117,14 +110,16 @@ function m.remove(uri) m.fileMap[uri] = nil m.globalVersion = m.globalVersion + 1 + m.onWatch('remove', originUri) end --- 移除所有文件 function m.removeAll() + m.globalVersion = m.globalVersion + 1 for uri in pairs(m.fileMap) do m.fileMap[uri] = nil + m.onWatch('remove', uri) end - m.globalVersion = m.globalVersion + 1 m.notifyCache = {} end @@ -213,6 +208,13 @@ function m.getOriginUri(uri) return file.uri end +function m.getUri(uri) + if platform.OS == 'Windows' then + uri = uri:lower() + end + return uri +end + --- 获取文件的自定义缓存信息(在文件内容更新后自动失效) function m.getCache(uri) if platform.OS == 'Windows' then @@ -269,4 +271,15 @@ function m.isLua(uri) return matcher(path) end +--- 注册事件 +function m.watch(callback) + m.watchList[#m.watchList+1] = callback +end + +function m.onWatch(ev, ...) + for _, callback in ipairs(m.watchList) do + callback(ev, ...) + end +end + return m diff --git a/script-beta/provider/diagnostic.lua b/script-beta/provider/diagnostic.lua index 38076aea..87b8cb26 100644 --- a/script-beta/provider/diagnostic.lua +++ b/script-beta/provider/diagnostic.lua @@ -10,7 +10,6 @@ local util = require 'utility' local m = {} m._start = false m.cache = {} -m.lastSynaxErrors = {} local function concat(t, sep) if type(t) ~= 'table' then @@ -106,7 +105,6 @@ local function merge(a, b) end function m.clear(uri) - m.lastSynaxErrors[uri] = nil if not m.cache[uri] then return end @@ -131,8 +129,8 @@ function m.syntaxErrors(uri, ast) return results end -function m.diagnostics(uri, syntaxOnly) - if syntaxOnly or not m._start then +function m.diagnostics(uri) + if not m._start then return m.cache[uri] end @@ -149,7 +147,7 @@ function m.diagnostics(uri, syntaxOnly) return results end -function m.doDiagnostic(uri, main, syntaxOnly) +function m.doDiagnostic(uri) local ast = files.getAst(uri) if not ast then m.clear(uri) @@ -157,7 +155,7 @@ function m.doDiagnostic(uri, main, syntaxOnly) end local syntax = m.syntaxErrors(uri, ast) - local diagnostics = m.diagnostics(uri, syntaxOnly) + local diagnostics = m.diagnostics(uri) local full = merge(syntax, diagnostics) if not full then m.clear(uri) @@ -168,14 +166,6 @@ function m.doDiagnostic(uri, main, syntaxOnly) return end m.cache[uri] = full - if main - and syntaxOnly - and (syntax and #syntax or 0) > (m.lastSynaxErrors[uri] or 0) then - await.sleep(2, function () - return files.globalVersion - end) - end - m.lastSynaxErrors[uri] = syntax and #syntax or 0 proto.notify('textDocument/publishDiagnostics', { uri = uri, @@ -214,9 +204,23 @@ function m.refresh(uri) end) end +function m.diagnosticsAll() + for uri in files.eachFile() do + m.doDiagnostic(uri) + end +end + function m.start() m._start = true - m.refresh() + m.diagnosticsAll() end +files.watch(function (env, uri) + if env == 'remove' then + m.clear(uri) + elseif env == 'update' then + m.doDiagnostic(uri) + end +end) + return m -- cgit v1.2.3