aboutsummaryrefslogtreecommitdiff
path: root/share/clang
diff options
context:
space:
mode:
Diffstat (limited to 'share/clang')
-rwxr-xr-xshare/clang/clang-format-diff.py16
-rwxr-xr-xshare/clang/clang-format.py33
2 files changed, 32 insertions, 17 deletions
diff --git a/share/clang/clang-format-diff.py b/share/clang/clang-format-diff.py
index 122db49..6e653a1 100755
--- a/share/clang/clang-format-diff.py
+++ b/share/clang/clang-format-diff.py
@@ -13,9 +13,13 @@ This script reads input from a unified diff and reformats all the changed
lines. This is useful to reformat all the lines touched by a specific patch.
Example usage for git/svn users:
- git diff -U0 --no-color HEAD^ | clang-format-diff.py -p1 -i
+ git diff -U0 --no-color --relative HEAD^ | clang-format-diff.py -p1 -i
svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
+It should be noted that the filename contained in the diff is used unmodified
+to determine the source file to update. Users calling this script directly
+should be careful to ensure that the path in the diff is correct relative to the
+current working directory.
"""
from __future__ import absolute_import, division, print_function
@@ -43,8 +47,8 @@ def main():
help='custom pattern selecting file paths to reformat '
'(case sensitive, overrides -iregex)')
parser.add_argument('-iregex', metavar='PATTERN', default=
- r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hh|hpp|m|mm|inc|js|ts|proto'
- r'|protodevel|java|cs)',
+ r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hh|hpp|hxx|m|mm|inc|js|ts'
+ r'|proto|protodevel|java|cs)',
help='custom pattern selecting file paths to reformat '
'(case insensitive, overridden by -regex)')
parser.add_argument('-sort-includes', action='store_true', default=False,
@@ -52,8 +56,8 @@ def main():
parser.add_argument('-v', '--verbose', action='store_true',
help='be more verbose, ineffective without -i')
parser.add_argument('-style',
- help='formatting style to apply (LLVM, Google, Chromium, '
- 'Mozilla, WebKit)')
+ help='formatting style to apply (LLVM, GNU, Google, Chromium, '
+ 'Microsoft, Mozilla, WebKit)')
parser.add_argument('-binary', default='clang-format',
help='location of binary to use for clang-format')
args = parser.parse_args()
@@ -65,7 +69,7 @@ def main():
match = re.search(r'^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
if match:
filename = match.group(2)
- if filename == None:
+ if filename is None:
continue
if args.regex is not None:
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()