summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-12-30 18:11:43 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-12-31 17:57:25 +0800
commit83427096a606baedebb7c59af4e525e8a290f1eb (patch)
tree693c416ee96567e09c2533f16e7fea946d325636
parent79adf4a94669b5b278675ba172c9d407879ab438 (diff)
downloadlua-language-server-83427096a606baedebb7c59af4e525e8a290f1eb.zip
clean up code
-rw-r--r--script/core/completion.lua56
-rw-r--r--script/parser/luadoc.lua14
2 files changed, 49 insertions, 21 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua
index e864a62d..79dff6ab 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -1296,6 +1296,15 @@ local function getComment(ast, offset)
return nil
end
+local function getLuaDoc(ast, offset)
+ for _, doc in ipairs(ast.ast.docs) do
+ if offset >= doc.start and offset <= doc.range then
+ return doc
+ end
+ end
+ return nil
+end
+
local function tryLuaDocCate(line, results)
local word = line:sub(3)
for _, docType in ipairs {
@@ -1377,6 +1386,7 @@ local function tryLuaDocBySource(ast, offset, source, results)
end
end
end
+ return true
elseif source.type == 'doc.type.name' then
for _, doc in ipairs(vm.getDocTypes '*') do
if (doc.type == 'doc.class.name' or doc.type == 'doc.alias.name')
@@ -1388,6 +1398,7 @@ local function tryLuaDocBySource(ast, offset, source, results)
}
end
end
+ return true
elseif source.type == 'doc.param.name' then
local funcs = {}
guide.eachSourceBetween(ast.ast, offset, math.huge, function (src)
@@ -1410,7 +1421,9 @@ local function tryLuaDocBySource(ast, offset, source, results)
}
end
end
+ return true
end
+ return false
end
local function tryLuaDocByErr(ast, offset, err, docState, results)
@@ -1476,47 +1489,50 @@ local function tryLuaDocByErr(ast, offset, err, docState, results)
end
end
-local function tryLuaDocFeatures(line, ast, comm, offset, results)
-end
-
local function tryLuaDoc(ast, text, offset, results)
- local comm = getComment(ast, offset)
- local line = text:sub(comm.start, offset)
+ local doc = getLuaDoc(ast, offset)
+ local line = text:sub(doc.start, offset)
if not line then
return
end
- if line:sub(1, 2) ~= '-@' then
- return
- end
- -- 尝试 ---@$
- local cate = line:match('%a*', 3)
- if #cate + 2 >= #line then
- tryLuaDocCate(line, results)
- return
+ -- 尝试 ---
+ if line:sub(1, 1) == '-' then
+
end
- -- 尝试一些其他特征
- if tryLuaDocFeatures(line, ast, comm, offset, results) then
- return
+ if doc.type == 'doc.comment' then
+ -- 尝试 ---@$
+ local cate = line:match('^-@(%a*)')
+ if cate and #cate + 2 >= #line then
+ tryLuaDocCate(line, results)
+ return
+ end
end
-- 根据输入中的source来补全
local source = getLuaDocByContain(ast, offset)
if source then
- tryLuaDocBySource(ast, offset, source, results)
- return
+ local suc = tryLuaDocBySource(ast, offset, source, results)
+ if suc then
+ return
+ end
end
-- 根据附近的错误消息来补全
- local err, doc = getLuaDocByErr(ast, text, comm.start, offset)
+ local err, expectDoc = getLuaDocByErr(ast, text, doc.start, offset)
if err then
- tryLuaDocByErr(ast, offset, err, doc, results)
+ tryLuaDocByErr(ast, offset, err, expectDoc, results)
return
end
end
local function tryComment(ast, text, offset, results)
local word = findWord(text, offset)
+ local doc = getLuaDoc(ast, offset)
+ local line = text:sub(doc.start, offset)
if not word then
return
end
+ if doc and doc.type ~= 'doc.comment' then
+ return
+ end
checkCommon(word, text, offset, results)
end
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index 94544218..647a6bed 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -915,6 +915,7 @@ local function buildLuaDoc(comment)
type = 'doc.comment',
start = comment.start,
finish = comment.finish,
+ range = comment.finish,
comment = comment,
}
end
@@ -924,6 +925,7 @@ local function buildLuaDoc(comment)
parseTokens(doc, comment.start + startPos - 1)
local result = convertTokens()
if result then
+ result.range = comment.finish
local cstart = text:find('%S', result.finish - comment.start + 2)
if cstart and cstart < comment.finish then
result.comment = {
@@ -935,7 +937,17 @@ local function buildLuaDoc(comment)
end
end
- return result
+ if result then
+ return result
+ end
+
+ return {
+ type = 'doc.comment',
+ start = comment.start,
+ finish = comment.finish,
+ range = comment.finish,
+ comment = comment,
+ }
end
---当前行在注释doc前是否有代码