Skip to content

Instantly share code, notes, and snippets.

@moyashipan
Last active August 29, 2015 14:00
Show Gist options
  • Save moyashipan/11177018 to your computer and use it in GitHub Desktop.
Save moyashipan/11177018 to your computer and use it in GitHub Desktop.
PwdYaml
" .vim/autoload/yml.vim
function! yml#search(pos)
let indent = indent(a:pos)
let keys = [s:get_key(getline(a:pos))]
for n in range(line(a:pos), 1, -1)
if indent(n) == (indent - &tabstop)
call insert(keys, s:get_key(getline(n)))
let indent = indent - &tabstop
endif
endfor
echo join(keys, '/')
endfunction
function! yml#jump(path) range
let indent = 0
let min_line = 1
let max_line = line("$")
for key in split(a:path, '/')
call cursor(min_line, 0)
let space = repeat(" ", indent)
call search('^' . space . key . ':', 'W', max_line)
let min_line = line('.')
call search('^' . space . '[^ ]\+:', 'W', max_line)
if line('.') != min_line
let max_line = line('.')
endif
let indent = indent + &tabstop
endfor
endfunction
function! s:get_key(line_str)
let space_with_key = matchstr(a:line_str, '^[^:]\+')
return substitute(space_with_key, '[ :]*', '', 'g')
endfunction
" .vim/ftplugin/yaml.vim
command! -nargs=0 YamlPwd call yml#search('.')
command! -nargs=1 YamlJump call yml#jump(<f-args>)
@alpaca-tc
Copy link

そのままでもいい感じですね!

" .vim/autoload/yml.vim
function! yml#search(pos)
  let indent = indent(a:pos)

  let keys = []
  for n in range(line(a:pos), 1, -1)
    if indent(n) == (indent - &tabstop)
      call insert(keys, s:get_key(getline(n)))
      let indent = indent - &tabstop
    endif
  endfor

  echo join(keys, '/')
endfunction

function! s:get_key(line_str)
  return substitute(a:line_str, '[ :]*', '', 'g')
endfunction

" .vim/ftplugin/yaml.vim
command! -nargs=0 PwdYaml call yml#search('.')

@moyashipan
Copy link
Author

わーい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment