blob: 4c38f71ef76a9811b9a2ba2bd3112b74622be7f9 [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#
Nico Weber43356f52019-03-25 14:09:10 +00005# noremap <leader>cf :pyf path/to/llvm/source/tools/clang/tools/extra/clang-include-fixer/tool/clang-include-fixer.py<cr>
Eric Liuc7f3b102016-05-18 14:10:16 +00006#
Nico Weber43356f52019-03-25 14:09:10 +00007# This enables clang-include-fixer for NORMAL and VISUAL mode. Change
8# "<leader>cf" to another binding if you need clang-include-fixer on a
9# different key.
Eric Liuc7f3b102016-05-18 14:10:16 +000010#
Nico Weber43356f52019-03-25 14:09:10 +000011# To set up clang-include-fixer, see
12# http://clang.llvm.org/extra/clang-include-fixer.html
Eric Liuc7f3b102016-05-18 14:10:16 +000013#
14# With this integration you can press the bound key and clang-include-fixer will
15# be run on the current buffer.
16#
17# It operates on the current, potentially unsaved buffer and does not create
18# or save any files. To revert a fix, just undo.
19
20import argparse
Eric Liu702cfd12016-05-19 08:21:09 +000021import difflib
Haojian Wucd637012016-09-07 16:34:35 +000022import json
23import re
Eric Liuc7f3b102016-05-18 14:10:16 +000024import subprocess
Eric Liuc7f3b102016-05-18 14:10:16 +000025import vim
26
27# set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
28# on the path.
29# Change this to the full path if clang-include-fixer is not on the path.
30binary = 'clang-include-fixer'
31if vim.eval('exists("g:clang_include_fixer_path")') == "1":
32 binary = vim.eval('g:clang_include_fixer_path')
33
Benjamin Kramerfdaed4c2016-07-04 16:47:17 +000034maximum_suggested_headers = 3
Haojian Wu11e9bd22016-05-31 09:31:51 +000035if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
36 maximum_suggested_headers = max(
37 1,
38 vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
39
Benjamin Kramerfdaed4c2016-07-04 16:47:17 +000040increment_num = 5
Eric Liuf4a57102016-06-10 12:09:33 +000041if vim.eval('exists("g:clang_include_fixer_increment_num")') == "1":
42 increment_num = max(
43 1,
44 vim.eval('g:clang_include_fixer_increment_num'))
Haojian Wu11e9bd22016-05-31 09:31:51 +000045
Haojian Wufff3ad62016-07-18 10:04:45 +000046jump_to_include = False
47if vim.eval('exists("g:clang_include_fixer_jump_to_include")') == "1":
48 jump_to_include = vim.eval('g:clang_include_fixer_jump_to_include') != "0"
49
Haojian Wuea99ff72016-10-10 14:21:55 +000050query_mode = False
Haojian Wucd637012016-09-07 16:34:35 +000051if vim.eval('exists("g:clang_include_fixer_query_mode")') == "1":
52 query_mode = vim.eval('g:clang_include_fixer_query_mode') != "0"
53
Benjamin Kramerfdaed4c2016-07-04 16:47:17 +000054
Eric Liuf832eb72016-06-07 12:21:43 +000055def GetUserSelection(message, headers, maximum_suggested_headers):
56 eval_message = message + '\n'
57 for idx, header in enumerate(headers[0:maximum_suggested_headers]):
Benjamin Kramerfdaed4c2016-07-04 16:47:17 +000058 eval_message += "({0}). {1}\n".format(idx + 1, header)
Eric Liuf832eb72016-06-07 12:21:43 +000059 eval_message += "Enter (q) to quit;"
60 if maximum_suggested_headers < len(headers):
Eric Liuf4a57102016-06-10 12:09:33 +000061 eval_message += " (m) to show {0} more candidates.".format(
62 min(increment_num, len(headers) - maximum_suggested_headers))
63
Eric Liuf832eb72016-06-07 12:21:43 +000064 eval_message += "\nSelect (default 1): "
65 res = vim.eval("input('{0}')".format(eval_message))
66 if res == '':
67 # choose the top ranked header by default
68 idx = 1
69 elif res == 'q':
70 raise Exception(' Insertion cancelled...')
Eric Liuf4a57102016-06-10 12:09:33 +000071 elif res == 'm':
72 return GetUserSelection(message,
73 headers, maximum_suggested_headers + increment_num)
Eric Liuf832eb72016-06-07 12:21:43 +000074 else:
75 try:
76 idx = int(res)
77 if idx <= 0 or idx > len(headers):
78 raise Exception()
79 except Exception:
Eric Liuf4a57102016-06-10 12:09:33 +000080 # Show a new prompt on invalid option instead of aborting so that users
Nico Weber43356f52019-03-25 14:09:10 +000081 # don't need to wait for another clang-include-fixer run.
Eric Liuf4a57102016-06-10 12:09:33 +000082 print >> sys.stderr, "Invalid option:", res
83 return GetUserSelection(message, headers, maximum_suggested_headers)
Benjamin Kramerfdaed4c2016-07-04 16:47:17 +000084 return headers[idx - 1]
85
Haojian Wu11e9bd22016-05-31 09:31:51 +000086
87def execute(command, text):
88 p = subprocess.Popen(command,
89 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
90 stdin=subprocess.PIPE)
91 return p.communicate(input=text)
92
93
94def InsertHeaderToVimBuffer(header, text):
Benjamin Kramerfdaed4c2016-07-04 16:47:17 +000095 command = [binary, "-stdin", "-insert-header=" + json.dumps(header),
Haojian Wu11e9bd22016-05-31 09:31:51 +000096 vim.current.buffer.name]
97 stdout, stderr = execute(command, text)
Haojian Wufff3ad62016-07-18 10:04:45 +000098 if stderr:
99 raise Exception(stderr)
Haojian Wu11e9bd22016-05-31 09:31:51 +0000100 if stdout:
101 lines = stdout.splitlines()
102 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
Haojian Wufff3ad62016-07-18 10:04:45 +0000103 line_num = None
Haojian Wu11e9bd22016-05-31 09:31:51 +0000104 for op in reversed(sequence.get_opcodes()):
Haojian Wufff3ad62016-07-18 10:04:45 +0000105 if op[0] != 'equal':
Haojian Wu11e9bd22016-05-31 09:31:51 +0000106 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
Haojian Wufff3ad62016-07-18 10:04:45 +0000107 if op[0] == 'insert':
108 # line_num in vim is 1-based.
109 line_num = op[1] + 1
110
111 if jump_to_include and line_num:
112 vim.current.window.cursor = (line_num, 0)
Haojian Wu11e9bd22016-05-31 09:31:51 +0000113
114
Haojian Wucd637012016-09-07 16:34:35 +0000115# The vim internal implementation (expand("cword"/"cWORD")) doesn't support
116# our use case very well, we re-implement our own one.
117def get_symbol_under_cursor():
118 line = vim.eval("line(\".\")")
119 # column number in vim is 1-based.
120 col = int(vim.eval("col(\".\")")) - 1
121 line_text = vim.eval("getline({0})".format(line))
122 if len(line_text) == 0: return ""
123 symbol_pos_begin = col
124 p = re.compile('[a-zA-Z0-9:_]')
125 while symbol_pos_begin >= 0 and p.match(line_text[symbol_pos_begin]):
126 symbol_pos_begin -= 1
127
128 symbol_pos_end = col
129 while symbol_pos_end < len(line_text) and p.match(line_text[symbol_pos_end]):
130 symbol_pos_end += 1
131 return line_text[symbol_pos_begin+1:symbol_pos_end]
132
133
Eric Liuc7f3b102016-05-18 14:10:16 +0000134def main():
135 parser = argparse.ArgumentParser(
136 description='Vim integration for clang-include-fixer')
137 parser.add_argument('-db', default='yaml',
138 help='clang-include-fixer input format.')
139 parser.add_argument('-input', default='',
140 help='String to initialize the database.')
Haojian Wudbc39112017-05-17 14:13:59 +0000141 # Don't throw exception when parsing unknown arguements to make the script
142 # work in neovim.
143 # Neovim (at least v0.2.1) somehow mangles the sys.argv in a weird way: it
144 # will pass additional arguments (e.g. "-c script_host.py") to sys.argv,
145 # which makes the script fail.
146 args, _ = parser.parse_known_args()
Eric Liuc7f3b102016-05-18 14:10:16 +0000147
148 # Get the current text.
149 buf = vim.current.buffer
150 text = '\n'.join(buf)
151
Haojian Wucd637012016-09-07 16:34:35 +0000152 if query_mode:
153 symbol = get_symbol_under_cursor()
154 if len(symbol) == 0:
155 print "Skip querying empty symbol."
156 return
157 command = [binary, "-stdin", "-query-symbol="+get_symbol_under_cursor(),
158 "-db=" + args.db, "-input=" + args.input,
159 vim.current.buffer.name]
160 else:
161 # Run command to get all headers.
162 command = [binary, "-stdin", "-output-headers", "-db=" + args.db,
163 "-input=" + args.input, vim.current.buffer.name]
Haojian Wu11e9bd22016-05-31 09:31:51 +0000164 stdout, stderr = execute(command, text)
Haojian Wu17a54e32016-06-01 11:43:10 +0000165 if stderr:
166 print >> sys.stderr, "Error while running clang-include-fixer: " + stderr
167 return
168
169 include_fixer_context = json.loads(stdout)
Haojian Wu62aee522016-07-21 13:47:09 +0000170 query_symbol_infos = include_fixer_context["QuerySymbolInfos"]
171 if not query_symbol_infos:
172 print "The file is fine, no need to add a header."
173 return
174 symbol = query_symbol_infos[0]["RawIdentifier"]
Nico Weber43356f52019-03-25 14:09:10 +0000175 # The header_infos is already sorted by clang-include-fixer.
Haojian Wu68c34a02016-07-13 16:43:54 +0000176 header_infos = include_fixer_context["HeaderInfos"]
177 # Deduplicate headers while keeping the order, so that the same header would
178 # not be suggested twice.
179 unique_headers = []
180 seen = set()
181 for header_info in header_infos:
182 header = header_info["Header"]
183 if header not in seen:
184 seen.add(header)
185 unique_headers.append(header)
Haojian Wu17a54e32016-06-01 11:43:10 +0000186
Haojian Wu68c34a02016-07-13 16:43:54 +0000187 if not unique_headers:
Haojian Wufff3ad62016-07-18 10:04:45 +0000188 print "Couldn't find a header for {0}.".format(symbol)
Haojian Wu11e9bd22016-05-31 09:31:51 +0000189 return
Eric Liuc7f3b102016-05-18 14:10:16 +0000190
Eric Liuf832eb72016-06-07 12:21:43 +0000191 try:
Haojian Wu25df3b82016-09-01 12:17:28 +0000192 selected = unique_headers[0]
Haojian Wuc99f7282016-08-09 08:26:19 +0000193 inserted_header_infos = header_infos
194 if len(unique_headers) > 1:
195 selected = GetUserSelection(
196 "choose a header file for {0}.".format(symbol),
197 unique_headers, maximum_suggested_headers)
198 inserted_header_infos = [
199 header for header in header_infos if header["Header"] == selected]
200 include_fixer_context["HeaderInfos"] = inserted_header_infos
Haojian Wufff3ad62016-07-18 10:04:45 +0000201
Haojian Wuc99f7282016-08-09 08:26:19 +0000202 InsertHeaderToVimBuffer(include_fixer_context, text)
Haojian Wufff3ad62016-07-18 10:04:45 +0000203 print "Added #include {0} for {1}.".format(selected, symbol)
Eric Liuf832eb72016-06-07 12:21:43 +0000204 except Exception as error:
205 print >> sys.stderr, error.message
206 return
Haojian Wu11e9bd22016-05-31 09:31:51 +0000207
Eric Liuc7f3b102016-05-18 14:10:16 +0000208
209if __name__ == '__main__':
210 main()