r179098 - Improvements to clang-format integrations.
Daniel Jasper
djasper at google.com
Tue Apr 9 08:23:04 PDT 2013
Author: djasper
Date: Tue Apr 9 10:23:04 2013
New Revision: 179098
URL: http://llvm.org/viewvc/llvm-project?rev=179098&view=rev
Log:
Improvements to clang-format integrations.
This adds an emacs editor integration (thanks to Ami Fischman). Also
pulls out the style into a variable for the vi integration and just
uses clang-formats defaults style in clang-format-diff.py.
Added:
cfe/trunk/tools/clang-format/clang-format.el
Modified:
cfe/trunk/tools/clang-format/clang-format-diff.py
cfe/trunk/tools/clang-format/clang-format.py
Modified: cfe/trunk/tools/clang-format/clang-format-diff.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format-diff.py?rev=179098&r1=179097&r2=179098&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format-diff.py (original)
+++ cfe/trunk/tools/clang-format/clang-format-diff.py Tue Apr 9 10:23:04 2013
@@ -63,9 +63,10 @@ def formatRange(r, style):
offset, length = getOffsetLength(filename, line_number, line_count)
with open(filename, 'r') as f:
text = f.read()
- p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length),
- '-style', style],
- stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ command = [binary, '-offset', str(offset), '-length', str(length)]
+ if style:
+ command.append('-style', style)
+ p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
stdout, stderr = p.communicate(input=text)
if stderr:
@@ -84,8 +85,7 @@ def main():
'Reformat changed lines in diff')
parser.add_argument('-p', default=1,
help='strip the smallest prefix containing P slashes')
- parser.add_argument('-style', default='LLVM',
- help='formatting style to apply (LLVM, Google)')
+ parser.add_argument('-style', help='formatting style to apply (LLVM, Google)')
args = parser.parse_args()
filename = None
Added: cfe/trunk/tools/clang-format/clang-format.el
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.el?rev=179098&view=auto
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format.el (added)
+++ cfe/trunk/tools/clang-format/clang-format.el Tue Apr 9 10:23:04 2013
@@ -0,0 +1,47 @@
+;;; Clang-format emacs integration for use with C/Objective-C/C++.
+
+;; This defines a function clang-format-region that you can bind to a key.
+;; A minimal .emacs would contain:
+;;
+;; (load "<path-to-clang>/tools/clang/clang-format/clang-format.el")
+;; (global-set-key [C-M-tab] 'clang-format-region)
+;;
+;; Depending on your configuration and coding style, you might need to modify
+;; 'style' and 'binary' below.
+(defun clang-format-region ()
+ (interactive)
+ (let ((orig-file buffer-file-name)
+ (orig-point (point))
+ (orig-mark (mark t))
+ (orig-mark-active mark-active)
+ (binary "clang-format")
+ (style "LLVM")
+ replacement-text replaced beg end)
+ (basic-save-buffer)
+ (save-restriction
+ (widen)
+ (if mark-active
+ (setq beg (1- (region-beginning))
+ end (1- (region-end)))
+ (setq beg (1- (line-beginning-position))
+ end (1- (line-end-position))))
+ (with-temp-buffer
+ (call-process
+ binary orig-file '(t nil) t
+ "-offset" (number-to-string beg)
+ "-length" (number-to-string (- end beg))
+ "-style" style)
+ (setq replacement-text
+ (buffer-substring-no-properties (point-min) (point-max))))
+ (unless (string= replacement-text
+ (buffer-substring-no-properties (point-min) (point-max)))
+ (delete-region (point-min) (point-max))
+ (insert replacement-text)
+ (setq replaced t)))
+ (ignore-errors
+ (when orig-mark
+ (push-mark orig-mark)
+ (when orig-mark-active
+ (activate-mark)
+ (setq deactivate-mark nil)))
+ (goto-char orig-point))))
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=179098&r1=179097&r2=179098&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format.py (original)
+++ cfe/trunk/tools/clang-format/clang-format.py Tue Apr 9 10:23:04 2013
@@ -23,6 +23,10 @@ import subprocess
# Change this to the full path if clang-format is not on the path.
binary = 'clang-format'
+# Change this to format according to other formatting styles (see
+# clang-format -help)
+style = 'LLVM'
+
# Get the current text.
buf = vim.current.buffer
text = "\n".join(buf)
@@ -34,7 +38,8 @@ length = int(vim.eval('line2byte(' +
str(vim.current.range.end + 2) + ')')) - offset - 2
# Call formatter.
-p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length)],
+p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length),
+ '-style', style],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
stdout, stderr = p.communicate(input=text)
More information about the cfe-commits
mailing list