]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/clang-rename/clang-rename.py
Vendor import of clang trunk r306956:
[FreeBSD/FreeBSD.git] / tools / clang-rename / clang-rename.py
1 '''
2 Minimal clang-rename integration with Vim.
3
4 Before 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
10 To install, simply put this into your ~/.vimrc
11
12     noremap <leader>cr :pyf <path-to>/clang-rename.py<cr>
13
14 IMPORTANT NOTE: Before running the tool, make sure you saved the file.
15
16 All you have to do now is to place a cursor on a variable/function/class which
17 you would like to rename and press '<leader>cr'. You will be prompted for a new
18 name if the cursor points to a valid symbol.
19 '''
20
21 import vim
22 import subprocess
23 import sys
24
25 def main():
26     binary = 'clang-rename'
27     if vim.eval('exists("g:clang_rename_path")') == "1":
28         binary = vim.eval('g:clang_rename_path')
29
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.
57     vim.command("checktime")
58
59
60 if __name__ == '__main__':
61     main()