Skip to content

Commit

Permalink
🐛 fix behavior with empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
ezpuzz committed Jun 17, 2019
1 parent ee49765 commit 2994f51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If you often use Yaml files, you know they are very readable at the beginning, b

Moving to the indicated line will automatically make vim echo

myRoot > secondChild > myVar2 > specialKey
myRoot > secondChild > myVar2

### Search for a specific key

Expand All @@ -46,3 +46,4 @@ Add `Plugin 'Einenlum/yaml-revealer'` to your `.vimrc`, reload your config and r
## Credits

Thanks to [@PedroTroller](https://github.com/PedroTroller) for his useful help :).
Thanks to [@ezpuzz](https://github.com/ezpuzz) for improving performance.
17 changes: 12 additions & 5 deletions ftplugin/yaml.vim
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ function! SearchYamlKey()
endif
endfunction

function! GetAncestors(line, indent)
if(indent(a:line) == 0 && a:line > 0)
function! GetAncestors(line)
if(indent(a:line) == 0 && a:line == 1)
return ''
endif

if(indent(a:line) == 0 && a:line > 1)
if(getline(a:line) !~ '^\s*$') " not an empty line
return ''
else
" sometimes there are newlines within a multiline key
return GetAncestors(a:line-1, a:indent) " return ancestors of previous line
return GetAncestors(a:line-1) " return ancestors of previous line
endif
endif

Expand All @@ -75,10 +79,13 @@ function! GetAncestors(line, indent)
let key = matchstr(getline(lastKeyLine), '\s*\zs.\+\ze:')

if(indent(lastKeyLine) > 0)
return GetAncestors(lastKeyLine, indent(lastKeyLine)).' > '.key
return GetAncestors(lastKeyLine).' > '.key
endif

return key
endfunction

autocmd CursorMoved <buffer> redraw | echo GetAncestors(line('.'), indent('.'))
augroup YamlRevealer
au!
autocmd CursorMoved <buffer> redraw | echo GetAncestors(line('.'))
aug END

0 comments on commit 2994f51

Please sign in to comment.