diff options
Diffstat (limited to 'script')
-rw-r--r-- | script/await.lua | 4 | ||||
-rw-r--r-- | script/client.lua | 3 | ||||
-rw-r--r-- | script/config/loader.lua | 3 | ||||
-rw-r--r-- | script/core/completion/completion.lua | 90 | ||||
-rw-r--r-- | script/core/diagnostics/init.lua | 6 | ||||
-rw-r--r-- | script/core/hover/args.lua (renamed from script/core/hover/arg.lua) | 24 | ||||
-rw-r--r-- | script/core/hover/label.lua | 6 | ||||
-rw-r--r-- | script/core/semantic-tokens.lua | 24 | ||||
-rw-r--r-- | script/files.lua | 2 | ||||
-rw-r--r-- | script/fs-utility.lua | 39 | ||||
-rw-r--r-- | script/global.d.lua | 3 | ||||
-rw-r--r-- | script/inspect.lua | 337 | ||||
-rw-r--r-- | script/jsonrpc.lua | 4 | ||||
-rw-r--r-- | script/library.lua | 11 | ||||
-rw-r--r-- | script/log.lua | 21 | ||||
-rw-r--r-- | script/parser/guide.lua | 3 | ||||
-rw-r--r-- | script/proto/proto.lua | 31 | ||||
-rw-r--r-- | script/provider/completion.lua | 11 | ||||
-rw-r--r-- | script/provider/diagnostic.lua | 8 | ||||
-rw-r--r-- | script/provider/provider.lua | 23 | ||||
-rw-r--r-- | script/pub/pub.lua | 2 | ||||
-rw-r--r-- | script/service/service.lua | 4 | ||||
-rw-r--r-- | script/service/telemetry.lua | 4 | ||||
-rw-r--r-- | script/workspace/loading.lua | 13 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 25 |
25 files changed, 561 insertions, 140 deletions
diff --git a/script/await.lua b/script/await.lua index 4fdab0a2..4fb81cd8 100644 --- a/script/await.lua +++ b/script/await.lua @@ -185,7 +185,7 @@ function m.stop() end local function warnStepTime(passed, waker) - if passed < 1 then + if passed < 2 then log.warn(('Await step takes [%.3f] sec.'):format(passed)) return end @@ -215,7 +215,7 @@ function m.step() local clock = os.clock() resume() local passed = os.clock() - clock - if passed > 0.1 then + if passed > 0.5 then warnStepTime(passed, resume) end return true diff --git a/script/client.lua b/script/client.lua index 6c3e503d..07520937 100644 --- a/script/client.lua +++ b/script/client.lua @@ -8,6 +8,7 @@ local converter = require 'proto.converter' local json = require 'json-beautify' local await = require 'await' local scope = require 'workspace.scope' +local inspect = require 'inspect' local m = {} @@ -398,7 +399,7 @@ local function hookPrint() end function m.init(t) - log.debug('Client init', util.dump(t)) + log.debug('Client init', inspect(t)) m.info = t nonil.enable() m.client(t.clientInfo.name) diff --git a/script/config/loader.lua b/script/config/loader.lua index c0bd66d7..c53f9399 100644 --- a/script/config/loader.lua +++ b/script/config/loader.lua @@ -4,6 +4,7 @@ local lang = require 'language' local util = require 'utility' local workspace = require 'workspace' local scope = require 'workspace.scope' +local inspect = require 'inspect' local function errorMessage(msg) proto.notify('window/showMessage', { @@ -105,7 +106,7 @@ function m.loadClientConfig(uri) }, }) if not configs or not configs[1] then - log.warn('No config?', util.dump(configs)) + log.warn('No config?', inspect(configs)) return nil end diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index aed3e83a..b6fd15ee 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -3,7 +3,7 @@ local files = require 'files' local matchKey = require 'core.matchkey' local vm = require 'vm' local getName = require 'core.hover.name' -local getArg = require 'core.hover.arg' +local getArgs = require 'core.hover.args' local getHover = require 'core.hover' local config = require 'config' local util = require 'utility' @@ -153,15 +153,36 @@ end local function buildFunctionSnip(source, value, oop) local name = (getName(source) or ''):gsub('^.+[$.:]', '') - local args = getArg(value, oop) - local id = 0 - args = args:gsub('[^,]+', function (arg) - id = id + 1 - return arg:gsub('^(%s*)(.+)', function (sp, word) + local args = getArgs(value) + if oop then + table.remove(args, 1) + end + local len = #args + local truncated = false + if len > 0 and args[len]:match('^%s*%.%.%.:') then + table.remove(args) + truncated = true + end + for i = #args, 1, -1 do + if args[i]:match('^%s*[^?]+%?:') then + table.remove(args) + truncated = true + else + break + end + end + + local snipArgs = {} + for id, arg in ipairs(args) do + local str = arg:gsub('^(%s*)(.+)', function (sp, word) return ('%s${%d:%s}'):format(sp, id, word) end) - end) - return ('%s(%s)'):format(name, args) + table.insert(snipArgs, str) + end + if truncated and #snipArgs == 0 then + snipArgs = {'$1'} + end + return ('%s(%s)'):format(name, table.concat(snipArgs, ', ')) end local function buildDetail(source) @@ -197,7 +218,7 @@ local function getSnip(source) goto CONTINUE end local firstRow = guide.rowColOf(def.start) - local lastRow = firstRow + context + local lastRow = math.min(guide.rowColOf(def.finish) + 1, firstRow + context) local lastOffset = lines[lastRow] and (lines[lastRow] - 1) or #text local snip = text:sub(lines[firstRow], lastOffset) return snip @@ -227,7 +248,9 @@ local function buildFunction(results, source, value, oop, data) if snipType == 'Both' or snipType == 'Replace' then local snipData = util.deepCopy(data) - snipData.kind = define.CompletionItemKind.Snippet + snipData.kind = snipType == 'Both' + and define.CompletionItemKind.Snippet + or data.kind snipData.insertText = buildFunctionSnip(source, value, oop) snipData.insertTextFormat = 2 snipData.command = { @@ -1378,34 +1401,37 @@ local function getCallEnumsAndFuncs(source, index, oop, call) return end local results = {} - if currentIndex == 1 then - for _, doc in ipairs(class.fields) do - if doc.field ~= source - and doc.field[1] == source[1] then - local eventName = noder.getFieldEventName(doc) - if eventName then - results[#results+1] = { - label = ('%q'):format(eventName), - description = doc.comment, - kind = define.CompletionItemKind.EnumMember, - } + local valueBeforeIndex = index > 1 and call.args[index - 1][1] + + for _, doc in ipairs(class.fields) do + if doc.field ~= source + and doc.field[1] == source[1] then + local indexType = currentIndex + if not oop then + local args = noder.getFieldArgs(doc) + -- offset if doc's first arg is `self` + if args and args[1] and args[1].name[1] == 'self' then + indexType = indexType - 1 end end - end - elseif currentIndex == 2 then - local myEventName = call.args[index - 1][1] - for _, doc in ipairs(class.fields) do - if doc.field ~= source - and doc.field[1] == source[1] then - local eventName = noder.getFieldEventName(doc) - if eventName and eventName == myEventName then - local docFunc = doc.extends.types[1].args[2].extends.types[1] + local eventName = noder.getFieldEventName(doc) + if eventName then + if indexType == 1 then results[#results+1] = { label = infer.getInfer(docFunc):view(), description = doc.comment, - kind = define.CompletionItemKind.Function, - insertText = buildInsertDocFunction(docFunc), + kind = define.CompletionItemKind.EnumMember, } + elseif indexType == 2 then + if eventName == valueBeforeIndex then + local docFunc = doc.extends.types[1].args[index].extends.types[1] + results[#results+1] = { + label = infer.viewDocFunction(docFunc), + description = doc.comment, + kind = define.CompletionItemKind.Function, + insertText = buildInsertDocFunction(docFunc), + } + end end end end diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index 4368f51a..369a6ba2 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -6,8 +6,10 @@ local vm = require "vm.vm" -- 把耗时最长的诊断放到最后面 local diagSort = { - ['deprecated'] = 98, - ['undefined-field'] = 99, + ['redundant-value'] = 96, + ['not-yieldable'] = 97, + ['deprecated'] = 98, + ['undefined-field'] = 99, ['redundant-parameter'] = 100, } diff --git a/script/core/hover/arg.lua b/script/core/hover/args.lua index 7611a895..900a7496 100644 --- a/script/core/hover/arg.lua +++ b/script/core/hover/args.lua @@ -13,7 +13,7 @@ local function optionalArg(arg) end end -local function asFunction(source, oop) +local function asFunction(source) local args = {} local methodDef local parent = source.parent @@ -47,14 +47,10 @@ local function asFunction(source, oop) ::CONTINUE:: end end - if oop then - return table.concat(args, ', ', 2) - else - return table.concat(args, ', ') - end + return args end -local function asDocFunction(source, oop) +local function asDocFunction(source) if not source.args then return '' end @@ -68,19 +64,15 @@ local function asDocFunction(source, oop) arg.extends and infer.getInfer(arg.extends):view 'any' or 'any' ) end - if oop then - return table.concat(args, ', ', 2) - else - return table.concat(args, ', ') - end + return args end -return function (source, oop) +return function (source) if source.type == 'function' then - return asFunction(source, oop) + return asFunction(source) end if source.type == 'doc.type.function' then - return asDocFunction(source, oop) + return asDocFunction(source) end - return '' + return {} end diff --git a/script/core/hover/label.lua b/script/core/hover/label.lua index c037b3f5..7f9f29a4 100644 --- a/script/core/hover/label.lua +++ b/script/core/hover/label.lua @@ -1,5 +1,5 @@ local buildName = require 'core.hover.name' -local buildArg = require 'core.hover.arg' +local buildArgs = require 'core.hover.args' local buildReturn = require 'core.hover.return' local buildTable = require 'core.hover.table' local infer = require 'vm.infer' @@ -12,7 +12,7 @@ local guide = require 'parser.guide' local function asFunction(source, oop) local name = buildName(source, oop) - local arg = buildArg(source, oop) + local args = buildArgs(source) local rtn = buildReturn(source) local lines = {} @@ -20,7 +20,7 @@ local function asFunction(source, oop) , vm.isAsync(source) and 'async ' or '' , oop and 'method' or 'function' , name or '' - , arg + , oop and table.concat(args, ', ', 2) or table.concat(args, ', ') ) lines[2] = rtn diff --git a/script/core/semantic-tokens.lua b/script/core/semantic-tokens.lua index 8d991df9..066ef68c 100644 --- a/script/core/semantic-tokens.lua +++ b/script/core/semantic-tokens.lua @@ -160,17 +160,7 @@ local Care = util.switch() } return end - -- 5. References to other functions - if infer.hasType(loc, 'function') then - results[#results+1] = { - start = source.start, - finish = source.finish, - type = define.TokenTypes['function'], - modifieres = source.type == 'setlocal' and define.TokenModifiers.declaration or nil, - } - return - end - -- 6. Class declaration + -- 5. Class declaration -- only search this local if loc.bindDocs then for i, doc in ipairs(loc.bindDocs) do @@ -188,7 +178,17 @@ local Care = util.switch() end end end - -- 6. const 变量 | Const variable + -- 6. References to other functions + if infer.hasType(loc, 'function') then + results[#results+1] = { + start = source.start, + finish = source.finish, + type = define.TokenTypes['function'], + modifieres = source.type == 'setlocal' and define.TokenModifiers.declaration or nil, + } + return + end + -- 7. const 变量 | Const variable if loc.attrs then for _, attr in ipairs(loc.attrs) do local name = attr[1] diff --git a/script/files.lua b/script/files.lua index 9ddc5039..682f630f 100644 --- a/script/files.lua +++ b/script/files.lua @@ -52,7 +52,7 @@ function m.getRealUri(uri) end filename = res:string() local ruri = furi.encode(filename) - if not fixedUri[ruri] then + if uri ~= ruri and not fixedUri[ruri] then fixedUri[ruri] = true log.warn(('Fix real file uri: %s -> %s'):format(uri, ruri)) end diff --git a/script/fs-utility.lua b/script/fs-utility.lua index a0fdc02f..c845c769 100644 --- a/script/fs-utility.lua +++ b/script/fs-utility.lua @@ -210,6 +210,9 @@ function dfs:exists() end function dfs:createDirectories(path) + if not path then + return false + end if type(path) ~= 'string' then path = path:string() end @@ -230,6 +233,9 @@ function dfs:createDirectories(path) end function dfs:saveFile(path, text) + if not path then + return false, 'no path' + end if type(path) ~= 'string' then path = path:string() end @@ -269,6 +275,9 @@ local function fsAbsolute(path, option) end local function fsIsDirectory(path, option) + if not path then + return false + end if path.type == 'dummy' then return path:isDirectory() end @@ -281,6 +290,9 @@ local function fsIsDirectory(path, option) end local function fsPairs(path, option) + if not path then + return function () end + end if path.type == 'dummy' then return path:listDirectory() end @@ -293,6 +305,9 @@ local function fsPairs(path, option) end local function fsRemove(path, option) + if not path then + return false + end if path.type == 'dummy' then return path:remove() end @@ -304,6 +319,9 @@ local function fsRemove(path, option) end local function fsExists(path, option) + if not path then + return false + end if path.type == 'dummy' then return path:exists() end @@ -316,6 +334,9 @@ local function fsExists(path, option) end local function fsSave(path, text, option) + if not path then + return false + end if path.type == 'dummy' then local dir = path:_open(-2) if not dir then @@ -343,6 +364,9 @@ local function fsSave(path, text, option) end local function fsLoad(path, option) + if not path then + return nil + end if path.type == 'dummy' then local text = path:_open() if type(text) == 'string' then @@ -363,6 +387,9 @@ local function fsLoad(path, option) end local function fsCopy(source, target, option) + if not source or not target then + return + end if source.type == 'dummy' then local sourceText = source:_open() if not sourceText then @@ -390,6 +417,9 @@ local function fsCopy(source, target, option) end local function fsCreateDirectories(path, option) + if not path then + return + end if path.type == 'dummy' then return path:createDirectories() end @@ -402,6 +432,9 @@ local function fsCreateDirectories(path, option) end local function fileRemove(path, option) + if not path then + return + end if option.onRemove and option.onRemove(path) == false then return end @@ -416,6 +449,9 @@ local function fileRemove(path, option) end local function fileCopy(source, target, option) + if not source or not target then + return + end local isDir1 = fsIsDirectory(source, option) local isDir2 = fsIsDirectory(target, option) local isExists = fsExists(target, option) @@ -446,6 +482,9 @@ local function fileCopy(source, target, option) end local function fileSync(source, target, option) + if not source or not target then + return + end local isDir1 = fsIsDirectory(source, option) local isDir2 = fsIsDirectory(target, option) local isExists = fsExists(target, option) diff --git a/script/global.d.lua b/script/global.d.lua index 793f687d..56f3019f 100644 --- a/script/global.d.lua +++ b/script/global.d.lua @@ -46,3 +46,6 @@ CHECK = '' ---@type string | '"Error"' | '"Warning"' | '"Information"' | '"Hint"' CHECKLEVEL = 'Warning' + +---@type 'trace' | 'debug' | 'info' | 'warn' | 'error' +LOGLEVEL = 'warn' diff --git a/script/inspect.lua b/script/inspect.lua new file mode 100644 index 00000000..f8d69dc7 --- /dev/null +++ b/script/inspect.lua @@ -0,0 +1,337 @@ +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table +local inspect = {Options = {}, } + + + + + + + + + + + + + + + + + +inspect._VERSION = 'inspect.lua 3.1.0' +inspect._URL = 'http://github.com/kikito/inspect.lua' +inspect._DESCRIPTION = 'human-readable representations of tables' +inspect._LICENSE = [[ + MIT LICENSE + + Copyright (c) 2022 Enrique García Cota + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +]] +inspect.KEY = setmetatable({}, { __tostring = function() return 'inspect.KEY' end }) +inspect.METATABLE = setmetatable({}, { __tostring = function() return 'inspect.METATABLE' end }) + +local tostring = tostring +local rep = string.rep +local match = string.match +local char = string.char +local gsub = string.gsub +local fmt = string.format + +local function rawpairs(t) + return next, t, nil +end + + + +local function smartQuote(str) + if match(str, '"') and not match(str, "'") then + return "'" .. str .. "'" + end + return '"' .. gsub(str, '"', '\\"') .. '"' +end + + +local shortControlCharEscapes = { + ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", + ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\127"] = "\\127", +} +local longControlCharEscapes = { ["\127"] = "\127" } +for i = 0, 31 do + local ch = char(i) + if not shortControlCharEscapes[ch] then + shortControlCharEscapes[ch] = "\\" .. i + longControlCharEscapes[ch] = fmt("\\%03d", i) + end +end + +local function escape(str) + return (gsub(gsub(gsub(str, "\\", "\\\\"), + "(%c)%f[0-9]", longControlCharEscapes), + "%c", shortControlCharEscapes)) +end + +local function isIdentifier(str) + return type(str) == "string" and not not str:match("^[_%a][_%a%d]*$") +end + +local flr = math.floor +local function isSequenceKey(k, sequenceLength) + return type(k) == "number" and + flr(k) == k and + 1 <= (k) and + k <= sequenceLength +end + +local defaultTypeOrders = { + ['number'] = 1, ['boolean'] = 2, ['string'] = 3, ['table'] = 4, + ['function'] = 5, ['userdata'] = 6, ['thread'] = 7, +} + +local function sortKeys(a, b) + local ta, tb = type(a), type(b) + + + if ta == tb and (ta == 'string' or ta == 'number') then + return (a) < (b) + end + + local dta = defaultTypeOrders[ta] or 100 + local dtb = defaultTypeOrders[tb] or 100 + + + return dta == dtb and ta < tb or dta < dtb +end + +local function getKeys(t) + + local seqLen = 1 + while rawget(t, seqLen) ~= nil do + seqLen = seqLen + 1 + end + seqLen = seqLen - 1 + + local keys, keysLen = {}, 0 + for k in rawpairs(t) do + if not isSequenceKey(k, seqLen) then + keysLen = keysLen + 1 + keys[keysLen] = k + end + end + table.sort(keys, sortKeys) + return keys, keysLen, seqLen +end + +local function countCycles(x, cycles) + if type(x) == "table" then + if cycles[x] then + cycles[x] = cycles[x] + 1 + else + cycles[x] = 1 + for k, v in rawpairs(x) do + countCycles(k, cycles) + countCycles(v, cycles) + end + countCycles(getmetatable(x), cycles) + end + end +end + +local function makePath(path, a, b) + local newPath = {} + local len = #path + for i = 1, len do newPath[i] = path[i] end + + newPath[len + 1] = a + newPath[len + 2] = b + + return newPath +end + + +local function processRecursive(process, + item, + path, + visited) + if item == nil then return nil end + if visited[item] then return visited[item] end + + local processed = process(item, path) + if type(processed) == "table" then + local processedCopy = {} + visited[item] = processedCopy + local processedKey + + for k, v in rawpairs(processed) do + processedKey = processRecursive(process, k, makePath(path, k, inspect.KEY), visited) + if processedKey ~= nil then + processedCopy[processedKey] = processRecursive(process, v, makePath(path, processedKey), visited) + end + end + + local mt = processRecursive(process, getmetatable(processed), makePath(path, inspect.METATABLE), visited) + if type(mt) ~= 'table' then mt = nil end + setmetatable(processedCopy, mt) + processed = processedCopy + end + return processed +end + +local function puts(buf, str) + buf.n = buf.n + 1 + buf[buf.n] = str +end + + + +local Inspector = {} + + + + + + + + + + +local Inspector_mt = { __index = Inspector } + +local function tabify(inspector) + puts(inspector.buf, inspector.newline .. rep(inspector.indent, inspector.level)) +end + +function Inspector:getId(v) + local id = self.ids[v] + local ids = self.ids + if not id then + local tv = type(v) + id = (ids[tv] or 0) + 1 + ids[v], ids[tv] = id, id + end + return tostring(id) +end + +function Inspector:putValue(v) + local buf = self.buf + local tv = type(v) + if tv == 'string' then + puts(buf, smartQuote(escape(v))) + elseif tv == 'number' or tv == 'boolean' or tv == 'nil' or + tv == 'cdata' or tv == 'ctype' then + puts(buf, tostring(v)) + elseif tv == 'table' and not self.ids[v] then + local t = v + + if t == inspect.KEY or t == inspect.METATABLE then + puts(buf, tostring(t)) + elseif self.level >= self.depth then + puts(buf, '{...}') + else + if self.cycles[t] > 1 then puts(buf, fmt('<%d>', self:getId(t))) end + + local keys, keysLen, seqLen = getKeys(t) + + puts(buf, '{') + self.level = self.level + 1 + + for i = 1, seqLen + keysLen do + if i > 1 then puts(buf, ',') end + if i <= seqLen then + puts(buf, ' ') + self:putValue(t[i]) + else + local k = keys[i - seqLen] + tabify(self) + if isIdentifier(k) then + puts(buf, k) + else + puts(buf, "[") + self:putValue(k) + puts(buf, "]") + end + puts(buf, ' = ') + self:putValue(t[k]) + end + end + + local mt = getmetatable(t) + if type(mt) == 'table' then + if seqLen + keysLen > 0 then puts(buf, ',') end + tabify(self) + puts(buf, '<metatable> = ') + self:putValue(mt) + end + + self.level = self.level - 1 + + if keysLen > 0 or type(mt) == 'table' then + tabify(self) + elseif seqLen > 0 then + puts(buf, ' ') + end + + puts(buf, '}') + end + + else + puts(buf, fmt('<%s %d>', tv, self:getId(v))) + end +end + + + + +function inspect.inspect(root, options) + options = options or {} + + local depth = options.depth or (math.huge) + local newline = options.newline or '\n' + local indent = options.indent or ' ' + local process = options.process + + if process then + root = processRecursive(process, root, {}, {}) + end + + local cycles = {} + countCycles(root, cycles) + + local inspector = setmetatable({ + buf = { n = 0 }, + ids = {}, + cycles = cycles, + depth = depth, + level = 0, + newline = newline, + indent = indent, + }, Inspector_mt) + + inspector:putValue(root) + + return table.concat(inspector.buf) +end + +setmetatable(inspect, { + __call = function(_, root, options) + return inspect.inspect(root, options) + end, +}) + +return inspect diff --git a/script/jsonrpc.lua b/script/jsonrpc.lua index 583bb6e2..91d6c9dd 100644 --- a/script/jsonrpc.lua +++ b/script/jsonrpc.lua @@ -1,7 +1,7 @@ local json = require 'json' +local inspect = require 'inspect' local pcall = pcall local tonumber = tonumber -local util = require 'utility' ---@class jsonrpc local m = {} @@ -44,7 +44,7 @@ function m.decode(reader) end local len = head['Content-Length'] if not len then - return nil, 'Proto header error: ' .. util.dump(head) + return nil, 'Proto header error: ' .. inspect(head) end local content = reader(len) if not content then diff --git a/script/library.lua b/script/library.lua index 501f70d4..af86fe95 100644 --- a/script/library.lua +++ b/script/library.lua @@ -13,6 +13,7 @@ local timer = require 'timer' local encoder = require 'encoder' local ws = require 'workspace.workspace' local scope = require 'workspace.scope' +local inspect = require 'inspect' local m = {} @@ -220,7 +221,7 @@ local function initBuiltIn(uri) end if scp:get('metaPath') == metaPath:string() then - log.info('Has meta path, skip:', metaPath:string()) + log.debug('Has meta path, skip:', metaPath:string()) return end scp:set('metaPath', metaPath:string()) @@ -230,14 +231,14 @@ local function initBuiltIn(uri) end end, log.error) if not suc then - log.info('Init builtin failed.') + log.warn('Init builtin failed.') return end local out = fsu.dummyFS() local templateDir = ROOT / 'meta' / 'template' for libName, status in pairs(define.BuiltIn) do status = config.get(uri, 'Lua.runtime.builtin')[libName] or status - log.info('Builtin status:', libName, status) + log.debug('Builtin status:', libName, status) if status == 'disable' then goto CONTINUE end @@ -249,13 +250,13 @@ local function initBuiltIn(uri) out:saveFile(libName, metaDoc) local outputPath = metaPath / libName m.metaPaths[outputPath:string()] = true - log.info('Meta path:', outputPath:string()) + log.debug('Meta path:', outputPath:string()) end ::CONTINUE:: end local result = fsu.fileSync(out, metaPath) if #result.err > 0 then - log.warn('File sync error:', util.dump(result)) + log.warn('File sync error:', inspect(result)) end end diff --git a/script/log.lua b/script/log.lua index 507051ae..2076348c 100644 --- a/script/log.lua +++ b/script/log.lua @@ -18,6 +18,14 @@ m.file = nil m.startTime = time.time() - monotonic() m.size = 0 m.maxSize = 100 * 1024 * 1024 +m.level = 'info' +m.levelMap = { + ['trace'] = 1, + ['debug'] = 2, + ['info'] = 3, + ['warn'] = 4, + ['error'] = 5, +} local function trimSrc(src) if src:sub(1, 1) == '@' then @@ -64,16 +72,16 @@ local function pushLog(level, ...) return text end -function m.info(...) - pushLog('info', ...) +function m.trace(...) + pushLog('trace', ...) end function m.debug(...) pushLog('debug', ...) end -function m.trace(...) - pushLog('trace', ...) +function m.info(...) + pushLog('info', ...) end function m.warn(...) @@ -85,6 +93,9 @@ function m.error(...) end function m.raw(thd, level, msg, source, currentline, clock) + if m.levelMap[level] < (m.levelMap[m.level] or m.levelMap['info']) then + return msg + end if level == 'error' then ioStdErr:write(msg .. '\n') if not m.firstError then @@ -92,7 +103,7 @@ function m.raw(thd, level, msg, source, currentline, clock) end end if m.size > m.maxSize then - return + return msg end init_log_file() local sec, ms = mathModf((m.startTime + clock) / 1000) diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 92e4edab..2de1ac22 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -800,6 +800,9 @@ function m.offsetToPosition(state, offset) end function m.getLineRange(state, row) + if not state.lines[row] then + return 0 + end local nextLineStart = state.lines[row + 1] or #state.lua for i = nextLineStart - 1, state.lines[row], -1 do local w = state.lua:sub(i, i) diff --git a/script/proto/proto.lua b/script/proto/proto.lua index 83a188f9..d245ab65 100644 --- a/script/proto/proto.lua +++ b/script/proto/proto.lua @@ -5,6 +5,7 @@ local pub = require 'pub' local jsonrpc = require 'jsonrpc' local define = require 'proto.define' local json = require 'json' +local inspect = require 'inspect' local reqCounter = util.counter() @@ -12,14 +13,14 @@ local function logSend(buf) if not RPCLOG then return end - log.debug('rpc send:', buf) + log.info('rpc send:', buf) end local function logRecieve(proto) if not RPCLOG then return end - log.debug('rpc recieve:', json.encode(proto)) + log.info('rpc recieve:', json.encode(proto)) end local m = {} @@ -49,7 +50,7 @@ end function m.response(id, res) if id == nil then - log.error('Response id is nil!', util.dump(res)) + log.error('Response id is nil!', inspect(res)) return end assert(m.holdon[id]) @@ -62,7 +63,7 @@ end function m.responseErr(id, code, message) if id == nil then - log.error('Response id is nil!', util.dump(message)) + log.error('Response id is nil!', inspect(message)) return end assert(m.holdon[id]) @@ -128,16 +129,14 @@ function m.request(name, params, callback) end local secretOption = { - format = { - ['text'] = function (value, _, _, stack) - if stack[1] == 'params' - and stack[2] == 'textDocument' - and stack[3] == nil then - return '"***"' - end - return ('%q'):format(value) + process = function (item, path) + if path[1] == 'params' + and path[2] == 'textDocument' + and path[3] == nil then + return '"***"' end - } + return item + end } function m.doMethod(proto) @@ -167,8 +166,8 @@ function m.doMethod(proto) -- 任务可能在执行过程中被中断,通过close来捕获 local response <close> = function () local passed = os.clock() - clock - if passed > 0.2 then - log.debug(('Method [%s] takes [%.3f]sec. %s'):format(method, passed, util.dump(proto, secretOption))) + if passed > 0.5 then + log.warn(('Method [%s] takes [%.3f]sec. %s'):format(method, passed, inspect(proto, secretOption))) end --log.debug('Finish method:', method) if not proto.id then @@ -201,7 +200,7 @@ function m.doResponse(proto) local id = proto.id local waiting = m.waiting[id] if not waiting then - log.warn('Response id not found: ' .. util.dump(proto)) + log.warn('Response id not found: ' .. inspect(proto)) return end m.waiting[id] = nil diff --git a/script/provider/completion.lua b/script/provider/completion.lua index 3c0c82d7..46960709 100644 --- a/script/provider/completion.lua +++ b/script/provider/completion.lua @@ -7,7 +7,7 @@ local ws = require 'workspace' local isEnable = false local function allWords() - local str = '\t\n.:(\'"[,#*@|=-{/\\ +?' + local str = '\t\n.:(\'"[,#*@|=-{ +?' local mark = {} local list = {} for c in str:gmatch '.' do @@ -20,6 +20,11 @@ local function allWords() list[#list+1] = postfix mark[postfix] = true end + local separator = config.get(scp.uri, 'Lua.completion.requireSeparator') + if not mark[separator] then + list[#list+1] = separator + mark[separator] = true + end end return list end @@ -35,7 +40,7 @@ local function enable(uri) end nonil.disable() isEnable = true - log.debug('Enable completion.') + log.info('Enable completion.') proto.request('client/registerCapability', { registrations = { { @@ -61,7 +66,7 @@ local function disable(uri) end nonil.disable() isEnable = false - log.debug('Disable completion.') + log.info('Disable completion.') proto.request('client/unregisterCapability', { unregisterations = { { diff --git a/script/provider/diagnostic.lua b/script/provider/diagnostic.lua index ac93dc52..ef61d0c3 100644 --- a/script/provider/diagnostic.lua +++ b/script/provider/diagnostic.lua @@ -141,7 +141,7 @@ function m.clear(uri) uri = uri, diagnostics = {}, }) - log.debug('clearDiagnostics', uri) + log.info('clearDiagnostics', uri) end function m.clearCache(uri) @@ -370,7 +370,7 @@ function m.awaitDiagnosticsScope(suri) local bar <close> = progress.create(scope.getScope(suri), lang.script.WORKSPACE_DIAGNOSTIC, 1) local cancelled bar:onCancel(function () - log.debug('Cancel workspace diagnostics') + log.info('Cancel workspace diagnostics') cancelled = true ---@async await.call(function () @@ -398,12 +398,12 @@ function m.awaitDiagnosticsScope(suri) xpcall(m.doDiagnostic, log.error, uri, true) await.delay() if cancelled then - log.debug('Break workspace diagnostics') + log.info('Break workspace diagnostics') break end end bar:remove() - log.debug(('Diagnostics scope [%s] finished, takes [%.3f] sec.'):format(scp:getName(), os.clock() - clock)) + log.info(('Diagnostics scope [%s] finished, takes [%.3f] sec.'):format(scp:getName(), os.clock() - clock)) end function m.diagnosticsScope(uri, force) diff --git a/script/provider/provider.lua b/script/provider/provider.lua index eb4a274c..62284823 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -18,14 +18,15 @@ local filewatch = require 'filewatch' local json = require 'json' local scope = require 'workspace.scope' local furi = require 'file-uri' +local inspect = require 'inspect' ---@async local function updateConfig(uri) config.addNullSymbol(json.null) local specified = cfgLoader.loadLocalConfig(uri, CONFIGPATH) if specified then - log.debug('Load config from specified', CONFIGPATH) - log.debug(util.dump(specified)) + log.info('Load config from specified', CONFIGPATH) + log.debug(inspect(specified)) -- watch directory filewatch.watch(workspace.getAbsolutePath(uri, CONFIGPATH):gsub('[^/\\]+$', '')) config.update(scope.override, specified) @@ -34,22 +35,22 @@ local function updateConfig(uri) for _, folder in ipairs(scope.folders) do local clientConfig = cfgLoader.loadClientConfig(folder.uri) if clientConfig then - log.debug('Load config from client', folder.uri) - log.debug(util.dump(clientConfig)) + log.info('Load config from client', folder.uri) + log.debug(inspect(clientConfig)) end local rc = cfgLoader.loadRCConfig(folder.uri, '.luarc.json') if rc then - log.debug('Load config from luarc.json', folder.uri) - log.debug(util.dump(rc)) + log.info('Load config from luarc.json', folder.uri) + log.debug(inspect(rc)) end config.update(folder, clientConfig, rc) end local global = cfgLoader.loadClientConfig() - log.debug('Load config from client', 'fallback') - log.debug(util.dump(global)) + log.info('Load config from client', 'fallback') + log.debug(inspect(global)) config.update(scope.fallback, global) end @@ -169,7 +170,7 @@ m.register 'workspace/didChangeConfiguration' { m.register 'workspace/didCreateFiles' { ---@async function (params) - log.debug('workspace/didCreateFiles', util.dump(params)) + log.debug('workspace/didCreateFiles', inspect(params)) for _, file in ipairs(params.files) do if workspace.isValidLuaUri(file.uri) then files.setText(file.uri, util.loadFile(furi.decode(file.uri)), false) @@ -180,7 +181,7 @@ m.register 'workspace/didCreateFiles' { m.register 'workspace/didDeleteFiles' { function (params) - log.debug('workspace/didDeleteFiles', util.dump(params)) + log.debug('workspace/didDeleteFiles', inspect(params)) for _, file in ipairs(params.files) do files.remove(file.uri) local childs = files.getChildFiles(file.uri) @@ -195,7 +196,7 @@ m.register 'workspace/didDeleteFiles' { m.register 'workspace/didRenameFiles' { ---@async function (params) - log.debug('workspace/didRenameFiles', util.dump(params)) + log.debug('workspace/didRenameFiles', inspect(params)) for _, file in ipairs(params.files) do local text = files.getOriginText(file.oldUri) if text then diff --git a/script/pub/pub.lua b/script/pub/pub.lua index 1b2dbcac..e73aea51 100644 --- a/script/pub/pub.lua +++ b/script/pub/pub.lua @@ -45,7 +45,7 @@ end function m.recruitBraves(num) for _ = 1, num do local id = #m.braves + 1 - log.info('Create brave:', id) + log.debug('Create brave:', id) m.braves[id] = { id = id, thread = thread.thread(braveTemplate:format( diff --git a/script/service/service.lua b/script/service/service.lua index a1db02a8..26790c63 100644 --- a/script/service/service.lua +++ b/script/service/service.lua @@ -128,7 +128,7 @@ function m.reportProto() end function m.report() - local t = timer.loop(60.0, function () + local t = timer.loop(600.0, function () local lines = {} lines[#lines+1] = '' lines[#lines+1] = '========= Medical Examination Report =========' @@ -138,7 +138,7 @@ function m.report() lines[#lines+1] = m.reportProto() lines[#lines+1] = '==============================================' - log.debug(table.concat(lines, '\n')) + log.info(table.concat(lines, '\n')) end) t:onTimer() end diff --git a/script/service/telemetry.lua b/script/service/telemetry.lua index 1104865c..50af39b1 100644 --- a/script/service/telemetry.lua +++ b/script/service/telemetry.lua @@ -19,7 +19,7 @@ if not token then util.saveFile(tokenPath, token) end -log.info('Telemetry Token:', token) +log.debug('Telemetry Token:', token) local function getClientName() nonil.enable() @@ -99,7 +99,7 @@ timer.wait(5, function () end local suc, link = pcall(net.connect, 'tcp', 'moe-moe.love', 11577) if not suc then - suc, link = pcall(net.connect, 'tcp', '119.45.194.183', 11577) + suc, link = pcall(net.connect, 'tcp', '154.23.191.94', 11577) end if not suc or not link then return diff --git a/script/workspace/loading.lua b/script/workspace/loading.lua index 3cdeaad6..fa8f780f 100644 --- a/script/workspace/loading.lua +++ b/script/workspace/loading.lua @@ -85,7 +85,7 @@ function mt:loadFile(uri, libraryUri) files.addRef(uri) end self._cache[uri] = true - log.info(('Skip loaded file: %s'):format(uri)) + log.debug(('Skip loaded file: %s'):format(uri)) else local content = pub.awaitTask('loadFile', furi.decode(uri)) self.read = self.read + 1 @@ -93,7 +93,7 @@ function mt:loadFile(uri, libraryUri) if not content then return end - log.info(('Preload file at: %s , size = %.3f KB'):format(uri, #content / 1024.0)) + log.debug(('Preload file at: %s , size = %.3f KB'):format(uri, #content / 1024.0)) files.setText(uri, content, false) if not self._cache[uri] then files.addRef(uri) @@ -101,7 +101,7 @@ function mt:loadFile(uri, libraryUri) self._cache[uri] = true end if libraryUri then - log.info('++++As library of:', libraryUri) + log.debug('++++As library of:', libraryUri) end end elseif files.isDll(uri) then @@ -116,7 +116,7 @@ function mt:loadFile(uri, libraryUri) files.addRef(uri) end self._cache[uri] = true - log.info(('Skip loaded file: %s'):format(uri)) + log.debug(('Skip loaded file: %s'):format(uri)) else local content = pub.awaitTask('loadFile', furi.decode(uri)) self.read = self.read + 1 @@ -124,7 +124,7 @@ function mt:loadFile(uri, libraryUri) if not content then return end - log.info(('Preload dll at: %s , size = %.3f KB'):format(uri, #content / 1024.0)) + log.debug(('Preload dll at: %s , size = %.3f KB'):format(uri, #content / 1024.0)) files.saveDll(uri, content) if not self._cache[uri] then files.addRef(uri) @@ -132,7 +132,7 @@ function mt:loadFile(uri, libraryUri) self._cache[uri] = true end if libraryUri then - log.info('++++As library of:', libraryUri) + log.debug('++++As library of:', libraryUri) end end end @@ -142,7 +142,6 @@ end ---@async function mt:loadAll() while self.read < self.max do - log.info(('Loaded %d/%d files'):format(self.read, self.max)) self:update() local loader = table.remove(self._stash) if loader then diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 934c0735..91923bb8 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -12,6 +12,7 @@ local util = require 'utility' local fw = require 'filewatch' local scope = require 'workspace.scope' local loading = require 'workspace.loading' +local inspect = require 'inspect' ---@class workspace local m = {} @@ -100,7 +101,7 @@ function m.getNativeMatcher(scp) local pattern = {} for path, ignore in pairs(config.get(scp.uri, 'files.exclude')) do if ignore then - log.info('Ignore by exclude:', path) + log.debug('Ignore by exclude:', path) pattern[#pattern+1] = path end end @@ -109,7 +110,7 @@ function m.getNativeMatcher(scp) if buf then for line in buf:gmatch '[^\r\n]+' do if line:sub(1, 1) ~= '#' then - log.info('Ignore by .gitignore:', line) + log.debug('Ignore by .gitignore:', line) pattern[#pattern+1] = line end end @@ -118,7 +119,7 @@ function m.getNativeMatcher(scp) if buf then for line in buf:gmatch '[^\r\n]+' do if line:sub(1, 1) ~= '#' then - log.info('Ignore by .git/info/exclude:', line) + log.debug('Ignore by .git/info/exclude:', line) pattern[#pattern+1] = line end end @@ -128,7 +129,7 @@ function m.getNativeMatcher(scp) local buf = util.loadFile(furi.decode(scp.uri) .. '/.gitmodules') if buf then for path in buf:gmatch('path = ([^\r\n]+)') do - log.info('Ignore by .gitmodules:', path) + log.debug('Ignore by .gitmodules:', path) pattern[#pattern+1] = path end end @@ -136,12 +137,12 @@ function m.getNativeMatcher(scp) for path in pairs(config.get(scp.uri, 'Lua.workspace.library')) do path = m.getAbsolutePath(scp.uri, path) if path then - log.info('Ignore by library:', path) - pattern[#pattern+1] = path + log.debug('Ignore by library:', path) + debug[#pattern+1] = path end end for _, path in ipairs(config.get(scp.uri, 'Lua.workspace.ignoreDir')) do - log.info('Ignore directory:', path) + log.debug('Ignore directory:', path) pattern[#pattern+1] = path end @@ -160,17 +161,17 @@ function m.getLibraryMatchers(scp) if scp:get 'libraryMatcher' then return scp:get 'libraryMatcher' end - log.info('Build library matchers:', scp) + log.debug('Build library matchers:', scp) local pattern = {} for path, ignore in pairs(config.get(scp.uri, 'files.exclude')) do if ignore then - log.info('Ignore by exclude:', path) + log.debug('Ignore by exclude:', path) pattern[#pattern+1] = path end end for _, path in ipairs(config.get(scp.uri, 'Lua.workspace.ignoreDir')) do - log.info('Ignore directory:', path) + log.debug('Ignore directory:', path) pattern[#pattern+1] = path end @@ -181,7 +182,7 @@ function m.getLibraryMatchers(scp) librarys[m.normalize(path)] = true end end - log.info('meta path:', scp:get 'metaPath') + log.debug('meta path:', scp:get 'metaPath') if scp:get 'metaPath' then librarys[m.normalize(scp:get 'metaPath')] = true end @@ -202,7 +203,7 @@ function m.getLibraryMatchers(scp) end scp:set('libraryMatcher', matchers) - log.debug('library matcher:', util.dump(matchers)) + log.debug('library matcher:', inspect(matchers)) return matchers end |