[PATCH] D22087: [clang-rename] add basic vim integration

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 7 04:25:21 PDT 2016


omtcyf0 created this revision.
omtcyf0 added reviewers: alexfh, klimek.
omtcyf0 added a subscriber: cfe-commits.

This patch introduces basic Vim integration for clang-rename tool.

For setup reference see clang-rename/tool/clang-rename.py

http://reviews.llvm.org/D22087

Files:
  clang-rename/tool/ClangRename.cpp
  clang-rename/tool/clang-rename.py

Index: clang-rename/tool/clang-rename.py
===================================================================
--- /dev/null
+++ clang-rename/tool/clang-rename.py
@@ -0,0 +1,55 @@
+'''
+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
+
+    map ,cf :pyf <path-to>/clang-include-fixer.py<cr>
+
+All you have to do now is to place a cursor on a variable/function/class which
+you would like to rename and press ',cf'. You will be promted a new name if the
+cursor points to a valid symbol.
+'''
+
+import vim
+import subprocess
+
+
+def main():
+    binary = 'clang-rename'
+    if vim.eval('exists("g:clang_rename_path")') == "1":
+        binary = vim.eval('g:clang_rename')
+
+    # 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?'
+        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)]
+    p = subprocess.Popen(command,
+                         stdout=subprocess.PIPE,
+                         stderr=subprocess.PIPE,
+                         stdin=subprocess.PIPE)
+    stdout, stderr = p.communicate()
+
+    # Reload all buffers in Vim.
+    vim.command("bufdo edit")
+
+
+if __name__ == '__main__':
+    main()
Index: clang-rename/tool/ClangRename.cpp
===================================================================
--- clang-rename/tool/ClangRename.cpp
+++ clang-rename/tool/ClangRename.cpp
@@ -36,7 +36,6 @@
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/Support/Host.h"
-#include <cstdlib>
 #include <string>
 
 using namespace llvm;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22087.63050.patch
Type: text/x-patch
Size: 2252 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160707/1a3aa4bd/attachment-0001.bin>


More information about the cfe-commits mailing list