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 | # |
| 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 | |
| 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 | |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame^] | 43 | |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 44 | def GetUserSelection(message, headers, maximum_suggested_headers): |
| 45 | eval_message = message + '\n' |
| 46 | for idx, header in enumerate(headers[0:maximum_suggested_headers]): |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame^] | 47 | eval_message += "({0}). {1}\n".format(idx + 1, header) |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 48 | eval_message += "Enter (q) to quit;" |
| 49 | if maximum_suggested_headers < len(headers): |
Eric Liu | f4a5710 | 2016-06-10 12:09:33 +0000 | [diff] [blame] | 50 | eval_message += " (m) to show {0} more candidates.".format( |
| 51 | min(increment_num, len(headers) - maximum_suggested_headers)) |
| 52 | |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 53 | eval_message += "\nSelect (default 1): " |
| 54 | res = vim.eval("input('{0}')".format(eval_message)) |
| 55 | if res == '': |
| 56 | # choose the top ranked header by default |
| 57 | idx = 1 |
| 58 | elif res == 'q': |
| 59 | raise Exception(' Insertion cancelled...') |
Eric Liu | f4a5710 | 2016-06-10 12:09:33 +0000 | [diff] [blame] | 60 | elif res == 'm': |
| 61 | return GetUserSelection(message, |
| 62 | headers, maximum_suggested_headers + increment_num) |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 63 | else: |
| 64 | try: |
| 65 | idx = int(res) |
| 66 | if idx <= 0 or idx > len(headers): |
| 67 | raise Exception() |
| 68 | except Exception: |
Eric Liu | f4a5710 | 2016-06-10 12:09:33 +0000 | [diff] [blame] | 69 | # Show a new prompt on invalid option instead of aborting so that users |
| 70 | # don't need to wait for another include-fixer run. |
| 71 | print >> sys.stderr, "Invalid option:", res |
| 72 | return GetUserSelection(message, headers, maximum_suggested_headers) |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame^] | 73 | return headers[idx - 1] |
| 74 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 75 | |
| 76 | def execute(command, text): |
| 77 | p = subprocess.Popen(command, |
| 78 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 79 | stdin=subprocess.PIPE) |
| 80 | return p.communicate(input=text) |
| 81 | |
| 82 | |
| 83 | def InsertHeaderToVimBuffer(header, text): |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame^] | 84 | command = [binary, "-stdin", "-insert-header=" + json.dumps(header), |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 85 | vim.current.buffer.name] |
| 86 | stdout, stderr = execute(command, text) |
| 87 | if stdout: |
| 88 | lines = stdout.splitlines() |
| 89 | sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) |
| 90 | for op in reversed(sequence.get_opcodes()): |
| 91 | if op[0] is not 'equal': |
| 92 | vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] |
| 93 | |
| 94 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 95 | def main(): |
| 96 | parser = argparse.ArgumentParser( |
| 97 | description='Vim integration for clang-include-fixer') |
| 98 | parser.add_argument('-db', default='yaml', |
| 99 | help='clang-include-fixer input format.') |
| 100 | parser.add_argument('-input', default='', |
| 101 | help='String to initialize the database.') |
| 102 | args = parser.parse_args() |
| 103 | |
| 104 | # Get the current text. |
| 105 | buf = vim.current.buffer |
| 106 | text = '\n'.join(buf) |
| 107 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 108 | # Run command to get all headers. |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame^] | 109 | command = [binary, "-stdin", "-output-headers", "-db=" + args.db, |
| 110 | "-input=" + args.input, vim.current.buffer.name] |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 111 | stdout, stderr = execute(command, text) |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 112 | if stderr: |
| 113 | print >> sys.stderr, "Error while running clang-include-fixer: " + stderr |
| 114 | return |
| 115 | |
| 116 | include_fixer_context = json.loads(stdout) |
| 117 | symbol = include_fixer_context["SymbolIdentifier"] |
| 118 | headers = include_fixer_context["Headers"] |
| 119 | |
| 120 | if not symbol: |
| 121 | print "The file is fine, no need to add a header.\n" |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 122 | return |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 123 | |
| 124 | if not headers: |
| 125 | print "Couldn't find a header for {0}.\n".format(symbol) |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 126 | return |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 127 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 128 | # The first line is the symbol name. |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 129 | # If there is only one suggested header, insert it directly. |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 130 | if len(headers) == 1 or maximum_suggested_headers == 1: |
| 131 | InsertHeaderToVimBuffer({"SymbolIdentifier": symbol, |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame^] | 132 | "Headers": [headers[0]]}, text) |
Haojian Wu | 17a54e3 | 2016-06-01 11:43:10 +0000 | [diff] [blame] | 133 | print "Added #include {0} for {1}.\n".format(headers[0], symbol) |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 134 | return |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 135 | |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 136 | try: |
| 137 | selected = GetUserSelection("choose a header file for {0}.".format(symbol), |
| 138 | headers, maximum_suggested_headers) |
| 139 | # Insert a selected header. |
| 140 | InsertHeaderToVimBuffer({"SymbolIdentifier": symbol, |
Benjamin Kramer | fdaed4c | 2016-07-04 16:47:17 +0000 | [diff] [blame^] | 141 | "Headers": [selected]}, text) |
Eric Liu | f832eb7 | 2016-06-07 12:21:43 +0000 | [diff] [blame] | 142 | print "Added #include {0} for {1}.\n".format(selected, symbol) |
| 143 | except Exception as error: |
| 144 | print >> sys.stderr, error.message |
| 145 | return |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 146 | |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 147 | |
| 148 | if __name__ == '__main__': |
| 149 | main() |