[include-fixer] Add an options allow moving the cursor to #include line in vim
after inserting a missing header. Summary: A small improvement: Don't print newline character at the end of message, so that users don't have to type ENTER manually after running the python script. Reviewers: bkramer Subscribers: djasper, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D22351 llvm-svn: 275781
This commit is contained in:
		
							parent
							
								
									d7ae23b563
								
							
						
					
					
						commit
						fff3ad6e82
					
				| 
						 | 
					@ -40,6 +40,10 @@ if vim.eval('exists("g:clang_include_fixer_increment_num")') == "1":
 | 
				
			||||||
      1,
 | 
					      1,
 | 
				
			||||||
      vim.eval('g:clang_include_fixer_increment_num'))
 | 
					      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):
 | 
					def GetUserSelection(message, headers, maximum_suggested_headers):
 | 
				
			||||||
  eval_message = message + '\n'
 | 
					  eval_message = message + '\n'
 | 
				
			||||||
| 
						 | 
					@ -84,12 +88,21 @@ def InsertHeaderToVimBuffer(header, text):
 | 
				
			||||||
  command = [binary, "-stdin", "-insert-header=" + json.dumps(header),
 | 
					  command = [binary, "-stdin", "-insert-header=" + json.dumps(header),
 | 
				
			||||||
             vim.current.buffer.name]
 | 
					             vim.current.buffer.name]
 | 
				
			||||||
  stdout, stderr = execute(command, text)
 | 
					  stdout, stderr = execute(command, text)
 | 
				
			||||||
 | 
					  if stderr:
 | 
				
			||||||
 | 
					    raise Exception(stderr)
 | 
				
			||||||
  if stdout:
 | 
					  if stdout:
 | 
				
			||||||
    lines = stdout.splitlines()
 | 
					    lines = stdout.splitlines()
 | 
				
			||||||
    sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
 | 
					    sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
 | 
				
			||||||
 | 
					    line_num = None
 | 
				
			||||||
    for op in reversed(sequence.get_opcodes()):
 | 
					    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]]
 | 
					        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():
 | 
					def main():
 | 
				
			||||||
| 
						 | 
					@ -128,22 +141,22 @@ def main():
 | 
				
			||||||
      unique_headers.append(header)
 | 
					      unique_headers.append(header)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if not symbol:
 | 
					  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
 | 
					    return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if not unique_headers:
 | 
					  if not unique_headers:
 | 
				
			||||||
    print "Couldn't find a header for {0}.\n".format(symbol)
 | 
					    print "Couldn't find a header for {0}.".format(symbol)
 | 
				
			||||||
    return
 | 
					    return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  try:
 | 
				
			||||||
    # If there is only one suggested header, insert it directly.
 | 
					    # If there is only one suggested header, insert it directly.
 | 
				
			||||||
    if len(unique_headers) == 1 or maximum_suggested_headers == 1:
 | 
					    if len(unique_headers) == 1 or maximum_suggested_headers == 1:
 | 
				
			||||||
      InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
 | 
					      InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
 | 
				
			||||||
                               "Range": include_fixer_context["Range"],
 | 
					                               "Range": include_fixer_context["Range"],
 | 
				
			||||||
                               "HeaderInfos": header_infos}, text)
 | 
					                               "HeaderInfos": header_infos}, text)
 | 
				
			||||||
    print "Added #include {0} for {1}.\n".format(unique_headers[0], symbol)
 | 
					      print "Added #include {0} for {1}.".format(unique_headers[0], symbol)
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  try:
 | 
					 | 
				
			||||||
    selected = GetUserSelection("choose a header file for {0}.".format(symbol),
 | 
					    selected = GetUserSelection("choose a header file for {0}.".format(symbol),
 | 
				
			||||||
                                unique_headers, maximum_suggested_headers)
 | 
					                                unique_headers, maximum_suggested_headers)
 | 
				
			||||||
    selected_header_infos = [
 | 
					    selected_header_infos = [
 | 
				
			||||||
| 
						 | 
					@ -153,7 +166,7 @@ def main():
 | 
				
			||||||
    InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
 | 
					    InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
 | 
				
			||||||
                             "Range": include_fixer_context["Range"],
 | 
					                             "Range": include_fixer_context["Range"],
 | 
				
			||||||
                             "HeaderInfos": selected_header_infos}, text)
 | 
					                             "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:
 | 
					  except Exception as error:
 | 
				
			||||||
    print >> sys.stderr, error.message
 | 
					    print >> sys.stderr, error.message
 | 
				
			||||||
  return
 | 
					  return
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue