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