blob: 3cc6644ff8f0a93a6da137a5f0b833d27c663b8d [file] [log] [blame]
Benjamin Kramer3a12ca52016-07-07 14:35:32 +00001'''
2Minimal clang-rename integration with Vim.
3
4Before installing make sure one of the following is satisfied:
5
6* clang-rename is in your PATH
7* `g:clang_rename_path` in ~/.vimrc points to valid clang-rename executable
8* `binary` in clang-rename.py points to valid to clang-rename executable
9
10To install, simply put this into your ~/.vimrc
11
Kirill Bobyrevc4018e22016-07-27 14:23:47 +000012 noremap <leader>cr :pyf <path-to>/clang-rename.py<cr>
Benjamin Kramer3a12ca52016-07-07 14:35:32 +000013
14IMPORTANT NOTE: Before running the tool, make sure you saved the file.
15
16All you have to do now is to place a cursor on a variable/function/class which
Kirill Bobyrevc4018e22016-07-27 14:23:47 +000017you would like to rename and press '<leader>cr'. You will be prompted for a new
18name if the cursor points to a valid symbol.
Benjamin Kramer3a12ca52016-07-07 14:35:32 +000019'''
20
21import vim
22import subprocess
23import sys
24
25def main():
26 binary = 'clang-rename'
27 if vim.eval('exists("g:clang_rename_path")') == "1":
Saleem Abdulrasoolb12d27b2016-07-19 02:13:08 +000028 binary = vim.eval('g:clang_rename_path')
Benjamin Kramer3a12ca52016-07-07 14:35:32 +000029
30 # Get arguments for clang-rename binary.
31 offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2
32 if offset < 0:
33 print >> sys.stderr, '''Couldn\'t determine cursor position.
34 Is your file empty?'''
35 return
36 filename = vim.current.buffer.name
37
38 new_name_request_message = 'type new name:'
39 new_name = vim.eval("input('{}\n')".format(new_name_request_message))
40
41 # Call clang-rename.
42 command = [binary,
43 filename,
44 '-i',
45 '-offset', str(offset),
46 '-new-name', str(new_name)]
47 # FIXME: make it possible to run the tool on unsaved file.
48 p = subprocess.Popen(command,
49 stdout=subprocess.PIPE,
50 stderr=subprocess.PIPE)
51 stdout, stderr = p.communicate()
52
53 if stderr:
54 print stderr
55
56 # Reload all buffers in Vim.
Kirill Bobyrevbd80bbd2016-09-26 07:26:32 +000057 vim.command("checktime")
Benjamin Kramer3a12ca52016-07-07 14:35:32 +000058
59
60if __name__ == '__main__':
61 main()