summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-06-23 16:12:29 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-06-23 16:12:29 +0800
commita782eb79202e29326b96fab7cd7591ba881a7778 (patch)
tree01e2e9d8369b154067b0671b533bf84b5302619f
parent3c4203ac592c00fa8f3072faf77e1966149a7860 (diff)
downloadlua-language-server-a782eb79202e29326b96fab7cd7591ba881a7778.zip
自动完成显示代码块参考
-rw-r--r--script/capability/completion.lua2
-rw-r--r--script/core/completion.lua53
-rw-r--r--script/method/completionItem/resolve.lua27
-rw-r--r--script/method/init.lua1
-rw-r--r--test/completion/init.lua14
-rw-r--r--test/crossfile/completion.lua11
6 files changed, 91 insertions, 17 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'
diff --git a/test/completion/init.lua b/test/completion/init.lua
index 043d54a0..3847363f 100644
--- a/test/completion/init.lua
+++ b/test/completion/init.lua
@@ -71,6 +71,10 @@ function TEST(script)
local result = core.completion(vm, new_script, pos)
if expect then
assert(result)
+ -- 不检查 data 字段
+ for _, item in ipairs(result) do
+ item.data = nil
+ end
assert(eq(expect, result))
else
assert(result == nil)
@@ -1488,11 +1492,17 @@ f($)
{
label = "'选项1'",
kind = CompletionItemKind.EnumMember,
- documentation = '注释1',
+ documentation = {
+ kind = 'markdown',
+ value = '注释1',
+ },
},
{
label = "'选项2'",
kind = CompletionItemKind.EnumMember,
- documentation = '注释2',
+ documentation = {
+ kind = 'markdown',
+ value = '注释2',
+ },
},
}
diff --git a/test/crossfile/completion.lua b/test/crossfile/completion.lua
index 14744430..26579de2 100644
--- a/test/crossfile/completion.lua
+++ b/test/crossfile/completion.lua
@@ -92,6 +92,17 @@ function TEST(data)
local expect = data.completion
if expect then
assert(result)
+ for _, item in ipairs(result) do
+ item.data = nil
+ end
+ for _, item in ipairs(expect) do
+ if item.documentation then
+ item.documentation = {
+ kind = 'markdown',
+ value = item.documentation,
+ }
+ end
+ end
assert(eq(expect, result))
else
assert(result == nil)