[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master a65924c 26/40: align: Remove spaces between two '*'
From: |
Daiki Ueno |
Subject: |
[elpa] master a65924c 26/40: align: Remove spaces between two '*' |
Date: |
Fri, 22 Jan 2016 22:48:44 +0000 |
branch: master
commit a65924c04cc51aff9650cba4cab3383ff37ad5d4
Author: Daiki Ueno <address@hidden>
Commit: Daiki Ueno <address@hidden>
align: Remove spaces between two '*'
---
gnome-c-align.el | 67 +++++++++++++++++++++++++++++++++++++++--------------
gnome-c-tests.el | 19 +++++++++++++++
2 files changed, 68 insertions(+), 18 deletions(-)
diff --git a/gnome-c-align.el b/gnome-c-align.el
index d8c6d11..c7bcddd 100644
--- a/gnome-c-align.el
+++ b/gnome-c-align.el
@@ -43,10 +43,10 @@
(cl-defstruct (gnome-c-align--argument
(:constructor nil)
(:constructor gnome-c-align--make-argument (type-start
- type-identifier-end
- type-end
- identifier-start
- identifier-end))
+ type-identifier-end
+ type-end
+ identifier-start
+ identifier-end))
(:copier nil)
(:predicate nil))
(type-start nil :read-only t)
@@ -62,7 +62,7 @@
(defun gnome-c-align--indent-to-column (column)
;; Prefer 'char **foo' than 'char ** foo'
- (when (looking-back "\*+" nil t)
+ (when (looking-back "\\*+" nil t)
(setq column (- column (- (match-end 0) (match-beginning 0))))
(goto-char (match-beginning 0)))
;; FIXME: should respect indent-tabs-mode?
@@ -82,7 +82,8 @@
(defun gnome-c-align--arglist-identifier-start-column (arglist start-column)
(let ((max-type-identifier-width
(apply #'max
- (mapcar #'gnome-c-align--argument-type-identifier-width
arglist)))
+ (mapcar #'gnome-c-align--argument-type-identifier-width
+ arglist)))
(max-extra-width
(apply #'max
(mapcar
@@ -103,7 +104,7 @@
(defun gnome-c-align--arglist-identifier-width (arglist)
(apply #'max (mapcar #'gnome-c-align--argument-identifier-width arglist)))
-(defun gnome-c-align--normalize-arglist-region (beg end)
+(defun gnome-c-align--normalize-arglist-region (arglist beg end)
(save-excursion
(save-restriction
(narrow-to-region beg end)
@@ -121,7 +122,20 @@
(replace-match ""))
;; Remove empty lines
(goto-char (point-min))
- (delete-matching-lines "^$"))))
+ (delete-matching-lines "^$")
+ ;; 'int * * * foo' -> 'int ***foo'
+ (dolist (argument arglist)
+ (goto-char (gnome-c-align--argument-type-end argument))
+ (while (re-search-backward
+ "\\(\\*+\\)\\s-+"
+ (gnome-c-align--argument-type-identifier-end argument)
+ t)
+ (replace-match "\\1"))
+ (when (gnome-c-align--argument-identifier-start argument)
+ (goto-char (gnome-c-align--argument-identifier-start argument))
+ (if (looking-back "\\* " nil)
+ (delete-char -1)))
+ (goto-char (gnome-c-align--argument-type-end argument))))))
(defun gnome-c-align--parse-arglist (beg end)
(save-excursion
@@ -164,14 +178,14 @@
(setq type-start last-token-start)
(save-excursion
(goto-char type-end)
- (skip-chars-backward "*" type-start)
+ (skip-chars-backward "* " type-start)
(c-backward-syntactic-ws)
(setq type-identifier-end (point-marker))))
(push (gnome-c-align--make-argument type-start
- type-identifier-end
- type-end
- identifier-start
- identifier-end)
+ type-identifier-end
+ type-end
+ identifier-start
+ identifier-end)
arglist))
arglist))))
@@ -188,7 +202,8 @@
(save-restriction
(narrow-to-region beg end)
(setq arglist (gnome-c-align--parse-arglist (point-min) (point-max)))
- (gnome-c-align--normalize-arglist-region (point-min) (point-max))
+ (gnome-c-align--normalize-arglist-region
+ arglist (point-min) (point-max))
(unless identifier-start-column
(setq identifier-start-column
(gnome-c-align--arglist-identifier-start-column arglist 0)))
@@ -319,18 +334,34 @@
(defun gnome-c-align--normalize-decl (decl)
(save-excursion
+ ;; Replace newlines with a space
(save-restriction
- (narrow-to-region (gnome-c-align--decl-identifier-start decl)
+ ;; Ignore lines before identifier-start
+ (goto-char (gnome-c-align--decl-identifier-start decl))
+ (beginning-of-line)
+ (narrow-to-region (point)
(gnome-c-align--decl-arglist-end decl))
(goto-char (point-min))
(while (re-search-forward "\n" nil t)
(replace-match " ")))
+ ;; Replace consequent spaces with a space
(save-restriction
- (narrow-to-region (gnome-c-align--decl-start decl)
- (gnome-c-align--decl-end decl))
+ ;; Ignore lines before identifier-start
+ (goto-char (gnome-c-align--decl-identifier-start decl))
+ (beginning-of-line)
+ (narrow-to-region (point)
+ (gnome-c-align--decl-arglist-end decl))
(goto-char (point-min))
(while (re-search-forward "\\s-+" nil t)
- (replace-match " ")))))
+ (replace-match " ")))
+ (goto-char (gnome-c-align--decl-identifier-start decl))
+ (if (looking-back "\\* " nil)
+ (delete-char -1))
+ ;; Normalize the argument list
+ (gnome-c-align--normalize-arglist-region
+ (gnome-c-align--decl-arglist decl)
+ (gnome-c-align--decl-arglist-start decl)
+ (gnome-c-align--decl-arglist-end decl))))
(defun gnome-c-align--arglist-region-at-point (point)
(save-excursion
diff --git a/gnome-c-tests.el b/gnome-c-tests.el
index ad4c9b2..c49b0b5 100644
--- a/gnome-c-tests.el
+++ b/gnome-c-tests.el
@@ -69,6 +69,15 @@ int foo (struct foo ***a,
...) G_GNUC_CONST;
")
+(defconst gnome-c-test-program-5 "\
+int * bar (const char * const * * a, int b);
+")
+
+(defconst gnome-c-test-program-5-aligned "\
+int *bar (const char * const **a,
+ int b);
+")
+
(ert-deftest gnome-c-test-align--compute-optimal-columns ()
"Tests the `gnome-c-align--compute-optimal-columns'."
(with-temp-buffer
@@ -101,6 +110,16 @@ int foo (struct foo ***a,
(gnome-c-align-region (point-min) (point-max)))
(should (equal (buffer-string) gnome-c-test-program-4-aligned))))
+(ert-deftest gnome-c-test-align-region-3 ()
+ "Tests the `gnome-c-align-region'."
+ (with-temp-buffer
+ (insert gnome-c-test-program-5)
+ (c-mode)
+ (let (gnome-c-align-max-column)
+ (gnome-c-align-compute-optimal-columns (point-min) (point-max))
+ (gnome-c-align-region (point-min) (point-max)))
+ (should (equal (buffer-string) gnome-c-test-program-5-aligned))))
+
(ert-deftest gnome-c-test-align-guess-columns-1 ()
"Tests the `gnome-c-align-guess-columns'."
(with-temp-buffer
- [elpa] master e8714b1 13/40: align: -guess-columns -> -compute-optimal-columns, (continued)
- [elpa] master e8714b1 13/40: align: -guess-columns -> -compute-optimal-columns, Daiki Ueno, 2016/01/22
- [elpa] master abb9dd4 21/40: Revert "align: Add a room before '*' for arguments", Daiki Ueno, 2016/01/22
- [elpa] master d608680 20/40: align: Add a room before '*' for arguments, Daiki Ueno, 2016/01/22
- [elpa] master 147931d 27/40: Fix typo, Daiki Ueno, 2016/01/22
- [elpa] master ec40108 23/40: align: Use `max' as much as possible, Daiki Ueno, 2016/01/22
- [elpa] master d6d27bd 22/40: align: Reimplement d6086809, Daiki Ueno, 2016/01/22
- [elpa] master ccb6062 24/40: tests: Bind `gnome-align-max-column', Daiki Ueno, 2016/01/22
- [elpa] master f339477 28/40: Add references to developer.gnome.org, Daiki Ueno, 2016/01/22
- [elpa] master 78a0cce 31/40: align: Fix `max' usage, Daiki Ueno, 2016/01/22
- [elpa] master 03f8dca 32/40: tests: Add line wrapping tests, Daiki Ueno, 2016/01/22
- [elpa] master a65924c 26/40: align: Remove spaces between two '*',
Daiki Ueno <=
- [elpa] master 39e7fb6 29/40: Rename some commands, Daiki Ueno, 2016/01/22
- [elpa] master 570b52f 30/40: snippet: Fix indentation, Daiki Ueno, 2016/01/22
- [elpa] master e84487c 35/40: snippet: Add FIXME comment, Daiki Ueno, 2016/01/22
- [elpa] master b04d18e 33/40: Change copyright holder to FSF, Daiki Ueno, 2016/01/22
- [elpa] master 06c8a6a 38/40: * packages/gnome-c-style/gnome-c-tests.el: Really add copyright blurb, Daiki Ueno, 2016/01/22
- [elpa] master 8d0f8c1 37/40: Fix command names in comment, Daiki Ueno, 2016/01/22
- [elpa] master b26ba73 34/40: snippet: Add missing GObjectClass vfunc, Daiki Ueno, 2016/01/22
- [elpa] master 1054ea1 39/40: Add links to GNOME programming guideline, Daiki Ueno, 2016/01/22
- [elpa] master 957d395 36/40: maint: Fix license notice, Daiki Ueno, 2016/01/22
- [elpa] master eaa2c7a 40/40: Merge commit '1054ea1bc5b07a1438a18c1b33f4266b28ff9d77', Daiki Ueno, 2016/01/22