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 |
| 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. |
| 26 | binary = 'clang-include-fixer' |
| 27 | if vim.eval('exists("g:clang_include_fixer_path")') == "1": |
| 28 | binary = vim.eval('g:clang_include_fixer_path') |
| 29 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame^] | 30 | maximum_suggested_headers=3 |
| 31 | if 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 | |
| 37 | def 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 | |
| 44 | def 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 | |
| 51 | def 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 Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 63 | def 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 Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame^] | 76 | # Run command to get all headers. |
| 77 | command = [binary, "-stdin", "-output-headers", "-db="+args.db, "-input="+args.input, "-debug", |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 78 | vim.current.buffer.name] |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame^] | 79 | stdout, stderr = execute(command, text) |
| 80 | lines = stdout.splitlines() |
| 81 | if len(lines) < 2: |
| 82 | print "No header is included.\n" |
| 83 | return |
Eric Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 84 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame^] | 85 | # 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 Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 92 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame^] | 93 | 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 Liu | c7f3b10 | 2016-05-18 14:10:16 +0000 | [diff] [blame] | 106 | |
| 107 | if __name__ == '__main__': |
| 108 | main() |