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

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 7 05:28:17 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL272004: [include-fixer] try to make vim header selection more friendly. (authored by ioeric).

Changed prior to commit:
  http://reviews.llvm.org/D21019?vs=59858&id=59871#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D21019

Files:
  clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.py

Index: clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.py
===================================================================
--- clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.py
+++ clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.py
@@ -35,12 +35,30 @@
       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): "
+  res = vim.eval("input('{0}')".format(eval_message))
+  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:
+    try:
+      idx = int(res)
+      if idx <= 0 or idx > len(headers):
+        raise Exception()
+    except Exception:
+        raise Exception('   ERROR: Invalid option "{0}"...Abort!'.format(res))
+  return headers[idx-1]
 
 def execute(command, text):
   p = subprocess.Popen(command,
@@ -88,7 +106,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 +120,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 >> sys.stderr, error.message
+  return
 
 
 if __name__ == '__main__':


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21019.59871.patch
Type: text/x-patch
Size: 3033 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160607/f2c4f212/attachment-0001.bin>


More information about the cfe-commits mailing list