Coc.nvim: Something like split-if-not-open for coc.preferences.jumpCommand

Created on 24 Mar 2019  路  11Comments  路  Source: neoclide/coc.nvim

I want to split the window only if the target is not in the open files, none of edit, split, vsplit, tabe, drop and tab drop can make it done right, could we supply an option to do that?

low priority

Most helpful comment

@ramsgoli Thanks a lot! 馃憤

I modified it to suit my need 馃槄

function! CocSplitIfNotCurrentFile(...)
    " Ref: https://github.com/neoclide/coc.nvim/blob/7e9e0e91e24fc447e96079ae59e9f6caffe604a4/autoload/coc/util.vim#L380-L383
    let cursorCmd = ''
    let fname = a:1
    if a:0 == 2  " Two arguments.
        let cursorCmd = a:1
        let fname = a:2
    endif
    if fname != fnamemodify(expand('%'), ':p:~:.')
        exec 'split '.fname
    endif
    if len(cursorCmd)
        exec cursorCmd
    endif
endfunction

All 11 comments

the target is not in the open files

What does open means? Buffer loaded or shown in window?

Try use <C-o> and <C-i> for jump list.

Jump to definition will split the window if I using coc.preferences.jumpCommand="split" and the definition already in the open files.

I only want to split the window if the definition is not in the open files.

You can use drop if you want to use exists windows, it doesn't split but reuse current window so you can make use of jump list.

The behavior is not hard to implemented but I don't think it's necessary.

Yeah, but sometimes reopens the old one and closes the new one drive me crazy...

You can use :bd or use plugin like https://github.com/moll/vim-bbye if you want to remove current buffer and jump to previous one.

split-if-not-open could be confusing, you can create your own command for open files, and set it to "coc.preferences.jumpCommand"

@damnever I too had the same wish and figured out a solution that I wrote about here https://ramgo.li/posts/coc.nvim_split_if_not_open/ 馃槃

@ramsgoli Thanks a lot! 馃憤

I modified it to suit my need 馃槄

function! CocSplitIfNotCurrentFile(...)
    " Ref: https://github.com/neoclide/coc.nvim/blob/7e9e0e91e24fc447e96079ae59e9f6caffe604a4/autoload/coc/util.vim#L380-L383
    let cursorCmd = ''
    let fname = a:1
    if a:0 == 2  " Two arguments.
        let cursorCmd = a:1
        let fname = a:2
    endif
    if fname != fnamemodify(expand('%'), ':p:~:.')
        exec 'split '.fname
    endif
    if len(cursorCmd)
        exec cursorCmd
    endif
endfunction

@damnever I too had the same wish and figured out a solution that I wrote about here https://ramgo.li/posts/coc.nvim_split_if_not_open/

This function kept giving me a "not enough arguments" error when there were multiple definitions (causing the locations list to open), for example in a destructured require in Nodejs.

I modified it to detect which arguments were available, similar to @damnever's solution, which seems to fix the problem.

function! SplitIfNotOpen(...)
    let fname = a:1
    let call = ''
    if a:0 == 2
    let fname = a:2
    let call = a:1
    endif
    let bufnum=bufnr(expand(fname))
    let winnum=bufwinnr(bufnum)
    if winnum != -1
    " Jump to existing split
    exe winnum . "wincmd w"
    else
    " Make new split as usual
    exe "vsplit " . fname
    endif
    " Execute the cursor movement command
    exe call
endfunction

command! -nargs=+ CocSplitIfNotOpen :call SplitIfNotOpen(<f-args>)

@jack0son Nice!

It can be mapped with :call CocAction('jumpDefinition'). Works like a charm!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rkulla picture rkulla  路  3Comments

aareman picture aareman  路  3Comments

svenstaro picture svenstaro  路  4Comments

skylite21 picture skylite21  路  3Comments

chemzqm picture chemzqm  路  3Comments