[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode 8f1297a 281/486: Merge pull request #187 from ts
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode 8f1297a 281/486: Merge pull request #187 from tspiteri/format-other-buffers |
Date: |
Sat, 7 Aug 2021 09:25:36 -0400 (EDT) |
branch: elpa/rust-mode
commit 8f1297a5f6da4a587abf604756d7a99b046616d5
Merge: cd09a54 664c7b0
Author: Niko Matsakis <niko@alum.mit.edu>
Commit: GitHub <noreply@github.com>
Merge pull request #187 from tspiteri/format-other-buffers
handle indirect buffers and multiple windows in rust-format-buffer
---
rust-mode.el | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 114 insertions(+), 10 deletions(-)
diff --git a/rust-mode.el b/rust-mode.el
index 1877234..b8f26d3 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -1283,23 +1283,127 @@ This is written mainly to be used as
`end-of-defun-function' for Rust."
(kill-buffer))
(error "Rustfmt failed, see *rustfmt* buffer for details"))))
+(defconst rust--format-word
"\\b\\(else\\|enum\\|fn\\|for\\|if\\|let\\|loop\\|match\\|struct\\|unsafe\\|while\\)\\b")
+(defconst rust--format-line "\\([\n]\\)")
+
+;; Counts number of matches of regex beginning up to max-beginning,
+;; leaving the point at the beginning of the last match.
+(defun rust--format-count (regex max-beginning)
+ (let ((count 0)
+ save-point
+ beginning)
+ (while (and (< (point) max-beginning)
+ (re-search-forward regex max-beginning t))
+ (setq count (1+ count))
+ (setq beginning (match-beginning 1)))
+ ;; try one more in case max-beginning lies in the middle of a match
+ (setq save-point (point))
+ (when (re-search-forward regex nil t)
+ (let ((try-beginning (match-beginning 1)))
+ (if (> try-beginning max-beginning)
+ (goto-char save-point)
+ (setq count (1+ count))
+ (setq beginning try-beginning))))
+ (when beginning (goto-char beginning))
+ count))
+
+;; Gets list describing pos or (point).
+;; The list contains:
+;; 1. the number of matches of rust--format-word,
+;; 2. the number of matches of rust--format-line after that,
+;; 3. the number of columns after that.
+(defun rust--format-get-loc (buffer &optional pos)
+ (with-current-buffer buffer
+ (save-excursion
+ (let ((pos (or pos (point)))
+ words lines columns)
+ (goto-char (point-min))
+ (setq words (rust--format-count rust--format-word pos))
+ (setq lines (rust--format-count rust--format-line pos))
+ (if (> lines 0)
+ (if (= (point) pos)
+ (setq columns -1)
+ (forward-char 1)
+ (goto-char pos)
+ (setq columns (current-column)))
+ (let ((initial-column (current-column)))
+ (goto-char pos)
+ (setq columns (- (current-column) initial-column))))
+ (list words lines columns)))))
+
+;; Moves the point forward by count matches of regex up to max-pos,
+;; and returns new max-pos making sure final position does not include another
match.
+(defun rust--format-forward (regex count max-pos)
+ (when (< (point) max-pos)
+ (let ((beginning (point)))
+ (while (> count 0)
+ (setq count (1- count))
+ (re-search-forward regex nil t)
+ (setq beginning (match-beginning 1)))
+ (when (re-search-forward regex nil t)
+ (setq max-pos (min max-pos (match-beginning 1))))
+ (goto-char beginning)))
+ max-pos)
+
+;; Gets the position from a location list obtained using rust--format-get-loc.
+(defun rust--format-get-pos (buffer loc)
+ (with-current-buffer buffer
+ (save-excursion
+ (goto-char (point-min))
+ (let ((max-pos (point-max))
+ (words (pop loc))
+ (lines (pop loc))
+ (columns (pop loc)))
+ (setq max-pos (rust--format-forward rust--format-word words max-pos))
+ (setq max-pos (rust--format-forward rust--format-line lines max-pos))
+ (when (> lines 0) (forward-char))
+ (let ((initial-column (current-column))
+ (save-point (point)))
+ (move-end-of-line nil)
+ (when (> (current-column) (+ initial-column columns))
+ (goto-char save-point)
+ (forward-char columns)))
+ (min (point) max-pos)))))
+
(defun rust-format-buffer ()
"Format the current buffer using rustfmt."
(interactive)
(unless (executable-find rust-rustfmt-bin)
(error "Could not locate executable \"%s\"" rust-rustfmt-bin))
- (let ((cur-line (line-number-at-pos))
- (cur-column (current-column))
- (cur-win-start (window-start)))
+ (let* ((current (current-buffer))
+ (base (or (buffer-base-buffer current) current))
+ buffer-loc
+ window-loc)
+ (dolist (buffer (buffer-list))
+ (when (or (eq buffer base)
+ (eq (buffer-base-buffer buffer) base))
+ (push (list buffer
+ (rust--format-get-loc buffer nil))
+ buffer-loc)))
+ (dolist (window (window-list))
+ (let ((buffer (window-buffer window)))
+ (when (or (eq buffer base)
+ (eq (buffer-base-buffer buffer) base))
+ (let ((start (window-start window))
+ (point (window-point window)))
+ (push (list window
+ (rust--format-get-loc buffer start)
+ (rust--format-get-loc buffer point))
+ window-loc)))))
(rust--format-call (current-buffer))
- ;; Move to the same line and column as before. This is best
- ;; effort: if rustfmt inserted lines before point, we end up in
- ;; the wrong place. See issue #162.
- (goto-char (point-min))
- (forward-line (1- cur-line))
- (forward-char cur-column)
- (set-window-start (selected-window) cur-win-start))
+ (dolist (loc buffer-loc)
+ (let* ((buffer (pop loc))
+ (pos (rust--format-get-pos buffer (pop loc))))
+ (with-current-buffer buffer
+ (goto-char pos))))
+ (dolist (loc window-loc)
+ (let* ((window (pop loc))
+ (buffer (window-buffer window))
+ (start (rust--format-get-pos buffer (pop loc)))
+ (pos (rust--format-get-pos buffer (pop loc))))
+ (set-window-start window start)
+ (set-window-point window pos))))
;; Issue #127: Running this on a buffer acts like a revert, and could cause
;; the fontification to get out of sync. Call the same hook to ensure it is
- [nongnu] elpa/rust-mode eae291a 343/486: Merge pull request #240 from michael-zapata/feat/add-yield-keyword, (continued)
- [nongnu] elpa/rust-mode eae291a 343/486: Merge pull request #240 from michael-zapata/feat/add-yield-keyword, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b7cd368 346/486: % as punctuation character., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 04e3078 347/486: Merge pull request #244 from kubo39/percent-as-punctuation-character, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b911835 356/486: Merge pull request #262 from kngwyu/highlight-dyn, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode cc2435b 359/486: Merge pull request #264 from jjwest/master, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 3ced187 360/486: remove old-style compilation regex; support `:::` regex, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 07e16c6 364/486: Merge pull request #265 from brotzeit/infotype, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode ee262cc 373/486: Merge pull request #266 from brotzeit/format-on-save, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 12cb169 375/486: Merge pull request #284 from riscy/master, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 8452093 278/486: put remaining defcustoms into the rust-mode group, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 8f1297a 281/486: Merge pull request #187 from tspiteri/format-other-buffers,
ELPA Syncer <=
- [nongnu] elpa/rust-mode 5c68caf 282/486: Merge pull request #175 from mrBliss/imenu-unsafe, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f269c5e 292/486: fix syntax of "<" appearing after "?", ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 491ae0e 301/486: Remove unnecessary debugging message, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 60a1f36 310/486: Merge pull request #216 from Aankhen/set-compile-command, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 128601b 317/486: Improve content of `test-project/Cargo.toml'., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 30a9d39 322/486: Merge pull request #221 from Wilfred/highlight_union_contextual_keyword, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 6093d38 327/486: Add syntax coloring for type-inferred constants and variables, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 38f7d89 330/486: Fix recognition of "<" as operator in some context, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 0ded5ea 332/486: Add support for u128 and i128 types, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f57a8eb 333/486: Merge pull request #233 from shanavas786/add-128, ELPA Syncer, 2021/08/07