diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-10-20 20:26:18 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-10-20 20:26:18 +0800 |
commit | 4b9ccc38721abd2a152cbd3920582a11e04da8a9 (patch) | |
tree | 1e033b7f24b5355aef20963551afeca1a108e12b /script-beta/parser | |
parent | 2a9902fab374166ae4f73451281c007f86673af0 (diff) | |
download | lua-language-server-4b9ccc38721abd2a152cbd3920582a11e04da8a9.zip |
将 doc 绑定到 source 上
Diffstat (limited to 'script-beta/parser')
-rw-r--r-- | script-beta/parser/luadoc.lua | 56 |
1 files changed, 56 insertions, 0 deletions
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 |