diff options
Diffstat (limited to 'script')
-rw-r--r-- | script/capability/completion.lua | 2 | ||||
-rw-r--r-- | script/core/completion.lua | 53 | ||||
-rw-r--r-- | script/method/completionItem/resolve.lua | 27 | ||||
-rw-r--r-- | script/method/init.lua | 1 |
4 files changed, 68 insertions, 15 deletions
diff --git a/script/capability/completion.lua b/script/capability/completion.lua index e3fb2e98..e302f30d 100644 --- a/script/capability/completion.lua +++ b/script/capability/completion.lua @@ -31,7 +31,7 @@ local function enable(lsp) id = 'completion', method = 'textDocument/completion', registerOptions = { - resolveProvider = false, + resolveProvider = true, triggerCharacters = allWords(), }, }, diff --git a/script/core/completion.lua b/script/core/completion.lua index a62a5f1c..d0301d3a 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -264,7 +264,7 @@ local function sortPairs(t) end end -local function searchFieldsByInfo(parent, word, source, map) +local function searchFieldsByInfo(parent, word, source, map, srcMap) parent:eachInfo(function (info, src) local k = info[1] if src == source then @@ -291,12 +291,13 @@ local function searchFieldsByInfo(parent, word, source, map) end if matchKey(word, k) then map[k] = v + srcMap[k] = src end end) end -local function searchFieldsByChild(parent, word, source, map) - parent:eachChild(function (k, v) +local function searchFieldsByChild(parent, word, source, map, srcMap) + parent:eachChild(function (k, v, src) if map[k] then return end @@ -314,6 +315,7 @@ local function searchFieldsByChild(parent, word, source, map) end if matchKey(word, k) then map[k] = v + srcMap[k] = src end end) end @@ -325,17 +327,18 @@ local function searchFields(vm, source, word, callback, pos) return end local map = {} + local srcMap = {} local current = parent for _ = 1, 3 do - searchFieldsByInfo(current, word, source, map) + searchFieldsByInfo(current, word, source, map, srcMap) current = current:getMetaMethod('__index') if not current then break end end - searchFieldsByChild(parent, word, source, map) + searchFieldsByChild(parent, word, source, map, srcMap) for k, v in sortPairs(map) do - callback(k, nil, CompletionItemKind.Field, getValueData('field', k, v, pos, source)) + callback(k, srcMap[k], CompletionItemKind.Field, getValueData('field', k, v, pos, source)) end end @@ -421,17 +424,18 @@ end local function searchGlobals(vm, source, word, callback, pos) local global = vm.env:getValue() local map = {} + local srcMap = {} local current = global for _ = 1, 3 do - searchFieldsByInfo(current, word, source, map) + searchFieldsByInfo(current, word, source, map, srcMap) current = current:getMetaMethod('__index') if not current then break end end - searchFieldsByChild(global, word, source, map) + searchFieldsByChild(global, word, source, map, srcMap) for k, v in sortPairs(map) do - callback(k, nil, CompletionItemKind.Field, getValueData('field', k, v, pos, source)) + callback(k, srcMap[k], CompletionItemKind.Field, getValueData('field', k, v, pos, source)) end end @@ -683,7 +687,10 @@ local function searchInRequire(vm, source, callback) end for _, str in ipairs(list) do local data = buildTextEdit(source.start, source.finish, str, source[2]) - data.documentation = map[str] + data.documentation = { + value = map[str], + kind = 'markdown', + } callback(str, nil, CompletionItemKind.Reference, data) end end @@ -708,17 +715,26 @@ local function searchEnumAsLib(vm, source, word, callback, pos, args, lib) if strSource then if source.type == 'string' then local data = buildTextEdit(source.start, source.finish, strSource[1], source[2]) - data.documentation = enum.description + data.documentation = { + kind = 'markdown', + value = enum.description, + } callback(enum.enum, nil, CompletionItemKind.EnumMember, data) else callback(enum.enum, nil, CompletionItemKind.EnumMember, { - documentation = enum.description + documentation = { + value = enum.description, + kind = 'markdown', + } }) end end else callback(enum.enum, nil, CompletionItemKind.EnumMember, { - documentation = enum.description + documentation = { + value = enum.description, + kind = 'markdown', + } }) end end @@ -740,7 +756,10 @@ local function buildEmmyEnumComment(enum, data) if not data then data = {} end - data.documentation = tostring(enum.comment) + data.documentation = { + value = tostring(enum.comment), + kind = 'markdown', + } return data end @@ -956,6 +975,12 @@ local function makeList(source, pos, word) if not data.kind then data.kind = kind end + if src then + data.data = { + uri = src.uri, + offset = src.start, + } + end list[#list+1] = data if data.snip then local snipType = config.config.completion.callSnippet diff --git a/script/method/completionItem/resolve.lua b/script/method/completionItem/resolve.lua new file mode 100644 index 00000000..9909166a --- /dev/null +++ b/script/method/completionItem/resolve.lua @@ -0,0 +1,27 @@ +return function (lsp, item) + if not item.data then + return item + end + local offset = item.data.offset + local uri = item.data.uri + local _, lines, text = lsp:getVM(uri) + if not lines then + return item + end + local row = lines:rowcol(offset) + local firstRow = lines[row] + local lastRow = lines[math.min(row + 5, #lines)] + local snip = text:sub(firstRow.start, lastRow.finish) + local document = ([[ +%s +------------ +```lua +%s +``` +]]):format(item.documentation and item.documentation.value or '', snip) + item.documentation = { + kind = 'markdown', + value = document, + } + return item +end diff --git a/script/method/init.lua b/script/method/init.lua index dd662a2d..cd9010bb 100644 --- a/script/method/init.lua +++ b/script/method/init.lua @@ -8,6 +8,7 @@ init 'exit' init 'initialize' init 'initialized' init 'shutdown' +init 'completionItem/resolve' init 'textDocument/codeAction' init 'textDocument/completion' init 'textDocument/definition' |