Skip to content

Commit 8627811

Browse files
committed
[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
1 parent 4548fca commit 8627811

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

clang/tools/clang-format/clang-format.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import difflib
4343
import json
44+
import os.path
4445
import platform
4546
import subprocess
4647
import sys
@@ -76,7 +77,8 @@ def main():
7677
# Determine range to format.
7778
if vim.eval('exists("l:lines")') == '1':
7879
lines = ['-lines', vim.eval('l:lines')]
79-
elif vim.eval('exists("l:formatdiff")') == '1':
80+
elif vim.eval('exists("l:formatdiff")') == '1' and \
81+
os.path.exists(vim.current.buffer.name):
8082
with open(vim.current.buffer.name, 'r') as f:
8183
ondisk = f.read().splitlines();
8284
sequence = difflib.SequenceMatcher(None, ondisk, vim.current.buffer)

0 commit comments

Comments
 (0)