From 6b04f490fc0c45a0e3a70f3ca27c976f70b620c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 17 Oct 2022 11:41:20 +0800 Subject: update workspace-symbol --- script/core/workspace-symbol.lua | 31 +++++++++++++++++++++++++++++++ script/vm/global.lua | 28 +++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) (limited to 'script') diff --git a/script/core/workspace-symbol.lua b/script/core/workspace-symbol.lua index 9dd768db..9d52572b 100644 --- a/script/core/workspace-symbol.lua +++ b/script/core/workspace-symbol.lua @@ -3,6 +3,7 @@ local guide = require 'parser.guide' local matchKey = require 'core.matchkey' local define = require 'proto.define' local await = require 'await' +local vm = require 'vm' local function buildSource(uri, source, key, results) if source.type == 'local' @@ -54,10 +55,40 @@ local function searchFile(uri, key, results) end) end +---@async +---@param key string +---@param results table[] +local function searchGlobalAndClass(key, results) + for _, global in pairs(vm.getAllGlobals()) do + local name = global:getCodeName() + if matchKey(key, name) then + for _, set in ipairs(global:getAllSets()) do + local kind + if set.type == 'doc.class' then + kind = define.SymbolKind.Class + elseif set.type == 'doc.alias' then + kind = define.SymbolKind.Namespace + else + kind = define.SymbolKind.Variable + end + results[#results+1] = { + name = name, + kind = kind, + uri = guide.getUri(set), + range = { set.start, set.finish }, + } + end + await.delay() + end + end +end + ---@async return function (key) local results = {} + searchGlobalAndClass(key, results) + for uri in files.eachFile() do searchFile(uri, key, results) if #results > 1000 then diff --git a/script/vm/global.lua b/script/vm/global.lua index 590264bd..29474b57 100644 --- a/script/vm/global.lua +++ b/script/vm/global.lua @@ -42,7 +42,7 @@ function mt:addGet(uri, source) self.getsCache = nil end ----@param suri uri +---@param suri uri ---@return parser.object[] function mt:getSets(suri) if not self.setsCache then @@ -72,6 +72,27 @@ function mt:getSets(suri) return cache end +---@return parser.object[] +function mt:getAllSets() + if not self.setsCache then + self.setsCache = {} + end + local cache = self.setsCache['*'] + if cache then + return cache + end + cache = {} + self.setsCache['*'] = cache + for _, link in pairs(self.links) do + if link.sets then + for _, source in ipairs(link.sets) do + cache[#cache+1] = source + end + end + end + return cache +end + ---@return parser.object[] function mt:getGets(suri) if not self.getsCache then @@ -467,6 +488,11 @@ function vm.getGlobals(cate) return globals end +---@return table +function vm.getAllGlobals() + return allGlobals +end + ---@param suri uri ---@param cate vm.global.cate ---@return parser.object[] -- cgit v1.2.3