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