lyuts / vim-rtags

Vim bindings for rtags, llvm/clang based c++ code indexer.
BSD 2-Clause "Simplified" License
282 stars 56 forks source link

jump list cause undotree change #65

Open pangchol opened 7 years ago

pangchol commented 7 years ago

HI dear lyuts, I find the undo tree will be changed when I using vim-rtags jump some times. I try to find out what cause it. then I think I find a bug about vim-rtags: in plugin/rtags.vim

rtags#JumpTo() call rtags#getCurrentLocation() to get then current locaition.

rtags#getCurrentLocation() using return printf("%s:%s:%s", expand("%"), lnum, col) to return the location. here expand("%") will got the relative file path. so then g:rtagsJumpStack list will storage a relative file path.

when We need jump back we call rtags#JumpBack() and it get the last location for g:rtagsJumpStack then call rtags#jumpToLocationInternal(jump_file, lnum, col).

in rtags#jumpToLocationInternal function , there have follow code:

           try
            if a:file != expand("%:p")
                exe "e ".a:file
            endif
            call cursor(a:line, a:col)
            return 1
        catch /.*/
            echohl ErrorMsg
            echomsg v:exception
            echohl None
            return 0
        endtry

here using expand("%:p"), then it well get the absolute file path, cause of a:file is relative file path. so this well always ture even if we jump back in a same file, so the exe "e ".a:file will always called. then problam is come from here:

I test in the vim and find that: if we using ":edit" command to reopen the same file which is opening. the undo tree ofen will change even the file do not change any longer. (using the ":echo undotree()" cmd can see it). bacause of it, vim-rtags's jump back function will ofen case undotree change when we jump back in a same file.

I suggest we can save the absolute file path in the g:rtagsJumpStack, because the relative file path well cause jump back failed when user set the 'autochdir' in the vimrc(the "pwd" path will change when user open a file not in current dir after vim open). the absolate file path will no this probalm.

here have some other suggest:

  1. filter the same posotion when save jump list:
    function! rtags#saveLocation()
    let [lnum, col] = getpos('.')[1:2]
    let oldpos = get(g:rtagsJumpStack, -1)
    if oldpos[0] != expand("%:p") || oldpos[1] != lnum
        call rtags#pushToStack([expand("%:p"), lnum, col])
    endif
    endfunction
  2. using ’normal zz‘ to let cursor always in the middle of window when jump back.