ycm-core / YouCompleteMe

A code-completion engine for Vim
http://ycm-core.github.io/YouCompleteMe/
GNU General Public License v3.0
25.36k stars 2.8k forks source link

Feature Request: RefactorRename support in C++ through clang-rename #3456

Closed ozars closed 4 years ago

ozars commented 4 years ago

Issue Prelude

Please complete these steps and check these boxes (by putting an x inside the brackets) before filing your issue:

Issue Details

I was wondering if RefactorRename would be supoorted in C++ projects using clang-rename.

There are two somewhat limited ways I could find for integrating it to vim currently: the integration provided by clang and third party vim-clang-rename plugin. Official integration replaces files inplace, therefore it requires saving buffers before and loading them after. Compiler flags are read from compilation database. This is somewhat limited compared to .ycm_extra_config (e.g. header files). The latter plugin has an option g:clang_rename#flags, but it didn't look like a convenient way for managing flags for multiple projects.

This would be a vastly helpful feature if it were to be included in YCM. Thanks.

puremourning commented 4 years ago

clangd has local-rename support. See the docs for how to use clangd.

puremourning commented 4 years ago

Re-open because the docs don't actually say which features clangd supports vs libclang. Maybe we can improve that. Anyway let us know how you get on with clangd.

ozars commented 4 years ago

Thanks. I tried it with clangd, but it appears it doesn't recognize .ycm_extra_conf.py.

I tried it with this file:

  #include <variant>                                                                                     

  int main() {                                                                                           
      std::variant<int, double> myvar;                                                                   
      myvar = 3;                                                                                         
      myvar = 5.0;                                                                                       
      return 0;                                                                                          
  }              

It gives below errors:

rename.cpp|4 col 5 error| Use of undeclared identifier 'std' [undeclared_var_use]                        
rename.cpp|5 col 5 error| Use of undeclared identifier 'myvar' [undeclared_var_use]                      
rename.cpp|6 col 5 error| Use of undeclared identifier 'myvar' [undeclared_var_use]

Also tried with this and it gives error:

  #if __cplusplus == 201402L                                                                             
  #error "Using C++14"                                                                                   
  #endif

