diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-11-24 15:13:40 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-11-24 15:13:40 +0800 |
commit | 90d2f4190567891a2497cee80186657d416b19b0 (patch) | |
tree | be8f3d13381ea347e8a605da54c841d19d1e53cb /script/core/completion | |
parent | 91a09a1f08cbafe60212b13f5c62fa64f0d5ec4d (diff) | |
download | lua-language-server-90d2f4190567891a2497cee80186657d416b19b0.zip |
@method
Diffstat (limited to 'script/core/completion')
-rw-r--r-- | script/core/completion/postfix.lua | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/script/core/completion/postfix.lua b/script/core/completion/postfix.lua index 71d3c616..a04b5e65 100644 --- a/script/core/completion/postfix.lua +++ b/script/core/completion/postfix.lua @@ -16,8 +16,72 @@ local function register(key) end end +local function hasNonFieldInNode(source) + local block = guide.getParentBlock(source) + while source ~= block do + if source.type == 'call' + or source.type == 'getindex' + or source.type == 'getmethod' then + return true + end + source = source.parent + end + return false +end + +register 'function' { + function (state, source, callback) + if source.type ~= 'getglobal' + and source.type ~= 'getfield' + and source.type ~= 'getlocal' + and source.type ~= 'local' then + return + end + if hasNonFieldInNode(source) then + return + end + local subber = subString(state) + callback(string.format('function %s($1)\n\t$0\nend' + , subber(source.start + 1, source.finish) + )) + end +} + +register 'method' { + function (state, source, callback) + if source.type == 'getfield' then + if hasNonFieldInNode(source) then + return + end + local subber = subString(state) + callback(string.format('function %s:%s($1)\n\t$0\nend' + , subber(source.start + 1, source.dot.start) + , subber(source.dot .finish + 1, source.finish) + )) + end + if source.type == 'getmethod' then + if hasNonFieldInNode(source.parent) then + return + end + local subber = subString(state) + callback(string.format('function %s:%s($1)\n\t$0\nend' + , subber(source.start + 1, source.colon.start) + , subber(source.colon.finish + 1, source.finish) + )) + end + end +} + register 'pcall' { 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 @@ -40,6 +104,14 @@ register 'pcall' { register 'xpcall' { 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 @@ -61,6 +133,8 @@ register 'xpcall' { } local accepts = { + ['local'] = true, + ['getlocal'] = true, ['getglobal'] = true, ['getfield'] = true, ['getindex'] = true, |