blob: 866baf50f104f166b2949c2cbd3aed7ca9ab4f10 [file] [log] [blame]
Daniel Jasper7c4a9a02013-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
Daniel Jasper6bd3b932013-05-21 12:21:39 +000020import json
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000021import subprocess
Reid Kleckner57e68472013-06-10 14:16:26 +000022import sys
Daniel Jasper6bd3b932013-05-21 12:21:39 +000023import vim
Daniel Jasper7c4a9a02013-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
Daniel Jasper63911832013-04-09 15:23:04 +000028# Change this to format according to other formatting styles (see
29# clang-format -help)
30style = 'LLVM'
31
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000032# Get the current text.
33buf = vim.current.buffer
Daniel Jasper6bd3b932013-05-21 12:21:39 +000034text = '\n'.join(buf)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000035
36# Determine range to format.
Daniel Jasper6bd3b932013-05-21 12:21:39 +000037cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
Alexander Kornienko3727c8f2013-07-20 01:01:25 +000038lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000039
Reid Kleckner57e68472013-06-10 14:16:26 +000040# Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
41startupinfo = None
42if sys.platform.startswith('win32'):
43 startupinfo = subprocess.STARTUPINFO()
44 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
45 startupinfo.wShowWindow = subprocess.SW_HIDE
46
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000047# Call formatter.
Alexander Kornienko3727c8f2013-07-20 01:01:25 +000048p = subprocess.Popen([binary, '-lines', lines, '-style', style,
49 '-cursor', str(cursor)],
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000050 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
Reid Kleckner57e68472013-06-10 14:16:26 +000051 stdin=subprocess.PIPE, startupinfo=startupinfo)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000052stdout, stderr = p.communicate(input=text)
53
54# If successful, replace buffer contents.
55if stderr:
56 message = stderr.splitlines()[0]
57 parts = message.split(' ', 2)
58 if len(parts) > 2:
59 message = parts[2]
60 print 'Formatting failed: %s (total %d warnings, %d errors)' % (
61 message, stderr.count('warning:'), stderr.count('error:'))
62
63if not stdout:
64 print ('No output from clang-format (crashed?).\n' +
65 'Please report to bugs.llvm.org.')
Daniel Jasper6bd3b932013-05-21 12:21:39 +000066else:
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000067 lines = stdout.split('\n')
Daniel Jasper6bd3b932013-05-21 12:21:39 +000068 output = json.loads(lines[0])
69 lines = lines[1:]
70 if '\n'.join(lines) != text:
Daniel Jasper72482172013-07-19 09:30:44 +000071 common_length = min(len(buf), len(lines))
72 buf[:common_length] = lines[:common_length]
Daniel Jasper6bd3b932013-05-21 12:21:39 +000073 for line in lines[len(buf):]:
74 buf.append(line)
75 del buf[len(lines):]
76 vim.command('goto %d' % (output['Cursor'] + 1))