blob: bc47fcbb7d2e080562d8bdcc96c016c6b618e8e0 [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
djasperd0252b72013-05-21 12:21:39 +000022import vim
djasper7f663602013-03-20 09:53:23 +000023
24# Change this to the full path if clang-format is not on the path.
25binary = 'clang-format'
26
djasper05e73312013-04-09 15:23:04 +000027# Change this to format according to other formatting styles (see
28# clang-format -help)
29style = 'LLVM'
30
djasper7f663602013-03-20 09:53:23 +000031# Get the current text.
32buf = vim.current.buffer
djasperd0252b72013-05-21 12:21:39 +000033text = '\n'.join(buf)
djasper7f663602013-03-20 09:53:23 +000034
35# Determine range to format.
djasperd0252b72013-05-21 12:21:39 +000036cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
djasper7f663602013-03-20 09:53:23 +000037offset = int(vim.eval('line2byte(' +
38 str(vim.current.range.start + 1) + ')')) - 1
39length = int(vim.eval('line2byte(' +
40 str(vim.current.range.end + 2) + ')')) - offset - 2
41
42# Call formatter.
djasper05e73312013-04-09 15:23:04 +000043p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length),
djasperd0252b72013-05-21 12:21:39 +000044 '-style', style, '-cursor', str(cursor)],
djasper7f663602013-03-20 09:53:23 +000045 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
46 stdin=subprocess.PIPE)
47stdout, stderr = p.communicate(input=text)
48
49# If successful, replace buffer contents.
50if stderr:
51 message = stderr.splitlines()[0]
52 parts = message.split(' ', 2)
53 if len(parts) > 2:
54 message = parts[2]
55 print 'Formatting failed: %s (total %d warnings, %d errors)' % (
56 message, stderr.count('warning:'), stderr.count('error:'))
57
58if not stdout:
59 print ('No output from clang-format (crashed?).\n' +
60 'Please report to bugs.llvm.org.')
djasperd0252b72013-05-21 12:21:39 +000061else:
djasper7f663602013-03-20 09:53:23 +000062 lines = stdout.split('\n')
djasperd0252b72013-05-21 12:21:39 +000063 output = json.loads(lines[0])
64 lines = lines[1:]
65 if '\n'.join(lines) != text:
66 for i in range(min(len(buf), len(lines))):
67 buf[i] = lines[i]
68 for line in lines[len(buf):]:
69 buf.append(line)
70 del buf[len(lines):]
71 vim.command('goto %d' % (output['Cursor'] + 1))