2013-03-20 17:53:23 +08:00
|
|
|
# This file is a minimal clang-format vim-integration. To install:
|
|
|
|
# - Change 'binary' if clang-format is not on the path (see below).
|
|
|
|
# - Add to your .vimrc:
|
|
|
|
#
|
|
|
|
# map <C-I> :pyf <path-to-this-file>/clang-format.py<CR>
|
|
|
|
# imap <C-I> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i
|
|
|
|
#
|
|
|
|
# The first line enables clang-format for NORMAL and VISUAL mode, the second
|
|
|
|
# line adds support for INSERT mode. Change "C-I" to another binding if you
|
|
|
|
# need clang-format on a different key (C-I stands for Ctrl+i).
|
|
|
|
#
|
|
|
|
# With this integration you can press the bound key and clang-format will
|
|
|
|
# format the current line in NORMAL and INSERT mode or the selected region in
|
|
|
|
# VISUAL mode. The line or region is extended to the next bigger syntactic
|
|
|
|
# entity.
|
|
|
|
#
|
|
|
|
# It operates on the current, potentially unsaved buffer and does not create
|
|
|
|
# or save any files. To revert a formatting, just undo.
|
|
|
|
|
2013-07-21 18:45:33 +08:00
|
|
|
import difflib
|
2013-05-21 20:21:39 +08:00
|
|
|
import json
|
2013-03-20 17:53:23 +08:00
|
|
|
import subprocess
|
2013-06-10 22:16:26 +08:00
|
|
|
import sys
|
2013-05-21 20:21:39 +08:00
|
|
|
import vim
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
# Change this to the full path if clang-format is not on the path.
|
|
|
|
binary = 'clang-format'
|
|
|
|
|
2013-04-09 23:23:04 +08:00
|
|
|
# Change this to format according to other formatting styles (see
|
|
|
|
# clang-format -help)
|
|
|
|
style = 'LLVM'
|
|
|
|
|
2013-03-20 17:53:23 +08:00
|
|
|
# Get the current text.
|
|
|
|
buf = vim.current.buffer
|
2013-05-21 20:21:39 +08:00
|
|
|
text = '\n'.join(buf)
|
2013-03-20 17:53:23 +08:00
|
|
|
|
|
|
|
# Determine range to format.
|
2013-05-21 20:21:39 +08:00
|
|
|
cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
|
2013-07-20 09:01:25 +08:00
|
|
|
lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2013-06-10 22:16:26 +08:00
|
|
|
# Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
|
|
|
|
startupinfo = None
|
|
|
|
if sys.platform.startswith('win32'):
|
|
|
|
startupinfo = subprocess.STARTUPINFO()
|
|
|
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
|
|
|
startupinfo.wShowWindow = subprocess.SW_HIDE
|
|
|
|
|
2013-03-20 17:53:23 +08:00
|
|
|
# Call formatter.
|
2013-07-20 09:01:25 +08:00
|
|
|
p = subprocess.Popen([binary, '-lines', lines, '-style', style,
|
|
|
|
'-cursor', str(cursor)],
|
2013-03-20 17:53:23 +08:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
2013-06-10 22:16:26 +08:00
|
|
|
stdin=subprocess.PIPE, startupinfo=startupinfo)
|
2013-03-20 17:53:23 +08:00
|
|
|
stdout, stderr = p.communicate(input=text)
|
|
|
|
|
|
|
|
# If successful, replace buffer contents.
|
|
|
|
if stderr:
|
|
|
|
message = stderr.splitlines()[0]
|
|
|
|
parts = message.split(' ', 2)
|
|
|
|
if len(parts) > 2:
|
|
|
|
message = parts[2]
|
|
|
|
print 'Formatting failed: %s (total %d warnings, %d errors)' % (
|
|
|
|
message, stderr.count('warning:'), stderr.count('error:'))
|
|
|
|
|
|
|
|
if not stdout:
|
|
|
|
print ('No output from clang-format (crashed?).\n' +
|
|
|
|
'Please report to bugs.llvm.org.')
|
2013-05-21 20:21:39 +08:00
|
|
|
else:
|
2013-03-20 17:53:23 +08:00
|
|
|
lines = stdout.split('\n')
|
2013-05-21 20:21:39 +08:00
|
|
|
output = json.loads(lines[0])
|
|
|
|
lines = lines[1:]
|
2013-07-21 18:45:33 +08:00
|
|
|
sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
|
2013-07-23 00:22:13 +08:00
|
|
|
for op in reversed(sequence.get_opcodes()):
|
2013-07-21 18:45:33 +08:00
|
|
|
if op[0] is not 'equal':
|
|
|
|
vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
|
|
|
|
vim.command('goto %d' % (output['Cursor'] + 1))
|