blob: dfb5fcc585d4614ce08ec72e6d72ef06a884e5d2 [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
22
23# set g:clang_include_fixer_path to the path to clang-include-fixer if it is not
24# on the path.
25# Change this to the full path if clang-include-fixer is not on the path.
26binary = 'clang-include-fixer'
27if vim.eval('exists("g:clang_include_fixer_path")') == "1":
28 binary = vim.eval('g:clang_include_fixer_path')
29
Haojian Wu11e9bd22016-05-31 09:31:51 +000030maximum_suggested_headers=3
31if vim.eval('exists("g:clang_include_fixer_maximum_suggested_headers")') == "1":
32 maximum_suggested_headers = max(
33 1,
34 vim.eval('g:clang_include_fixer_maximum_suggested_headers'))
35
36
37def ShowDialog(message, choices, default_choice_index=0):
38 to_eval = "confirm('{0}', '{1}', '{2}')".format(message,
39 choices,
40 default_choice_index)
41 return int(vim.eval(to_eval));
42
43
44def execute(command, text):
45 p = subprocess.Popen(command,
46 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
47 stdin=subprocess.PIPE)
48 return p.communicate(input=text)
49
50
51def InsertHeaderToVimBuffer(header, text):
52 command = [binary, "-stdin", "-insert-header="+header,
53 vim.current.buffer.name]
54 stdout, stderr = execute(command, text)
55 if stdout:
56 lines = stdout.splitlines()
57 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
58 for op in reversed(sequence.get_opcodes()):
59 if op[0] is not 'equal':
60 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
61
62
Eric Liuc7f3b102016-05-18 14:10:16 +000063def main():
64 parser = argparse.ArgumentParser(
65 description='Vim integration for clang-include-fixer')
66 parser.add_argument('-db', default='yaml',
67 help='clang-include-fixer input format.')
68 parser.add_argument('-input', default='',
69 help='String to initialize the database.')
70 args = parser.parse_args()
71
72 # Get the current text.
73 buf = vim.current.buffer
74 text = '\n'.join(buf)
75
Haojian Wu11e9bd22016-05-31 09:31:51 +000076 # Run command to get all headers.
77 command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug",
Eric Liuc7f3b102016-05-18 14:10:16 +000078 vim.current.buffer.name]
Haojian Wu11e9bd22016-05-31 09:31:51 +000079 stdout, stderr = execute(command, text)
80 lines = stdout.splitlines()
81 if len(lines) < 2:
82 print "No header is included.\n"
83 return
Eric Liuc7f3b102016-05-18 14:10:16 +000084
Haojian Wu11e9bd22016-05-31 09:31:51 +000085 # The first line is the symbol name.
86 symbol = lines[0]
87 # If there is only one suggested header, insert it directly.
88 if len(lines) == 2 or maximum_suggested_headers == 1:
89 InsertHeaderToVimBuffer(lines[1], text)
90 print "Added #include {0} for {1}.\n".format(lines[1], symbol)
91 return
Eric Liuc7f3b102016-05-18 14:10:16 +000092
Haojian Wu11e9bd22016-05-31 09:31:51 +000093 choices_message = ""
94 index = 1;
95 for header in lines[1:1+maximum_suggested_headers]:
96 choices_message += "&" + str(index) + header + "\n"
97 index += 1
98
99 select = ShowDialog("choose a header file for {0}.".format(symbol),
100 choices_message)
101 # Insert a selected header.
102 InsertHeaderToVimBuffer(lines[select], text)
103 print "Added #include {0} for {1}.\n".format(lines[select], symbol)
104 return;
105
Eric Liuc7f3b102016-05-18 14:10:16 +0000106
107if __name__ == '__main__':
108 main()