aboutsummaryrefslogtreecommitdiff
path: root/share/clang/clang-format.py
diff options
context:
space:
mode:
authorDanny <danny@kdrag0n.dev>2021-01-09 23:34:32 +0000
committermosimchah <mosimchah@gmail.com>2021-01-22 03:35:20 -0800
commit783d21ff74759076d2fc503685ca47d2c29baea3 (patch)
treed650cc46cbf7ca53f15c77ced2682e97d492c068 /share/clang/clang-format.py
parentfdbc6f7102056fb52d26bfb2cbc6ea317890ee34 (diff)
Update to 20210109 buildHEADmaster
LLVM commit: https://github.com/llvm/llvm-project/commit/b02eab9058e58782fca32dd8b1e53c27ed93f866 binutils version: 2.35.1 Builder commit: https://github.com/kdrag0n/proton-clang-build/commit/ba42f701467c9103f23fbb90aca4b23858221ee2
Diffstat (limited to 'share/clang/clang-format.py')
-rwxr-xr-xshare/clang/clang-format.py33
1 files changed, 22 insertions, 11 deletions
diff --git a/share/clang/clang-format.py b/share/clang/clang-format.py
index 1a615b1..76fedb6 100755
--- a/share/clang/clang-format.py
+++ b/share/clang/clang-format.py
@@ -71,7 +71,7 @@ def main():
encoding = vim.eval("&encoding")
buf = get_buffer(encoding)
# Join the buffer into a single string with a terminating newline
- text = '\n'.join(buf) + '\n'
+ text = ('\n'.join(buf) + '\n').encode(encoding)
# Determine range to format.
if vim.eval('exists("l:lines")') == '1':
@@ -90,9 +90,14 @@ def main():
lines = ['-lines', '%s:%s' % (vim.current.range.start + 1,
vim.current.range.end + 1)]
- # Determine the cursor position.
- cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
- if cursor < 0:
+ # Convert cursor (line, col) to bytes.
+ # Don't use line2byte: https://github.com/vim/vim/issues/5930
+ _, cursor_line, cursor_col, _ = vim.eval('getpos(".")') # 1-based
+ cursor_byte = 0
+ for line in text.split(b'\n')[:int(cursor_line) - 1]:
+ cursor_byte += len(line) + 1
+ cursor_byte += int(cursor_col) - 1
+ if cursor_byte < 0:
print('Couldn\'t determine cursor position. Is your file empty?')
return
@@ -104,7 +109,7 @@ def main():
startupinfo.wShowWindow = subprocess.SW_HIDE
# Call formatter.
- command = [binary, '-cursor', str(cursor)]
+ command = [binary, '-cursor', str(cursor_byte)]
if lines != ['-lines', 'all']:
command += lines
if style:
@@ -116,7 +121,7 @@ def main():
p = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE, startupinfo=startupinfo)
- stdout, stderr = p.communicate(input=text.encode(encoding))
+ stdout, stderr = p.communicate(input=text)
# If successful, replace buffer contents.
if stderr:
@@ -128,18 +133,24 @@ def main():
'Please report to bugs.llvm.org.'
)
else:
- lines = stdout.decode(encoding).split('\n')
- output = json.loads(lines[0])
+ header, content = stdout.split(b'\n', 1)
+ header = json.loads(header)
# Strip off the trailing newline (added above).
# This maintains trailing empty lines present in the buffer if
# the -lines specification requests them to remain unchanged.
- lines = lines[1:-1]
+ lines = content.decode(encoding).split('\n')[:-1]
sequence = difflib.SequenceMatcher(None, buf, lines)
for op in reversed(sequence.get_opcodes()):
if op[0] != 'equal':
vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]
- if output.get('IncompleteFormat'):
+ if header.get('IncompleteFormat'):
print('clang-format: incomplete (syntax errors)')
- vim.command('goto %d' % (output['Cursor'] + 1))
+ # Convert cursor bytes to (line, col)
+ # Don't use goto: https://github.com/vim/vim/issues/5930
+ cursor_byte = int(header['Cursor'])
+ prefix = content[0:cursor_byte]
+ cursor_line = 1 + prefix.count(b'\n')
+ cursor_column = 1 + len(prefix.rsplit(b'\n', 1)[-1])
+ vim.command('call cursor(%d, %d)' % (cursor_line, cursor_column))
main()