diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-02-15 20:10:00 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-02-15 20:10:00 +0800 |
commit | 12d6a9eb75f31b64cbc89eb3265805bc8dad01f0 (patch) | |
tree | 031a449c1222ef1705d34fbb22c11ef1ea388ee5 /script/core/completion/completion.lua | |
parent | bf8b18fe8d2f3cc470021e32b9d5f11ec765a643 (diff) | |
download | lua-language-server-12d6a9eb75f31b64cbc89eb3265805bc8dad01f0.zip |
cleanup
Diffstat (limited to 'script/core/completion/completion.lua')
-rw-r--r-- | script/core/completion/completion.lua | 111 |
1 files changed, 57 insertions, 54 deletions
diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index a8000c91..2c3a354d 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -23,7 +23,7 @@ local noder = require 'core.noder' local await = require 'await' local postfix = require 'core.completion.postfix' -local DiagnosticModes = { +local diagnosticModes = { 'disable-next-line', 'disable-line', 'disable', @@ -101,9 +101,9 @@ local function findParent(state, position) goto CONTINUE end local oop - if char == '.' then + if char == '.' then -- `..` 的情况 - if text:sub(i-1, i-1) == '.' then + if text:sub(i - 1, i - 1) == '.' then return nil, nil end oop = false @@ -112,7 +112,7 @@ local function findParent(state, position) else return nil, nil end - local anyOffset = lookBackward.findAnyOffset(text, i-1) + local anyOffset = lookBackward.findAnyOffset(text, i - 1) if not anyOffset then return nil, nil end @@ -198,10 +198,10 @@ local function getSnip(source) if vm.isMetaFile(uri) then goto CONTINUE end - local firstRow = guide.rowColOf(def.start) - local lastRow = firstRow + context + local firstRow = guide.rowColOf(def.start) + local lastRow = firstRow + context local lastOffset = lines[lastRow] and (lines[lastRow] - 1) or #text - local snip = text:sub(lines[firstRow], lastOffset) + local snip = text:sub(lines[firstRow], lastOffset) return snip end ::CONTINUE:: @@ -228,19 +228,21 @@ local function buildFunction(results, source, value, oop, data) end if snipType == 'Both' or snipType == 'Replace' then local snipData = util.deepCopy(data) - snipData.kind = define.CompletionItemKind.Snippet - snipData.insertText = buildFunctionSnip(source, value, oop) + + snipData.kind = define.CompletionItemKind.Snippet + snipData.insertText = buildFunctionSnip(source, value, oop) snipData.insertTextFormat = 2 - snipData.command = { + snipData.command = { title = 'trigger signature', command = 'editor.action.triggerParameterHints', } - snipData.id = stack(function () ---@async + snipData.id = stack(function () ---@async return { detail = buildDetail(source), description = buildDesc(source), } end) + results[#results+1] = snipData end end @@ -262,7 +264,7 @@ local function getParams(func, oop) end local args = {} for _, arg in ipairs(func.args) do - if arg.type == '...' then + if arg.type == '...' then args[#args+1] = '...' elseif arg.type == 'doc.type.arg' then args[#args+1] = arg.name[1] @@ -294,11 +296,11 @@ local function checkLocal(state, word, position, results) or def.type == 'doc.type.function' then local funcLabel = name .. getParams(def, false) buildFunction(results, source, def, false, { - label = funcLabel, - match = name, + label = funcLabel, + match = name, insertText = name, - kind = define.CompletionItemKind.Function, - id = stack(function () ---@async + kind = define.CompletionItemKind.Function, + id = stack(function () ---@async return { detail = buildDetail(source), description = buildDesc(source), @@ -327,7 +329,7 @@ local function checkModule(state, word, position, results) if not config.get(state.uri, 'Lua.completion.autoRequire') then return end - local locals = guide.getVisibleLocals(state.ast, position) + local locals = guide.getVisibleLocals(state.ast, position) for uri in files.eachFile(state.uri) do if uri == guide.getUri(state.ast) then goto CONTINUE @@ -362,10 +364,10 @@ local function checkModule(state, word, position, results) goto CONTINUE end results[#results+1] = { - label = stemName, - kind = define.CompletionItemKind.Variable, - commitCharacters = {'.'}, - command = { + label = stemName, + kind = define.CompletionItemKind.Variable, + commitCharacters = { '.' }, + command = { title = 'autoRequire', command = 'lua.autoRequire', arguments = { @@ -376,7 +378,7 @@ local function checkModule(state, word, position, results) }, }, }, - id = stack(function () ---@async + id = stack(function () ---@async local md = markdown() md:add('md', lang.script('COMPLETION_IMPORT_FROM', ('[%s](%s)'):format( workspace.getRelativePath(uri), @@ -428,9 +430,9 @@ local function checkFieldFromFieldToIndex(state, name, src, parent, word, startP local nxt = parent.next if nxt then local dotStart, dotFinish - if nxt.type == 'setfield' - or nxt.type == 'getfield' - or nxt.type == 'tablefield' then + if nxt.type == 'setfield' + or nxt.type == 'getfield' + or nxt.type == 'tablefield' then dotStart = nxt.dot.start dotFinish = nxt.dot.finish elseif nxt.type == 'setmethod' @@ -506,13 +508,14 @@ local function checkFieldThen(state, name, src, word, startPos, position, parent kind = kind, deprecated = vm.isDeprecated(src) or nil, textEdit = textEdit, - additionalTextEdits = additionalTextEdits, id = stack(function () ---@async return { detail = buildDetail(src), description = buildDesc(src), } - end) + end), + + additionalTextEdits = additionalTextEdits, } end @@ -520,7 +523,7 @@ end local function checkFieldOfRefs(refs, state, word, startPos, position, parent, oop, results, locals, isGlobal) local fields = {} local funcs = {} - local count = 0 + local count = 0 for _, src in ipairs(refs) do local name = vm.getKeyName(src) if not name then @@ -727,7 +730,7 @@ local function checkKeyWord(state, start, position, word, hasSpace, afterLocal, text = text, start = start, uri = guide.getUri(state.ast), - position = position, + position = position, state = state, } for _, data in ipairs(keyWordMap) do @@ -870,7 +873,7 @@ local function checkFunctionArgByDocParam(state, word, startPos, results) end local function isAfterLocal(state, startPos) - local text = state.lua + local text = state.lua local offset = guide.positionToOffset(state, startPos) local pos = lookBackward.skipSpace(text, offset) local word = lookBackward.findWord(text, pos) @@ -972,11 +975,11 @@ local function collectRequireNames(mode, myUri, literal, source, smark, position end end results[#results+1] = { - label = label, - detail = infos.path, - kind = define.CompletionItemKind.File, + label = label, + detail = infos.path, + kind = define.CompletionItemKind.File, description = table.concat(des, '\n'), - textEdit = infos.textEdit, + textEdit = infos.textEdit, } end end @@ -1138,8 +1141,8 @@ local function checkEqualEnumLeft(state, position, source, results) end local function checkEqualEnum(state, position, results) - local text = state.lua - local start = lookBackward.findTargetSymbol(text, guide.positionToOffset(state, position), '=') + local text = state.lua + local start = lookBackward.findTargetSymbol(text, guide.positionToOffset(state, position), '=') if not start then return end @@ -1247,7 +1250,7 @@ local function tryWord(state, position, triggerCharacter, results) end else local parent, oop = findParent(state, startPos) - if parent then + if parent then checkField(state, word, startPos, position, parent, oop, results) elseif isFuncArg(state, position) then checkProvideLocal(state, word, startPos, results) @@ -1352,8 +1355,8 @@ local function getCallEnumsAndFuncs(source, index, oop, call) end end for _, doc in ipairs(source.bindDocs) do - if doc.type == 'doc.param' - and doc.param[1] == arg[1] then + if doc.type == 'doc.param' + and doc.param[1] == arg[1] then return pushCallEnumsAndFuncs(vm.getDefs(doc.extends)) elseif doc.type == 'doc.vararg' and arg.type == '...' then @@ -1377,7 +1380,7 @@ local function getCallEnumsAndFuncs(source, index, oop, call) return end local results = {} - if currentIndex == 1 then + if currentIndex == 1 then for _, doc in ipairs(class.fields) do if doc.field ~= source and doc.field[1] == source[1] then @@ -1468,10 +1471,10 @@ local function checkTableLiteralField(state, position, tbl, fields, results) local name = guide.getKeyName(field) if not mark[name] and matchKey(left, guide.getKeyName(field)) then results[#results+1] = { - label = guide.getKeyName(field), - kind = define.CompletionItemKind.Property, + label = guide.getKeyName(field), + kind = define.CompletionItemKind.Property, insertText = ('%s = $0'):format(guide.getKeyName(field)), - id = stack(function () ---@async + id = stack(function () ---@async return { detail = buildDetail(field), description = buildDesc(field), @@ -1598,7 +1601,7 @@ local function getluaDocByContain(state, position) if not src.start then return end - if range >= position - src.start + if range >= position - src.start and position <= src.finish then range = position - src.start result = src @@ -1633,7 +1636,7 @@ local function getluaDocByErr(state, start, position) end local function tryluaDocBySource(state, position, source, results) - if source.type == 'doc.extends.name' then + if source.type == 'doc.extends.name' then if source.parent.type == 'doc.class' then local used = {} for _, doc in ipairs(vm.getDocDefines(state.uri, '*')) do @@ -1699,7 +1702,7 @@ local function tryluaDocBySource(state, position, source, results) end return true elseif source.type == 'doc.diagnostic' then - for _, mode in ipairs(DiagnosticModes) do + for _, mode in ipairs(diagnosticModes) do if matchKey(source.mode, mode) then results[#results+1] = { label = mode, @@ -1717,8 +1720,8 @@ local function tryluaDocBySource(state, position, source, results) for name in util.sortPairs(define.DiagnosticDefaultSeverity) do if matchKey(source[1], name) then results[#results+1] = { - label = name, - kind = define.CompletionItemKind.Value, + label = name, + kind = define.CompletionItemKind.Value, textEdit = { start = source.start, finish = source.start + #source[1] - 1, @@ -1734,7 +1737,7 @@ local function tryluaDocBySource(state, position, source, results) end local function tryluaDocByErr(state, position, err, docState, results) - if err.type == 'LUADOC_MISS_CLASS_EXTENDS_NAME' then + if err.type == 'LUADOC_MISS_CLASS_EXTENDS_NAME' then for _, doc in ipairs(vm.getDocDefines(state.uri, '*')) do if doc.type == 'doc.class.name' and doc.parent ~= docState then @@ -1794,7 +1797,7 @@ local function tryluaDocByErr(state, position, err, docState, results) end end elseif err.type == 'LUADOC_MISS_DIAG_MODE' then - for _, mode in ipairs(DiagnosticModes) do + for _, mode in ipairs(diagnosticModes) do results[#results+1] = { label = mode, kind = define.CompletionItemKind.Enum, @@ -1876,11 +1879,11 @@ local function tryluaDocOfFunction(doc, results) end local insertText = buildluaDocOfFunction(func) results[#results+1] = { - label = '@param;@return', - kind = define.CompletionItemKind.Snippet, + label = '@param;@return', + kind = define.CompletionItemKind.Snippet, insertTextFormat = 2, - filterText = '---', - insertText = insertText + filterText = '---', + insertText = insertText } end |