diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-08-18 19:55:04 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-08-18 19:55:04 +0800 |
commit | 38d46faae20bbdbc3578452f020276766aed67e0 (patch) | |
tree | 6ff276e89459b4d7098a5d1e50789a82ab2c6449 /script/workspace | |
parent | 1d0dc164b84fffdfe281615013278013baf9aae7 (diff) | |
download | lua-language-server-38d46faae20bbdbc3578452f020276766aed67e0.zip |
watching library changes
Diffstat (limited to 'script/workspace')
-rw-r--r-- | script/workspace/workspace.lua | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index ebd1b842..f7b2e97b 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -14,6 +14,7 @@ local define = require "proto.define" local client = require 'client' local plugin = require 'plugin' local util = require 'utility' +local fw = require 'filewatch' local m = {} m.type = 'workspace' @@ -41,6 +42,8 @@ function m.initPath(uri) client.logMessage('Log', 'Log path: ' .. furi.encode(logPath:string())) log.info('Log path: ', logPath) log.init(ROOT, logPath) + + fw.watch(m.path) end local globInteferFace = { @@ -183,6 +186,17 @@ function m.isIgnored(uri) return ignore(path) end +function m.isValidLuaUri(uri) + if not files.isLua(uri) then + return false + end + if m.isIgnored(uri) + and not files.isLibrary(uri) then + return false + end + return true +end + local function loadFileFactory(root, progressData, isLibrary) return function (path) local uri = furi.encode(path) @@ -325,7 +339,7 @@ function m.awaitPreload() local libraryLoader = loadFileFactory(library.path, progressData, true) log.info('Scan library at:', library.path) library.matcher:scan(library.path, libraryLoader) - m.watchers[#m.watchers+1] = client.watchFiles(library.path) + m.watchers[#m.watchers+1] = fw.watch(library.path) end local isLoadingFiles = false @@ -596,4 +610,44 @@ config.watch(function (key, value, oldValue) end end) +fw.event(function (changes) + m.awaitReady() + for _, change in ipairs(changes) do + local path = change.path + local uri = furi.encode(path) + if not m.isWorkspaceUri(uri) + and not files.isLibrary(uri) then + goto CONTINUE + end + 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) + local childs = files.getChildFiles(uri) + for _, curi in ipairs(childs) do + log.debug('FileChangeType.Deleted.Child', curi) + files.remove(curi) + end + elseif change.type == 'change' then + if m.isValidLuaUri(uri) then + -- 如果文件处于关闭状态,则立即更新;否则等待didChange协议来更新 + if not files.isOpen(uri) then + files.setText(uri, pub.awaitTask('loadFile', uri), false) + end + else + local filename = fs.path(path):filename():string() + -- 排除类文件发生更改需要重新扫描 + if filename == '.gitignore' + or filename == '.gitmodules' then + m.reload() + break + end + end + end + ::CONTINUE:: + end +end) + return m |