Skip to content

Commit 677fac0

Browse files
committed
snip for continue and ifcall
1 parent 2c95a6f commit 677fac0

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

script/core/completion/keyword.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local define = require 'proto.define'
22
local files = require 'files'
33
local guide = require 'parser.guide'
4+
local config = require 'config'
5+
local util = require 'utility'
46

57
local keyWordMap = {
68
{ 'do', function(info, results)
@@ -324,6 +326,66 @@ end"
324326
end
325327
return true
326328
end },
329+
{ 'continue', function (info, results)
330+
local nonstandardSymbol = config.get(info.uri, 'Lua.runtime.nonstandardSymbol')
331+
if util.arrayHas(nonstandardSymbol, 'continue') then
332+
return
333+
end
334+
local version = config.get(info.uri, 'Lua.runtime.version')
335+
if version == 'Lua 5.1' then
336+
return
337+
end
338+
local mostInsideBlock
339+
guide.eachSourceContain(info.state.ast, info.start, function (src)
340+
if src.type == 'while'
341+
or src.type == 'in'
342+
or src.type == 'loop'
343+
or src.type == 'repeat' then
344+
mostInsideBlock = src
345+
end
346+
end)
347+
if not mostInsideBlock then
348+
return
349+
end
350+
-- 找一下 end 的位置
351+
local endPos
352+
if mostInsideBlock.type == 'while' then
353+
endPos = mostInsideBlock.keyword[5]
354+
elseif mostInsideBlock.type == 'in' then
355+
endPos = mostInsideBlock.keyword[7]
356+
elseif mostInsideBlock.type == 'loop' then
357+
endPos = mostInsideBlock.keyword[5]
358+
elseif mostInsideBlock.type == 'repeat' then
359+
endPos = mostInsideBlock.keyword[3]
360+
end
361+
if not endPos then
362+
return
363+
end
364+
local endLine = guide.rowColOf(endPos)
365+
local tabStr = info.state.lua:sub(
366+
info.state.lines[endLine],
367+
guide.positionToOffset(info.state, endPos)
368+
)
369+
local newText
370+
if tabStr:match '^[\t ]*$' then
371+
newText = ' ::continue::\n' .. tabStr
372+
else
373+
newText = '::continue::'
374+
end
375+
results[#results+1] = {
376+
label = 'goto continue ..',
377+
kind = define.CompletionItemKind.Snippet,
378+
insertText = "goto continue",
379+
additionalTextEdits = {
380+
{
381+
start = endPos,
382+
finish = endPos,
383+
newText = newText,
384+
}
385+
}
386+
}
387+
return true
388+
end }
327389
}
328390

329391
return keyWordMap

script/core/completion/postfix.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,39 @@ register 'xpcall' {
133133
end
134134
}
135135

136+
register 'ifcall' {
137+
function (state, source, callback)
138+
if source.type ~= 'getglobal'
139+
and source.type ~= 'getfield'
140+
and source.type ~= 'getmethod'
141+
and source.type ~= 'getindex'
142+
and source.type ~= 'getlocal'
143+
and source.type ~= 'call' then
144+
return
145+
end
146+
local subber = subString(state)
147+
if source.type == 'call' then
148+
if source.args and #source.args > 0 then
149+
callback(string.format('if %s then %s(%s) end$0'
150+
, subber(source.node.start + 1, source.node.finish)
151+
, subber(source.node.start + 1, source.node.finish)
152+
, subber(source.args[1].start + 1, source.args[#source.args].finish)
153+
))
154+
else
155+
callback(string.format('if %s then %s() end$0'
156+
, subber(source.node.start + 1, source.node.finish)
157+
, subber(source.node.start + 1, source.node.finish)
158+
))
159+
end
160+
else
161+
callback(string.format('if %s then %s($1) end$0'
162+
, subber(source.node.start + 1, source.node.finish)
163+
, subber(source.node.start + 1, source.node.finish)
164+
))
165+
end
166+
end
167+
}
168+
136169
register 'local' {
137170
function (state, source, callback)
138171
if source.type ~= 'getglobal'

0 commit comments

Comments
 (0)