blob: f5a57569db07bef15d9097c83b9c9adc17f6b245 [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
Hans Wennborg9a7a50e2013-09-10 15:41:12 +000031# a '.clang-format' or '_clang-format' file to indicate the style that should be
32# used.
Chandler Carruth439fc852013-09-02 07:42:02 +000033style = 'file'
Daniel Jasper63911832013-04-09 15:23:04 +000034
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000035# Get the current text.
36buf = vim.current.buffer
Daniel Jasper6bd3b932013-05-21 12:21:39 +000037text = '\n'.join(buf)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000038
39# Determine range to format.
Daniel Jasper6bd3b932013-05-21 12:21:39 +000040cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
Alexander Kornienko3727c8f2013-07-20 01:01:25 +000041lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000042
Reid Kleckner57e68472013-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
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000050# Call formatter.
Nico Weber73e94312013-10-25 20:06:20 +000051command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)]
52if vim.current.buffer.name:
53 command.extend(['-assume-filename', vim.current.buffer.name])
54p = subprocess.Popen(command,
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000055 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
Reid Kleckner57e68472013-06-10 14:16:26 +000056 stdin=subprocess.PIPE, startupinfo=startupinfo)
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000057stdout, stderr = p.communicate(input=text)
58
59# If successful, replace buffer contents.
60if stderr:
61 message = stderr.splitlines()[0]
62 parts = message.split(' ', 2)
63 if len(parts) > 2:
64 message = parts[2]
65 print 'Formatting failed: %s (total %d warnings, %d errors)' % (
66 message, stderr.count('warning:'), stderr.count('error:'))
67
68if not stdout:
69 print ('No output from clang-format (crashed?).\n' +
70 'Please report to bugs.llvm.org.')
Daniel Jasper6bd3b932013-05-21 12:21:39 +000071else:
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000072 lines = stdout.split('\n')
Daniel Jasper6bd3b932013-05-21 12:21:39 +000073 output = json.loads(lines[0])
74 lines = lines[1:]
Daniel Jasper0e295f32013-07-21 10:45:33 +000075 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
Daniel Jasper4626a202013-07-22 16:22:13 +000076 for op in reversed(sequence.get_opcodes()):
Daniel Jasper0e295f32013-07-21 10:45:33 +000077 if op[0] is not 'equal':
78 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
79 vim.command('goto %d' % (output['Cursor'] + 1))