summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/matcher/completion.lua40
-rw-r--r--server/src/matcher/hover.lua4
-rw-r--r--server/src/method/textDocument/completion.lua22
-rw-r--r--server/test/completion/init.lua10
4 files changed, 59 insertions, 17 deletions
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua
index ba57532f..18f5c366 100644
--- a/server/src/matcher/completion.lua
+++ b/server/src/matcher/completion.lua
@@ -269,9 +269,13 @@ local function searchInArg(vm, inCall, inString, callback)
for _, enum in ipairs(lib.enums) do
if enum.name == name and enum.enum then
if inString then
- callback(enum.enum, CompletionItemKind.EnumMember, nil, enum.description)
+ callback(enum.enum, CompletionItemKind.EnumMember, {
+ documentation = enum.description
+ })
else
- callback(('%q'):format(enum.enum), CompletionItemKind.EnumMember, nil, enum.description)
+ callback(('%q'):format(enum.enum), CompletionItemKind.EnumMember, {
+ documentation = enum.description
+ })
end
end
end
@@ -391,7 +395,7 @@ end
local function makeList(source)
local list = {}
local mark = {}
- local function callback(var, defualt, detail, documentation)
+ local function callback(var, defualt, data)
local key
if type(var) == 'string' then
key = var
@@ -402,20 +406,16 @@ local function makeList(source)
return
end
mark[key] = true
+ data = data or {}
+ list[#list+1] = data
if var == key then
- list[#list+1] = {
- label = key,
- kind = defualt,
- detail = detail,
- documentation = documentation,
- }
+ data.label = var
+ data.kind = defualt
else
- list[#list+1] = {
- label = var.key,
- kind = getKind(var, defualt),
- detail = detail or getDetail(var),
- documentation = documentation or getDocument(var, source),
- }
+ data.label = var.key
+ data.kind = getKind(var, defualt)
+ data.detail = data.detail or getDetail(var)
+ data.documentation = data.documentation or getDocument(var, source)
end
end
return list, callback
@@ -461,7 +461,15 @@ local function searchSpecial(vm, pos, callback)
end
table.insert(name, 1, key)
end
- callback(table.concat(name, '.') .. '+1', CompletionItemKind.Snippet)
+ local label = table.concat(name, '.') .. '+1'
+ -- TODO 把index实例化才能拿到正确的位置
+ callback(label, CompletionItemKind.Snippet, {
+ textEdit = {
+ start = source.start + 1,
+ finish = source.finish + 1,
+ newText = ('%s] = '):format(label),
+ }
+ })
end
end
diff --git a/server/src/matcher/hover.lua b/server/src/matcher/hover.lua
index 900f9227..b0f194d1 100644
--- a/server/src/matcher/hover.lua
+++ b/server/src/matcher/hover.lua
@@ -183,8 +183,10 @@ local function buildValueName(result, source)
key = tostring(declarat[1])
elseif func.type == 'function' then
key = ''
- else
+ elseif type(result.key) == 'string' then
key = result.key
+ else
+ key = ''
end
local parentName = declarat.parentName
diff --git a/server/src/method/textDocument/completion.lua b/server/src/method/textDocument/completion.lua
index b866d18c..3a8a2e6b 100644
--- a/server/src/method/textDocument/completion.lua
+++ b/server/src/method/textDocument/completion.lua
@@ -1,5 +1,20 @@
local matcher = require 'matcher'
+local function posToRange(lines, start, finish)
+ local start_row, start_col = lines:rowcol(start)
+ local finish_row, finish_col = lines:rowcol(finish)
+ return {
+ start = {
+ line = start_row - 1,
+ character = start_col - 1,
+ },
+ ['end'] = {
+ line = finish_row - 1,
+ character = finish_col - 1,
+ },
+ }
+end
+
return function (lsp, params)
local uri = params.textDocument.uri
local vm, lines = lsp:loadVM(uri)
@@ -8,6 +23,7 @@ return function (lsp, params)
end
-- lua是从1开始的,因此都要+1
local position = lines:position(params.position.line + 1, params.position.character + 1)
+ log.debug(table.dump(params.position))
local items = matcher.completion(vm, position)
if not items then
return nil
@@ -17,10 +33,16 @@ return function (lsp, params)
end
for i, item in ipairs(items) do
item.sortText = ('%04d'):format(i)
+ if item.textEdit then
+ item.textEdit.range = posToRange(lines, item.textEdit.start, item.textEdit.finish)
+ item.textEdit.start = nil
+ item.textEdit.finish = nil
+ end
end
local response = {
isIncomplete = true,
items = items,
}
+ log.debug(table.dump(response))
return response
end
diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua
index b4ec7fd7..4d1c3e64 100644
--- a/server/test/completion/init.lua
+++ b/server/test/completion/init.lua
@@ -416,6 +416,11 @@ self.results.list[#@]
{
label = 'self.results.list+1',
kind = CompletionItemKind.Snippet,
+ textEdit = {
+ start = 20,
+ finish = 21,
+ newText = 'self.results.list+1] = ',
+ },
}
}
@@ -426,6 +431,11 @@ fff[#ff@]
{
label = 'fff+1',
kind = CompletionItemKind.Snippet,
+ textEdit = {
+ start = 6,
+ finish = 8,
+ newText = 'fff+1] = ',
+ },
},
{
label = 'fff',