From 6cf51333a9cac71ff19f3495a782f91fbb991a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Wed, 25 Mar 2020 14:46:30 +0800 Subject: =?UTF-8?q?#147=20=E4=BD=BF=E7=94=A8=20textEdit=20=E6=9D=A5?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20completion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/method/textDocument/completion.lua | 33 ++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'script/method/textDocument/completion.lua') 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 -- cgit v1.2.3