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 | |
| 10 | To install, simply put this into your ~/.vimrc |
| 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 | |
| 14 | IMPORTANT NOTE: Before running the tool, make sure you saved the file. |
| 15 | |
| 16 | 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] | 17 | you would like to rename and press '<leader>cr'. You will be prompted for a new |
| 18 | name if the cursor points to a valid symbol. |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 19 | ''' |
| 20 | |
| 21 | import vim |
| 22 | import subprocess |
| 23 | import sys |
| 24 | |
| 25 | def main(): |
| 26 | binary = 'clang-rename' |
| 27 | if vim.eval('exists("g:clang_rename_path")') == "1": |
Saleem Abdulrasool | b12d27b | 2016-07-19 02:13:08 +0000 | [diff] [blame] | 28 | binary = vim.eval('g:clang_rename_path') |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 29 | |
| 30 | # Get arguments for clang-rename binary. |
| 31 | offset = int(vim.eval('line2byte(line("."))+col(".")')) - 2 |
| 32 | if offset < 0: |
| 33 | print >> sys.stderr, '''Couldn\'t determine cursor position. |
| 34 | Is your file empty?''' |
| 35 | return |
| 36 | filename = vim.current.buffer.name |
| 37 | |
| 38 | new_name_request_message = 'type new name:' |
| 39 | new_name = vim.eval("input('{}\n')".format(new_name_request_message)) |
| 40 | |
| 41 | # Call clang-rename. |
| 42 | command = [binary, |
| 43 | filename, |
| 44 | '-i', |
| 45 | '-offset', str(offset), |
| 46 | '-new-name', str(new_name)] |
| 47 | # FIXME: make it possible to run the tool on unsaved file. |
| 48 | p = subprocess.Popen(command, |
| 49 | stdout=subprocess.PIPE, |
| 50 | stderr=subprocess.PIPE) |
| 51 | stdout, stderr = p.communicate() |
| 52 | |
| 53 | if stderr: |
| 54 | print stderr |
| 55 | |
| 56 | # Reload all buffers in Vim. |
Kirill Bobyrev | bd80bbd | 2016-09-26 07:26:32 +0000 | [diff] [blame^] | 57 | vim.command("checktime") |
Benjamin Kramer | 3a12ca5 | 2016-07-07 14:35:32 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | if __name__ == '__main__': |
| 61 | main() |