diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/core/completion.lua | 4 | ||||
-rw-r--r-- | script/core/keyword.lua | 21 | ||||
-rw-r--r-- | test/completion/init.lua | 47 |
4 files changed, 70 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md index 11f54ae2..96ef99cf 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## 1.18.0 * `NEW` `LuaDoc`: supports `---@diagnostic disable` * `NEW` code-action: convert JSON to Lua +* `NEW` completion: provide `then .. end` snippet * `CHG` `Windows`: dose not provide `ucrt` any more * `CHG` `Lua.workspace.library`: use `path[]` instead of `<path, true>` * `FIX` missed syntax error `local a <const>= 1` diff --git a/script/core/completion.lua b/script/core/completion.lua index f9fe7686..25305d30 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -739,7 +739,7 @@ local function checkKeyWord(ast, text, start, word, hasSpace, afterLocal, result if snipType == 'Both' or snipType == 'Replace' then local func = data[2] if func then - replaced = func(hasSpace, isExp, results) + replaced = func(hasSpace, isExp, results, text, start) extra = true end end @@ -752,7 +752,7 @@ local function checkKeyWord(ast, text, start, word, hasSpace, afterLocal, result label = key, kind = define.CompletionItemKind.Keyword, } - if extra then + if #results > 0 and extra then table.insert(results, #results, item) else results[#results+1] = item diff --git a/script/core/keyword.lua b/script/core/keyword.lua index 3a6c050c..d35bad75 100644 --- a/script/core/keyword.lua +++ b/script/core/keyword.lua @@ -256,7 +256,26 @@ until $1" end return false end}, - {'then'}, + {'then', function (hasSpace, isExp, results, text, start) + local first = text:match('%S+%s+(%S+)', start) + if first == 'end' + or first == 'else' + or first == 'elseif' then + return false + end + if not hasSpace then + results[#results+1] = { + label = 'then .. end', + kind = define.CompletionItemKind.Snippet, + insertTextFormat = 2, + insertText = '\z +then\ +\t$0\ +end' + } + end + return true + end}, {'true'}, {'until'}, {'while', function (hasSpace, isExp, results) diff --git a/test/completion/init.lua b/test/completion/init.lua index 930bc92f..d9e07b6f 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -2225,3 +2225,50 @@ m.f$ }, } Cared['insertText'] = nil + +TEST [[ +if true then$ +]] +{ + { + label = 'then', + kind = define.CompletionItemKind.Keyword, + }, + { + label = 'then .. end', + kind = define.CompletionItemKind.Snippet, + } +} + +TEST [[ +if true then$ +end +]] +{ + { + label = 'then', + kind = define.CompletionItemKind.Keyword, + }, +} + +TEST [[ +if true then$ +else +]] +{ + { + label = 'then', + kind = define.CompletionItemKind.Keyword, + }, +} + +TEST [[ +if true then$ +elseif +]] +{ + { + label = 'then', + kind = define.CompletionItemKind.Keyword, + }, +} |