diff options
Diffstat (limited to 'script')
-rw-r--r-- | script/client.lua | 6 | ||||
-rw-r--r-- | script/config/config.lua | 7 | ||||
-rw-r--r-- | script/core/formatting.lua | 8 | ||||
-rw-r--r-- | script/core/rangeformatting.lua | 4 | ||||
-rw-r--r-- | script/global.d.lua | 6 | ||||
-rw-r--r-- | script/parser/guide.lua | 18 | ||||
-rw-r--r-- | script/provider/capability.lua | 91 | ||||
-rw-r--r-- | script/provider/formatting.lua | 14 | ||||
-rw-r--r-- | script/provider/provider.lua | 117 | ||||
-rw-r--r-- | script/service/telemetry.lua | 29 | ||||
-rw-r--r-- | script/vm/getDocs.lua | 3 | ||||
-rw-r--r-- | script/workspace/require-path.lua | 3 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 1 |
13 files changed, 215 insertions, 92 deletions
diff --git a/script/client.lua b/script/client.lua index 0388ff39..f265cf9a 100644 --- a/script/client.lua +++ b/script/client.lua @@ -197,7 +197,7 @@ end ---@field prop? string ---@field value any ---@field action '"add"'|'"set"'|'"prop"' ----@field isGlobal? boolean +---@field global? boolean ---@field uri? uri ---@param cfg table @@ -298,7 +298,7 @@ local function tryModifyClientGlobal(finalChanges) local changes = {} for i = #finalChanges, 1, -1 do local change = finalChanges[i] - if change.isGlobal then + if change.global then changes[#changes+1] = change finalChanges[i] = finalChanges[#finalChanges] finalChanges[#finalChanges] = nil @@ -341,7 +341,7 @@ function m.setConfig(changes, onlyMemory) xpcall(function () local ws = require 'workspace' if #ws.folders == 0 then - tryModifyClient(finalChanges) + tryModifyClient(nil, finalChanges) return end tryModifyClientGlobal(finalChanges) diff --git a/script/config/config.lua b/script/config/config.lua index a53b77b5..8c06278f 100644 --- a/script/config/config.lua +++ b/script/config/config.lua @@ -129,7 +129,7 @@ end, function (self, subkey, subvalue, sep) self.sep = sep end) -register('Or', {}, function (self, value) +register('Or', nil, function (self, value) for _, sub in ipairs(self.subs) do if sub:checker(value) then return true @@ -208,11 +208,14 @@ local Template = { ['Lua.hint.arrayIndex'] = Type.Boolean >> 'Auto', ['Lua.window.statusBar'] = Type.Boolean >> true, ['Lua.window.progressBar'] = Type.Boolean >> true, + ['Lua.format.enable'] = Type.Boolean >> true, + ['Lua.format.defaultConfig'] = Type.Hash(Type.String, Type.String) + >> {}, ['Lua.IntelliSense.traceLocalSet'] = Type.Boolean >> false, ['Lua.IntelliSense.traceReturn'] = Type.Boolean >> false, ['Lua.IntelliSense.traceBeSetted'] = Type.Boolean >> false, ['Lua.IntelliSense.traceFieldInject'] = Type.Boolean >> false, - ['Lua.telemetry.enable'] = Type.Or(Type.Boolean >> false, Type.Nil), + ['Lua.telemetry.enable'] = Type.Or(Type.Boolean >> false, Type.Nil) >> nil, ['files.associations'] = Type.Hash(Type.String, Type.String), ['files.exclude'] = Type.Hash(Type.String, Type.Boolean), ['editor.semanticHighlighting.enabled'] = Type.Or(Type.Boolean, Type.String), diff --git a/script/core/formatting.lua b/script/core/formatting.lua index 6c57b8c2..49da6861 100644 --- a/script/core/formatting.lua +++ b/script/core/formatting.lua @@ -1,11 +1,11 @@ local codeFormat = require("code_format") -local files = require("files") -local log = require("log") +local files = require("files") +local log = require("log") -return function(uri) +return function(uri, options) local text = files.getText(uri) local ast = files.getState(uri) - local status, formattedText = codeFormat.format(uri, text) + local status, formattedText = codeFormat.format(uri, text, options) if not status then if formattedText ~= nil then diff --git a/script/core/rangeformatting.lua b/script/core/rangeformatting.lua index de9516c1..ccf2d21f 100644 --- a/script/core/rangeformatting.lua +++ b/script/core/rangeformatting.lua @@ -3,10 +3,10 @@ local files = require("files") local log = require("log") local converter = require("proto.converter") -return function(uri, range) +return function(uri, range, options) local text = files.getText(uri) local status, formattedText, startLine, endLine = codeFormat.range_format( - uri, text, range.start.line, range["end"].line) + uri, text, range.start.line, range["end"].line, options) if not status then if formattedText ~= nil then diff --git a/script/global.d.lua b/script/global.d.lua index 0435293e..ad4fb364 100644 --- a/script/global.d.lua +++ b/script/global.d.lua @@ -33,3 +33,9 @@ FOOTPRINT = false ---trace rpc, use command line: --rpclog=true ---@type boolean RPCLOG = false + +--enable preview features. +-- +--the current version is `formatting` +---@type boolean +PREVIEW = false diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 638c08bf..10b2badb 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -256,7 +256,7 @@ end ---@param obj parser.object ---@return parser.object function m.getParentFunction(obj) - for _ = 1, 1000 do + for _ = 1, 10000 do obj = obj.parent if not obj then break @@ -273,7 +273,7 @@ end ---@param obj parser.object ---@return parser.object function m.getBlock(obj) - for _ = 1, 1000 do + for _ = 1, 10000 do if not obj then return nil end @@ -302,7 +302,7 @@ end ---@param obj parser.object ---@return parser.object function m.getParentBlock(obj) - for _ = 1, 1000 do + for _ = 1, 10000 do obj = obj.parent if not obj then return nil @@ -319,7 +319,7 @@ end ---@param obj parser.object ---@return parser.object function m.getBreakBlock(obj) - for _ = 1, 1000 do + for _ = 1, 10000 do obj = obj.parent if not obj then return nil @@ -339,7 +339,7 @@ end ---@param obj parser.object ---@return parser.object function m.getDocState(obj) - for _ = 1, 1000 do + for _ = 1, 10000 do local parent = obj.parent if not parent then return obj @@ -356,7 +356,7 @@ end ---@param obj parser.object ---@return parser.object function m.getParentType(obj, want) - for _ = 1, 1000 do + for _ = 1, 10000 do obj = obj.parent if not obj then return nil @@ -376,7 +376,7 @@ function m.getRoot(obj) if source._root then return source._root end - for _ = 1, 1000 do + for _ = 1, 10000 do if obj.type == 'main' then source._root = obj return obj @@ -445,7 +445,7 @@ end ---@param pos integer {comment = '可见位置'} function m.getLocal(block, name, pos) block = m.getBlock(block) - for _ = 1, 1000 do + for _ = 1, 10000 do if not block then return nil end @@ -497,7 +497,7 @@ end ---@param name string {comment = '标签名'} function m.getLabel(block, name) block = m.getBlock(block) - for _ = 1, 1000 do + for _ = 1, 10000 do if not block then return nil end diff --git a/script/provider/capability.lua b/script/provider/capability.lua index e012f4b4..ba5690bf 100644 --- a/script/provider/capability.lua +++ b/script/provider/capability.lua @@ -7,17 +7,6 @@ local define = require 'proto.define' require 'provider.semantic-tokens' require 'provider.formatting' -local function toArray(map) - local array = {} - for k in pairs(map) do - array[#array+1] = k - end - table.sort(array, function (a, b) - return map[a] < map[b] - end) - return array -end - local m = {} local function testFileEvents(initer) @@ -58,8 +47,27 @@ local function testFileEvents(initer) } end -function m.getIniter() - local initer = { +m.fillings = {} + +local function mergeFillings(provider) + for _, filling in ipairs(m.fillings) do + for k, v in pairs(filling) do + if type(v) == 'table' then + if not provider[k] then + provider[k] = {} + end + for kk, vv in pairs(v) do + provider[k][kk] = vv + end + else + provider[k] = v + end + end + end +end + +function m.getProvider() + local provider = { offsetEncoding = client.getOffsetEncoding(), -- 文本同步方式 textDocumentSync = { @@ -68,53 +76,6 @@ function m.getIniter() -- 文本增量更新 change = 2, }, - - hoverProvider = true, - definitionProvider = true, - typeDefinitionProvider = true, - referencesProvider = true, - renameProvider = { - prepareProvider = true, - }, - documentSymbolProvider = true, - workspaceSymbolProvider = true, - documentHighlightProvider = true, - codeActionProvider = { - codeActionKinds = { - '', - 'quickfix', - 'refactor.rewrite', - 'refactor.extract', - }, - resolveProvider = false, - }, - signatureHelpProvider = { - triggerCharacters = { '(', ',' }, - }, - executeCommandProvider = { - commands = { - 'lua.removeSpace', - 'lua.solve', - 'lua.jsonToLua', - 'lua.setConfig', - 'lua.autoRequire', - }, - }, - foldingRangeProvider = true, - documentOnTypeFormattingProvider = { - firstTriggerCharacter = '\n', - moreTriggerCharacter = nil, -- string[] - }, - semanticTokensProvider = { - legend = { - tokenTypes = toArray(define.TokenTypes), - tokenModifiers = toArray(define.TokenModifiers), - }, - range = true, - full = false, - }, - documentFormattingProvider = true, - documentRangeFormattingProvider = true } --testFileEvents() @@ -122,14 +83,20 @@ function m.getIniter() nonil.enable() if not client.info.capabilities.textDocument.completion.dynamicRegistration or not client.info.capabilities.workspace.configuration then - initer.completionProvider = { + provider.completionProvider = { resolveProvider = true, triggerCharacters = completion.allWords(), } end nonil.disable() - return initer + mergeFillings(provider) + + return provider +end + +function m.filling(t) + m.fillings[#m.fillings+1] = t end return m diff --git a/script/provider/formatting.lua b/script/provider/formatting.lua index 9392259e..f73b0dc0 100644 --- a/script/provider/formatting.lua +++ b/script/provider/formatting.lua @@ -5,6 +5,7 @@ local fs = require 'bee.filesystem' local fw = require 'filewatch' local util = require 'utility' local diagnostics = require 'provider.diagnostic' +local config = require 'config' local loadedUris = {} @@ -31,10 +32,23 @@ fw.event(function (ev, path) end end) +config.watch(function (uri, key, value) + if key == "Lua.format.defaultConfig" then + codeFormat.set_default_config(value) + end +end) + local m = {} +m.loadedDefaultConfig = false + ---@param uri uri function m.updateConfig(uri) + if not m.loadedDefaultConfig then + m.loadedDefaultConfig = true + codeFormat.set_default_config(config.get(uri, 'Lua.format.defaultConfig')) + end + local currentUri = uri while true do currentUri = currentUri:match('^(.+)/[^/]*$') diff --git a/script/provider/provider.lua b/script/provider/provider.lua index df226c3f..3e8fec24 100644 --- a/script/provider/provider.lua +++ b/script/provider/provider.lua @@ -61,6 +61,12 @@ m.attributes = {} function m.register(method) return function (attrs) m.attributes[method] = attrs + if attrs.preview and not PREVIEW then + return + end + if attrs.capability then + cap.filling(attrs.capability) + end proto.on(method, attrs[1]) end end @@ -101,7 +107,7 @@ m.register 'initialize' { end return { - capabilities = cap.getIniter(), + capabilities = cap.getProvider(), serverInfo = { name = 'sumneko.lua', }, @@ -261,6 +267,9 @@ m.register 'textDocument/didChange' { } m.register 'textDocument/hover' { + capability = { + hoverProvider = true, + }, abortByFileUpdate = true, ---@async function (params) @@ -299,6 +308,9 @@ m.register 'textDocument/hover' { } m.register 'textDocument/definition' { + capability = { + definitionProvider = true, + }, abortByFileUpdate = true, ---@async function (params) @@ -338,6 +350,9 @@ m.register 'textDocument/definition' { } m.register 'textDocument/typeDefinition' { + capability = { + typeDefinitionProvider = true, + }, abortByFileUpdate = true, ---@async function (params) @@ -377,6 +392,9 @@ m.register 'textDocument/typeDefinition' { } m.register 'textDocument/references' { + capability = { + referencesProvider = true, + }, abortByFileUpdate = true, ---@async function (params) @@ -404,10 +422,14 @@ m.register 'textDocument/references' { } m.register 'textDocument/documentHighlight' { - abortByFileUpdate = true, + capability = { + documentHighlightProvider = true, + }, + ---@async function (params) local core = require 'core.highlight' local uri = files.getRealUri(params.textDocument.uri) + workspace.awaitReady(uri) if not files.exists(uri) then return nil end @@ -428,6 +450,11 @@ m.register 'textDocument/documentHighlight' { } m.register 'textDocument/rename' { + capability = { + renameProvider = { + prepareProvider = true, + }, + }, abortByFileUpdate = true, ---@async function (params) @@ -631,6 +658,11 @@ m.register 'completionItem/resolve' { } m.register 'textDocument/signatureHelp' { + capability = { + signatureHelpProvider = { + triggerCharacters = { '(', ',' }, + }, + }, abortByFileUpdate = true, ---@async function (params) @@ -677,6 +709,9 @@ m.register 'textDocument/signatureHelp' { } m.register 'textDocument/documentSymbol' { + capability = { + documentSymbolProvider = true, + }, abortByFileUpdate = true, ---@async function (params) @@ -723,6 +758,17 @@ m.register 'textDocument/documentSymbol' { } m.register 'textDocument/codeAction' { + capability = { + codeActionProvider = { + codeActionKinds = { + '', + 'quickfix', + 'refactor.rewrite', + 'refactor.extract', + }, + resolveProvider = false, + }, + }, abortByFileUpdate = true, function (params) local core = require 'core.code-action' @@ -757,6 +803,17 @@ m.register 'textDocument/codeAction' { } m.register 'workspace/executeCommand' { + capability = { + executeCommandProvider = { + commands = { + 'lua.removeSpace', + 'lua.solve', + 'lua.jsonToLua', + 'lua.setConfig', + 'lua.autoRequire', + }, + }, + }, ---@async function (params) local command = params.command:gsub(':.+', '') @@ -780,6 +837,9 @@ m.register 'workspace/executeCommand' { } m.register 'workspace/symbol' { + capability = { + workspaceSymbolProvider = true, + }, abortByFileUpdate = true, ---@async function (params) @@ -826,7 +886,29 @@ m.register 'textDocument/semanticTokens/full' { end } +local function toArray(map) + local array = {} + for k in pairs(map) do + array[#array+1] = k + end + table.sort(array, function (a, b) + return map[a] < map[b] + end) + return array +end + + m.register 'textDocument/semanticTokens/range' { + capability = { + semanticTokensProvider = { + legend = { + tokenTypes = toArray(define.TokenTypes), + tokenModifiers = toArray(define.TokenModifiers), + }, + range = true, + full = false, + }, + }, ---@async function (params) log.debug('textDocument/semanticTokens/range') @@ -843,11 +925,14 @@ m.register 'textDocument/semanticTokens/range' { } m.register 'textDocument/foldingRange' { - abortByFileUpdate = true, + capability = { + foldingRangeProvider = true, + }, ---@async function (params) local core = require 'core.folding' local uri = files.getRealUri(params.textDocument.uri) + workspace.awaitReady(uri) if not files.exists(uri) then return nil end @@ -915,6 +1000,10 @@ m.register '$/status/click' { } m.register 'textDocument/formatting' { + capability = { + documentFormattingProvider = true, + }, + preview = true, ---@async function(params) local uri = files.getRealUri(params.textDocument.uri) @@ -923,11 +1012,15 @@ m.register 'textDocument/formatting' { return nil end + if not config.get(uri, 'Lua.format.enable') then + return nil + end + local pformatting = require 'provider.formatting' pformatting.updateConfig(uri) local core = require 'core.formatting' - local edits = core(uri) + local edits = core(uri, params.options) if not edits or #edits == 0 then return nil end @@ -945,6 +1038,10 @@ m.register 'textDocument/formatting' { } m.register 'textDocument/rangeFormatting' { + capability = { + documentRangeFormattingProvider = true, + }, + preview = true, ---@async function(params) local uri = files.getRealUri(params.textDocument.uri) @@ -953,11 +1050,15 @@ m.register 'textDocument/rangeFormatting' { return nil end + if not config.get(uri, 'Lua.format.enable') then + return nil + end + local pformatting = require 'provider.formatting' pformatting.updateConfig(uri) local core = require 'core.rangeformatting' - local edits = core(uri, params.range) + local edits = core(uri, params.range, params.options) if not edits or #edits == 0 then return nil end @@ -975,6 +1076,12 @@ m.register 'textDocument/rangeFormatting' { } m.register 'textDocument/onTypeFormatting' { + capability = { + documentOnTypeFormattingProvider = { + firstTriggerCharacter = '\n', + moreTriggerCharacter = nil, -- string[] + }, + }, abortByFileUpdate = true, ---@async function (params) diff --git a/script/service/telemetry.lua b/script/service/telemetry.lua index 94335fb9..2d956ed0 100644 --- a/script/service/telemetry.lua +++ b/script/service/telemetry.lua @@ -59,12 +59,29 @@ local function pushVersion(link) )) end +local function occlusionPath(str) + return str:gsub('(%s*)([^:"\'\r\n]+)', function (left, chunk) + if not chunk:find '[/\\]' then + return + end + local newStr, count = chunk:gsub('.+([/\\]script[/\\])', '***%1') + if count > 0 then + return left .. newStr + elseif chunk:sub(1, 1) == '\\' + or chunk:sub(1, 1) == '/' + or chunk:sub(1, 3) == '...' then + return left .. '***' + end + end) +end + local function pushErrorLog(link) - if not log.firstError then + local err = log.firstError + if not err then return end - local err = log.firstError log.firstError = nil + err = occlusionPath(err) send(link, string.pack('zzzz' , 'error' , token @@ -78,7 +95,7 @@ local isValid = false timer.wait(5, function () timer.loop(300, function () - if not isValid then + if isValid ~= true then return end local suc, link = pcall(net.connect, 'tcp', 'moe-moe.love', 11577) @@ -97,7 +114,7 @@ timer.wait(5, function () end end)() timer.loop(1, function () - if not isValid then + if isValid ~= true then return end net.update() @@ -107,7 +124,9 @@ end) local m = {} function m.updateConfig() - isValid = config.get(nil, 'Lua.telemetry.enable') + local enable = config.get(nil, 'Lua.telemetry.enable') + assert(enable == true or enable == false or enable == nil) + isValid = enable if isValid == false then return end diff --git a/script/vm/getDocs.lua b/script/vm/getDocs.lua index d13878e6..4204d785 100644 --- a/script/vm/getDocs.lua +++ b/script/vm/getDocs.lua @@ -10,6 +10,9 @@ local define = require 'proto.define' ---@param name? string ---@return parser.object[] function vm.getDocDefines(uri, name) + if type(name) ~= 'string' then + return {} + end local cache = vm.getCache 'getDocDefines' if cache[name] then return cache[name] diff --git a/script/workspace/require-path.lua b/script/workspace/require-path.lua index f0a75eb1..56c94424 100644 --- a/script/workspace/require-path.lua +++ b/script/workspace/require-path.lua @@ -148,6 +148,9 @@ end local function removeVisiblePath(uri) local path = furi.decode(uri) path = workspace.normalize(path) + if not path then + return + end for _, scp in ipairs(workspace.folders) do scp:get('visiblePath')[path] = nil ---@type collector diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index cb8bd68b..80df7d9c 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -234,6 +234,7 @@ end ---@async function m.awaitLoadFile(uri) + m.awaitReady(uri) local scp = scope.getScope(uri) local ld <close> = loading.create(scp) local native = m.getNativeMatcher(scp) |