diff options
-rw-r--r-- | script/core/guide.lua | 83 | ||||
-rw-r--r-- | script/core/linker.lua | 94 | ||||
-rw-r--r-- | script/files.lua | 2 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 2 | ||||
-rw-r--r-- | script/proto/define.lua | 3 | ||||
-rw-r--r-- | script/vm/getDocs.lua | 90 | ||||
-rw-r--r-- | script/vm/getGlobals.lua | 2 | ||||
-rw-r--r-- | script/vm/guideInterface.lua | 4 | ||||
-rw-r--r-- | script/vm/vm.lua | 2 |
9 files changed, 168 insertions, 114 deletions
diff --git a/script/core/guide.lua b/script/core/guide.lua index 7797e6c3..bfbb941c 100644 --- a/script/core/guide.lua +++ b/script/core/guide.lua @@ -1,8 +1,6 @@ local linker = require 'core.linker' local guide = require 'parser.guide' - -local osClock = os.clock -local pairs = pairs +local files = require 'files' local m = {} @@ -89,21 +87,13 @@ function m.isGlobal(source) return false end ----搜索对象的引用 ----@param status guide.status ----@param source parser.guide.object ----@param mode guide.searchmode -function m.searchRefs(status, source, mode) - if source.type == 'field' - or source.type == 'method' then - source = source.parent - end - local root = guide.getRoot(source) - linker.compileLinks(root) - - if not linker.getLink(source) then +function m.searchRefsByID(status, uri, expect, mode) + local ast = files.getAst(uri) + if not ast then return end + local root = ast.ast + linker.compileLinks(root) local search @@ -180,20 +170,34 @@ function m.searchRefs(status, source, mode) end local function checkForward(link, field) - if not link.forward then - return + if link.forward then + for _, forwardSources in ipairs(link.forward) do + searchSource(forwardSources, field) + end end - for _, forwardSources in ipairs(link.forward) do - searchSource(forwardSources, field) + if link.fforward then + for _, func in ipairs(link.fforward) do + local forwards = func() + for _, forwardSources in ipairs(forwards) do + searchSource(forwardSources, field) + end + end end end local function checkBackward(link, field) - if not link.backward then - return + if link.backward then + for _, backSources in ipairs(link.backward) do + searchSource(backSources, field) + end end - for _, backSources in ipairs(link.backward) do - searchSource(backSources, field) + if link.fbackward then + for _, func in ipairs(link.fbackward) do + local backwards = func() + for _, backSources in ipairs(backwards) do + searchSource(backSources, field) + end + end end end @@ -222,8 +226,24 @@ function m.searchRefs(status, source, mode) stackCount = stackCount - 1 end - searchSource(source) - searchFunction(source) +end + +---搜索对象的引用 +---@param status guide.status +---@param source parser.guide.object +---@param mode guide.searchmode +function m.searchRefs(status, source, mode) + if source.type == 'field' + or source.type == 'method' then + source = source.parent + end + local uri = guide.getUri(source) + local id = linker.getID(source) + if not id then + return + end + + m.searchRefsByID(status, uri, id, mode) end ---@class guide.status @@ -239,21 +259,10 @@ function m.status(parentStatus, interface, deep) local status = { share = parentStatus and parentStatus.share or { count = 0, - cacheLock = {}, }, - depth = parentStatus and (parentStatus.depth + 1) or 0, - searchDeep= parentStatus and parentStatus.searchDeep or deep or -999, interface = parentStatus and parentStatus.interface or {}, - deep = parentStatus and parentStatus.deep, - clock = parentStatus and parentStatus.clock or osClock(), results = {}, } - if interface then - for k, v in pairs(interface) do - status.interface[k] = v - end - end - status.deep = status.depth <= status.searchDeep return status end diff --git a/script/core/linker.lua b/script/core/linker.lua index f4f18b84..b0083468 100644 --- a/script/core/linker.lua +++ b/script/core/linker.lua @@ -1,5 +1,6 @@ local util = require 'utility' local guide = require 'parser.guide' +local vm = require 'vm.vm' ---是否是全局变量(包括 _G.XXX 形式) ---@param source parser.guide.object @@ -79,6 +80,10 @@ local function getKey(source) return source.start, nil elseif source.type == 'select' then return ('%d:%d'):format(source.start, source.index) + elseif source.type == 'doc.class.name' + or source.type == 'doc.type.name' + or source.type == 'doc.alias.name' then + return source[1], nil end return nil, nil end @@ -90,6 +95,15 @@ local function checkMode(source) if source.type == 'select' then return 's' end + if source.type == 'doc.class.name' then + return 'dc' + end + if source.type == 'doc.type.name' then + return 'dt' + end + if source.type == 'doc.alias.name' then + return 'da' + end if isGlobal(source) then return 'g' end @@ -112,21 +126,38 @@ local function checkFunctionReturn(source) return nil end -local TempList = {} +local TempList1 = {} +local TempList2 = {} ---前进 ---@param source parser.guide.object ---@return parser.guide.object[] local function checkForward(source) - local list = TempList + local list1 = TempList1 + local list2 = TempList2 local parent = source.parent if source.value then -- x = y : x -> y - list[#list+1] = source.value + list1[#list1+1] = source.value elseif source.type == 'table' then -- x = {y = 1} : x -> x.y for _, keyvalue in ipairs(source) do - list[#list+1] = keyvalue + list1[#list1+1] = keyvalue + end + elseif source.type == 'doc.type.name' + or source.type == 'doc.class.name' + or source.type == 'doc.alias.name' then + list2[#list2+1] = function () + return vm.getDocNames(source[1], 'class') + end + list2[#list2+1] = function () + return vm.getDocNames(source[1], 'type') + end + list2[#list2+1] = function () + return vm.getDocNames(source[1], 'alias') + end + list2[#list2+1] = function () + return vm.getDocNames(source[1], 'extends') end end -- mt:f -> self @@ -136,41 +167,52 @@ local function checkForward(source) if func then local self = func.locals[1] if self.tag == 'self' then - list[#list+1] = self + list1[#list1+1] = self end end end - if #list == 0 then - return nil + if #list1 == 0 then + list1 = nil + else + TempList1 = {} + end + if #list2 == 0 then + list2 = nil else - TempList = {} - return list + TempList2 = {} end + return list1, list2 end ---后退 ---@param source parser.guide.object ---@return parser.guide.object[] local function checkBackward(source) - local list = TempList + local list1 = TempList1 + local list2 = TempList2 local parent = source.parent if parent.value == source then - list[#list+1] = parent + list1[#list1+1] = parent end -- self -> mt:xx if source.tag == 'self' then local func = guide.getParentFunction(source) local setmethod = func.parent if setmethod and setmethod.type == 'setmethod' then - list[#list+1] = setmethod.node + list1[#list1+1] = setmethod.node end end - if #list == 0 then - return nil + if #list1 == 0 then + list1 = nil + else + TempList1 = {} + end + if #list2 == 0 then + list2 = nil else - TempList = {} - return list + TempList2 = {} end + return list1, list2 end local IDList = {} @@ -233,6 +275,10 @@ end ---@field forward parser.guide.object[] -- 后退的关联单元 ---@field backward parser.guide.object[] +-- 前进的关联函数,调用此函数获取单元 +---@field fforward function[] +-- 后退的关联函数,调用此函数获取单元 +---@field fbackward function[] -- 缓存的关联links ---@field _links link[] @@ -244,13 +290,17 @@ local function createLink(source) if not id then return nil end + local forward, fforward = checkForward(source) + local backward, fbackward = checkBackward(source) return { - id = id, - source = source, - lastID = lastID, - freturn = checkFunctionReturn(node), - forward = checkForward(source), - backward = checkBackward(source), + id = id, + source = source, + lastID = lastID, + freturn = checkFunctionReturn(node), + forward = forward, + fforward = fforward, + backward = backward, + fbackward = fbackward, } end diff --git a/script/files.lua b/script/files.lua index 9cc6b549..3f3d633e 100644 --- a/script/files.lua +++ b/script/files.lua @@ -9,7 +9,7 @@ local await = require 'await' local timer = require 'timer' local plugin = require 'plugin' local util = require 'utility' -local guide = require 'core.guide' +local guide = require 'parser.guide' local smerger = require 'string-merger' local progress = require "progress" diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index 74e53718..423fa52d 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -1,7 +1,7 @@ local m = require 'lpeglabel' local re = require 'parser.relabel' local lines = require 'parser.lines' -local guide = require 'core.guide' +local guide = require 'parser.guide' local TokenTypes, TokenStarts, TokenFinishs, TokenContents local Ci, Offset, pushError, Ct, NextComment, Lines diff --git a/script/proto/define.lua b/script/proto/define.lua index 15e434de..abfaa9b0 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -1,6 +1,3 @@ -local guide = require 'core.guide' -local util = require 'utility' - local m = {} ---@alias location table diff --git a/script/vm/getDocs.lua b/script/vm/getDocs.lua index cfa9326f..a230a160 100644 --- a/script/vm/getDocs.lua +++ b/script/vm/getDocs.lua @@ -1,39 +1,52 @@ local files = require 'files' local util = require 'utility' -local guide = require 'core.guide' +local guide = require 'parser.guide' ---@type vm local vm = require 'vm.vm' local config = require 'config' -local function getTypesOfFile(uri) - local types = {} +local typeMap = { + ['doc.type.name'] = 'type', + ['doc.class.name'] = 'class', + ['doc.extends.name'] = 'extends', + ['doc.alias.name'] = 'alias', +} + +local function getNamesOfFile(uri) + local names = { + type = {}, + class = {}, + extends = {}, + alias = {}, + } local ast = files.getAst(uri) if not ast or not ast.ast.docs then - return types + return names end guide.eachSource(ast.ast.docs, function (src) - if src.type == 'doc.type.name' - or src.type == 'doc.class.name' - or src.type == 'doc.extends.name' - or src.type == 'doc.alias.name' then - if src.type == 'doc.type.name' then - if guide.getParentDocTypeTable(src) then - return - end - end - local name = src[1] - if name then - if not types[name] then - types[name] = {} - end - types[name][#types[name]+1] = src - end + local type = typeMap[src.type] + if not type then + return + end + --if src.type == 'doc.type.name' then + -- if guide.getParentDocTypeTable(src) then + -- return + -- end + --end + local name = src[1] + if not name then + return + end + local list = names[type] + if not list[name] then + list[name] = {} end + list[name][#list[name]+1] = src end) - return types + return names end -local function getDocTypes(name) +local function getDocNames(name, type) local results = {} if name == 'any' or name == 'nil' then @@ -41,16 +54,16 @@ local function getDocTypes(name) end for uri in files.eachFile() do local cache = files.getCache(uri) - cache.classes = cache.classes or getTypesOfFile(uri) + cache = cache or getNamesOfFile(uri) if name == '*' then - for _, sources in util.sortPairs(cache.classes) do + for _, sources in util.sortPairs(cache[type]) do for _, source in ipairs(sources) do results[#results+1] = source end end else - if cache.classes[name] then - for _, source in ipairs(cache.classes[name]) do + if cache[type][name] then + for _, source in ipairs(cache[type][name]) do results[#results+1] = source end end @@ -119,29 +132,14 @@ function vm.getDocTypeUnits(doc, mark, results) return results end -function vm.getDocTypes(name) - local cache = vm.getCache('getDocTypes')[name] +function vm.getDocNames(name, type) + local cacheName = 'docNames:' .. type + local cache = vm.getCache(cacheName)[name] if cache ~= nil then return cache end - cache = getDocTypes(name) - vm.getCache('getDocTypes')[name] = cache - return cache -end - -function vm.getDocClass(name) - local cache = vm.getCache('getDocClass')[name] - if cache ~= nil then - return cache - end - cache = {} - local results = getDocTypes(name) - for _, doc in ipairs(results) do - if doc.type == 'doc.class.name' then - cache[#cache+1] = doc - end - end - vm.getCache('getDocClass')[name] = cache + cache = getDocNames(name, type) + vm.getCache(cacheName)[name] = cache return cache end diff --git a/script/vm/getGlobals.lua b/script/vm/getGlobals.lua index 2752ce09..75512a75 100644 --- a/script/vm/getGlobals.lua +++ b/script/vm/getGlobals.lua @@ -1,4 +1,4 @@ -local guide = require 'core.guide' +local guide = require 'parser.guide' local await = require "await" ---@type vm local vm = require 'vm.vm' diff --git a/script/vm/guideInterface.lua b/script/vm/guideInterface.lua index ae060481..b966d5c9 100644 --- a/script/vm/guideInterface.lua +++ b/script/vm/guideInterface.lua @@ -87,9 +87,9 @@ function vm.interface.global(name, onlyDef) end end -function vm.interface.docType(name) +function vm.interface.doc(name, type) await.delay() - return vm.getDocTypes(name) + return vm.getDocNames(name, type) end function vm.interface.link(uri) diff --git a/script/vm/vm.lua b/script/vm/vm.lua index 0248ad8c..b7eb1cde 100644 --- a/script/vm/vm.lua +++ b/script/vm/vm.lua @@ -1,4 +1,4 @@ -local guide = require 'core.guide' +local guide = require 'parser.guide' local util = require 'utility' local files = require 'files' local timer = require 'timer' |