| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
from __future__ import absolute_import, division, print_function |
| 41 |
|
| 42 |
import difflib |
| 43 |
import json |
| 44 |
import os.path |
| 45 |
import platform |
| 46 |
import subprocess |
| 47 |
import sys |
| 48 |
import vim |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
binary = 'clang-format' |
| 53 |
if vim.eval('exists("g:clang_format_path")') == "1": |
| 54 |
binary = vim.eval('g:clang_format_path') |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
style = None |
| 61 |
fallback_style = None |
| 62 |
if vim.eval('exists("g:clang_format_fallback_style")') == "1": |
| 63 |
fallback_style = vim.eval('g:clang_format_fallback_style') |
| 64 |
|
| 65 |
def get_buffer(encoding): |
| 66 |
if platform.python_version_tuple()[0] == '3': |
| 67 |
return vim.current.buffer |
| 68 |
return [ line.decode(encoding) for line in vim.current.buffer ] |
| 69 |
|
| 70 |
def main(): |
| 71 |
|
| 72 |
encoding = vim.eval("&encoding") |
| 73 |
buf = get_buffer(encoding) |
| 74 |
|
| 75 |
text = ('\n'.join(buf) + '\n').encode(encoding) |
| 76 |
|
| 77 |
|
| 78 |
if vim.eval('exists("l:lines")') == '1': |
| 79 |
lines = ['-lines', vim.eval('l:lines')] |
| 80 |
elif vim.eval('exists("l:formatdiff")') == '1' and \ |
| 81 |
os.path.exists(vim.current.buffer.name): |
| 82 |
with open(vim.current.buffer.name, 'r') as f: |
| 83 |
ondisk = f.read().splitlines(); |
| 84 |
sequence = difflib.SequenceMatcher(None, ondisk, vim.current.buffer) |
| 85 |
lines = [] |
| 86 |
for op in reversed(sequence.get_opcodes()): |
| 87 |
if op[0] not in ['equal', 'delete']: |
| 88 |
lines += ['-lines', '%s:%s' % (op[3] + 1, op[4])] |
| 89 |
if lines == []: |
| 90 |
return |
| 91 |
else: |
| 92 |
lines = ['-lines', '%s:%s' % (vim.current.range.start + 1, |
| 93 |
vim.current.range.end + 1)] |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
_, cursor_line, cursor_col, _ = vim.eval('getpos(".")') |
| 98 |
cursor_byte = 0 |
| 99 |
for line in text.split(b'\n')[:int(cursor_line) - 1]: |
| 100 |
cursor_byte += len(line) + 1 |
| 101 |
cursor_byte += int(cursor_col) - 1 |
| 102 |
if cursor_byte < 0: |
| 103 |
print('Couldn\'t determine cursor position. Is your file empty?') |
| 104 |
return |
| 105 |
|
| 106 |
|
| 107 |
startupinfo = None |
| 108 |
if sys.platform.startswith('win32'): |
| 109 |
startupinfo = subprocess.STARTUPINFO() |
| 110 |
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
| 111 |
startupinfo.wShowWindow = subprocess.SW_HIDE |
| 112 |
|
| 113 |
|
| 114 |
command = [binary, '-cursor', str(cursor_byte)] |
| 115 |
if lines != ['-lines', 'all']: |
| 116 |
command += lines |
| 117 |
if style: |
| 118 |
command.extend(['-style', style]) |
| 119 |
if fallback_style: |
| 120 |
command.extend(['-fallback-style', fallback_style]) |
| 121 |
if vim.current.buffer.name: |
| 122 |
command.extend(['-assume-filename', vim.current.buffer.name]) |
| 123 |
p = subprocess.Popen(command, |
| 124 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 125 |
stdin=subprocess.PIPE, startupinfo=startupinfo) |
| 126 |
stdout, stderr = p.communicate(input=text) |
| 127 |
|
| 128 |
|
| 129 |
if stderr: |
| 130 |
print(stderr) |
| 131 |
|
| 132 |
if not stdout: |
| 133 |
print( |
| 134 |
'No output from clang-format (crashed?).\n' |
| 135 |
'Please report to bugs.llvm.org.' |
| 136 |
) |
| 137 |
else: |
| 138 |
header, content = stdout.split(b'\n', 1) |
| 139 |
header = json.loads(header.decode('utf-8')) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
lines = content.decode(encoding).split('\n')[:-1] |
| 144 |
sequence = difflib.SequenceMatcher(None, buf, lines) |
| 145 |
for op in reversed(sequence.get_opcodes()): |
| 146 |
if op[0] != 'equal': |
| 147 |
vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] |
| 148 |
if header.get('IncompleteFormat'): |
| 149 |
print('clang-format: incomplete (syntax errors)') |
| 150 |
|
| 151 |
|
| 152 |
cursor_byte = int(header['Cursor']) |
| 153 |
prefix = content[0:cursor_byte] |
| 154 |
cursor_line = 1 + prefix.count(b'\n') |
| 155 |
cursor_column = 1 + len(prefix.rsplit(b'\n', 1)[-1]) |
| 156 |
vim.command('call cursor(%d, %d)' % (cursor_line, cursor_column)) |
| 157 |
|
| 158 |
main() |