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