[PATCH] D22351: [include-fixer] Move curosr to #include line in vim after inserting a missing header.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 14 06:40:53 PDT 2016


hokein created this revision.
hokein added a reviewer: bkramer.
hokein added a subscriber: cfe-commits.

A small improvement: for only one suggested header, user don't have to
type ENTER manually after running the python script.

https://reviews.llvm.org/D22351

Files:
  include-fixer/tool/clang-include-fixer.py

Index: include-fixer/tool/clang-include-fixer.py
===================================================================
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -84,12 +84,18 @@
   command = [binary, "-stdin", "-insert-header=" + json.dumps(header),
              vim.current.buffer.name]
   stdout, stderr = execute(command, text)
+  if stderr:
+    raise Exception(stderr)
   if stdout:
     lines = stdout.splitlines()
     sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
     for op in reversed(sequence.get_opcodes()):
       if op[0] is not 'equal':
         vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+    # Set cursor to the #include line.
+    include_header = "#include " + header["HeaderInfos"][0]["Header"]
+    line_num = lines.index(include_header) + 1
+    vim.current.window.cursor = (line_num, 0)
 
 
 def main():
@@ -135,15 +141,16 @@
     print "Couldn't find a header for {0}.\n".format(symbol)
     return
 
-  # If there is only one suggested header, insert it directly.
-  if len(unique_headers) == 1 or maximum_suggested_headers == 1:
-    InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
-                             "Range": include_fixer_context["Range"],
-                             "HeaderInfos": header_infos}, text)
-    print "Added #include {0} for {1}.\n".format(unique_headers[0], symbol)
-    return
-
+  message = "Added #include for {0}".format(symbol)
   try:
+    # If there is only one suggested header, insert it directly.
+    if len(unique_headers) == 1 or maximum_suggested_headers == 1:
+      InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+                               "Range": include_fixer_context["Range"],
+                               "HeaderInfos": header_infos}, text)
+      print message
+      return
+
     selected = GetUserSelection("choose a header file for {0}.".format(symbol),
                                 unique_headers, maximum_suggested_headers)
     selected_header_infos = [
@@ -153,7 +160,7 @@
     InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
                              "Range": include_fixer_context["Range"],
                              "HeaderInfos": selected_header_infos}, text)
-    print "Added #include {0} for {1}.\n".format(selected, symbol)
+    print message
   except Exception as error:
     print >> sys.stderr, error.message
   return


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22351.63967.patch
Type: text/x-patch
Size: 2438 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160714/b3b949f9/attachment.bin>


More information about the cfe-commits mailing list