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

2016-07-18 Thread Benjamin Kramer via cfe-commits
bkramer accepted this revision.
bkramer added a comment.
This revision is now accepted and ready to land.

lgtm now


https://reviews.llvm.org/D22351



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2016-07-15 Thread Haojian Wu via cfe-commits
hokein marked an inline comment as done.
hokein added a comment.

https://reviews.llvm.org/D22351



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2016-07-15 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 64110.
hokein added a comment.

Avoid a hacky way to get #include line number.


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
@@ -40,6 +40,10 @@
   1,
   vim.eval('g:clang_include_fixer_increment_num'))
 
+jump_to_include = False
+if vim.eval('exists("g:clang_include_fixer_jump_to_include")') == "1":
+  jump_to_include = vim.eval('g:clang_include_fixer_jump_to_include') != "0"
+
 
 def GetUserSelection(message, headers, maximum_suggested_headers):
   eval_message = message + '\n'
@@ -84,12 +88,21 @@
   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)
+line_num = None
 for op in reversed(sequence.get_opcodes()):
-  if op[0] is not 'equal':
+  if op[0] != 'equal':
 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+  if op[0] == 'insert':
+# line_num in vim is 1-based.
+line_num = op[1] + 1
+
+if jump_to_include and line_num:
+  vim.current.window.cursor = (line_num, 0)
 
 
 def main():
@@ -128,22 +141,22 @@
   unique_headers.append(header)
 
   if not symbol:
-print "The file is fine, no need to add a header.\n"
+print "The file is fine, no need to add a header."
 return
 
   if not unique_headers:
-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)
+print "Couldn't find a header for {0}.".format(symbol)
 return
 
   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 "Added #include {0} for {1}.".format(unique_headers[0], symbol)
+  return
+
 selected = GetUserSelection("choose a header file for {0}.".format(symbol),
 unique_headers, maximum_suggested_headers)
 selected_header_infos = [
@@ -153,7 +166,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 "Added #include {0} for {1}.".format(selected, symbol)
   except Exception as error:
 print >> sys.stderr, error.message
   return


Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -40,6 +40,10 @@
   1,
   vim.eval('g:clang_include_fixer_increment_num'))
 
+jump_to_include = False
+if vim.eval('exists("g:clang_include_fixer_jump_to_include")') == "1":
+  jump_to_include = vim.eval('g:clang_include_fixer_jump_to_include') != "0"
+
 
 def GetUserSelection(message, headers, maximum_suggested_headers):
   eval_message = message + '\n'
@@ -84,12 +88,21 @@
   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)
+line_num = None
 for op in reversed(sequence.get_opcodes()):
-  if op[0] is not 'equal':
+  if op[0] != 'equal':
 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
+  if op[0] == 'insert':
+# line_num in vim is 1-based.
+line_num = op[1] + 1
+
+if jump_to_include and line_num:
+  vim.current.window.cursor = (line_num, 0)
 
 
 def main():
@@ -128,22 +141,22 @@
   unique_headers.append(header)
 
   if not symbol:
-print "The file is fine, no need to add a header.\n"
+print "The file is fine, no need to add a header."
 return
 
   if not unique_headers:
-print "Couldn't find a header for {0}.\n".format(symbol)
-return
-
-  # If there is 

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

2016-07-15 Thread Daniel Jasper via cfe-commits
djasper added a comment.

FWIW, I think I'd like this behavior as it shows me what is actually being 
done. I can undo/redo to double-check or C-O to get back to where I was. Of 
course making it configurable seems like the right thing to do.



Comment at: include-fixer/tool/clang-include-fixer.py:102
@@ +101,3 @@
+  # Set cursor to the #include line.
+  include_header = "#include " + header["HeaderInfos"][0]["Header"]
+  line_num = lines.index(include_header) + 1

Seems seems very hacky. Can't you extract it out of the diff sequence above?


https://reviews.llvm.org/D22351



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2016-07-14 Thread Haojian Wu via cfe-commits
hokein added a comment.

+Daniel who suggests this feature ;)


https://reviews.llvm.org/D22351



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2016-07-14 Thread Haojian Wu via cfe-commits
hokein updated this revision to Diff 64004.
hokein added a comment.

Add an option to make move-cursor configurable.


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
@@ -40,6 +40,10 @@
   1,
   vim.eval('g:clang_include_fixer_increment_num'))
 
+jump_to_include = False
+if vim.eval('exists("g:clang_include_fixer_jump_to_include")') == "1":
+  jump_to_include = vim.eval('g:clang_include_fixer_jump_to_include') != "0"
+
 
 def GetUserSelection(message, headers, maximum_suggested_headers):
   eval_message = message + '\n'
@@ -84,13 +88,21 @@
   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]]
 
+if jump_to_include:
+  # 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():
   parser = argparse.ArgumentParser(
@@ -128,22 +140,22 @@
   unique_headers.append(header)
 
   if not symbol:
-print "The file is fine, no need to add a header.\n"
+print "The file is fine, no need to add a header."
 return
 
   if not unique_headers:
-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)
+print "Couldn't find a header for {0}.".format(symbol)
 return
 
   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 "Added #include {0} for {1}.".format(unique_headers[0], symbol)
+  return
+
 selected = GetUserSelection("choose a header file for {0}.".format(symbol),
 unique_headers, maximum_suggested_headers)
 selected_header_infos = [
@@ -153,7 +165,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 "Added #include {0} for {1}.".format(selected, symbol)
   except Exception as error:
 print >> sys.stderr, error.message
   return


Index: include-fixer/tool/clang-include-fixer.py
===
--- include-fixer/tool/clang-include-fixer.py
+++ include-fixer/tool/clang-include-fixer.py
@@ -40,6 +40,10 @@
   1,
   vim.eval('g:clang_include_fixer_increment_num'))
 
+jump_to_include = False
+if vim.eval('exists("g:clang_include_fixer_jump_to_include")') == "1":
+  jump_to_include = vim.eval('g:clang_include_fixer_jump_to_include') != "0"
+
 
 def GetUserSelection(message, headers, maximum_suggested_headers):
   eval_message = message + '\n'
@@ -84,13 +88,21 @@
   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]]
 
+if jump_to_include:
+  # 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():
   parser = argparse.ArgumentParser(
@@ -128,22 +140,22 @@
   unique_headers.append(header)
 
   if not symbol:
-print "The file is fine, no need to add a header.\n"
+print "The file is fine, no need to add a header."
 return
 
   if not unique_headers:
-print 

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

2016-07-14 Thread Manuel Klimek via cfe-commits
klimek added a subscriber: klimek.
klimek added a comment.

+1 to not throwing users around by default. Can we make it configurable if 
folks want it?


https://reviews.llvm.org/D22351



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2016-07-14 Thread Benjamin Kramer via cfe-commits
bkramer added a comment.

I'm not entirely sure if jumping cursors are a good user interface. It will 
completely throw the user out of context, which is exactly what we want to 
avoid with include-fixer.


https://reviews.llvm.org/D22351



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits