From e0e3c9ab6b69b16b8949f93f309461f298038fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 23 Jun 2022 02:24:23 +0800 Subject: update --- meta/template/io.lua | 2 +- script/core/code-action.lua | 28 ++++++++++++++++++++-------- script/core/command/autoRequire.lua | 3 +++ script/core/completion/completion.lua | 3 +++ script/core/formatting.lua | 9 ++++++--- script/core/type-formatting.lua | 3 +++ script/files.lua | 18 ++++++++++++++---- script/fs-utility.lua | 17 ++++++++++++----- script/library.lua | 4 +++- script/parser/compile.lua | 1 + script/vm/infer.lua | 5 ++++- test/full/self.lua | 12 ++++++++++-- test/type_inference/init.lua | 7 +++++-- 13 files changed, 85 insertions(+), 27 deletions(-) diff --git a/meta/template/io.lua b/meta/template/io.lua index 3a6e37ff..ba1e9a02 100644 --- a/meta/template/io.lua +++ b/meta/template/io.lua @@ -101,7 +101,7 @@ function io.write(...) end ---@class file* local file = {} ----@alias readmode number +---@alias readmode integer ---#if VERSION >= 5.3 then ---| '"n"' # ---#DESTAIL 'readmode.n' ---| '"a"' # ---#DESTAIL 'readmode.a' diff --git a/script/core/code-action.lua b/script/core/code-action.lua index d2cf440a..f9926b64 100644 --- a/script/core/code-action.lua +++ b/script/core/code-action.lua @@ -14,6 +14,9 @@ local function checkDisableByLuaDocExits(uri, row, mode, code) return nil end local state = files.getState(uri) + if not state then + return nil + end local lines = state.lines if state.ast.docs and lines then return guide.eachSourceBetween( @@ -128,9 +131,12 @@ local function changeVersion(uri, version, results) end local function solveUndefinedGlobal(uri, diag, results) - local ast = files.getState(uri) - local start = converter.unpackRange(uri, diag.range) - guide.eachSourceContain(ast.ast, start, function (source) + local state = files.getState(uri) + if not state then + return + end + local start = converter.unpackRange(uri, diag.range) + guide.eachSourceContain(state.ast, start, function (source) if source.type ~= 'getglobal' then return end @@ -147,9 +153,12 @@ local function solveUndefinedGlobal(uri, diag, results) end local function solveLowercaseGlobal(uri, diag, results) - local ast = files.getState(uri) - local start = converter.unpackRange(uri, diag.range) - guide.eachSourceContain(ast.ast, start, function (source) + local state = files.getState(uri) + if not state then + return + end + local start = converter.unpackRange(uri, diag.range) + guide.eachSourceContain(state.ast, start, function (source) if source.type ~= 'setglobal' then return end @@ -160,8 +169,11 @@ local function solveLowercaseGlobal(uri, diag, results) end local function findSyntax(uri, diag) - local ast = files.getState(uri) - for _, err in ipairs(ast.errs) do + local state = files.getState(uri) + if not state then + return + end + for _, err in ipairs(state.errs) do if err.type:lower():gsub('_', '-') == diag.code then local range = converter.packRange(uri, err.start, err.finish) if util.equal(range, diag.range) then diff --git a/script/core/command/autoRequire.lua b/script/core/command/autoRequire.lua index c0deecfc..923c6f11 100644 --- a/script/core/command/autoRequire.lua +++ b/script/core/command/autoRequire.lua @@ -21,6 +21,9 @@ end local function findInsertRow(uri) local text = files.getText(uri) local state = files.getState(uri) + if not state then + return + end local lines = state.lines local fmt = { pair = false, diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 52ec7a18..4fc63962 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -204,6 +204,9 @@ local function getSnip(source) local uri = guide.getUri(def) local text = files.getText(uri) local state = files.getState(uri) + if not state then + goto CONTINUE + end local lines = state.lines if not text then goto CONTINUE diff --git a/script/core/formatting.lua b/script/core/formatting.lua index b52854a4..fb5ca9c7 100644 --- a/script/core/formatting.lua +++ b/script/core/formatting.lua @@ -4,7 +4,10 @@ local log = require("log") return function(uri, options) local text = files.getOriginText(uri) - local ast = files.getState(uri) + local state = files.getState(uri) + if not state then + return + end local status, formattedText = codeFormat.format(uri, text, options) if not status then @@ -17,8 +20,8 @@ return function(uri, options) return { { - start = ast.ast.start, - finish = ast.ast.finish, + start = state.ast.start, + finish = state.ast.finish, text = formattedText, } } diff --git a/script/core/type-formatting.lua b/script/core/type-formatting.lua index 24b4dde4..7005af26 100644 --- a/script/core/type-formatting.lua +++ b/script/core/type-formatting.lua @@ -6,6 +6,9 @@ local function insertIndentation(uri, position, edits) local text = files.getText(uri) local state = files.getState(uri) local row = guide.rowColOf(position) + if not state then + return + end local offset = state.lines[row] local indent = text:match('^%s*', offset) for _, edit in ipairs(edits) do diff --git a/script/files.lua b/script/files.lua index d0225f45..513fb52f 100644 --- a/script/files.lua +++ b/script/files.lua @@ -15,7 +15,17 @@ local encoder = require 'encoder' local scope = require 'workspace.scope' ---@class file ----@field content string +---@field content string +---@field _ref? integer +---@field trusted? boolean +---@field rows? integer[] +---@field originText? string +---@field text string +---@field version? integer +---@field originLines? integer[] +---@field state? parser.state +---@field _diffInfo? table[] +---@field cache table ---@class files local m = {} @@ -527,7 +537,7 @@ end --- 获取文件语法树 ---@param uri uri ----@return table state +---@return table? state function m.getState(uri) local file = m.fileMap[uri] if not file then @@ -537,7 +547,7 @@ function m.getState(uri) if not state then state = m.compileState(uri, file.text) m.astMap[uri] = state - file.ast = state + file.state = state --await.delay() end file.cacheActiveTime = timer.clock() @@ -549,7 +559,7 @@ function m.getLastState(uri) if not file then return nil end - return file.ast + return file.state end function m.getFile(uri) diff --git a/script/fs-utility.lua b/script/fs-utility.lua index cca65521..db8d177c 100644 --- a/script/fs-utility.lua +++ b/script/fs-utility.lua @@ -13,9 +13,13 @@ local tableSort = table.sort _ENV = nil +---@class fspath +---@field string fun(self):string + +---@class fs-utility local m = {} --- 读取文件 ----@param path string +---@param path string|fspath function m.loadFile(path, keepBom) if type(path) ~= 'string' then ---@diagnostic disable-next-line: undefined-field @@ -255,6 +259,9 @@ function dfs:saveFile(path, text) dir[filename] = text end +---@param path string|fspath +---@param option table +---@return fspath local function fsAbsolute(path, option) if type(path) == 'string' then local suc, res = pcall(fs.path, path) @@ -583,8 +590,8 @@ function m.fileRemove(path, option) end --- 复制文件(夹) ----@param source string ----@param target string +---@param source string|fspath +---@param target string|fspath ---@return table function m.fileCopy(source, target, option) option = buildOption(option) @@ -597,8 +604,8 @@ function m.fileCopy(source, target, option) end --- 同步文件(夹) ----@param source string ----@param target string +---@param source string|fspath +---@param target string|fspath ---@return table function m.fileSync(source, target, option) option = buildOption(option) diff --git a/script/library.lua b/script/library.lua index c7b9ea9a..57e77988 100644 --- a/script/library.lua +++ b/script/library.lua @@ -209,6 +209,7 @@ local function initBuiltIn(uri) local langID = lang.id local version = config.get(uri, 'Lua.runtime.version') local encoding = config.get(uri, 'Lua.runtime.fileEncoding') + ---@type fspath local metaPath = fs.path(METAPATH) / config.get(uri, 'Lua.runtime.meta'):gsub('%$%{(.-)%}', { version = version, language = langID, @@ -243,6 +244,7 @@ local function initBuiltIn(uri) goto CONTINUE end libName = libName .. '.lua' + ---@type fspath local libPath = templateDir / libName local metaDoc = compileSingleMetaDoc(uri, fsu.loadFile(libPath), metaLang, status) if metaDoc then @@ -261,7 +263,7 @@ local function initBuiltIn(uri) end local function loadSingle3rdConfig(libraryDir) - local configText = fsu.loadFile(libraryDir / 'config.lua') + local configText = fsu.loadFile(libraryDir / 'config.lua'--[[@as fspath]]) if not configText then return nil end diff --git a/script/parser/compile.lua b/script/parser/compile.lua index 446f181b..20546e4a 100644 --- a/script/parser/compile.lua +++ b/script/parser/compile.lua @@ -3792,6 +3792,7 @@ local function initState(lua, version, options) Chunk = {} Tokens = tokens(lua) Index = 1 + ---@class parser.state local state = { version = version, lua = lua, diff --git a/script/vm/infer.lua b/script/vm/infer.lua index 35a2f59f..e789214a 100644 --- a/script/vm/infer.lua +++ b/script/vm/infer.lua @@ -176,13 +176,16 @@ local viewNodeSwitch = util.switch() ---@class vm.node ---@field lastInfer? vm.infer ----@param source parser.object | vm.node +---@param source vm.object | vm.node ---@return vm.infer function vm.getInfer(source) + ---@type vm.node local node if source.type == 'vm.node' then + ---@cast source vm.node node = source else + ---@cast source vm.object node = vm.compileNode(source) end if node.lastInfer then diff --git a/test/full/self.lua b/test/full/self.lua index cfa6b710..09d2d154 100644 --- a/test/full/self.lua +++ b/test/full/self.lua @@ -39,13 +39,17 @@ local clock = os.clock() ---@diagnostic disable: await-in-sync for uri in files.eachFile() do - local status = files.getState(uri) - guide.eachSource(status.ast, function (src) + local state = files.getState(uri) + if not state then + goto CONTINUE + end + guide.eachSource(state.ast, function (src) assert(src.parent ~= nil or src.type == 'main') end) local fileClock = os.clock() diag.doDiagnostic(uri, true) print('诊断文件耗时:', os.clock() - fileClock, uri) + ::CONTINUE:: end local passed = os.clock() - clock @@ -58,6 +62,9 @@ local compileDatas = {} for uri in files.eachFile() do local state = files.getState(uri) + if not state then + goto CONTINUE + end local clock = os.clock() guide.eachSource(state.ast, function (src) vm.compileNode(src) @@ -66,6 +73,7 @@ for uri in files.eachFile() do passed = os.clock() - clock, uri = uri, } + ::CONTINUE:: end local printTexts = {} diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua index 33184569..254320e4 100644 --- a/test/type_inference/init.lua +++ b/test/type_inference/init.lua @@ -7,9 +7,12 @@ local vm = require 'vm' rawset(_G, 'TEST', true) local function getSource(pos) - local ast = files.getState('') + local state = files.getState('') + if not state then + return + end local result - guide.eachSourceContain(ast.ast, pos, function (source) + guide.eachSourceContain(state.ast, pos, function (source) if source.type == 'local' or source.type == 'getlocal' or source.type == 'setlocal' -- cgit v1.2.3