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