diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-03 16:15:46 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-03 16:15:46 +0800 |
commit | d92837f7ddcd2e4da3148e6ceba6aded46d6edfd (patch) | |
tree | 6036cc41fb861f9f1a9b85ec637d55f072480b22 /script-beta | |
parent | 9a69e16a4664b4deee3693d53a74336615700a6d (diff) | |
download | lua-language-server-d92837f7ddcd2e4da3148e6ceba6aded46d6edfd.zip |
doc.param 的自动完成
Diffstat (limited to 'script-beta')
-rw-r--r-- | script-beta/core/completion.lua | 71 | ||||
-rw-r--r-- | script-beta/parser/luadoc.lua | 5 |
2 files changed, 54 insertions, 22 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index cf6b7b06..42f56e06 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -959,7 +959,8 @@ local function getLuaDocByContain(ast, offset) if not src.start then return end - if range >= offset - src.start then + if range >= offset - src.start + and offset <= src.finish then range = offset - src.start result = src end @@ -989,13 +990,10 @@ local function getLuaDocByErr(ast, text, start, offset) break end end - if not targetDoc then - return nil - end return targetError, targetDoc end -local function tryLuaDocBySource(source, results) +local function tryLuaDocBySource(ast, offset, source, results) if source.type == 'doc.extends.name' then if source.parent.type == 'doc.class' then for _, doc in ipairs(vm.getDocTypes '*') do @@ -1020,10 +1018,27 @@ local function tryLuaDocBySource(source, results) } end end + elseif source.type == 'doc.param.name' then + local func = guide.eachSourceBetween(ast.ast, offset, math.huge, function (src) + if src.type == 'function' and src.start > offset then + return src + end + end) + if not func or not func.args then + return + end + for i, arg in ipairs(func.args) do + if matchKey(source[1], arg[1]) then + results[#results+1] = { + label = arg[1], + kind = define.CompletionItemKind.Interface, + } + end + end end end -local function tryLuaDocByErr(err, docState, results) +local function tryLuaDocByErr(ast, offset, err, docState, results) if err.type == 'LUADOC_MISS_CLASS_EXTENDS_NAME' then for _, doc in ipairs(vm.getDocTypes '*') do if doc.type == 'doc.class.name' @@ -1043,26 +1058,42 @@ local function tryLuaDocByErr(err, docState, results) } end end - end -end - -local function tryLuaDocFeatures(line, text, ast, lines, offset, results) - if trim(line) == '---@param' then - local func = guide.eachSourceBetween(ast.ast, offset, math.huge, function (source) - if source.type == 'function' then - return source + elseif err.type == 'LUADOC_MISS_PARAM_NAME' then + local func = guide.eachSourceBetween(ast.ast, offset, math.huge, function (src) + if src.type == 'function' and src.start > offset then + return src end end) if not func or not func.args then return end + local label = {} + local insertText = {} + for i, arg in ipairs(func.args) do + label[i] = arg[1] + if i == 1 then + insertText[i] = ('%s any'):format(arg[1]) + else + insertText[i] = ('---@param %s any'):format(arg[1]) + end + end results[#results+1] = { - label = table.concat(func.args, ', '), + label = table.concat(label, ', '), kind = define.CompletionItemKind.Snippet, + insertText = table.concat(insertText, '\n'), } + for i, arg in ipairs(func.args) do + results[#results+1] = { + label = arg[1], + kind = define.CompletionItemKind.Interface, + } + end 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) @@ -1079,19 +1110,19 @@ local function tryLuaDoc(ast, text, offset, results) return end -- 尝试一些其他特征 - --if tryLuaDocFeatures(line, text, ast, lines, offset, results) then - -- return - --end + if tryLuaDocFeatures(line, ast, comm, offset, results) then + return + end -- 根据输入中的source来补全 local source = getLuaDocByContain(ast, offset) if source then - tryLuaDocBySource(source, results) + tryLuaDocBySource(ast, offset, source, results) return end -- 根据附近的错误消息来补全 local err, doc = getLuaDocByErr(ast, text, comm.start, offset) if err then - tryLuaDocByErr(err, doc, results) + tryLuaDocByErr(ast, offset, err, doc, results) return end end diff --git a/script-beta/parser/luadoc.lua b/script-beta/parser/luadoc.lua index c6c9032a..fa768b81 100644 --- a/script-beta/parser/luadoc.lua +++ b/script-beta/parser/luadoc.lua @@ -451,7 +451,8 @@ local function parseParam() } return nil end - result.start = getStart() + result.start = result.param.start + result.finish = getFinish() result.extends = parseType(result) if checkToken('symbol', '?', 1) then nextToken() @@ -463,7 +464,7 @@ local function parseParam() start = getFinish(), finish = getFinish(), } - return nil + return result end result.finish = getFinish() return result |