diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-06-13 18:30:01 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-06-13 18:30:01 +0800 |
commit | 4081b303684b95ec27fd833a1cd10978d4ad5e10 (patch) | |
tree | bd286d27d262934d662926247e885b56b72bd489 | |
parent | cda4ec3f67a93a907752cc98572f0dac9c5dd83e (diff) | |
download | lua-language-server-4081b303684b95ec27fd833a1cd10978d4ad5e10.zip |
more smart completion for `continue`
fix #2042
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | script/core/completion/keyword.lua | 35 | ||||
-rw-r--r-- | script/parser/guide.lua | 2 | ||||
-rw-r--r-- | test/completion/common.lua | 70 |
4 files changed, 100 insertions, 9 deletions
diff --git a/changelog.md b/changelog.md index ab1a88d4..ff35198a 100644 --- a/changelog.md +++ b/changelog.md @@ -2,7 +2,9 @@ ## 3.6.22 * `FIX` [#2038] +* `FIX` [#2042] +[#2042]: https://github.com/LuaLS/lua-language-server/issues/2042 [#2038]: https://github.com/LuaLS/lua-language-server/issues/2038 ## 3.6.21 diff --git a/script/core/completion/keyword.lua b/script/core/completion/keyword.lua index e6f50242..aa0e2148 100644 --- a/script/core/completion/keyword.lua +++ b/script/core/completion/keyword.lua @@ -3,6 +3,7 @@ local files = require 'files' local guide = require 'parser.guide' local config = require 'config' local util = require 'utility' +local lookback = require 'core.look-backward' local keyWordMap = { { 'do', function(info, results) @@ -372,17 +373,35 @@ end" else newText = '::continue::' end + local additional = {} + + local word = lookback.findWord(info.state.lua, guide.positionToOffset(info.state, info.start) - 1) + if word ~= 'goto' then + additional[#additional+1] = { + start = info.start, + finish = info.start, + newText = 'goto ', + } + end + + local hasContinue = guide.eachSourceType(mostInsideBlock, 'label', function (src) + if src[1] == 'continue' then + return true + end + end) + + if not hasContinue then + additional[#additional+1] = { + start = endPos, + finish = endPos, + newText = newText, + } + end results[#results+1] = { label = 'goto continue ..', kind = define.CompletionItemKind.Snippet, - insertText = "goto continue", - additionalTextEdits = { - { - start = endPos, - finish = endPos, - newText = newText, - } - } + insertText = "continue", + additionalTextEdits = additional, } return true end } diff --git a/script/parser/guide.lua b/script/parser/guide.lua index e7eb3751..d7399b4d 100644 --- a/script/parser/guide.lua +++ b/script/parser/guide.lua @@ -720,7 +720,7 @@ end --- 遍历所有指定类型的source ---@param ast parser.object ---@param type string ----@param callback fun(src: parser.object) +---@param callback fun(src: parser.object): any ---@return any function m.eachSourceType(ast, type, callback) local cache = getSourceTypeCache(ast) diff --git a/test/completion/common.lua b/test/completion/common.lua index 8d23822a..8d53b89a 100644 --- a/test/completion/common.lua +++ b/test/completion/common.lua @@ -4114,3 +4114,73 @@ f({ kind = define.CompletionItemKind.Text, }, } + +TEST [[ +while true do + continue<??> +end +]] +{ + { + label = 'continue', + kind = define.CompletionItemKind.Keyword, + }, + { + label = 'goto continue ..', + kind = define.CompletionItemKind.Snippet, + additionalTextEdits = { + { + start = 10004, + finish = 10004, + newText = 'goto ', + }, + { + start = 20000, + finish = 20000, + newText = ' ::continue::\n', + }, + } + }, +} + +TEST [[ +while true do + goto continue<??> +end +]] +{ + { + label = 'continue', + kind = define.CompletionItemKind.Keyword, + }, + { + label = 'goto continue ..', + kind = define.CompletionItemKind.Snippet, + additionalTextEdits = { + { + start = 20000, + finish = 20000, + newText = ' ::continue::\n', + } + } + }, +} + +TEST [[ +while true do + goto continue<??> + ::continue:: +end +]] +{ + { + label = 'continue', + kind = define.CompletionItemKind.Keyword, + }, + { + label = 'goto continue ..', + kind = define.CompletionItemKind.Snippet, + additionalTextEdits = { + } + }, +} |