djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 1 | # This file is a minimal clang-format vim-integration. To install: |
| 2 | # - Change 'binary' if clang-format is not on the path (see below). |
| 3 | # - Add to your .vimrc: |
| 4 | # |
compnerd | 9216b27 | 2014-10-27 17:13:33 +0000 | [diff] [blame] | 5 | # map <C-I> :pyf <path-to-this-file>/clang-format.py<cr> |
compnerd | 3ddc341 | 2014-11-02 21:27:52 +0000 | [diff] [blame] | 6 | # imap <C-I> <c-o>:pyf <path-to-this-file>/clang-format.py<cr> |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 7 | # |
| 8 | # The first line enables clang-format for NORMAL and VISUAL mode, the second |
| 9 | # line adds support for INSERT mode. Change "C-I" to another binding if you |
| 10 | # need clang-format on a different key (C-I stands for Ctrl+i). |
| 11 | # |
| 12 | # With this integration you can press the bound key and clang-format will |
| 13 | # format the current line in NORMAL and INSERT mode or the selected region in |
| 14 | # VISUAL mode. The line or region is extended to the next bigger syntactic |
| 15 | # entity. |
| 16 | # |
djasper | 9de4e3f | 2015-07-11 06:46:26 +0000 | [diff] [blame] | 17 | # You can also pass in the variable "l:lines" to choose the range for |
| 18 | # formatting. This variable can either contain "<start line>:<end line>" or |
| 19 | # "all" to format the full file. So, to format the full file, write a function |
| 20 | # like: |
| 21 | # :function FormatFile() |
| 22 | # : let l:lines="all" |
| 23 | # : pyf <path-to-this-file>/clang-format.py |
| 24 | # :endfunction |
| 25 | # |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 26 | # It operates on the current, potentially unsaved buffer and does not create |
| 27 | # or save any files. To revert a formatting, just undo. |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 28 | from __future__ import print_function |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 29 | |
djasper | b7e02b1 | 2013-07-21 10:45:33 +0000 | [diff] [blame] | 30 | import difflib |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 31 | import json |
vedantk | f8c29ee | 2016-12-10 00:54:13 +0000 | [diff] [blame] | 32 | import platform |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 33 | import subprocess |
rnk | 8f19196 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 34 | import sys |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 35 | import vim |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 36 | |
compnerd | e19f744 | 2014-11-02 21:27:59 +0000 | [diff] [blame] | 37 | # set g:clang_format_path to the path to clang-format if it is not on the path |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 38 | # Change this to the full path if clang-format is not on the path. |
| 39 | binary = 'clang-format' |
djasper | 7480ff1 | 2014-11-04 10:40:26 +0000 | [diff] [blame] | 40 | if vim.eval('exists("g:clang_format_path")') == "1": |
compnerd | e19f744 | 2014-11-02 21:27:59 +0000 | [diff] [blame] | 41 | binary = vim.eval('g:clang_format_path') |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 42 | |
chandlerc | e6992fd | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 43 | # Change this to format according to other formatting styles. See the output of |
| 44 | # 'clang-format --help' for a list of supported styles. The default looks for |
hans | 79a882f | 2013-09-10 15:41:12 +0000 | [diff] [blame] | 45 | # a '.clang-format' or '_clang-format' file to indicate the style that should be |
| 46 | # used. |
chandlerc | e6992fd | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 47 | style = 'file' |
djasper | 701b33d | 2015-04-17 07:59:19 +0000 | [diff] [blame] | 48 | fallback_style = None |
djasper | 0f3ae42 | 2015-04-16 08:26:37 +0000 | [diff] [blame] | 49 | if vim.eval('exists("g:clang_format_fallback_style")') == "1": |
| 50 | fallback_style = vim.eval('g:clang_format_fallback_style') |
djasper | 05e7331 | 2013-04-09 15:23:04 +0000 | [diff] [blame] | 51 | |
vedantk | f8c29ee | 2016-12-10 00:54:13 +0000 | [diff] [blame] | 52 | def get_buffer(encoding): |
| 53 | if platform.python_version_tuple()[0] == '3': |
| 54 | return vim.current.buffer |
| 55 | return [ line.decode(encoding) for line in vim.current.buffer ] |
| 56 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 57 | def main(): |
| 58 | # Get the current text. |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 59 | encoding = vim.eval("&encoding") |
vedantk | f8c29ee | 2016-12-10 00:54:13 +0000 | [diff] [blame] | 60 | buf = get_buffer(encoding) |
alexfh | 5a36578 | 2016-10-27 13:46:49 +0000 | [diff] [blame] | 61 | text = '\n'.join(buf) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 62 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 63 | # Determine range to format. |
djasper | 9de4e3f | 2015-07-11 06:46:26 +0000 | [diff] [blame] | 64 | if vim.eval('exists("l:lines")') == '1': |
| 65 | lines = vim.eval('l:lines') |
djasper | 7b50452 | 2017-06-19 07:30:04 +0000 | [diff] [blame^] | 66 | elif vim.eval('exists("l:formatdiff")') == '1': |
| 67 | with open(vim.current.buffer.name, 'r') as f: |
| 68 | ondisk = f.read().splitlines(); |
| 69 | sequence = difflib.SequenceMatcher(None, ondisk, vim.current.buffer) |
| 70 | lines = [] |
| 71 | for op in reversed(sequence.get_opcodes()): |
| 72 | if op[0] not in ['equal', 'delete']: |
| 73 | lines += ['-lines', '%s:%s' % (op[3] + 1, op[4])] |
| 74 | if lines == []: |
| 75 | return |
djasper | 9de4e3f | 2015-07-11 06:46:26 +0000 | [diff] [blame] | 76 | else: |
djasper | 7b50452 | 2017-06-19 07:30:04 +0000 | [diff] [blame^] | 77 | lines = ['-lines', '%s:%s' % (vim.current.range.start + 1, |
| 78 | vim.current.range.end + 1)] |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 79 | |
djasper | 4d1cfae | 2014-05-22 11:37:05 +0000 | [diff] [blame] | 80 | # Determine the cursor position. |
| 81 | cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2 |
| 82 | if cursor < 0: |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 83 | print('Couldn\'t determine cursor position. Is your file empty?') |
djasper | 4d1cfae | 2014-05-22 11:37:05 +0000 | [diff] [blame] | 84 | return |
| 85 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 86 | # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format. |
| 87 | startupinfo = None |
| 88 | if sys.platform.startswith('win32'): |
| 89 | startupinfo = subprocess.STARTUPINFO() |
| 90 | startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
| 91 | startupinfo.wShowWindow = subprocess.SW_HIDE |
rnk | 8f19196 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 92 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 93 | # Call formatter. |
djasper | 71631c2 | 2015-11-16 12:38:56 +0000 | [diff] [blame] | 94 | command = [binary, '-style', style, '-cursor', str(cursor)] |
djasper | 9de4e3f | 2015-07-11 06:46:26 +0000 | [diff] [blame] | 95 | if lines != 'all': |
djasper | 7b50452 | 2017-06-19 07:30:04 +0000 | [diff] [blame^] | 96 | command += lines |
djasper | 0f3ae42 | 2015-04-16 08:26:37 +0000 | [diff] [blame] | 97 | if fallback_style: |
| 98 | command.extend(['-fallback-style', fallback_style]) |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 99 | if vim.current.buffer.name: |
| 100 | command.extend(['-assume-filename', vim.current.buffer.name]) |
| 101 | p = subprocess.Popen(command, |
| 102 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 103 | stdin=subprocess.PIPE, startupinfo=startupinfo) |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 104 | stdout, stderr = p.communicate(input=text.encode(encoding)) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 105 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 106 | # If successful, replace buffer contents. |
| 107 | if stderr: |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 108 | print(stderr) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 109 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 110 | if not stdout: |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 111 | print( |
| 112 | 'No output from clang-format (crashed?).\n' |
| 113 | 'Please report to bugs.llvm.org.' |
| 114 | ) |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 115 | else: |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 116 | lines = stdout.decode(encoding).split('\n') |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 117 | output = json.loads(lines[0]) |
| 118 | lines = lines[1:] |
alexfh | 5a36578 | 2016-10-27 13:46:49 +0000 | [diff] [blame] | 119 | sequence = difflib.SequenceMatcher(None, buf, lines) |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 120 | for op in reversed(sequence.get_opcodes()): |
| 121 | if op[0] is not 'equal': |
| 122 | vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] |
chandlerc | 02debe8 | 2015-06-04 21:23:07 +0000 | [diff] [blame] | 123 | if output.get('IncompleteFormat'): |
ldrumm | 73289d7 | 2016-08-31 13:36:36 +0000 | [diff] [blame] | 124 | print('clang-format: incomplete (syntax errors)') |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 125 | vim.command('goto %d' % (output['Cursor'] + 1)) |
| 126 | |
| 127 | main() |