From e72f5143dc1d2d41763b74809b1ffe5ec5f426c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 27 Jan 2021 16:59:05 +0800 Subject: fix modifying the code before loading finish --- script/files.lua | 6 +++++- script/provider/provider.lua | 19 ++++++++++++++----- script/workspace/workspace.lua | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) (limited to 'script') diff --git a/script/files.lua b/script/files.lua index 1e7c15e9..7e14bd9c 100644 --- a/script/files.lua +++ b/script/files.lua @@ -122,7 +122,7 @@ end --- 设置文件文本 ---@param uri uri ---@param text string -function m.setText(uri, text) +function m.setText(uri, text, isTrust) if not text then return end @@ -139,11 +139,15 @@ function m.setText(uri, text) m._pairsCache = nil end local file = m.fileMap[uri] + if file.trusted and not isTrust then + return + end local newText = pluginOnSetText(file, text) if file.text == newText then return end file.text = newText + file.trusted = isTrust file.originText = text m.linesMap[uri] = nil m.originLinesMap[uri] = nil diff --git a/script/provider/provider.lua b/script/provider/provider.lua index 7c7721a4..8ba5bb1c 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -149,7 +149,10 @@ proto.on('workspace/didChangeWatchedFiles', function (params) elseif change.type == define.FileChangeType.Changed then -- 如果文件处于关闭状态,则立即更新;否则等待didChange协议来更新 if files.isLua(uri) and not files.isOpen(uri) then - files.setText(uri, pub.awaitTask('loadFile', uri)) + while not plugin.isReady() do + await.sleep(0.1) + end + files.setText(uri, pub.awaitTask('loadFile', uri), false) else local path = furi.decode(uri) local filename = fs.path(path):filename():string() @@ -170,9 +173,12 @@ proto.on('textDocument/didOpen', function (params) local text = doc.text files.open(uri) while not plugin.isReady() do + if not files.isOpen(uri) then + return + end await.sleep(0.1) end - files.setText(uri, text) + files.setText(uri, text, true) end) proto.on('textDocument/didClose', function (params) @@ -188,8 +194,11 @@ proto.on('textDocument/didChange', function (params) local doc = params.textDocument local changes = params.contentChanges local uri = doc.uri - if not files.isLua(uri) and not files.isOpen(uri) then - return + while not plugin.isReady() or not files.exists(uri) do + if not files.isLua(uri) and not files.isOpen(uri) then + return + end + await.sleep(0.1) end files.clearDiff(uri) local text = files.getText(uri) or '' @@ -201,7 +210,7 @@ proto.on('textDocument/didChange', function (params) text = change.text end end - files.setText(uri, text) + files.setText(uri, text, true) end) proto.on('textDocument/hover', function (params) diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index ec30c9ea..94b1d9e5 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -200,7 +200,7 @@ local function loadFileFactory(root, progressData, isLibrary) log.info('++++As library of:', root) files.setLibraryPath(uri, root) end - files.setText(uri, text) + files.setText(uri, text, false) else files.remove(uri) end -- cgit v1.2.3