diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-03-25 14:46:30 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-03-25 14:46:30 +0800 |
commit | 6cf51333a9cac71ff19f3495a782f91fbb991a41 (patch) | |
tree | 335337c9dbc3f6e2c70f03d5a22dff7842ccbccb /script | |
parent | 2aea9e58cb8782caaaa865c9b61a371df61ed468 (diff) | |
download | lua-language-server-6cf51333a9cac71ff19f3495a782f91fbb991a41.zip |
#147 使用 textEdit 来实现 completion
Diffstat (limited to 'script')
-rw-r--r-- | script/method/textDocument/completion.lua | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/script/method/textDocument/completion.lua b/script/method/textDocument/completion.lua index 4c7581df..28a432f2 100644 --- a/script/method/textDocument/completion.lua +++ b/script/method/textDocument/completion.lua @@ -42,7 +42,7 @@ local function fastCompletion(lsp, params, lines) end end - return items + return items, position end local function finishCompletion(lsp, params, lines) @@ -64,6 +64,23 @@ local function finishCompletion(lsp, params, lines) return items end +local function cuterFactory(lines, text, position) + local start = position + local head = '' + for i = position, position - 100, -1 do + if not text:sub(i, i):match '[%w_]' then + start = i + 1 + head = text:sub(start, position) + end + end + return function (insertText) + return { + newText = insertText, + range = posToRange(lines, start, position) + } + end +end + return function (lsp, params) local uri = params.textDocument.uri local text, oldText = lsp:getText(uri) @@ -72,20 +89,27 @@ return function (lsp, params) end local lines = parser:lines(text, 'utf8') - local items = fastCompletion(lsp, params, lines) + local items, position = fastCompletion(lsp, params, lines) --local items = finishCompletion(lsp, params, lines) if not items then return nil end + -- TODO 在协议阶段将 `insertText` 转化为 `textEdit` , + -- 以避免不同客户端对 `insertText` 实现的不一致。 + -- 重构后直接在 core 中使用 `textEdit` 。 + local cuter = cuterFactory(lines, text, position) + for i, item in ipairs(items) do item.sortText = ('%04d'):format(i) item.insertTextFormat = 2 - item.insertText = item.insertText or item.label + if item.textEdit then item.textEdit.range = posToRange(lines, item.textEdit.start, item.textEdit.finish) item.textEdit.start = nil item.textEdit.finish = nil + else + item.textEdit = cuter(item.insertText or item.label) end if item.additionalTextEdits then for _, textEdit in ipairs(item.additionalTextEdits) do @@ -100,5 +124,8 @@ return function (lsp, params) isIncomplete = true, items = items, } + + log.debug(table.dump(response)) + return response end |