vim: Add clang-rename tool

Taken from
https://reviews.llvm.org/diffusion/L/browse/cfe/trunk/tools/clang-rename/clang-rename.py
This commit is contained in:
Mathieu Maret 2017-09-20 14:25:58 +02:00
parent da9b09ed58
commit 1d1ddf234d
2 changed files with 64 additions and 0 deletions

61
.vim/syntax/clang-rename.py Executable file
View File

@ -0,0 +1,61 @@
'''
Minimal clang-rename integration with Vim.
Before installing make sure one of the following is satisfied:
* clang-rename is in your PATH
* `g:clang_rename_path` in ~/.vimrc points to valid clang-rename executable
* `binary` in clang-rename.py points to valid to clang-rename executable
To install, simply put this into your ~/.vimrc
noremap <leader>cr :pyf <path-to>/clang-rename.py<cr>
IMPORTANT NOTE: Before running the tool, make sure you saved the file.
All you have to do now is to place a cursor on a variable/function/class which
you would like to rename and press '<leader>cr'. You will be prompted for a new
name if the cursor points to a valid symbol.
'''
import vim
import subprocess
import sys
def main():
binary = 'clang-rename'
if vim.eval('exists("g:clang_rename_path")') == "1":
binary = vim.eval('g:clang_rename_path')
# Get arguments for clang-rename binary.
offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2
if offset < 0:
print >> sys.stderr, '''Couldn\'t determine cursor position.
Is your file empty?'''
return
filename = vim.current.buffer.name
new_name_request_message = 'type new name:'
new_name = vim.eval("input('{}\n')".format(new_name_request_message))
# Call clang-rename.
command = [binary,
filename,
'-i',
'-offset', str(offset),
'-new-name', str(new_name)]
# FIXME: make it possible to run the tool on unsaved file.
p = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if stderr:
print stderr
# Reload all buffers in Vim.
vim.command("checktime")
if __name__ == '__main__':
main()

3
.vimrc
View File

@ -216,6 +216,9 @@ endif
map <C-K> :pyf ~/.vim/syntax/clang-format.py<CR>
imap <C-K> <ESC>:pyf ~/.vim/syntax/clang-format.py<CR>i
let g:clang_rename_path="clang-rename-4.0"
noremap <leader>cr :pyf ~/.vim/syntax/clang-rename.py<cr>
""""""""""
" SEARCH "
""""""""""