blob: 29be72e176b675b0333c96e34eb97efcaf085a9a [file] [log] [blame]
djasper7f663602013-03-20 09:53:23 +00001# 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#
5# map <C-I> :pyf <path-to-this-file>/clang-format.py<CR>
6# imap <C-I> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i
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#
17# It operates on the current, potentially unsaved buffer and does not create
18# or save any files. To revert a formatting, just undo.
19
djasperd0252b72013-05-21 12:21:39 +000020import json
djasper7f663602013-03-20 09:53:23 +000021import subprocess
rnk8f191962013-06-10 14:16:26 +000022import sys
djasperd0252b72013-05-21 12:21:39 +000023import vim
djasper7f663602013-03-20 09:53:23 +000024
25# Change this to the full path if clang-format is not on the path.
26binary = 'clang-format'
27
djasper05e73312013-04-09 15:23:04 +000028# Change this to format according to other formatting styles (see
29# clang-format -help)
30style = 'LLVM'
31
djasper7f663602013-03-20 09:53:23 +000032# Get the current text.
33buf = vim.current.buffer
djasperd0252b72013-05-21 12:21:39 +000034text = '\n'.join(buf)
djasper7f663602013-03-20 09:53:23 +000035
36# Determine range to format.
djasperd0252b72013-05-21 12:21:39 +000037cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
djasper7f663602013-03-20 09:53:23 +000038offset = int(vim.eval('line2byte(' +
39 str(vim.current.range.start + 1) + ')')) - 1
40length = int(vim.eval('line2byte(' +
41 str(vim.current.range.end + 2) + ')')) - offset - 2
42
rnk8f191962013-06-10 14:16:26 +000043# Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
44startupinfo = None
45if sys.platform.startswith('win32'):
46 startupinfo = subprocess.STARTUPINFO()
47 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
48 startupinfo.wShowWindow = subprocess.SW_HIDE
49
djasper7f663602013-03-20 09:53:23 +000050# Call formatter.
djasper05e73312013-04-09 15:23:04 +000051p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length),
djasperd0252b72013-05-21 12:21:39 +000052 '-style', style, '-cursor', str(cursor)],
djasper7f663602013-03-20 09:53:23 +000053 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
rnk8f191962013-06-10 14:16:26 +000054 stdin=subprocess.PIPE, startupinfo=startupinfo)
djasper7f663602013-03-20 09:53:23 +000055stdout, stderr = p.communicate(input=text)
56
57# If successful, replace buffer contents.
58if stderr:
59 message = stderr.splitlines()[0]
60 parts = message.split(' ', 2)
61 if len(parts) > 2:
62 message = parts[2]
63 print 'Formatting failed: %s (total %d warnings, %d errors)' % (
64 message, stderr.count('warning:'), stderr.count('error:'))
65
66if not stdout:
67 print ('No output from clang-format (crashed?).\n' +
68 'Please report to bugs.llvm.org.')
djasperd0252b72013-05-21 12:21:39 +000069else:
djasper7f663602013-03-20 09:53:23 +000070 lines = stdout.split('\n')
djasperd0252b72013-05-21 12:21:39 +000071 output = json.loads(lines[0])
72 lines = lines[1:]
73 if '\n'.join(lines) != text:
djasper2cdba9c2013-07-19 09:30:44 +000074 common_length = min(len(buf), len(lines))
75 buf[:common_length] = lines[:common_length]
djasperd0252b72013-05-21 12:21:39 +000076 for line in lines[len(buf):]:
77 buf.append(line)
78 del buf[len(lines):]
79 vim.command('goto %d' % (output['Cursor'] + 1))