aboutsummaryrefslogtreecommitdiff
path: root/.vim/ftplugin/yaml.vim
blob: ae4ce6662c60acc9d787774c332263df3de48c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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