blob: 4077b4180cf0045de7623ea3035bfce6bd55b979 [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 Jasper0e295f32013-07-21 10:45:33 +000020import difflib
Daniel Jasper6bd3b932013-05-21 12:21:39 +000021import json
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000022import subprocess
Reid Kleckner57e68472013-06-10 14:16:26 +000023import sys
Daniel Jasper6bd3b932013-05-21 12:21:39 +000024import vim
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000025
26# Change this to the full path if clang-format is not on the path.
27binary = 'clang-format'
28
Chandler Carruth439fc852013-09-02 07:42:02 +000029# Change this to format according to other formatting styles. See the output of
30# 'clang-format --help' for a list of supported styles. The default looks for
31# a '.clang-format' file to indicate the style that should be used.
32style = 'file'
Daniel Jasper63911832013-04-09 15:23:04 +000033
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000034# Get the current text.
35buf = vim.current.buffer
Daniel Jasper6bd3b932013-05-21 12:21:39 +000036text = '\n'.join(buf)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000037
38# Determine range to format.
Daniel Jasper6bd3b932013-05-21 12:21:39 +000039cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
Alexander Kornienko3727c8f2013-07-20 01:01:25 +000040lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000041
Reid Kleckner57e68472013-06-10 14:16:26 +000042# Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
43startupinfo = None
44if sys.platform.startswith('win32'):
45 startupinfo = subprocess.STARTUPINFO()
46 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
47 startupinfo.wShowWindow = subprocess.SW_HIDE
48
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000049# Call formatter.
Alexander Kornienko3727c8f2013-07-20 01:01:25 +000050p = subprocess.Popen([binary, '-lines', lines, '-style', style,
51 '-cursor', str(cursor)],
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000052 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
Reid Kleckner57e68472013-06-10 14:16:26 +000053 stdin=subprocess.PIPE, startupinfo=startupinfo)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000054stdout, stderr = p.communicate(input=text)
55
56# If successful, replace buffer contents.
57if stderr:
58 message = stderr.splitlines()[0]
59 parts = message.split(' ', 2)
60 if len(parts) > 2:
61 message = parts[2]
62 print 'Formatting failed: %s (total %d warnings, %d errors)' % (
63 message, stderr.count('warning:'), stderr.count('error:'))
64
65if not stdout:
66 print ('No output from clang-format (crashed?).\n' +
67 'Please report to bugs.llvm.org.')
Daniel Jasper6bd3b932013-05-21 12:21:39 +000068else:
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000069 lines = stdout.split('\n')
Daniel Jasper6bd3b932013-05-21 12:21:39 +000070 output = json.loads(lines[0])
71 lines = lines[1:]
Daniel Jasper0e295f32013-07-21 10:45:33 +000072 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
Daniel Jasper4626a202013-07-22 16:22:13 +000073 for op in reversed(sequence.get_opcodes()):
Daniel Jasper0e295f32013-07-21 10:45:33 +000074 if op[0] is not 'equal':
75 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
76 vim.command('goto %d' % (output['Cursor'] + 1))