David Hegland
David Hegland

Reputation: 1

VS-Code fold level by regex (similar to foldmethod=expr in vim)

I'm trying to see if there is an extension for VS-Code that allows for defining a fold level based on a string/regex. This would be similar to the foldmethod=expr folding in vim where you can define a function to set the fold level for a line. I'm looking to help examine log files easier in VS-Code, and want to set the fold level based on the log level for a line. I have a similar definition for vim, but I'm looking to migrate a similar solution to VS-Code.

For example, here is the .vimrc definition that accomplishes this:

" --- LogLevelFolding Setup {{{2
let g:LogLevelFoldMap = [
            \ [ '###',          0 ],
            \ [ '\[EMERG\]',    1 ],
            \ [ '\[ALERT\]',    2 ],
            \ [ '\[CRIT\]',     3 ],
            \ [ '\[ERR\]',      4 ],
            \ [ '\[WARN\]',     5 ],
            \ [ '\[NOTICE\]',   6 ],
            \ [ '\[INFO\]',     7 ],
            \ [ '\[DEBUG\]',    8 ],
            \ [ '\[VERBOSE\]',  9 ],
            \ ]

" --- FoldLevelLog() {{{2
function! FoldLevelLog(lnum)
    let line = getline(a:lnum)
    for [level, foldlevel] in g:LogLevelFoldMap
        if line =~? level
            return foldlevel
        endif
    endfor
    return len(g:LogLevelFoldMap)
endfunction

set foldmethod=expr
set foldexpr=FoldLevelLog(v:lnum)

Then with something like this, the the fold level can be set to 4 for example and all lines defined with a log level of ERR or higher would be seen and all other lines folded.

I've found a number of VS-Code extensions that allow for defining a single region to fold, but not one that actually sets the fold level based on an expression. Is there one out there I missed or does anyone know of a way to accomplish this?

Upvotes: 0

Views: 24

Answers (0)

Related Questions