From 6ac1c9a0bccea2e55a62803ba31a48337c21e131 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Tue, 2 Apr 2024 19:21:15 +0200 Subject: Add YAML ftplugin for showing the current line's hierarchy within a yaml document --- .vim/ftplugin/yaml.vim | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to '.vim') diff --git a/.vim/ftplugin/yaml.vim b/.vim/ftplugin/yaml.vim index 0c0c07f..ae4ce66 100644 --- a/.vim/ftplugin/yaml.vim +++ b/.vim/ftplugin/yaml.vim @@ -1,3 +1,52 @@ set shiftwidth=2 set tabstop=2 set expandtab + +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) " return ancestors of previous line + endif + endif + + let lowerIndent = indent(a:line)-1 + " check if line is part of a list + let isList = '' + if(getline(a:line) =~# '^\s*-') + let isList = '[]' + " find the first key above this in the file that has is not a list member + let lastKeyLine = search('^\s\{0,'.indent(a:line).'}[^-]\S\+:', 'bnW') + else + " find the first key above this in the file that has a lower indent or a + " containing list member + let lastKeyLine = search('^\s\{0,'.lowerIndent.'}\(\S\+\|-\s\S\+\):', 'bnW') + let lastKeyLineContent = getline(lastKeyLine) + " check if the containing key is not a member of the same object + if(lastKeyLineContent =~# ':\s.\+$' && lastKeyLineContent =~# '^\s*-') + let lastKeyLine = search('^\s\{0,'.lowerIndent.'}\(\S\+\|-\s\S\+\):\s*$', 'zbnW') + let isList = '[]' + endif + endif + + let key = matchstr(getline(lastKeyLine), '\s*[\-]\?\s*\zs.\+\ze:').isList + + if(indent(lastKeyLine) > 0) + return GetAncestors(lastKeyLine).' . '.key + endif + + return key +endfunction + +augroup YamlRevealer + au! + if(&filetype =~# 'yaml') + autocmd CursorMoved redraw | echo GetAncestors(line('.')) + endif +aug END -- cgit v1.1