34 lines
705 B
Lua
34 lines
705 B
Lua
|
-- Neovim lua api doesn't support multiline regexes so we use this hack instead
|
||
|
regexStrs = {
|
||
|
"^\\s*if err != nil {\\s*$",
|
||
|
"^\\s*return.\\{-}.err.*$",
|
||
|
"^\\s*}\\s*$"
|
||
|
}
|
||
|
|
||
|
regexes = {}
|
||
|
for _,str in pairs(regexStrs) do
|
||
|
table.insert(regexes, vim.regex(str))
|
||
|
end
|
||
|
function foldIfErrNills() do
|
||
|
local line_count = vim.api.nvim_buf_line_count(0)
|
||
|
for i = 0,(line_count-#regexStrs) do
|
||
|
local allMatched = true
|
||
|
for j,regex in pairs(regexes) do
|
||
|
if not regex:match_line(0,i+j-1) then
|
||
|
allMatched = false
|
||
|
break
|
||
|
end
|
||
|
end
|
||
|
if allMatched then
|
||
|
range_start = i+1
|
||
|
range_end = i+#regexes
|
||
|
vim.cmd(range_start..","..range_end.."fo")
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return {
|
||
|
foldIfErrNills = foldIfErrNills
|
||
|
}
|