From: Daniel Jasper Date: Sat, 11 Jul 2015 06:46:26 +0000 (+0000) Subject: clang-format: Extend vim integration so that a line range can be passed in. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3e0ef38ccb0a96aa9e577b9344cae2b0ddcbb161;p=clang clang-format: Extend vim integration so that a line range can be passed in. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241976 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/clang-format/clang-format.py b/tools/clang-format/clang-format.py index 49ca773b04..5cb41fcfa3 100644 --- a/tools/clang-format/clang-format.py +++ b/tools/clang-format/clang-format.py @@ -14,6 +14,15 @@ # VISUAL mode. The line or region is extended to the next bigger syntactic # entity. # +# You can also pass in the variable "l:lines" to choose the range for +# formatting. This variable can either contain ":" or +# "all" to format the full file. So, to format the full file, write a function +# like: +# :function FormatFile() +# : let l:lines="all" +# : pyf /clang-format.py +# :endfunction +# # It operates on the current, potentially unsaved buffer and does not create # or save any files. To revert a formatting, just undo. @@ -44,7 +53,10 @@ def main(): text = '\n'.join(buf) # Determine range to format. - lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1) + if vim.eval('exists("l:lines")') == '1': + lines = vim.eval('l:lines') + else: + 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 @@ -60,7 +72,9 @@ def main(): startupinfo.wShowWindow = subprocess.SW_HIDE # Call formatter. - command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)] + command = [binary, '-style', style, '-cursor', str(cursor)] + if lines != 'all': + command.extend(['-lines', lines]) if fallback_style: command.extend(['-fallback-style', fallback_style]) if vim.current.buffer.name: