2013-11-21 90 views
4

我在vim中編輯了一個非常大的嵌套JSON文檔(版權所有,請參考您的意見),並且很想知道當前的json路徑(如json的xpath)。例如:Vim:顯示當前的json路徑

鑑於JSON:

{          
    "leve1": {      
     "level2": {     
      "level3": {    
       "name": "goes here" 
      }       
     }        
    }         
} 

與我之間的 「名」 光標:和 「到這裏」 我想一個命令(或狀態行),顯示我:

level1/level2/level3/name 

或類似的。

這樣的事情是否存在?

回答

3

我已經寫了使用摺疊信息兩個映射(所以他們應該任何結構,不只是JSON工作)。對於你的例子,他們的輸出

{/"leve1": {/"level2": {/"level3": { 

和(加長版):

1 { 
2  "leve1": { 
3   "level2": { 
4    "level3": { 

這裏是小腳本。這取決於我的ingo-library plugin

" [count]z?  Print all lines (with numbers) that start a fold where 
"   the current line is contained in (for [count] upper 
"   levels). When a line consists of just a symbol like "{", 
"   the preceding non-empty line is printed, too. 
" [count]z/  Like z?, but use a short output format with all line 
"   contents concatenated, and without line numbers and 
"   symbols. 
if ! exists('g:PrintFoldHierarchySymbolLinePattern') 
    let g:PrintFoldHierarchySymbolLinePattern = '^\s*{\s*$' 
endif 
function! s:PrintFoldHierarchy(count, isJoin) 
    if foldclosed('.') != -1 
     return 0 
    endif 

    let l:save_view = winsaveview() 
     let l:levels = [] 
     let l:lnum = line('.') 
     while (a:count ? len(l:levels) < a:count : 1) 
      silent! normal! [z 
      if line('.') == l:lnum 
       break 
      endif 
      let l:lnum = line('.') 
      call insert(l:levels, l:lnum) 
      if getline(l:lnum) =~# g:PrintFoldHierarchySymbolLinePattern 
       let l:precedingLnum = prevnonblank(l:lnum - 1) 
       if l:precedingLnum > 0 
        if a:isJoin 
         let l:levels[0] = l:precedingLnum 
        else 
         call insert(l:levels, l:precedingLnum) 
        endif 
       endif 
      endif 
     endwhile 
    call winrestview(l:save_view) 

    if a:isJoin 
     echo 
     let l:isFirst = 1 
     for l:lnum in l:levels 
      if l:isFirst 
       let l:isFirst = 0 
      else 
       echohl SpecialKey 
       echon '/' 
       echohl None 
      endif 
      echon ingo#str#Trim(getline(l:lnum)) 
     endfor 
    else 
     for l:lnum in l:levels 
      echohl LineNr 
      echo printf('%' . (ingo#window#dimensions#GetNumberWidth(1) - 1) . 'd ', l:lnum) 
      echohl None 
      echon getline(l:lnum) 
     endfor 
    endif 

    return 1 
endfunction 
nnoremap <silent> z? :<C-u>if ! <SID>PrintFoldHierarchy(v:count, 0)<Bar>execute "normal! \<lt>C-\>\<lt>C-n>\<lt>Esc>"<Bar>endif<CR> 
nnoremap <silent> z/ :<C-u>if ! <SID>PrintFoldHierarchy(v:count, 1)<Bar>execute "normal! \<lt>C-\>\<lt>C-n>\<lt>Esc>"<Bar>endif<CR> 

你可以把到您的~/.vimrc(或單獨~/.vim/plugin/PrintFoldHierarchy.vim),並通過z?z/調用從正常模式的映射。