Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Last active February 22, 2017 17:08
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/d848ad9621b47b4151bbc61ab6c5765f to your computer and use it in GitHub Desktop.
Save AndrewRadev/d848ad9621b47b4151bbc61ab6c5765f to your computer and use it in GitHub Desktop.
" Define what color the private area will be
hi rubyPrivateArea ctermbg=darkgray
function! s:MarkPrivateArea()
" Clear out any previous matches
call clearmatches()
" Store the current view, in order to restore it later
let saved_view = winsaveview()
" start at the last char in the file and wrap for the
" first search to find match at start of file
normal! G$
let flags = "w"
while search('\<private\>', flags) > 0
let flags = "W"
if s:CurrentSyntaxName() !~# "rubyAccess"
" it's not a real access modifier, keep going
continue
endif
let start_line = line('.')
" look for the matching "end"
let saved_position = getpos('.')
while search('\<end\>', 'W') > 0
if s:CurrentSyntaxName() !~# "rubyClass"
" it's not an end that closes a class, keep going
continue
endif
let end_line = line('.') - 1
call matchadd('rubyPrivateArea', '\%'.start_line.'l\_.*\%'.end_line.'l')
break
endwhile
" restore where we were before we started looking for the "end"
call setpos('.', saved_position)
endwhile
" We're done highlighting, restore the view to what it was
call winrestview(saved_view)
endfunction
function! s:CurrentSyntaxName()
return synIDattr(synID(line("."), col("."), 0), "name")
endfunction
augroup rubyPrivateArea
autocmd!
" Initial marking
autocmd BufEnter <buffer> call <SID>MarkPrivateArea()
" Mark upon writing
autocmd BufWrite <buffer> call <SID>MarkPrivateArea()
" Mark when not moving the cursor for 'timeoutlen' time
autocmd CursorHold <buffer> call <SID>MarkPrivateArea()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment