Skip to content

Instantly share code, notes, and snippets.

@ctrngk
Last active June 8, 2018 11:55
Show Gist options
  • Select an option

  • Save ctrngk/92138847f7e2558eca022ae5eb39b589 to your computer and use it in GitHub Desktop.

Select an option

Save ctrngk/92138847f7e2558eca022ae5eb39b589 to your computer and use it in GitHub Desktop.
fold python hunter log files in vim
function! Hunter()
" It takes 2 mins to fold 400k lines in my macbook air.
function! ColBase()
" determine column number (3rd col, code part) in the first (base) line
call cursor(1, 1)
let mypattern = '\v(.+:\d+\s+\w+\s+!\s+\zs)|(.+:\d+\s+\w+\s+\zs)'
let [lnum, colbase] = searchpos(mypattern, 'n')
return colbase
endfunction
function! Info(lnum)
" determine the 3rd column position ( code part)
call cursor(a:lnum, 1)
let mypattern = '\v(.+:\d+\s+\w+\s+!\s+\zs)|(.+:\d+\s+\w+\s+\zs)'
let [lnum, col_pos] = searchpos(mypattern, 'n')
" dterminte the 3rd col indentation
let indentlevel = (col_pos - w:colbase) / 3
let info = [col_pos, indentlevel]
return info
endfunction
function! GetPotionFold(lnum)
let info = Info(a:lnum)
let colcode = info[0]
let indentlevel = info[1]
let current_content = getline(a:lnum)
if current_content[colcode-1: colcode+1] ==# '=> '
let tmp = indentlevel + 1
return '>' . tmp
endif
if current_content[colcode-1: colcode+1] ==# '<= '
let tmp = indentlevel + 1
return tmp
endif
return indentlevel
endfunction
let w:colbase = ColBase()
set foldmethod=expr
set foldexpr=GetPotionFold(v:lnum)
" change fold default color
highlight Folded ctermbg=Black ctermfg=darkgray
" render 3rd column color
function! HunterColor()
syntax include @Python syntax/python.vim
syntax region pythonCode matchgroup=SnipPy start=/\v^.+:\d+\s+\w+\s+/ end=/$/ contains=@Python keepend
hi SnipPy ctermfg=darkgray guifg=darkgray
hi Snip1 ctermfg=darkred guifg=darkred gui=bold
hi Snip2 ctermfg=lightred guifg=lightred
match Snip1 /\v\<\=\s\w+:/
2match Snip2 /\v\=\>\s\w+/
endfunction
call HunterColor()
" enable click
" set mouse=a
set foldcolumn=8
endfunction
@ctrngk
Copy link
Author

ctrngk commented Jun 4, 2018

Put above code inside .vimrc
It is folding by syntax '=>' and '<=' and ignore '! exception'
usage:
:call Hunter()
It takes 2 mins to fold 400k lines in my macbook air.
If it is a large file, color of python code part may not be rendered. You need to redraw with CTRL-L

if you are beginner about folding
zo (o)pen the fold
zc (c)lose the fold

In non-gui vim, You might want to activate mouse action to click open/close the folds
set mouse=a
set foldcolumn=8

a friendly beginner tutorial about folding is covered inside vim docs
:help usr_28

@ctrngk
Copy link
Author

ctrngk commented Jun 8, 2018

If it is a large file, color of python part may not be rendered. you need to redraw with ctrl-L

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment