File: clang-rename.py

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (70 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
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 for python2 support

    noremap <leader>cr :pyf <path-to>/clang-rename.py<cr>

For python3 use the following command (note the change from :pyf to :py3f)

    noremap <leader>cr :py3f <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.
"""

from __future__ import absolute_import, division, print_function
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(
            "Couldn't determine cursor position. Is your file empty?", file=sys.stderr
        )
        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()