vim: update clang-rename

This commit is contained in:
Mathieu Maret 2024-03-06 17:58:18 +01:00
parent e6516a0517
commit 6feae4f96e
1 changed files with 27 additions and 18 deletions

View File

@ -1,4 +1,4 @@
''' """
Minimal clang-rename integration with Vim. Minimal clang-rename integration with Vim.
Before installing make sure one of the following is satisfied: Before installing make sure one of the following is satisfied:
@ -7,55 +7,64 @@ Before installing make sure one of the following is satisfied:
* `g:clang_rename_path` in ~/.vimrc points to valid clang-rename executable * `g:clang_rename_path` in ~/.vimrc points to valid clang-rename executable
* `binary` in clang-rename.py points to valid to clang-rename executable * `binary` in clang-rename.py points to valid to clang-rename executable
To install, simply put this into your ~/.vimrc To install, simply put this into your ~/.vimrc for python2 support
noremap <leader>cr :pyf <path-to>/clang-rename.py<cr> 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. 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 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 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. name if the cursor points to a valid symbol.
''' """
from __future__ import absolute_import, division, print_function
import vim import vim
import subprocess import subprocess
import sys import sys
def main(): def main():
binary = 'clang-rename' binary = "clang-rename"
if vim.eval('exists("g:clang_rename_path")') == "1": if vim.eval('exists("g:clang_rename_path")') == "1":
binary = vim.eval('g:clang_rename_path') binary = vim.eval("g:clang_rename_path")
# Get arguments for clang-rename binary. # Get arguments for clang-rename binary.
offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2 offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2
if offset < 0: if offset < 0:
print >> sys.stderr, '''Couldn\'t determine cursor position. print(
Is your file empty?''' "Couldn't determine cursor position. Is your file empty?", file=sys.stderr
)
return return
filename = vim.current.buffer.name filename = vim.current.buffer.name
new_name_request_message = 'type new name:' new_name_request_message = "type new name:"
new_name = vim.eval("input('{}\n')".format(new_name_request_message)) new_name = vim.eval("input('{}\n')".format(new_name_request_message))
# Call clang-rename. # Call clang-rename.
command = [binary, command = [
filename, binary,
'-i', filename,
'-offset', str(offset), "-i",
'-new-name', str(new_name)] "-offset",
str(offset),
"-new-name",
str(new_name),
]
# FIXME: make it possible to run the tool on unsaved file. # FIXME: make it possible to run the tool on unsaved file.
p = subprocess.Popen(command, p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
if stderr: if stderr:
print stderr print(stderr)
# Reload all buffers in Vim. # Reload all buffers in Vim.
vim.command("checktime") vim.command("checktime")
if __name__ == '__main__': if __name__ == "__main__":
main() main()