Last active
February 14, 2021 11:42
-
-
Save AndrewRadev/af5982d1b72be8aad1b28fbdeade48b3 to your computer and use it in GitHub Desktop.
Ruby private method highlight (basic)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ~/.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