diff options
Diffstat (limited to 'tools/clang-format/clang-format.el')
-rw-r--r-- | tools/clang-format/clang-format.el | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el index ab0991b..ca46144 100644 --- a/tools/clang-format/clang-format.el +++ b/tools/clang-format/clang-format.el @@ -61,6 +61,7 @@ of the buffer." (unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements)) (error "Expected <replacements> node")) (let ((nodes (xml-node-children xml-node)) + (incomplete-format (xml-get-attribute xml-node 'incomplete_format)) replacements cursor) (dolist (node nodes) @@ -76,11 +77,11 @@ of the buffer." (when (cdr children) (error "More than one child node in <replacement> node")) - (setq offset (1+ (string-to-number offset))) + (setq offset (string-to-number offset)) (setq length (string-to-number length)) (push (list offset length text) replacements))) ('cursor - (setq cursor (1+ (string-to-number text)))))))) + (setq cursor (string-to-number text))))))) ;; Sort by decreasing offset, length. (setq replacements (sort (delq nil replacements) @@ -89,16 +90,18 @@ of the buffer." (and (= (car a) (car b)) (> (cadr a) (cadr b))))))) - (cons replacements cursor))) + (list replacements cursor (string= incomplete-format "true")))) (defun clang-format--replace (offset length &optional text) - (goto-char offset) - (delete-char length) - (when text - (insert text))) + (let ((start (byte-to-position (1+ offset))) + (end (byte-to-position (+ 1 offset length)))) + (goto-char start) + (delete-region start end) + (when text + (insert text)))) ;;;###autoload -(defun clang-format-region (start end &optional style) +(defun clang-format-region (char-start char-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'." @@ -110,7 +113,10 @@ is no active region. If no style is given uses `clang-format-style'." (unless style (setq style clang-format-style)) - (let ((temp-buffer (generate-new-buffer " *clang-format-temp*")) + (let ((start (1- (position-bytes char-start))) + (end (1- (position-bytes char-end))) + (cursor (1- (position-bytes (point)))) + (temp-buffer (generate-new-buffer " *clang-format-temp*")) (temp-file (make-temp-file "clang-format"))) (unwind-protect (let (status stderr operations) @@ -122,9 +128,9 @@ is no active region. If no style is given uses `clang-format-style'." "-output-replacements-xml" "-assume-filename" (or (buffer-file-name) "") "-style" style - "-offset" (number-to-string (1- start)) + "-offset" (number-to-string start) "-length" (number-to-string (- end start)) - "-cursor" (number-to-string (1- (point))))) + "-cursor" (number-to-string cursor))) (setq stderr (with-temp-buffer (insert-file-contents temp-file) @@ -137,20 +143,24 @@ is no active region. If no style is given uses `clang-format-style'." ((stringp status) (error "(clang-format killed by signal %s%s)" status stderr)) ((not (equal 0 status)) - (error "(clang-format failed with code %d%s)" status stderr)) - (t (message "(clang-format succeeded%s)" stderr))) + (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 (car operations)) - (cursor (cdr operations))) + (let ((replacements (nth 0 operations)) + (cursor (nth 1 operations)) + (incomplete-format (nth 2 operations))) (save-excursion (mapc (lambda (rpl) (apply #'clang-format--replace rpl)) replacements)) (when cursor - (goto-char cursor)))) + (goto-char (byte-to-position (1+ cursor)))) + (message "%s" incomplete-format) + (if incomplete-format + (message "(clang-format: incomplete (syntax errors)%s)" stderr) + (message "(clang-format: success%s)" stderr)))) (delete-file temp-file) (when (buffer-name temp-buffer) (kill-buffer temp-buffer))))) |