diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-03-01 20:36:55 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-03-01 20:36:55 +0800 |
commit | 677fac00286628af29a1a24c11a0d6de7d8afe0d (patch) | |
tree | 99cd8f2108104d66867a130409ec29e1d837afd2 | |
parent | 2c95a6f41af204be845edd57fecc405cac93f3e2 (diff) | |
download | lua-language-server-677fac00286628af29a1a24c11a0d6de7d8afe0d.zip |
snip for `continue` and `ifcall`
-rw-r--r-- | script/core/completion/keyword.lua | 62 | ||||
-rw-r--r-- | script/core/completion/postfix.lua | 33 |
2 files changed, 95 insertions, 0 deletions
diff --git a/script/core/completion/keyword.lua b/script/core/completion/keyword.lua index 5558106a..e6f50242 100644 --- a/script/core/completion/keyword.lua +++ b/script/core/completion/keyword.lua @@ -1,6 +1,8 @@ local define = require 'proto.define' local files = require 'files' local guide = require 'parser.guide' +local config = require 'config' +local util = require 'utility' local keyWordMap = { { 'do', function(info, results) @@ -324,6 +326,66 @@ end" end return true end }, + { 'continue', function (info, results) + local nonstandardSymbol = config.get(info.uri, 'Lua.runtime.nonstandardSymbol') + if util.arrayHas(nonstandardSymbol, 'continue') then + return + end + local version = config.get(info.uri, 'Lua.runtime.version') + if version == 'Lua 5.1' then + return + end + local mostInsideBlock + guide.eachSourceContain(info.state.ast, info.start, function (src) + if src.type == 'while' + or src.type == 'in' + or src.type == 'loop' + or src.type == 'repeat' then + mostInsideBlock = src + end + end) + if not mostInsideBlock then + return + end + -- 找一下 end 的位置 + local endPos + if mostInsideBlock.type == 'while' then + endPos = mostInsideBlock.keyword[5] + elseif mostInsideBlock.type == 'in' then + endPos = mostInsideBlock.keyword[7] + elseif mostInsideBlock.type == 'loop' then + endPos = mostInsideBlock.keyword[5] + elseif mostInsideBlock.type == 'repeat' then + endPos = mostInsideBlock.keyword[3] + end + if not endPos then + return + end + local endLine = guide.rowColOf(endPos) + local tabStr = info.state.lua:sub( + info.state.lines[endLine], + guide.positionToOffset(info.state, endPos) + ) + local newText + if tabStr:match '^[\t ]*$' then + newText = ' ::continue::\n' .. tabStr + else + newText = '::continue::' + end + results[#results+1] = { + label = 'goto continue ..', + kind = define.CompletionItemKind.Snippet, + insertText = "goto continue", + additionalTextEdits = { + { + start = endPos, + finish = endPos, + newText = newText, + } + } + } + return true + end } } return keyWordMap diff --git a/script/core/completion/postfix.lua b/script/core/completion/postfix.lua index c5988ef6..1331a0e4 100644 --- a/script/core/completion/postfix.lua +++ b/script/core/completion/postfix.lua @@ -133,6 +133,39 @@ register 'xpcall' { end } +register 'ifcall' { + function (state, source, callback) + if source.type ~= 'getglobal' + and source.type ~= 'getfield' + and source.type ~= 'getmethod' + and source.type ~= 'getindex' + and source.type ~= 'getlocal' + and source.type ~= 'call' then + return + end + local subber = subString(state) + if source.type == 'call' then + if source.args and #source.args > 0 then + callback(string.format('if %s then %s(%s) end$0' + , subber(source.node.start + 1, source.node.finish) + , subber(source.node.start + 1, source.node.finish) + , subber(source.args[1].start + 1, source.args[#source.args].finish) + )) + else + callback(string.format('if %s then %s() end$0' + , subber(source.node.start + 1, source.node.finish) + , subber(source.node.start + 1, source.node.finish) + )) + end + else + callback(string.format('if %s then %s($1) end$0' + , subber(source.node.start + 1, source.node.finish) + , subber(source.node.start + 1, source.node.finish) + )) + end + end +} + register 'local' { function (state, source, callback) if source.type ~= 'getglobal' |