]> granicus.if.org Git - clang/commitdiff
clang-format: Fix vim-integration for empty files.
authorDaniel Jasper <djasper@google.com>
Thu, 22 May 2014 11:37:05 +0000 (11:37 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 22 May 2014 11:37:05 +0000 (11:37 +0000)
Discovered by Ahmed Charles in http://reviews.llvm.org/D3018.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209417 91177308-0d34-0410-b5e6-96231b3b80d8

tools/clang-format/clang-format.py

index 46af0682aa7438b2e1c8582539a1be843457ef18..16a187910832bf2d8b6c28bbd703c87519f9de0c 100644 (file)
@@ -38,9 +38,14 @@ def main():
   text = '\n'.join(buf)
 
   # Determine range to format.
-  cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2
   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:
+    print 'Couldn\'t determine cursor position. Is your file empty?'
+    return
+
   # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format.
   startupinfo = None
   if sys.platform.startswith('win32'):