My .ycm_extra_conf is pretty much same with this except it uses C++17:

 ~/tmp/rename  diff .ycm_extra_conf.py <(wget https://raw.githubusercontent.com/puremourning/ycmd-1/master/ycmd/default_ycm_extra_conf.py -O-)

Redirecting output to ‘wget-log’.
148c148
<     '-std=c++17',
---
>     '-std=c++11',
155c155
<     '-std=c11',
---
>     '-std=c99',

Some more debugging info:

Printing YouCompleteMe debug information...                                                              
-- Client logfile: /tmp/ycm_d19xl7kt.log                                                                 
-- Server Python interpreter: /usr/bin/python3                                                           
-- Server Python version: 3.7.3                                                                          
-- Server has Clang support compiled in: True                                                            
-- Clang version: clang version 7.0.0 (tags/RELEASE_700/final)                                           
-- Extra configuration file found and loaded                                                             
-- Extra configuration path: /home/omer/tmp/rename/.ycm_extra_conf.py                                    
-- C-family completer debug information:                                                                 
--   Clangd running                                                                                      
--   Clangd process ID: 26346                                                                            
--   Clangd executable: ['/usr/bin/clangd-9', '-header-insertion-decorators=0']                          
--   Clangd logfiles:                                                                                    
--     /tmp/clangd_stderrrxj5mzz2.log                                                                    
--   Clangd Server State: Initialized                                                                    
--   Clangd Project Directory: /home/omer/tmp/rename                                                     
--   Clangd Settings: {}                                                                                 
--   Clangd Compilation Command: False                                                                   
-- Server running at: http://127.0.0.1:50865                                                             
-- Server process ID: 26302                                                                              
-- Server logfiles:                                                                                      
--   /tmp/ycmd_50865_stdout_2rk5pd9i.log                                                                 
--   /tmp/ycmd_50865_stderr_xlw92ses.log

/tmp/ycm_d19xl7kt.log was empty

/tmp/clangd_stderrrxj5mzz2.log

I[15:25:00.320] <-- initialize("1")                                                                      
I[15:25:00.320] --> reply:initialize("1") 0 ms                                                           
I[15:25:00.320] <-- initialized                                                                          
I[15:25:00.320] unhandled notification initialized                                                       
I[15:25:00.320] <-- workspace/didChangeConfiguration                                                     
I[15:25:00.320] <-- textDocument/didOpen                                                                 
I[15:25:00.321] Failed to find compilation database for /home/omer/tmp/rename/rename.cpp                 
I[15:25:00.321] Updating file /home/omer/tmp/rename/rename.cpp with command clangd fallback              
[/home/omer/tmp/rename]                                                                                  
/usr/lib/llvm-9/bin/clang /home/omer/tmp/rename/rename.cpp -fsyntax-only -resource-dir=/usr/lib/llvm-9/lib/clang/9.0.0                                                                                            
I[15:25:00.328] --> textDocument/publishDiagnostics                                                      

/tmp/ycmd_50865_stdout_2rk5pd9i.log

serving on http://localhost:50865                                                                        

/tmp/ycmd_50865_stderr_xlw92ses.log

2019-07-31 15:25:00,275 - INFO - Received ready request                                                  
2019-07-31 15:25:00,277 - INFO - Received event notification                                             
2019-07-31 15:25:00,278 - INFO - Received event notification                                             
2019-07-31 15:25:00,278 - INFO - Adding buffer identifiers for file: /home/omer/tmp/rename/rename.cpp    
2019-07-31 15:25:00,308 - INFO - Using Clangd from /usr/bin/clangd-9                                     
2019-07-31 15:25:00,309 - INFO - Computed Clangd command: ['/usr/bin/clangd-9', '-header-insertion-decorators=0']                                                                                                 
2019-07-31 15:25:00,309 - INFO - Returning cached Clangd command: ['/usr/bin/clangd-9', '-header-insertion-decorators=0']                                                                                         
2019-07-31 15:25:00,310 - INFO - Starting Clangd: ['/usr/bin/clangd-9', '-header-insertion-decorators=0']2019-07-31 15:25:00,313 - INFO - Clangd started                                                          
2019-07-31 15:25:00,320 - INFO - Language server requires sync type of Incremental                       
2019-07-31 15:25:00,380 - INFO - Received filetype completion available request                          
2019-07-31 15:25:10,234 - INFO - Received debug info request                                             
2019-07-31 15:25:48,716 - INFO - Received debug info request                                             

vim --version

VIM - Vi IMproved 8.1 (2018 May 18, compiled Jun 15 2019 16:41:15)                                       Included patches: 1-875, 878, 884, 948, 1046, 1365-1368, 1382, 1401                                      Modified by team+vim@tracker.debian.org                                                                  Compiled by team+vim@tracker.debian.org                                                                  Huge version with GTK3 GUI.  Features included (+) or not (-):
+acl               +extra_search      +mouse_netterm     +tag_old_static                                 +arabic            +farsi             +mouse_sgr         -tag_any_white                                  +autocmd           +file_in_path      -mouse_sysmouse    +tcl                                           
+autochdir         +find_in_path      +mouse_urxvt       +termguicolors                                  -autoservername    +float             +mouse_xterm       +terminal                                       +balloon_eval      +folding           +multi_byte        +terminfo                                       +balloon_eval_term -footer            +multi_lang        +termresponse
+browse            +fork()            -mzscheme          +textobjects
++builtin_terms    +gettext           +netbeans_intg     +textprop
+byte_offset       -hangul_input      +num64             +timers
+channel           +iconv             +packages          +title
+cindent           +insert_expand     +path_extra        +toolbar
+clientserver      +job               +perl              +user_commands
+clipboard         +jumplist          +persistent_undo   +vartabs
+cmdline_compl     +keymap            +postscript        +vertsplit
+cmdline_hist      +lambda            +printer           +virtualedit
+cmdline_info      +langmap           +profile           +visual
+comments          +libcall           -python            +visualextra
+conceal           +linebreak         +python3           +viminfo
+cryptv            +lispindent        +quickfix          +vreplace
+cscope            +listcmds          +reltime           +wildignore
+cursorbind        +localmap          +rightleft         +wildmenu
+cursorshape       +lua               +ruby              +windows
+dialog_con_gui    +menu              +scrollbind        +writebackup
+diff              +mksession         +signs             +X11
+digraphs          +modify_fname      +smartindent       -xfontset
+dnd               +mouse             +startuptime       +xim
-ebcdic            +mouseshape        +statusline        +xpm
+emacs_tags        +mouse_dec         -sun_workshop      +xsmp_interact
+eval              +mouse_gpm         +syntax            +xterm_clipboard
+ex_extra          -mouse_jsbterm     +tag_binary        -xterm_save
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cairo -I/usr/include/libdrm -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/fribidi -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/uuid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -Wdate-time  -g -O2 -fdebug-prefix-map=/build/vim-4Pursk/vim-8.1.0875=. -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
Linking: gcc   -L. -Wl,-z,relro -Wl,-z,now -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E  -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim   -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE  -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl  -L/usr/lib -llua5.2 -Wl,-E  -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/x86_64-linux-gnu/perl/5.28/CORE -lperl -ldl -lm -lpthread -lcrypt  -L/usr/lib/python3.7/config-3.7m-x86_64-linux-gnu -lpython3.7m -lcrypt -lpthread -ldl -lutil -lm -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -ldl -lz -lpthread -lm -lruby-2.5 -lpthread -lgmp -ldl -lcrypt -lm

git stuff:

 ~/.vim/plugged/YouCompleteMe   master  git --no-pager log -1 --pretty=oneline
afa2ea03d03e6793d34704e4c75f2846ecbffd52 (HEAD -> master, origin/master, origin/HEAD) Add link to compiledb to README
 ~/.vim/plugged/YouCompleteMe   master  git --no-pager submodule foreach git log -1 --pretty=oneline
Entering 'third_party/python-future'
a8114e48ce7dbc4cecbf6a764d73e83d03b0d6ba (HEAD, origin/master, origin/HEAD, master) Merge pull request #406 from jmadler/v0.17.x
Entering 'third_party/requests-futures'
98712e7d0f6be2a090b6fda2a925f85e63656b58 (HEAD) Merge pull request #10 from mkai/_max_workers_poolsize
Entering 'third_party/requests_deps/certifi'
5b9e05c06e69fe5c7835052cfc3ae1c899dfc8b1 (HEAD) Merge pull request #96 from adamchainz/https
Entering 'third_party/requests_deps/chardet'
9b8c5c2fb118d76c6beeab9affd01c332732a530 (HEAD, tag: 3.0.4, origin/stable) Bump version to 3.0.4
Entering 'third_party/requests_deps/idna'
0f50bdcea71e6602bf4cd22897970d71fc4074d9 (HEAD) Preliminary Unicode 11.0.0 data
Entering 'third_party/requests_deps/requests'
6cfbe1aedd56f8c2f9ff8b968efe65b22669795b (HEAD, tag: v2.20.1) v2.20.1
Entering 'third_party/requests_deps/urllib3'
a6ec68a5c5c5743c59fe5c62c635c929586c429b (HEAD, tag: 1.24.1, origin/release) Merging new release version: 1.24.1
Entering 'third_party/ycmd'
14a616c846f13a9ccb3003b04f390bd6071c7a95 (HEAD) Merge pull request #1276 from bstaletic/cs-no-debug-subs
ozars commented 4 years ago

Nevermind. It's stated in README that clangd doesn't support this. hmph.

puremourning commented 4 years ago

are you using the latest ycm ? ycm supports extra conf with clangd but you have to use the Settings function rather than FlagsForFile

ozars commented 4 years ago

Yes. I tested it with up to date YCM. I was actually using something based on your default config, but I guess I need to add Settings to it. Let me try adding it.

ozars commented 4 years ago

Appending Settings solves it indeed:

def Settings( **kwargs ):                                                                                
    return FlagsForFile( kwargs[ 'filename' ], **kwargs)

:YcmCompleter RefactorRename also worked fine. Thanks!

puremourning commented 4 years ago

Great!

ozars commented 4 years ago

Correction:

def Settings( **kwargs ):
    filename = kwargs.pop( 'filename' )
    return FlagsForFile( filename, **kwargs )
mstaz commented 3 years ago

For information: To get multi-file refactor support the clangd's experimental cross-file-rename must be enabled with option --cross-file-rename, e.g. let g:ycm_clangd_args = ['-cross-file-rename']

mr-j0nes commented 3 years ago

For information: To get multi-file refactor support the clangd's experimental cross-file-rename must be enabled with option --cross-file-rename, e.g. let g:ycm_clangd_args = ['-cross-file-rename']

This should be added to the documentation, section "Multi-file Refactor" ...

puremourning commented 3 years ago

For information: To get multi-file refactor support the clangd's experimental cross-file-rename must be enabled with option --cross-file-rename, e.g. let g:ycm_clangd_args = ['-cross-file-rename']

This should be added to the documentation, section "Multi-file Refactor" ...

I believe this flag is no longer needed now that we're on clangd 11.

mr-j0nes commented 3 years ago

For information: To get multi-file refactor support the clangd's experimental cross-file-rename must be enabled with option --cross-file-rename, e.g. let g:ycm_clangd_args = ['-cross-file-rename']

This should be added to the documentation, section "Multi-file Refactor" ...

I believe this flag is no longer needed now that we're on clangd 11.

Yesterday I installed YCM as a fresh installation and it installed clangd version 10. It took me a while to find this thread to make the RefactorRename feature work.

#:~/.vim/bundle/YouCompleteMe$ ./third_party/ycmd/third_party/clangd/output/bin/clangd --version
clangd version 10.0.0 (https://github.com/ycm-core/llvm 038587147cf2f97d1c3e677042f69560c65b5bea)

How to make it install clangd 11 ?

puremourning commented 3 years ago

Oh maybe we didn’t update the ycmd submodule yet. It won’t be needed when we do that.