aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2024-04-02 19:21:15 +0200
committerPatrick Spek <p.spek@tyil.nl>2024-04-02 19:24:58 +0200
commit6ac1c9a0bccea2e55a62803ba31a48337c21e131 (patch)
treeac4012d39fff6ec587bb5e158083886f7257c63a
parent1b119b23e8d06ae9ec31326049722117b8f9b4c5 (diff)
Add YAML ftplugin for showing the current line's hierarchy within a yaml document
-rw-r--r--.vim/ftplugin/yaml.vim49
1 files changed, 49 insertions, 0 deletions
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 <buffer> redraw | echo GetAncestors(line('.'))
+ endif
+aug END