2024-03-06 17:58:18 +01:00
|
|
|
"""
|
2017-09-20 14:25:58 +02:00
|
|
|
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
|
|
|
|
|
2024-03-06 17:58:18 +01:00
|
|
|
To install, simply put this into your ~/.vimrc for python2 support
|
2017-09-20 14:25:58 +02:00
|
|
|
|
|
|
|
noremap <leader>cr :pyf <path-to>/clang-rename.py<cr>
|
|
|
|
|
2024-03-06 17:58:18 +01:00
|
|
|
For python3 use the following command (note the change from :pyf to :py3f)
|
|
|
|
|
|
|
|
noremap <leader>cr :py3f <path-to>/clang-rename.py<cr>
|
|
|
|
|
2017-09-20 14:25:58 +02:00
|
|
|
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.
|
2024-03-06 17:58:18 +01:00
|
|
|
"""
|
2017-09-20 14:25:58 +02:00
|
|
|
|
2024-03-06 17:58:18 +01:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2017-09-20 14:25:58 +02:00
|
|
|
import vim
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2024-03-06 17:58:18 +01:00
|
|
|
|
2017-09-20 14:25:58 +02:00
|
|
|
def main():
|
2024-03-06 17:58:18 +01:00
|
|
|
binary = "clang-rename"
|
2017-09-20 14:25:58 +02:00
|
|
|
if vim.eval('exists("g:clang_rename_path")') == "1":
|
2024-03-06 17:58:18 +01:00
|
|
|
binary = vim.eval("g:clang_rename_path")
|
2017-09-20 14:25:58 +02:00
|
|
|
|
|
|
|
# Get arguments for clang-rename binary.
|
|
|
|
offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2
|
|
|
|
if offset < 0:
|
2024-03-06 17:58:18 +01:00
|
|
|
print(
|
|
|
|
"Couldn't determine cursor position. Is your file empty?", file=sys.stderr
|
|
|
|
)
|
2017-09-20 14:25:58 +02:00
|
|
|
return
|
|
|
|
filename = vim.current.buffer.name
|
|
|
|
|
2024-03-06 17:58:18 +01:00
|
|
|
new_name_request_message = "type new name:"
|
2017-09-20 14:25:58 +02:00
|
|
|
new_name = vim.eval("input('{}\n')".format(new_name_request_message))
|
|
|
|
|
|
|
|
# Call clang-rename.
|
2024-03-06 17:58:18 +01:00
|
|
|
command = [
|
|
|
|
binary,
|
|
|
|
filename,
|
|
|
|
"-i",
|
|
|
|
"-offset",
|
|
|
|
str(offset),
|
|
|
|
"-new-name",
|
|
|
|
str(new_name),
|
|
|
|
]
|
2017-09-20 14:25:58 +02:00
|
|
|
# FIXME: make it possible to run the tool on unsaved file.
|
2024-03-06 17:58:18 +01:00
|
|
|
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2017-09-20 14:25:58 +02:00
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
|
|
|
|
if stderr:
|
2024-03-06 17:58:18 +01:00
|
|
|
print(stderr)
|
2017-09-20 14:25:58 +02:00
|
|
|
|
|
|
|
# Reload all buffers in Vim.
|
|
|
|
vim.command("checktime")
|
|
|
|
|
|
|
|
|
2024-03-06 17:58:18 +01:00
|
|
|
if __name__ == "__main__":
|
2017-09-20 14:25:58 +02:00
|
|
|
main()
|