Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active February 14, 2021 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRadev/af5982d1b72be8aad1b28fbdeade48b3 to your computer and use it in GitHub Desktop.
Save AndrewRadev/af5982d1b72be8aad1b28fbdeade48b3 to your computer and use it in GitHub Desktop.
Ruby private method highlight (basic)
" ~/.vim/ftplugin/ruby/textprop_demo_1.vim
" Define what color the private methods will have
hi rubyPrivateMethod cterm=underline gui=underline
function! RubyMarkPrivateArea()
if empty(prop_type_get('private_method', {'bufnr': bufnr()}))
call prop_type_add('private_method', {
\ 'bufnr': bufnr(),
\ 'highlight': 'rubyPrivateMethod',
\ })
endif
" Clear out any previous matches
call prop_remove({'type': 'private_method', 'all': v:true})
" Store the current view, in order to restore it later
let saved_view = winsaveview()
" Note: this logic is pretty simple:
" Find the "private" access modifier
if search('\<private\>') == 0
return
endif
" Find all the methods after the "private" call
while search('\<def\>', 'W') > 0
call prop_add(line('.'), col('.'), {
\ 'length': len('def'),
\ 'type': 'private_method'
\ })
endwhile
" We're done highlighting, restore the view to what it was
call winrestview(saved_view)
endfunction
" Initial activation
call RubyMarkPrivateArea()
" Run after saving the buffer
autocmd BufWritePost <buffer> call RubyMarkPrivateArea()
" Run when the cursor holds still for 'timeoutlen' time
autocmd CursorHold <buffer> call RubyMarkPrivateArea()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment