Vim:显示当前的json路径

sim*_*nvc 5 api vim json

我正在vim中编辑一个非常大的,嵌套的JSON doc(你感兴趣的版权所有),并且很想知道当前的json-path(比如json的xpath)类似于:

鉴于JSON:

 {                                    
     "leve1": {                       
         "level2": {                  
             "level3": {              
                 "name": "goes here"  
             }                        
         }                            
     }                                
}
Run Code Online (Sandbox Code Playgroud)

我的光标位于"name":和"go here"之间,我想要一个显示我的命令(或状态行):

level1/level2/level3/name
Run Code Online (Sandbox Code Playgroud)

或类似的.

有这样的事吗?

mog*_*rod 6

我最近为此编写了一个名为vim-jsonpath的插件。目前它提供了以下命令(当然可以映射):

  • :JsonPath:回显光标下标识符的路径。
  • :JsonPath path.to.prop:在活动缓冲区中搜索给定路径,如果找到则将光标放在其上。


Ing*_*kat 3

我已经编写了两个使用折叠信息的映射(因此它们应该适用于任何结构,而不仅仅是 JSON)。对于你的例子,他们输出

{ / "leve1": { / "level2": { / "level3": {
Run Code Online (Sandbox Code Playgroud)

和(长版):

1  {
2      "leve1": {
3          "level2": {
4              "level3": {
Run Code Online (Sandbox Code Playgroud)

这是脚本。这取决于我的ingo-library 插件

" [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>
Run Code Online (Sandbox Code Playgroud)

您可以将其放入您的~/.vimrc(或单独的)中,并通过和~/.vim/plugin/PrintFoldHierarchy.vim从正常模式调用映射。z?z/