diff options
-rw-r--r-- | script/core/completion/postfix.lua | 74 | ||||
-rw-r--r-- | script/core/substring.lua | 3 | ||||
-rw-r--r-- | test/completion/common.lua | 66 |
3 files changed, 143 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, diff --git a/script/core/substring.lua b/script/core/substring.lua index 0e4520c4..cfce5282 100644 --- a/script/core/substring.lua +++ b/script/core/substring.lua @@ -1,6 +1,9 @@ local guide = require 'parser.guide' return function (state) + ---@param pos1 parser.position + ---@param pos2 parser.position + ---@return string return function (pos1, pos2) return state.lua:sub( guide.positionToOffset(state, pos1), diff --git a/test/completion/common.lua b/test/completion/common.lua index 0dfabb07..d3e56616 100644 --- a/test/completion/common.lua +++ b/test/completion/common.lua @@ -2887,3 +2887,69 @@ xx(1, 2, 3)@xpcall<??> } }, } + +TEST [[ +xx@function<??> +]] +{ + [1] = { + label = 'function', + kind = define.CompletionItemKind.Event, + textEdit = { + start = 3, + finish = 11, + newText = 'function xx($1)\n\t$0\nend', + }, + additionalTextEdits = { + { + start = 0, + finish = 3, + newText = '' + } + } + }, +} + +TEST [[ +xx.yy@method<??> +]] +{ + [1] = { + label = 'method', + kind = define.CompletionItemKind.Event, + textEdit = { + start = 6, + finish = 12, + newText = 'function xx:yy($1)\n\t$0\nend', + }, + additionalTextEdits = { + { + start = 0, + finish = 6, + newText = '' + } + } + }, +} + +TEST [[ +xx:yy@method<??> +]] +{ + [1] = { + label = 'method', + kind = define.CompletionItemKind.Event, + textEdit = { + start = 6, + finish = 12, + newText = 'function xx:yy($1)\n\t$0\nend', + }, + additionalTextEdits = { + { + start = 0, + finish = 6, + newText = '' + } + } + }, +} |