<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Mon, Jun 19, 2017 at 12:30 AM Daniel Jasper via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: djasper<br>
Date: Mon Jun 19 02:30:04 2017<br>
New Revision: 305665<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=305665&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=305665&view=rev</a><br>
Log:<br>
clang-format: Add capability to format the diff on save in vim.<br>
<br>
With this patch, one can configure a BufWrite hook that will make the<br>
clang-format integration compute a diff of the current buffer with the file<br>
that's on disk and format all changed lines. This should create a<br>
zero-overhead auto-format solution that doesn't require the file to<br>
already be clang-format clean to avoid spurious diffs.<br>
<br>
Review: <a href="https://reviews.llvm.org/D32429" rel="noreferrer" target="_blank">https://reviews.llvm.org/D32429</a><br>
<br>
Modified:<br>
    cfe/trunk/docs/ClangFormat.rst<br>
    cfe/trunk/tools/clang-format/clang-format.py<br>
<br>
Modified: cfe/trunk/docs/ClangFormat.rst<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ClangFormat.rst?rev=305665&r1=305664&r2=305665&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ClangFormat.rst?rev=305665&r1=305664&r2=305665&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/docs/ClangFormat.rst (original)<br>
+++ cfe/trunk/docs/ClangFormat.rst Mon Jun 19 02:30:04 2017<br>
@@ -120,6 +120,18 @@ entity.<br>
 It operates on the current, potentially unsaved buffer and does not create<br>
 or save any files. To revert a formatting, just undo.<br>
<br>
+An alternative option is to format changes when saving a file and thus to<br>
+have a zero-effort integration into the coding workflow. To do this, add this to<br>
+your `.vimrc`:<br>
+<br>
+.. code-block:: vim<br>
+<br>
+  function! Formatonsave()<br>
+    let l:formatdiff = 1<br>
+    pyf ~/llvm/tools/clang/tools/clang-format/clang-format.py<br>
+  endfunction<br>
+  autocmd BufWritePre *.h,*.cc,*.cpp call Formatonsave()<br></blockquote><div><br>A cursory experiment with this didn't seem to quite do what I was expecting. Am I holding it wrong?<br><br>Opened a file containing this:<br><font face="monospace"><br></font><div><font face="monospace">void f3();</font></div><div><font face="monospace">__attribute__((used)) inline void f1() { </font></div><div><font face="monospace">  f3();</font></div><div><font face="monospace">}</font></div><div><font face="monospace">__attribute__((used)) inline void f2() {</font></div><div><font face="monospace">  f1();</font></div><div><font face="monospace">}</font><br><br>I removed a space from the beginining of line 3 (the call to f3()) and then removed a space from between "f2()" and "{" on line 5.<br><br>I edited edit-mode and ran ":w" - only the second edit location was reformatted. The f3() call remained indented with one space.<br><br>When I move the cursor up to line 3 (or 2 or 4) and save, it reformats that line.<br><br>Indeed even if I don't modify anything, and run ":w" on lines 2-7, it reformats either f1 or f2, depending on which range the cursor is in when I write the file.?</div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+<br>
<br>
 Emacs Integration<br>
 =================<br>
<br>
Modified: cfe/trunk/tools/clang-format/clang-format.py<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.py?rev=305665&r1=305664&r2=305665&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.py?rev=305665&r1=305664&r2=305665&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/tools/clang-format/clang-format.py (original)<br>
+++ cfe/trunk/tools/clang-format/clang-format.py Mon Jun 19 02:30:04 2017<br>
@@ -63,8 +63,19 @@ def main():<br>
   # Determine range to format.<br>
   if vim.eval('exists("l:lines")') == '1':<br>
     lines = vim.eval('l:lines')<br>
+  elif vim.eval('exists("l:formatdiff")') == '1':<br>
+    with open(<a href="http://vim.current.buffer.name" rel="noreferrer" target="_blank">vim.current.buffer.name</a>, 'r') as f:<br>
+      ondisk = f.read().splitlines();<br>
+    sequence = difflib.SequenceMatcher(None, ondisk, vim.current.buffer)<br>
+    lines = []<br>
+    for op in reversed(sequence.get_opcodes()):<br>
+      if op[0] not in ['equal', 'delete']:<br>
+        lines += ['-lines', '%s:%s' % (op[3] + 1, op[4])]<br>
+    if lines == []:<br>
+      return<br>
   else:<br>
-    lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1)<br>
+    lines = ['-lines', '%s:%s' % (vim.current.range.start + 1,<br>
+                                  vim.current.range.end + 1)]<br>
<br>
   # Determine the cursor position.<br>
   cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2<br>
@@ -82,7 +93,7 @@ def main():<br>
   # Call formatter.<br>
   command = [binary, '-style', style, '-cursor', str(cursor)]<br>
   if lines != 'all':<br>
-    command.extend(['-lines', lines])<br>
+    command += lines<br>
   if fallback_style:<br>
     command.extend(['-fallback-style', fallback_style])<br>
   if <a href="http://vim.current.buffer.name" rel="noreferrer" target="_blank">vim.current.buffer.name</a>:<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div></div>