From a65924c04cc51aff9650cba4cab3383ff37ad5d4 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Tue, 19 Jan 2016 12:57:06 +0900 Subject: [PATCH] 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 d8c6d11d1..c7bcdddb3 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 ad4c9b204..c49b0b51b 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 -- 2.39.2