blob: 3bb89e04b19f2e2a8b27512f2e937b57272c55a3 [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#
compnerd9216b272014-10-27 17:13:33 +00005# map <C-I> :pyf <path-to-this-file>/clang-format.py<cr>
compnerd3ddc3412014-11-02 21:27:52 +00006# imap <C-I> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
djasper7f663602013-03-20 09:53:23 +00007#
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
djasperb7e02b12013-07-21 10:45:33 +000020import difflib
djasperd0252b72013-05-21 12:21:39 +000021import json
djasper7f663602013-03-20 09:53:23 +000022import subprocess
rnk8f191962013-06-10 14:16:26 +000023import sys
djasperd0252b72013-05-21 12:21:39 +000024import vim
djasper7f663602013-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
chandlerce6992fd2013-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
hans79a882f2013-09-10 15:41:12 +000031# a '.clang-format' or '_clang-format' file to indicate the style that should be
32# used.
chandlerce6992fd2013-09-02 07:42:02 +000033style = 'file'
djasper05e73312013-04-09 15:23:04 +000034
ace2001acba28cea2014-03-10 22:12:14 +000035def main():
36 # Get the current text.
37 buf = vim.current.buffer
38 text = '\n'.join(buf)
djasper7f663602013-03-20 09:53:23 +000039
ace2001acba28cea2014-03-10 22:12:14 +000040 # Determine range to format.
ace2001acba28cea2014-03-10 22:12:14 +000041 lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)
djasper7f663602013-03-20 09:53:23 +000042
djasper4d1cfae2014-05-22 11:37:05 +000043 # Determine the cursor position.
44 cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
45 if cursor < 0:
46 print 'Couldn\'t determine cursor position. Is your file empty?'
47 return
48
ace2001acba28cea2014-03-10 22:12:14 +000049 # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
50 startupinfo = None
51 if sys.platform.startswith('win32'):
52 startupinfo = subprocess.STARTUPINFO()
53 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
54 startupinfo.wShowWindow = subprocess.SW_HIDE
rnk8f191962013-06-10 14:16:26 +000055
ace2001acba28cea2014-03-10 22:12:14 +000056 # Call formatter.
57 command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)]
58 if vim.current.buffer.name:
59 command.extend(['-assume-filename', vim.current.buffer.name])
60 p = subprocess.Popen(command,
61 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
62 stdin=subprocess.PIPE, startupinfo=startupinfo)
63 stdout, stderr = p.communicate(input=text)
djasper7f663602013-03-20 09:53:23 +000064
ace2001acba28cea2014-03-10 22:12:14 +000065 # If successful, replace buffer contents.
66 if stderr:
nico43f8ef42014-04-17 17:51:57 +000067 print stderr
djasper7f663602013-03-20 09:53:23 +000068
ace2001acba28cea2014-03-10 22:12:14 +000069 if not stdout:
70 print ('No output from clang-format (crashed?).\n' +
71 'Please report to bugs.llvm.org.')
72 else:
73 lines = stdout.split('\n')
74 output = json.loads(lines[0])
75 lines = lines[1:]
76 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
77 for op in reversed(sequence.get_opcodes()):
78 if op[0] is not 'equal':
79 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
80 vim.command('goto %d' % (output['Cursor'] + 1))
81
82main()