blob: f7fc011631ba0315a3b1fb9d324eefb5e88ee6e1 [file] [log] [blame]
Eric Liuc7f3b102016-05-18 14:10:16 +00001# This file is a minimal clang-include-fixer vim-integration. To install:
2# - Change 'binary' if clang-include-fixer is not on the path (see below).
3# - Add to your .vimrc:
4#
5# map ,cf :pyf path/to/llvm/source/tools/clang/tools/extra/include-fixer/tool/clang-include-fixer.py<cr>
6#
7# This enables clang-include-fixer for NORMAL and VISUAL mode. Change ",cf" to
8# another binding if you need clang-include-fixer on a different key.
9#
10# To set up clang-include-fixer, see http://clang.llvm.org/extra/include-fixer.html
11#
12# With this integration you can press the bound key and clang-include-fixer will
13# be run on the current buffer.
14#
15# It operates on the current, potentially unsaved buffer and does not create
16# or save any files. To revert a fix, just undo.
17
18import argparse
Eric Liu702cfd12016-05-19 08:21:09 +000019import difflib
Eric Liuc7f3b102016-05-18 14:10:16 +000020import subprocess
Eric Liuc7f3b102016-05-18 14:10:16 +000021import vim
Haojian Wu17a54e32016-06-01 11:43:10 +000022import json
Eric Liuc7f3b102016-05-18 14:10:16 +000023
24# set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
25# on the path.
26# Change this to the full path if clang-include-fixer is not on the path.
27binary = 'clang-include-fixer'
28if vim.eval('exists("g:clang_include_fixer_path")') == "1":
29 binary = vim.eval('g:clang_include_fixer_path')
30
Haojian Wu11e9bd22016-05-31 09:31:51 +000031maximum_suggested_headers=3
32if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
33 maximum_suggested_headers = max(
34 1,
35 vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
36
37
38def ShowDialog(message, choices, default_choice_index=0):
39 to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
Benjamin Kramer8f961aa2016-05-31 11:28:34 +000040 choices.strip(),
Haojian Wu11e9bd22016-05-31 09:31:51 +000041 default_choice_index)
42 return int(vim.eval(to_eval));
43
44
45def execute(command, text):
46 p = subprocess.Popen(command,
47 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
48 stdin=subprocess.PIPE)
49 return p.communicate(input=text)
50
51
52def InsertHeaderToVimBuffer(header, text):
Haojian Wu17a54e32016-06-01 11:43:10 +000053 command = [binary, "-stdin", "-insert-header="+json.dumps(header),
Haojian Wu11e9bd22016-05-31 09:31:51 +000054 vim.current.buffer.name]
55 stdout, stderr = execute(command, text)
56 if stdout:
57 lines = stdout.splitlines()
58 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
59 for op in reversed(sequence.get_opcodes()):
60 if op[0] is not 'equal':
61 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
62
63
Eric Liuc7f3b102016-05-18 14:10:16 +000064def main():
65 parser = argparse.ArgumentParser(
66 description='Vim integration for clang-include-fixer')
67 parser.add_argument('-db', default='yaml',
68 help='clang-include-fixer input format.')
69 parser.add_argument('-input', default='',
70 help='String to initialize the database.')
71 args = parser.parse_args()
72
73 # Get the current text.
74 buf = vim.current.buffer
75 text = '\n'.join(buf)
76
Haojian Wu11e9bd22016-05-31 09:31:51 +000077 # Run command to get all headers.
Benjamin Kramer8f961aa2016-05-31 11:28:34 +000078 command = [binary, "-stdin", "-output-headers", "-db="+args.db,
79 "-input="+args.input, vim.current.buffer.name]
Haojian Wu11e9bd22016-05-31 09:31:51 +000080 stdout, stderr = execute(command, text)
Haojian Wu17a54e32016-06-01 11:43:10 +000081 if stderr:
82 print >> sys.stderr, "Error while running clang-include-fixer: " + stderr
83 return
84
85 include_fixer_context = json.loads(stdout)
86 symbol = include_fixer_context["SymbolIdentifier"]
87 headers = include_fixer_context["Headers"]
88
89 if not symbol:
90 print "The file is fine, no need to add a header.\n"
91 return;
92
93 if not headers:
94 print "Couldn't find a header for {0}.\n".format(symbol)
Haojian Wu11e9bd22016-05-31 09:31:51 +000095 return
Eric Liuc7f3b102016-05-18 14:10:16 +000096
Haojian Wu11e9bd22016-05-31 09:31:51 +000097 # The first line is the symbol name.
Haojian Wu11e9bd22016-05-31 09:31:51 +000098 # If there is only one suggested header, insert it directly.
Haojian Wu17a54e32016-06-01 11:43:10 +000099 if len(headers) == 1 or maximum_suggested_headers == 1:
100 InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
101 "Headers":[headers[0]]}, text)
102 print "Added #include {0} for {1}.\n".format(headers[0], symbol)
Haojian Wu11e9bd22016-05-31 09:31:51 +0000103 return
Eric Liuc7f3b102016-05-18 14:10:16 +0000104
Haojian Wu11e9bd22016-05-31 09:31:51 +0000105 choices_message = ""
106 index = 1;
Haojian Wu17a54e32016-06-01 11:43:10 +0000107 for header in headers[0:maximum_suggested_headers]:
Benjamin Kramer8f961aa2016-05-31 11:28:34 +0000108 choices_message += "&{0} {1}\n".format(index, header)
Haojian Wu11e9bd22016-05-31 09:31:51 +0000109 index += 1
110
111 select = ShowDialog("choose a header file for {0}.".format(symbol),
112 choices_message)
113 # Insert a selected header.
Haojian Wu17a54e32016-06-01 11:43:10 +0000114 InsertHeaderToVimBuffer({"SymbolIdentifier": symbol,
115 "Headers":[headers[select-1]]}, text)
116 print "Added #include {0} for {1}.\n".format(headers[select-1], symbol)
Haojian Wu11e9bd22016-05-31 09:31:51 +0000117 return;
118
Eric Liuc7f3b102016-05-18 14:10:16 +0000119
120if __name__ == '__main__':
121 main()