[PATCH] D21019: [include-fixer] try to make vim header selection more friendly.

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 6 05:13:59 PDT 2016


ioeric created this revision.
ioeric added reviewers: bkramer, hokein.
ioeric added a subscriber: cfe-commits.

use 'input()' to get user's input so that we can support more options.

http://reviews.llvm.org/D21019

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
@@ -35,12 +35,31 @@
       vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
 
 
-def ShowDialog(message, choices, default_choice_index=0):
-  to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
-                                                  choices.strip(),
-                                                  default_choice_index)
-  return int(vim.eval(to_eval));
-
+def GetUserSelection(message, headers, maximum_suggested_headers):
+  eval_message = message + '\n'
+  for idx, header in enumerate(headers[0:maximum_suggested_headers]):
+    eval_message += "({0}). {1}\n".format(idx+1, header)
+  eval_message += "Enter (q) to quit;"
+  if maximum_suggested_headers < len(headers):
+    eval_message += " (a) to show all candidates.";
+  eval_message += "\nSelect (default 1): "
+  to_eval = "input('{0}')".format(eval_message)
+  res = vim.eval(to_eval)
+  try:
+    idx = int(res)
+    if idx <= 0 or idx >= len(headers):
+      raise Exception()
+  except Exception:
+    if res == '':
+      # choose the top ranked header by default
+      idx = 1
+    elif res == 'q':
+      raise Exception('   Insertion cancelled...')
+    elif res == 'a' and maximum_suggested_headers < len(headers):
+      return GetUserSelection(message, headers, len(headers))
+    else:
+      raise Exception("   ERROR: Invalid option {0}...Abort!".format(res))
+  return headers[idx-1]
 
 def execute(command, text):
   p = subprocess.Popen(command,
@@ -88,7 +107,7 @@
 
   if not symbol:
     print "The file is fine, no need to add a header.\n"
-    return;
+    return
 
   if not headers:
     print "Couldn't find a header for {0}.\n".format(symbol)
@@ -102,19 +121,16 @@
     print "Added #include {0} for {1}.\n".format(headers[0], symbol)
     return
 
-  choices_message = ""
-  index = 1;
-  for header in headers[0:maximum_suggested_headers]:
-    choices_message += "&{0} {1}\n".format(index, header)
-    index += 1
-
-  select = ShowDialog("choose a header file for {0}.".format(symbol),
-                      choices_message)
-  # Insert a selected header.
-  InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
-                           "Headers":[headers[select-1]]}, text)
-  print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
-  return;
+  try:
+    selected = GetUserSelection("choose a header file for {0}.".format(symbol),
+                                headers, maximum_suggested_headers)
+    # Insert a selected header.
+    InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
+                             "Headers":[selected]}, text)
+    print "Added #include {0} for {1}.\n".format(selected, symbol)
+  except Exception as error:
+    print error.message
+  return
 
 
 if __name__ == '__main__':


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21019.59707.patch
Type: text/x-patch
Size: 2970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160606/cc394467/attachment.bin>


More information about the cfe-commits mailing list