Skip to content

Instantly share code, notes, and snippets.

@kezipe
Created September 19, 2025 05:11
Show Gist options
  • Select an option

  • Save kezipe/b8dcad0cf2acdd7a087af84e723ff1ef to your computer and use it in GitHub Desktop.

Select an option

Save kezipe/b8dcad0cf2acdd7a087af84e723ff1ef to your computer and use it in GitHub Desktop.
; You’ve configured Vim to use expression-based folding (foldmethod=expr) with two custom functions:
; • ClassFoldExpr() → decides where folds start and end (fold structure).
; • ClassFoldText() → decides how folded text is displayed (fold summary).
;
; This lets Vim fold your custom language in a hierarchical way:
; • Level 1 → folds whole classes (classname { ... })
; • Level 2 → folds functions inside classes (functionname { ... })
set foldmethod=expr
set foldexpr=ClassFoldExpr()
set foldtext=ClassFoldText()
function! ClassFoldExpr()
let l:line = getline(v:lnum)
" Top-level class (level 1)
if l:line =~ '^[A-Za-z0-9_]\+\s*{'
return '>1'
endif
" Function inside class (indented, level 2)
if l:line =~ '^\s\+\w\+\s*{'
return '>2'
endif
" Closing braces
if l:line =~ '^}'
return '<1' " closes a class
endif
if l:line =~ '^\s\+}'
return '<2' " closes a function
endif
return '='
endfunction
function! ClassFoldText()
" First and last lines
let l:first = getline(v:foldstart)
let l:last = getline(v:foldend)
" Folded line count
let l:count = v:foldend - v:foldstart - 1
" Build summary: show opening line, placeholder, closing brace
return l:first . ' +-- ' . l:count . ' lines: ----- ' . l:last
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment