Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 1 | # 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 | # |
Kirill Bobyrev | c4018e2 | 2016-07-27 14:23:47 +0000 | [diff] [blame] | 5 | # noremap <leader>cf :pyf path/to/llvm/source/tools/clang/tools/extra/include-fixer/tool/clang-include-fixer.py<cr> |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 6 | # |
Kirill Bobyrev | c4018e2 | 2016-07-27 14:23:47 +0000 | [diff] [blame] | 7 | # This enables clang-include-fixer for NORMAL and VISUAL mode. Change "<leader>cf" |
| 8 | # to another binding if you need clang-include-fixer on a different key. |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 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 | |
| 18 | import argparse |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 19 | import difflib |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 20 | import subprocess |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 21 | import vim |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 22 | import json |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 23 | |
| 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. |
| 27 | binary = 'clang-include-fixer' |
| 28 | if vim.eval('exists("g:clang_include_fixer_path")') == "1": |
| 29 | binary = vim.eval('g:clang_include_fixer_path') |
| 30 | |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame] | 31 | maximum_suggested_headers = 3 |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 32 | if 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 | |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame] | 37 | increment_num = 5 |
Eric Liu | f4a5710 | 2016-06-10 12:09:33 +0000 | [diff] [blame] | 38 | if vim.eval('exists("g:clang_include_fixer_increment_num")') == "1": |
| 39 | increment_num = max( |
| 40 | 1, |
| 41 | vim.eval('g:clang_include_fixer_increment_num')) |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 42 | |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 43 | jump_to_include = False |
| 44 | if vim.eval('exists("g:clang_include_fixer_jump_to_include")') == "1": |
| 45 | jump_to_include = vim.eval('g:clang_include_fixer_jump_to_include') != "0" |
| 46 | |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame] | 47 | |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 48 | def GetUserSelection(message, headers, maximum_suggested_headers): |
| 49 | eval_message = message + '\n' |
| 50 | for idx, header in enumerate(headers[0:maximum_suggested_headers]): |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame] | 51 | eval_message += "({0}). {1}\n".format(idx + 1, header) |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 52 | eval_message += "Enter (q) to quit;" |
| 53 | if maximum_suggested_headers < len(headers): |
Eric Liu | f4a5710 | 2016-06-10 12:09:33 +0000 | [diff] [blame] | 54 | eval_message += " (m) to show {0} more candidates.".format( |
| 55 | min(increment_num, len(headers) - maximum_suggested_headers)) |
| 56 | |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 57 | eval_message += "\nSelect (default 1): " |
| 58 | res = vim.eval("input('{0}')".format(eval_message)) |
| 59 | if res == '': |
| 60 | # choose the top ranked header by default |
| 61 | idx = 1 |
| 62 | elif res == 'q': |
| 63 | raise Exception(' Insertion cancelled...') |
Eric Liu | f4a5710 | 2016-06-10 12:09:33 +0000 | [diff] [blame] | 64 | elif res == 'm': |
| 65 | return GetUserSelection(message, |
| 66 | headers, maximum_suggested_headers + increment_num) |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 67 | else: |
| 68 | try: |
| 69 | idx = int(res) |
| 70 | if idx <= 0 or idx > len(headers): |
| 71 | raise Exception() |
| 72 | except Exception: |
Eric Liu | f4a5710 | 2016-06-10 12:09:33 +0000 | [diff] [blame] | 73 | # Show a new prompt on invalid option instead of aborting so that users |
| 74 | # don't need to wait for another include-fixer run. |
| 75 | print >> sys.stderr, "Invalid option:", res |
| 76 | return GetUserSelection(message, headers, maximum_suggested_headers) |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame] | 77 | return headers[idx - 1] |
| 78 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 79 | |
| 80 | def execute(command, text): |
| 81 | p = subprocess.Popen(command, |
| 82 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 83 | stdin=subprocess.PIPE) |
| 84 | return p.communicate(input=text) |
| 85 | |
| 86 | |
| 87 | def InsertHeaderToVimBuffer(header, text): |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame] | 88 | command = [binary, "-stdin", "-insert-header=" + json.dumps(header), |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 89 | vim.current.buffer.name] |
| 90 | stdout, stderr = execute(command, text) |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 91 | if stderr: |
| 92 | raise Exception(stderr) |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 93 | if stdout: |
| 94 | lines = stdout.splitlines() |
| 95 | sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 96 | line_num = None |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 97 | for op in reversed(sequence.get_opcodes()): |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 98 | if op[0] != 'equal': |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 99 | vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 100 | if op[0] == 'insert': |
| 101 | # line_num in vim is 1-based. |
| 102 | line_num = op[1] + 1 |
| 103 | |
| 104 | if jump_to_include and line_num: |
| 105 | vim.current.window.cursor = (line_num, 0) |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 106 | |
| 107 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 108 | def main(): |
| 109 | parser = argparse.ArgumentParser( |
| 110 | description='Vim integration for clang-include-fixer') |
| 111 | parser.add_argument('-db', default='yaml', |
| 112 | help='clang-include-fixer input format.') |
| 113 | parser.add_argument('-input', default='', |
| 114 | help='String to initialize the database.') |
| 115 | args = parser.parse_args() |
| 116 | |
| 117 | # Get the current text. |
| 118 | buf = vim.current.buffer |
| 119 | text = '\n'.join(buf) |
| 120 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 121 | # Run command to get all headers. |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame] | 122 | command = [binary, "-stdin", "-output-headers", "-db=" + args.db, |
| 123 | "-input=" + args.input, vim.current.buffer.name] |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 124 | stdout, stderr = execute(command, text) |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 125 | if stderr: |
| 126 | print >> sys.stderr, "Error while running clang-include-fixer: " + stderr |
| 127 | return |
| 128 | |
| 129 | include_fixer_context = json.loads(stdout) |
Haojian Wu | 62aee52 | 2016-07-21 13:47:09 +0000 | [diff] [blame] | 130 | query_symbol_infos = include_fixer_context["QuerySymbolInfos"] |
| 131 | if not query_symbol_infos: |
| 132 | print "The file is fine, no need to add a header." |
| 133 | return |
| 134 | symbol = query_symbol_infos[0]["RawIdentifier"] |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 135 | # The header_infos is already sorted by include-fixer. |
| 136 | header_infos = include_fixer_context["HeaderInfos"] |
| 137 | # Deduplicate headers while keeping the order, so that the same header would |
| 138 | # not be suggested twice. |
| 139 | unique_headers = [] |
| 140 | seen = set() |
| 141 | for header_info in header_infos: |
| 142 | header = header_info["Header"] |
| 143 | if header not in seen: |
| 144 | seen.add(header) |
| 145 | unique_headers.append(header) |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 146 | |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 147 | if not unique_headers: |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 148 | print "Couldn't find a header for {0}.".format(symbol) |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 149 | return |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 150 | |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 151 | try: |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame^] | 152 | inserted_header_infos = header_infos |
| 153 | if len(unique_headers) > 1: |
| 154 | selected = GetUserSelection( |
| 155 | "choose a header file for {0}.".format(symbol), |
| 156 | unique_headers, maximum_suggested_headers) |
| 157 | inserted_header_infos = [ |
| 158 | header for header in header_infos if header["Header"] == selected] |
| 159 | include_fixer_context["HeaderInfos"] = inserted_header_infos |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 160 | |
Haojian Wu | c99f728 | 2016-08-09 08:26:19 +0000 | [diff] [blame^] | 161 | InsertHeaderToVimBuffer(include_fixer_context, text) |
Haojian Wu | fff3ad6 | 2016-07-18 10:04:45 +0000 | [diff] [blame] | 162 | print "Added #include {0} for {1}.".format(selected, symbol) |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 163 | except Exception as error: |
| 164 | print >> sys.stderr, error.message |
| 165 | return |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 166 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 167 | |
| 168 | if __name__ == '__main__': |
| 169 | main() |