vim: update clang-format.py
update from SVN https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/clang-format.py
This commit is contained in:
parent
01da65f526
commit
8fbbcb7c10
@ -25,9 +25,11 @@
|
|||||||
#
|
#
|
||||||
# It operates on the current, potentially unsaved buffer and does not create
|
# It operates on the current, potentially unsaved buffer and does not create
|
||||||
# or save any files. To revert a formatting, just undo.
|
# or save any files. To revert a formatting, just undo.
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
import difflib
|
import difflib
|
||||||
import json
|
import json
|
||||||
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import vim
|
import vim
|
||||||
@ -47,24 +49,38 @@ fallback_style = None
|
|||||||
if vim.eval('exists("g:clang_format_fallback_style")') == "1":
|
if vim.eval('exists("g:clang_format_fallback_style")') == "1":
|
||||||
fallback_style = vim.eval('g:clang_format_fallback_style')
|
fallback_style = vim.eval('g:clang_format_fallback_style')
|
||||||
|
|
||||||
|
def get_buffer(encoding):
|
||||||
|
if platform.python_version_tuple()[0] == '3':
|
||||||
|
return vim.current.buffer
|
||||||
|
return [ line.decode(encoding) for line in vim.current.buffer ]
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Get the current text.
|
# Get the current text.
|
||||||
buf = vim.current.buffer
|
encoding = vim.eval("&encoding")
|
||||||
|
buf = get_buffer(encoding)
|
||||||
text = '\n'.join(buf)
|
text = '\n'.join(buf)
|
||||||
|
|
||||||
# Determine range to format.
|
# Determine range to format.
|
||||||
if vim.eval('exists("l:lines")') == '1':
|
if vim.eval('exists("l:lines")') == '1':
|
||||||
lines = vim.eval('l:lines')
|
lines = ['-lines', vim.eval('l:lines')]
|
||||||
|
elif vim.eval('exists("l:formatdiff")') == '1':
|
||||||
|
with open(vim.current.buffer.name, 'r') as f:
|
||||||
|
ondisk = f.read().splitlines();
|
||||||
|
sequence = difflib.SequenceMatcher(None, ondisk, vim.current.buffer)
|
||||||
|
lines = []
|
||||||
|
for op in reversed(sequence.get_opcodes()):
|
||||||
|
if op[0] not in ['equal', 'delete']:
|
||||||
|
lines += ['-lines', '%s:%s' % (op[3] + 1, op[4])]
|
||||||
|
if lines == []:
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)
|
lines = ['-lines', '%s:%s' % (vim.current.range.start + 1,
|
||||||
|
vim.current.range.end + 1)]
|
||||||
if vim.eval('exists("g:clang_style")') == '1':
|
|
||||||
style = vim.eval('g:clang_style')
|
|
||||||
|
|
||||||
# Determine the cursor position.
|
# Determine the cursor position.
|
||||||
cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
|
cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
|
||||||
if cursor < 0:
|
if cursor < 0:
|
||||||
print 'Couldn\'t determine cursor position. Is your file empty?'
|
print('Couldn\'t determine cursor position. Is your file empty?')
|
||||||
return
|
return
|
||||||
|
|
||||||
# Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
|
# Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
|
||||||
@ -77,7 +93,7 @@ def main():
|
|||||||
# Call formatter.
|
# Call formatter.
|
||||||
command = [binary, '-style', style, '-cursor', str(cursor)]
|
command = [binary, '-style', style, '-cursor', str(cursor)]
|
||||||
if lines != 'all':
|
if lines != 'all':
|
||||||
command.extend(['-lines', lines])
|
command += lines
|
||||||
if fallback_style:
|
if fallback_style:
|
||||||
command.extend(['-fallback-style', fallback_style])
|
command.extend(['-fallback-style', fallback_style])
|
||||||
if vim.current.buffer.name:
|
if vim.current.buffer.name:
|
||||||
@ -85,25 +101,27 @@ def main():
|
|||||||
p = subprocess.Popen(command,
|
p = subprocess.Popen(command,
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
stdin=subprocess.PIPE, startupinfo=startupinfo)
|
stdin=subprocess.PIPE, startupinfo=startupinfo)
|
||||||
stdout, stderr = p.communicate(input=text)
|
stdout, stderr = p.communicate(input=text.encode(encoding))
|
||||||
|
|
||||||
# If successful, replace buffer contents.
|
# If successful, replace buffer contents.
|
||||||
if stderr:
|
if stderr:
|
||||||
print stderr
|
print(stderr)
|
||||||
|
|
||||||
if not stdout:
|
if not stdout:
|
||||||
print ('No output from clang-format (crashed?).\n' +
|
print(
|
||||||
'Please report to bugs.llvm.org.')
|
'No output from clang-format (crashed?).\n'
|
||||||
|
'Please report to bugs.llvm.org.'
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
lines = stdout.split('\n')
|
lines = stdout.decode(encoding).split('\n')
|
||||||
output = json.loads(lines[0])
|
output = json.loads(lines[0])
|
||||||
lines = lines[1:]
|
lines = lines[1:]
|
||||||
sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines)
|
sequence = difflib.SequenceMatcher(None, buf, lines)
|
||||||
for op in reversed(sequence.get_opcodes()):
|
for op in reversed(sequence.get_opcodes()):
|
||||||
if op[0] is not 'equal':
|
if op[0] is not 'equal':
|
||||||
vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
|
vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
|
||||||
if output.get('IncompleteFormat'):
|
if output.get('IncompleteFormat'):
|
||||||
print 'clang-format: incomplete (syntax errors)'
|
print('clang-format: incomplete (syntax errors)')
|
||||||
vim.command('goto %d' % (output['Cursor'] + 1))
|
vim.command('goto %d' % (output['Cursor'] + 1))
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user