r283206 - Minor cleanups in clang-format.el.

Manuel Klimek via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 4 02:53:05 PDT 2016


Author: klimek
Date: Tue Oct  4 04:53:04 2016
New Revision: 283206

URL: http://llvm.org/viewvc/llvm-project?rev=283206&view=rev
Log:
Minor cleanups in clang-format.el.

- Enable lexical binding
- Formatting
- Enable file name completion for the clang-format-executable variable
- Add a missing docstring
- When available, use bufferpos-to-filepos and filepos-to-bufferpos. These functions given more precise mapping than byte-to-position and position-bytes.
- Rename arguments of clang-format-region to match the docstring
- Instead of binding local variables to nil and then assigning them, bind them directly to their values
- Make use of the fact that insert-file-contents returns the number of characters it inserted
- Use cl-destructuring-bind to make the code a bit shorter
- Use standard iteration (dolist) instead of mapc with a lambda, which is more common and shorter
- Remove a message that was most likely only present for debugging purposes

Patch by Philipp Stephani.

Modified:
    cfe/trunk/tools/clang-format/clang-format.el

Modified: cfe/trunk/tools/clang-format/clang-format.el
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format.el?rev=283206&r1=283205&r2=283206&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format.el (original)
+++ cfe/trunk/tools/clang-format/clang-format.el Tue Oct  4 04:53:04 2016
@@ -1,4 +1,4 @@
-;;; clang-format.el --- Format code using clang-format
+;;; clang-format.el --- Format code using clang-format  -*- lexical-binding: t; -*-
 
 ;; Keywords: tools, c
 ;; Package-Requires: ((cl-lib "0.3"))
@@ -15,7 +15,7 @@
 ;;   M-x package-install clang-format
 ;;
 ;; when ("melpa" . "http://melpa.org/packages/") is included in
-;; `package-archives'. Alternatively, ensure the directory of this
+;; `package-archives'.  Alternatively, ensure the directory of this
 ;; file is in your `load-path' and add
 ;;
 ;;   (require 'clang-format)
@@ -42,7 +42,7 @@
 
 A string containing the name or the full path of the executable."
   :group 'clang-format
-  :type 'string
+  :type '(file :must-match t)
   :risky t)
 
 (defcustom clang-format-style "file"
@@ -93,15 +93,32 @@ of the buffer."
     (list replacements cursor (string= incomplete-format "true"))))
 
 (defun clang-format--replace (offset length &optional text)
-  (let ((start (byte-to-position (1+ offset)))
-        (end (byte-to-position (+ 1 offset length))))
+  "Replace the region defined by OFFSET and LENGTH with TEXT.
+OFFSET and LENGTH are measured in bytes, not characters.  OFFSET
+is a zero-based file offset."
+  (let ((start (clang-format--filepos-to-bufferpos offset 'exact))
+        (end (clang-format--filepos-to-bufferpos (+ offset length) 'exact)))
     (goto-char start)
     (delete-region start end)
     (when text
       (insert text))))
 
+;; ‘bufferpos-to-filepos’ and ‘filepos-to-bufferpos’ are new in Emacs 25.1.
+;; Provide fallbacks for older versions.
+(defalias 'clang-format--bufferpos-to-filepos
+  (if (fboundp 'bufferpos-to-filepos)
+      'bufferpos-to-filepos
+    (lambda (position &optional _quality _coding-system)
+      (1- (position-bytes position)))))
+
+(defalias 'clang-format--filepos-to-bufferpos
+  (if (fboundp 'filepos-to-bufferpos)
+      'filepos-to-bufferpos
+    (lambda (byte &optional _quality _coding-system)
+      (byte-to-position (1+ byte)))))
+
 ;;;###autoload
-(defun clang-format-region (char-start char-end &optional style)
+(defun clang-format-region (start end &optional style)
   "Use clang-format to format the code between START and END according to STYLE.
 If called interactively uses the region or the current statement if there
 is no active region.  If no style is given uses `clang-format-style'."
@@ -113,51 +130,41 @@ is no active region.  If no style is giv
   (unless style
     (setq style clang-format-style))
 
-  (let ((start (1- (position-bytes char-start)))
-        (end (1- (position-bytes char-end)))
-        (cursor (1- (position-bytes (point))))
+  (let ((file-start (clang-format--bufferpos-to-filepos start 'approximate))
+        (file-end (clang-format--bufferpos-to-filepos end 'approximate))
+        (cursor (clang-format--bufferpos-to-filepos (point) 'exact))
         (temp-buffer (generate-new-buffer " *clang-format-temp*"))
         (temp-file (make-temp-file "clang-format")))
     (unwind-protect
-        (let (status stderr operations)
-          (setq status
-                (call-process-region
-                 nil nil clang-format-executable
-                 nil `(,temp-buffer ,temp-file) nil
-
-                 "-output-replacements-xml"
-                 "-assume-filename" (or (buffer-file-name) "")
-                 "-style" style
-                 "-offset" (number-to-string start)
-                 "-length" (number-to-string (- end start))
-                 "-cursor" (number-to-string cursor)))
-          (setq stderr
-                (with-temp-buffer
-                  (insert-file-contents temp-file)
-                  (when (> (point-max) (point-min))
-                    (insert ": "))
-                  (buffer-substring-no-properties
-                   (point-min) (line-end-position))))
-
+        (let ((status (call-process-region
+                       nil nil clang-format-executable
+                       nil `(,temp-buffer ,temp-file) nil
+
+                       "-output-replacements-xml"
+                       "-assume-filename" (or (buffer-file-name) "")
+                       "-style" style
+                       "-offset" (number-to-string file-start)
+                       "-length" (number-to-string (- file-end file-start))
+                       "-cursor" (number-to-string cursor)))
+              (stderr (with-temp-buffer
+                        (unless (zerop (cadr (insert-file-contents temp-file)))
+                          (insert ": "))
+                        (buffer-substring-no-properties
+                         (point-min) (line-end-position)))))
           (cond
            ((stringp status)
             (error "(clang-format killed by signal %s%s)" status stderr))
-           ((not (equal 0 status))
+           ((not (zerop status))
             (error "(clang-format failed with code %d%s)" status stderr)))
 
-          (with-current-buffer temp-buffer
-            (setq operations (clang-format--extract (car (xml-parse-region)))))
-
-          (let ((replacements (nth 0 operations))
-                (cursor (nth 1 operations))
-                (incomplete-format (nth 2 operations)))
+          (cl-destructuring-bind (replacements cursor incomplete-format)
+              (with-current-buffer temp-buffer
+                (clang-format--extract (car (xml-parse-region))))
             (save-excursion
-              (mapc (lambda (rpl)
-                      (apply #'clang-format--replace rpl))
-                    replacements))
+              (dolist (rpl replacements)
+                (apply #'clang-format--replace rpl)))
             (when cursor
-              (goto-char (byte-to-position (1+ cursor))))
-            (message "%s" incomplete-format)
+              (goto-char (clang-format--filepos-to-bufferpos cursor 'exact)))
             (if incomplete-format
                 (message "(clang-format: incomplete (syntax errors)%s)" stderr)
               (message "(clang-format: success%s)" stderr))))




More information about the cfe-commits mailing list