xref: /netbsd-src/external/apache2/llvm/dist/clang/tools/clang-rename/clang-rename.py (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg'''
2*7330f729SjoergMinimal clang-rename integration with Vim.
3*7330f729Sjoerg
4*7330f729SjoergBefore installing make sure one of the following is satisfied:
5*7330f729Sjoerg
6*7330f729Sjoerg* clang-rename is in your PATH
7*7330f729Sjoerg* `g:clang_rename_path` in ~/.vimrc points to valid clang-rename executable
8*7330f729Sjoerg* `binary` in clang-rename.py points to valid to clang-rename executable
9*7330f729Sjoerg
10*7330f729SjoergTo install, simply put this into your ~/.vimrc for python2 support
11*7330f729Sjoerg
12*7330f729Sjoerg    noremap <leader>cr :pyf <path-to>/clang-rename.py<cr>
13*7330f729Sjoerg
14*7330f729SjoergFor python3 use the following command (note the change from :pyf to :py3f)
15*7330f729Sjoerg
16*7330f729Sjoerg    noremap <leader>cr :py3f <path-to>/clang-rename.py<cr>
17*7330f729Sjoerg
18*7330f729SjoergIMPORTANT NOTE: Before running the tool, make sure you saved the file.
19*7330f729Sjoerg
20*7330f729SjoergAll you have to do now is to place a cursor on a variable/function/class which
21*7330f729Sjoergyou would like to rename and press '<leader>cr'. You will be prompted for a new
22*7330f729Sjoergname if the cursor points to a valid symbol.
23*7330f729Sjoerg'''
24*7330f729Sjoerg
25*7330f729Sjoergfrom __future__ import absolute_import, division, print_function
26*7330f729Sjoergimport vim
27*7330f729Sjoergimport subprocess
28*7330f729Sjoergimport sys
29*7330f729Sjoerg
30*7330f729Sjoergdef main():
31*7330f729Sjoerg    binary = 'clang-rename'
32*7330f729Sjoerg    if vim.eval('exists("g:clang_rename_path")') == "1":
33*7330f729Sjoerg        binary = vim.eval('g:clang_rename_path')
34*7330f729Sjoerg
35*7330f729Sjoerg    # Get arguments for clang-rename binary.
36*7330f729Sjoerg    offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2
37*7330f729Sjoerg    if offset < 0:
38*7330f729Sjoerg        print('Couldn\'t determine cursor position. Is your file empty?',
39*7330f729Sjoerg              file=sys.stderr)
40*7330f729Sjoerg        return
41*7330f729Sjoerg    filename = vim.current.buffer.name
42*7330f729Sjoerg
43*7330f729Sjoerg    new_name_request_message = 'type new name:'
44*7330f729Sjoerg    new_name = vim.eval("input('{}\n')".format(new_name_request_message))
45*7330f729Sjoerg
46*7330f729Sjoerg    # Call clang-rename.
47*7330f729Sjoerg    command = [binary,
48*7330f729Sjoerg               filename,
49*7330f729Sjoerg               '-i',
50*7330f729Sjoerg               '-offset', str(offset),
51*7330f729Sjoerg               '-new-name', str(new_name)]
52*7330f729Sjoerg    # FIXME: make it possible to run the tool on unsaved file.
53*7330f729Sjoerg    p = subprocess.Popen(command,
54*7330f729Sjoerg                         stdout=subprocess.PIPE,
55*7330f729Sjoerg                         stderr=subprocess.PIPE)
56*7330f729Sjoerg    stdout, stderr = p.communicate()
57*7330f729Sjoerg
58*7330f729Sjoerg    if stderr:
59*7330f729Sjoerg        print(stderr)
60*7330f729Sjoerg
61*7330f729Sjoerg    # Reload all buffers in Vim.
62*7330f729Sjoerg    vim.command("checktime")
63*7330f729Sjoerg
64*7330f729Sjoerg
65*7330f729Sjoergif __name__ == '__main__':
66*7330f729Sjoerg    main()
67