diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-07-29 15:21:02 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-07-29 15:21:02 +0800 |
commit | f832278c05667e2741946fc53d9fe580f52103bc (patch) | |
tree | 4d426e45455bbaebc6eab1c343d99105571200e4 | |
parent | 012dc21e55493b0c0d020163e58206a41acee266 (diff) | |
download | lua-language-server-f832278c05667e2741946fc53d9fe580f52103bc.zip |
compile `addChilds`
-rw-r--r-- | script/parser/guide.lua | 74 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 29 | ||||
-rw-r--r-- | script/utility.lua | 8 | ||||
-rw-r--r-- | test.lua | 2 |
4 files changed, 76 insertions, 37 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua index c14f5a37..397447cb 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -83,7 +83,7 @@ local breakBlockTypes = { ['repeat'] = true, } -m.childMap = { +local childMap = { ['main'] = {'#', 'docs'}, ['repeat'] = {'#', 'filter'}, ['while'] = {'filter', '#'}, @@ -139,6 +139,44 @@ m.childMap = { ['doc.see'] = {'name', 'field'}, } +---@type table<string, fun(obj: parser.guide.object, list: parser.guide.object[])> +local compiledChildMap = setmetatable({}, {__index = function (self, name) + local defs = childMap[name] + if not defs then + self[name] = false + return false + end + local text = {} + text[#text+1] = 'local obj, list = ...' + for _, def in ipairs(defs) do + if def == '#' then + text[#text+1] = [[ +for i = 1, #obj do + list[#list+1] = obj[i] +end +]] + elseif type(def) == 'string' and def:sub(1, 1) == '#' then + local key = def:sub(2) + text[#text+1] = ([[ +local childs = obj.%s +if childs then + for i = 1, #childs do + list[#list+1] = childs[i] + end +end +]]):format(key) + elseif type(def) == 'string' then + text[#text+1] = ('list[#list+1] = obj.%s'):format(def) + else + text[#text+1] = ('list[#list+1] = obj[%q]'):format(def) + end + end + local buf = table.concat(text, '\n') + local f = load(buf, buf, 't') + self[name] = f + return f +end}) + m.actionMap = { ['main'] = {'#'}, ['repeat'] = {'#'}, @@ -542,28 +580,12 @@ function m.isBetweenRange(source, tStart, tFinish) end --- 添加child -function m.addChilds(list, obj, map) - local keys = map[obj.type] - if keys then - for i = 1, #keys do - local key = keys[i] - if key == '#' then - for j = 1, #obj do - list[#list+1] = obj[j] - end - elseif obj[key] then - list[#list+1] = obj[key] - elseif type(key) == 'string' - and key:sub(1, 1) == '#' then - key = key:sub(2) - if obj[key] then - for j = 1, #obj[key] do - list[#list+1] = obj[key][j] - end - end - end - end +function m.addChilds(list, obj) + local f = compiledChildMap[obj.type] + if not f then + return end + f(obj, list) end --- 遍历所有包含offset的source @@ -586,7 +608,7 @@ function m.eachSourceContain(ast, offset, callback) return res end end - m.addChilds(list, obj, m.childMap) + m.addChilds(list, obj) end end end @@ -612,7 +634,7 @@ function m.eachSourceBetween(ast, start, finish, callback) return res end end - m.addChilds(list, obj, m.childMap) + m.addChilds(list, obj) end end end @@ -667,7 +689,7 @@ function m.eachSource(ast, callback) if res == false then return end - m.addChilds(list, obj, m.childMap) + m.addChilds(list, obj) end ::CONTINUE:: end @@ -695,6 +717,8 @@ end ---@return integer {name = 'row'} ---@return integer {name = 'col'} function m.positionOf(lines, offset) + tracy.ZoneBeginN('positionOf') + local _ <close> = tracy.ZoneEnd if offset <= 0 then return 1, 0 end diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index e5e10331..a7062862 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -3,6 +3,7 @@ local re = require 'parser.relabel' local lines = require 'parser.lines' local guide = require 'parser.guide' local grammar = require 'parser.grammar' +local util = require 'utility' local TokenTypes, TokenStarts, TokenFinishs, TokenContents local Ci, Offset, pushError, Ct, NextComment, Lines @@ -1343,27 +1344,29 @@ local function bindDoc(sources, lns, binded) bindClassAndFields(binded) end +local bindDocAccept = util.arrayToHash { + 'local' , 'setlocal' , 'setglobal', + 'setfield' , 'setmethod' , 'setindex' , + 'tablefield', 'tableindex', + 'function' , 'table' , '...' , +} + local function bindDocs(state) + tracy.ZoneBeginN('bindDocs #1') local text = state.lua local sources = {} guide.eachSource(state.ast, function (src) - if src.type == 'local' - or src.type == 'setlocal' - or src.type == 'setglobal' - or src.type == 'setfield' - or src.type == 'setmethod' - or src.type == 'setindex' - or src.type == 'tablefield' - or src.type == 'tableindex' - or src.type == 'function' - or src.type == 'table' - or src.type == '...' then + if bindDocAccept[src.type] then sources[#sources+1] = src end end) + tracy.ZoneEnd() + tracy.ZoneBeginN('bindDocs #2') table.sort(sources, function (a, b) return a.start < b.start end) + tracy.ZoneEnd() + tracy.ZoneBeginN('bindDocs #3') local binded for _, doc in ipairs(state.ast.docs) do if not isNextLine(Lines, text, binded, doc) then @@ -1378,9 +1381,13 @@ local function bindDocs(state) end end bindDoc(sources, Lines, binded) + tracy.ZoneEnd() end +require 'tracy'.enable() return function (_, state) + tracy.ZoneBeginN('luadoc') + local _ <close> = tracy.ZoneEnd local ast = state.ast local comments = state.comms table.sort(comments, function (a, b) diff --git a/script/utility.lua b/script/utility.lua index b838f3a8..ed11c28b 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -647,4 +647,12 @@ function m.expandPath(path) end end +function m.arrayToHash(l) + local t = {} + for i = 1, #l do + t[l[i]] = true + end + return t +end + return m @@ -91,7 +91,7 @@ local function main() --config.Lua.intelliSense.searchDepth = 5 --loadDocMetas() - --test 'full' + test 'full' require 'bee.platform'.OS = 'Windows' testAll() |