[clang] 8627811 - [clang-format] Support new file formatting with vim

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 21 13:19:54 PST 2022


Author: Ji, Jinsong
Date: 2022-11-21T13:12:31-08:00
New Revision: 86278114085bdce39b9cf1895447070454bb5fe3

URL: https://github.com/llvm/llvm-project/commit/86278114085bdce39b9cf1895447070454bb5fe3
DIFF: https://github.com/llvm/llvm-project/commit/86278114085bdce39b9cf1895447070454bb5fe3.diff

LOG: [clang-format] Support new file formatting with vim

The vim Formatonsave integration is not working if we create a new file directly using vim.
eg: vi -V9t.log t.cpp

It will not able to format the buffer.

> Traceback (most recent call last):
>   File "<string>", line 1, in <module>
>   File "...clang/tools/clang-format/clang-format.py", line 156, in <module>
>     main()
>   File "...clang/tools/clang-format/clang-format.py", line 80, in main
>     with open(vim.current.buffer.name, 'r') as f:
> FileNotFoundError: [Errno 2] No such file or directory: '...t.cpp'

This patch check the file before we try to open it.

Reviewed By: owenpan

Differential Revision: https://reviews.llvm.org/D138234

Added: 
    

Modified: 
    clang/tools/clang-format/clang-format.py

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/clang-format.py b/clang/tools/clang-format/clang-format.py
index 5bc108bfc713b..7933dbcfadd88 100644
--- a/clang/tools/clang-format/clang-format.py
+++ b/clang/tools/clang-format/clang-format.py
@@ -41,6 +41,7 @@
 
 import 
diff lib
 import json
+import os.path
 import platform
 import subprocess
 import sys
@@ -76,7 +77,8 @@ def main():
   # Determine range to format.
   if vim.eval('exists("l:lines")') == '1':
     lines = ['-lines', vim.eval('l:lines')]
-  elif vim.eval('exists("l:format
diff ")') == '1':
+  elif vim.eval('exists("l:format
diff ")') == '1' and \
+       os.path.exists(vim.current.buffer.name):
     with open(vim.current.buffer.name, 'r') as f:
       ondisk = f.read().splitlines();
     sequence = 
diff lib.SequenceMatcher(None, ondisk, vim.current.buffer)


        


More information about the cfe-commits mailing list