diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-01 01:34:21 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-01 01:34:21 +0800 |
commit | 15cf11aa55a061ffe7957d1f3d3fc4911ef70957 (patch) | |
tree | 7e4df20a7026ccfc2811a8d77385f55ed033ff67 /script-beta | |
parent | c7362d4d82f42e29749ac89ae90ed6395e2d501b (diff) | |
download | lua-language-server-15cf11aa55a061ffe7957d1f3d3fc4911ef70957.zip |
支持自动完成 class 的 extends
Diffstat (limited to 'script-beta')
-rw-r--r-- | script-beta/core/completion.lua | 76 |
1 files changed, 72 insertions, 4 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua index 6b3d5bfe..b9f4fc63 100644 --- a/script-beta/core/completion.lua +++ b/script-beta/core/completion.lua @@ -936,14 +936,82 @@ local function isInComment(ast, offset) return false end -local function tryLuaDoc(ast, text, offset, results) - if text:sub(offset - 3, offset) == '---@' then - for _, docType in ipairs {'class', 'type', 'alias', 'param', 'return', 'field', 'generic', 'vararg', 'overload'} do - +local function tryLuaDocCate(line, results) + local word = line:sub(5) + for _, docType in ipairs {'class', 'type', 'alias', 'param', 'return', 'field', 'generic', 'vararg', 'overload'} do + if matchKey(word, docType) then + results[#results+1] = { + label = docType, + kind = define.CompletionItemKind.Event, + } end end end +local function tryLuaDocClass(ast, text, offset, results) + local ok = guide.eachSourceContain(ast.ast.docs, offset, function (src) + if src.type == 'doc.extends.name' then + local myName = src[1] + local classDoc = src.parent.class + for _, doc in ipairs(vm.getDocTypes '*') do + if doc.type == 'doc.class.name' + and classDoc ~= doc + and matchKey(myName, doc[1]) then + results[#results+1] = { + label = doc[1], + kind = define.CompletionItemKind.Class, + } + end + end + return true + end + end) + if ok then + return + end + local symbol, soffset = findSymbol(text, offset) + if symbol == ':' then + local woffset = skipSpace(text, soffset - 1) + guide.eachSourceContain(ast.ast.docs, woffset, function (src) + if src.type == 'doc.class.name' then + for _, doc in ipairs(vm.getDocTypes '*') do + if doc.type == 'doc.class.name' + and src ~= doc then + results[#results+1] = { + label = doc[1], + kind = define.CompletionItemKind.Class, + } + end + end + return true + end + end) + end +end + +local function tryLuaDoc(ast, text, offset, results) + local lines = files.getLines(ast.uri) + local row = guide.positionOf(lines, offset) + local ln = lines[row] + local line = text:sub(ln.start, offset) + if not line then + return + end + if line:sub(1, 4) ~= '---@' then + return + end + -- 尝试 ---@$ + local cate = line:match('%a*', 5) + if #cate + 4 >= #line then + tryLuaDocCate(line, results) + return + end + if cate == 'class' then + tryLuaDocClass(ast, text, offset, results) + return + end +end + local function completion(uri, offset) local ast = files.getAst(uri) local text = files.getText(uri) |