Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Minimal clang-rename integration with Vim. |
| 3 | |
| 4 | Before installing make sure one of the following is satisfied: |
| 5 | |
| 6 | * clang-rename is in your PATH |
| 7 | * `g:clang_rename_path` in ~/.vimrc points to valid clang-rename executable |
| 8 | * `binary` in clang-rename.py points to valid to clang-rename executable |
| 9 | |
Jonas Toth | 1188e5d | 2018-08-06 09:08:06 +0000 | [diff] [blame] | 10 | To install, simply put this into your ~/.vimrc for python2 support |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 11 | |
Kirill Bobyrev | c4018e2 | 2016-07-27 14:23:47 +0000 | [diff] [blame] | 12 | noremap <leader>cr :pyf <path-to>/clang-rename.py<cr> |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 13 | |
Jonas Toth | 1188e5d | 2018-08-06 09:08:06 +0000 | [diff] [blame] | 14 | For python3 use the following command (note the change from :pyf to :py3f) |
| 15 | |
| 16 | noremap <leader>cr :py3f <path-to>/clang-rename.py<cr> |
| 17 | |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 18 | IMPORTANT NOTE: Before running the tool, make sure you saved the file. |
| 19 | |
| 20 | All you have to do now is to place a cursor on a variable/function/class which |
Kirill Bobyrev | c4018e2 | 2016-07-27 14:23:47 +0000 | [diff] [blame] | 21 | you would like to rename and press '<leader>cr'. You will be prompted for a new |
| 22 | name if the cursor points to a valid symbol. |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 23 | ''' |
| 24 | |
Serge Guelton | b748c0e | 2018-12-18 16:07:37 +0000 | [diff] [blame] | 25 | from __future__ import absolute_import, division, print_function |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 26 | import vim |
| 27 | import subprocess |
| 28 | import sys |
| 29 | |
| 30 | def main(): |
| 31 | binary = 'clang-rename' |
| 32 | if vim.eval('exists("g:clang_rename_path")') == "1": |
Saleem Abdulrasool | b12d27b | 2016-07-19 02:13:08 +0000 | [diff] [blame] | 33 | binary = vim.eval('g:clang_rename_path') |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 34 | |
| 35 | # Get arguments for clang-rename binary. |
| 36 | offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2 |
| 37 | if offset < 0: |
Jonas Toth | 1188e5d | 2018-08-06 09:08:06 +0000 | [diff] [blame] | 38 | print('Couldn\'t determine cursor position. Is your file empty?', |
| 39 | file=sys.stderr) |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 40 | return |
| 41 | filename = vim.current.buffer.name |
| 42 | |
| 43 | new_name_request_message = 'type new name:' |
| 44 | new_name = vim.eval("input('{}\n')".format(new_name_request_message)) |
| 45 | |
| 46 | # Call clang-rename. |
| 47 | command = [binary, |
| 48 | filename, |
| 49 | '-i', |
| 50 | '-offset', str(offset), |
| 51 | '-new-name', str(new_name)] |
| 52 | # FIXME: make it possible to run the tool on unsaved file. |
| 53 | p = subprocess.Popen(command, |
| 54 | stdout=subprocess.PIPE, |
| 55 | stderr=subprocess.PIPE) |
| 56 | stdout, stderr = p.communicate() |
| 57 | |
| 58 | if stderr: |
Jonas Toth | 1188e5d | 2018-08-06 09:08:06 +0000 | [diff] [blame] | 59 | print(stderr) |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 60 | |
| 61 | # Reload all buffers in Vim. |
Kirill Bobyrev | bd80bbd | 2016-09-26 07:26:32 +0000 | [diff] [blame] | 62 | vim.command("checktime") |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | main() |