diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2024-01-10 10:34:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-10 10:34:15 +0800 |
commit | cff4d48818d308535d865bce8a3a93b82a5404c6 (patch) | |
tree | fec3d69437de0c7405ee47d466c44200bcb22a31 /script/parser | |
parent | ded415e393cf77c27f1a053f914ea040b1a0955b (diff) | |
parent | 94a1cef1100621082b7f56d0a0fdd46c0662a79f (diff) | |
download | lua-language-server-cff4d48818d308535d865bce8a3a93b82a5404c6.zip |
Merge pull request #2472 from fesily/plugin-add-OnTransformAst
plugin: add OnTransformAst interface
Diffstat (limited to 'script/parser')
-rw-r--r-- | script/parser/guide.lua | 34 | ||||
-rw-r--r-- | script/parser/init.lua | 2 | ||||
-rw-r--r-- | script/parser/luadoc.lua | 38 |
3 files changed, 71 insertions, 3 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua index 0dd6338e..4efe066b 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -422,6 +422,22 @@ function m.getParentType(obj, want) error('guide.getParentType overstack') end +--- 寻找所在父类型 +---@param obj parser.object +---@return parser.object? +function m.getParentTypes(obj, wants) + for _ = 1, 10000 do + obj = obj.parent + if not obj then + return nil + end + if wants[obj.type] then + return obj + end + end + error('guide.getParentTypes overstack') +end + --- 寻找根区块 ---@param obj parser.object ---@return parser.object @@ -1292,4 +1308,22 @@ function m.isParam(source) return true end +---@param source parser.object +---@param index integer +---@return parser.object? +function m.getParam(source, index) + if source.type == 'call' then + local args = source.args + assert(args.type == 'callargs', 'call.args type is\'t callargs') + return args[index] + elseif source.type == 'callargs' then + return source[index] + elseif source.type == 'function' then + local args = source.args + assert(args.type == 'funcargs', 'function.args type is\'t callargs') + return args[index] + end + return nil +end + return m diff --git a/script/parser/init.lua b/script/parser/init.lua index bc004f77..9848ce00 100644 --- a/script/parser/init.lua +++ b/script/parser/init.lua @@ -2,7 +2,7 @@ local api = { compile = require 'parser.compile', lines = require 'parser.lines', guide = require 'parser.guide', - luadoc = require 'parser.luadoc', + luadoc = require 'parser.luadoc'.luadoc, } return api diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua index d7338918..64394e92 100644 --- a/script/parser/luadoc.lua +++ b/script/parser/luadoc.lua @@ -1964,6 +1964,14 @@ local function bindDocWithSources(sources, binded) bindGeneric(binded) bindCommentsAndFields(binded) bindReturnIndex(binded) + + -- doc is special node + if lastDoc.special then + if bindDoc(lastDoc.special, binded) then + return + end + end + local row = guide.rowColOf(lastDoc.finish) local suc = bindDocsBetween(sources, binded, guide.positionOf(row, 0), lastDoc.start) if not suc then @@ -1999,7 +2007,8 @@ local function bindDocs(state) binded = nil else local nextDoc = state.ast.docs[i+1] - if not isNextLine(doc, nextDoc) then + if nextDoc and nextDoc.special + or not isNextLine(doc, nextDoc) then bindDocWithSources(sources, binded) binded = nil end @@ -2028,7 +2037,7 @@ local function findTouch(state, doc) end end -return function (state) +local function luadoc(state) local ast = state.ast local comments = state.comms table.sort(comments, function (a, b) @@ -2097,6 +2106,15 @@ return function (state) end end end + + if ast.state.pluginDocs then + for i, doc in ipairs(ast.state.pluginDocs) do + insertDoc(doc, doc.originalComment) + end + table.sort(ast.docs, function (a, b) + return a.start < b.start + end) + end ast.docs.start = ast.start ast.docs.finish = ast.finish @@ -2107,3 +2125,19 @@ return function (state) bindDocs(state) end + +return { + buildAndBindDoc = function (ast, src, comment) + local doc = buildLuaDoc(comment) + if doc then + local pluginDocs = ast.state.pluginDocs or {} + pluginDocs[#pluginDocs+1] = doc + doc.special = src + doc.originalComment = comment + ast.state.pluginDocs = pluginDocs + return true + end + return false + end, + luadoc = luadoc +} |