From 43f774f396d1457ed59341e2fe94f0f5af1e3a93 Mon Sep 17 00:00:00 2001 From: kevinhwang91 Date: Mon, 7 Feb 2022 01:16:41 +0800 Subject: feat(postfix): reduce its priority and add templates In the language server like gopls, which supports `var` to assign variable and `range` to iterate collection, we should add these templates for postfix in Lua as well. --- script/core/completion/postfix.lua | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'script/core/completion/postfix.lua') diff --git a/script/core/completion/postfix.lua b/script/core/completion/postfix.lua index 2ac24933..0bdc1d54 100644 --- a/script/core/completion/postfix.lua +++ b/script/core/completion/postfix.lua @@ -133,6 +133,60 @@ register 'xpcall' { end } +register 'local' { + 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' + and source.type ~= 'table' then + return + end + local subber = subString(state) + callback(string.format('local $1 = %s$0' + , subber(source.start + 1, source.finish) + )) + end +} + +register 'ipairs' { + 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' + and source.type ~= 'table' then + return + end + local subber = subString(state) + callback(string.format('for ${1:i}, ${2:v} in ipairs(%s) do\n\t$0\nend' + , subber(source.start + 1, source.finish) + )) + end +} + +register 'pairs' { + 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' + and source.type ~= 'table' then + return + end + local subber = subString(state) + callback(string.format('for ${1:k}, ${2:v} in pairs(%s) do\n\t$0\nend' + , subber(source.start + 1, source.finish) + )) + end +} + register 'insert' { function (state, source, callback) if source.type ~= 'getglobal' -- cgit v1.2.3