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:
|
|
|
|
#
|
2014-10-28 01:13:33 +08:00
|
|
|
# map <C-I> :pyf <path-to-this-file>/clang-format.py<cr>
|
2014-11-03 05:27:52 +08:00
|
|
|
# imap <C-I> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
|
2013-03-20 17:53:23 +08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2015-07-11 14:46:26 +08:00
|
|
|
# You can also pass in the variable "l:lines" to choose the range for
|
|
|
|
# formatting. This variable can either contain "<start line>:<end line>" or
|
|
|
|
# "all" to format the full file. So, to format the full file, write a function
|
|
|
|
# like:
|
|
|
|
# :function FormatFile()
|
|
|
|
# : let l:lines="all"
|
|
|
|
# : pyf <path-to-this-file>/clang-format.py
|
|
|
|
# :endfunction
|
|
|
|
#
|
2013-03-20 17:53:23 +08:00
|
|
|
# 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
|
|
|
|
2014-11-03 05:27:59 +08:00
|
|
|
# set g:clang_format_path to the path to clang-format if it is not on the path
|
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'
|
2014-11-04 18:40:26 +08:00
|
|
|
if vim.eval('exists("g:clang_format_path")') == "1":
|
2014-11-03 05:27:59 +08:00
|
|
|
binary = vim.eval('g:clang_format_path')
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2013-09-02 15:42:02 +08:00
|
|
|
# Change this to format according to other formatting styles. See the output of
|
|
|
|
# 'clang-format --help' for a list of supported styles. The default looks for
|
2013-09-10 23:41:12 +08:00
|
|
|
# a '.clang-format' or '_clang-format' file to indicate the style that should be
|
|
|
|
# used.
|
2013-09-02 15:42:02 +08:00
|
|
|
style = 'file'
|
2015-04-17 15:59:19 +08:00
|
|
|
fallback_style = None
|
2015-04-16 16:26:37 +08:00
|
|
|
if vim.eval('exists("g:clang_format_fallback_style")') == "1":
|
|
|
|
fallback_style = vim.eval('g:clang_format_fallback_style')
|
2013-04-09 23:23:04 +08:00
|
|
|
|
2014-03-11 06:12:14 +08:00
|
|
|
def main():
|
|
|
|
# Get the current text.
|
|
|
|
buf = vim.current.buffer
|
|
|
|
text = '\n'.join(buf)
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2014-03-11 06:12:14 +08:00
|
|
|
# Determine range to format.
|
2015-07-11 14:46:26 +08:00
|
|
|
if vim.eval('exists("l:lines")') == '1':
|
|
|
|
lines = vim.eval('l:lines')
|
|
|
|
else:
|
|
|
|
lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2014-05-22 19:37:05 +08:00
|
|
|
# Determine the cursor position.
|
|
|
|
cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
|
|
|
|
if cursor < 0:
|
|
|
|
print 'Couldn\'t determine cursor position. Is your file empty?'
|
|
|
|
return
|
|
|
|
|
2014-03-11 06:12:14 +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-06-10 22:16:26 +08:00
|
|
|
|
2014-03-11 06:12:14 +08:00
|
|
|
# Call formatter.
|
2015-11-16 20:38:56 +08:00
|
|
|
command = [binary, '-style', style, '-cursor', str(cursor)]
|
2015-07-11 14:46:26 +08:00
|
|
|
if lines != 'all':
|
|
|
|
command.extend(['-lines', lines])
|
2015-04-16 16:26:37 +08:00
|
|
|
if fallback_style:
|
|
|
|
command.extend(['-fallback-style', fallback_style])
|
2014-03-11 06:12:14 +08:00
|
|
|
if vim.current.buffer.name:
|
|
|
|
command.extend(['-assume-filename', vim.current.buffer.name])
|
|
|
|
p = subprocess.Popen(command,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE, startupinfo=startupinfo)
|
|
|
|
stdout, stderr = p.communicate(input=text)
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2014-03-11 06:12:14 +08:00
|
|
|
# If successful, replace buffer contents.
|
|
|
|
if stderr:
|
2014-04-18 01:51:57 +08:00
|
|
|
print stderr
|
2013-03-20 17:53:23 +08:00
|
|
|
|
2014-03-11 06:12:14 +08:00
|
|
|
if not stdout:
|
|
|
|
print ('No output from clang-format (crashed?).\n' +
|
|
|
|
'Please report to bugs.llvm.org.')
|
|
|
|
else:
|
|
|
|
lines = stdout.split('\n')
|
|
|
|
output = json.loads(lines[0])
|
|
|
|
lines = lines[1:]
|
|
|
|
sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
|
|
|
|
for op in reversed(sequence.get_opcodes()):
|
|
|
|
if op[0] is not 'equal':
|
|
|
|
vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
|
2015-06-05 05:23:07 +08:00
|
|
|
if output.get('IncompleteFormat'):
|
2015-06-02 20:01:50 +08:00
|
|
|
print 'clang-format: incomplete (syntax errors)'
|
2014-03-11 06:12:14 +08:00
|
|
|
vim.command('goto %d' % (output['Cursor'] + 1))
|
|
|
|
|
|
|
|
main()
|