diff options
-rw-r--r-- | script-beta/core/completion.lua | 9 | ||||
-rw-r--r-- | script-beta/parser/luadoc.lua | 56 | ||||
-rw-r--r-- | test-beta/references/init.lua | 2 |
3 files changed, 64 insertions, 3 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index 0b555e58..a8c52cc5 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -138,10 +138,15 @@ local function findParent(ast, text, offset) end local function findParentInStringIndex(ast, text, offset) - local near + local near, nearStart guide.eachSourceContain(ast.ast, offset, function (source) - if not near or near.start < source.start then + local start = guide.getStartFinish(source) + if not start then + return + end + if not nearStart or nearStart < start then near = source + nearStart = start end end) if not near or near.type ~= 'string' then diff --git a/script-beta/parser/luadoc.lua b/script-beta/parser/luadoc.lua index 5481168f..121b25eb 100644 --- a/script-beta/parser/luadoc.lua +++ b/script-beta/parser/luadoc.lua @@ -1,5 +1,7 @@ local m = require 'lpeglabel' local re = require 'parser.relabel' +local lines = require 'parser.lines' +local guide = require 'parser.guide' local TokenTypes, TokenStarts, TokenFinishs, TokenContents local Ci, Offset, pushError @@ -669,6 +671,58 @@ local function buildLuaDoc(comment) return result end +local function isNextLine(lns, binded, doc) + if not binded then + return false + end + local lastDoc = binded[#binded] + local lastRow = guide.positionOf(lns, lastDoc.start) + local newRow = guide.positionOf(lns, doc.start) + return newRow - lastRow == 1 +end + +local function bindDoc(state, lns, binded) + if not binded then + return + end + local lastDoc = binded[#binded] + if not lastDoc then + return + end + local row = guide.positionOf(lns, lastDoc.start) + local start, finish = guide.lineRange(lns, row + 1) + if start >= finish then + -- 空行 + return + end + guide.eachSourceBetween(state.ast, start, finish, 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' then + src.docs = binded + end + end) +end + +local function bindDocs(state) + local lns = lines(nil, state.lua) + local binded + for _, doc in ipairs(state.ast.docs) do + if not isNextLine(lns, binded, doc) then + bindDoc(state, binded) + binded = {} + end + binded[#binded+1] = doc + end + bindDoc(state, lns, binded) +end + return function (_, state) local ast = state.ast local comments = state.comms @@ -695,4 +749,6 @@ return function (_, state) end end end + + bindDocs(state) end diff --git a/test-beta/references/init.lua b/test-beta/references/init.lua index fc7cf4cb..af9c38af 100644 --- a/test-beta/references/init.lua +++ b/test-beta/references/init.lua @@ -274,7 +274,7 @@ TEST [[ ---@type <!Class!> ---@type <!Class!> ]] -do return end + TEST [[ ---@class Class local <?t?> |