From d6d27bde98bd641b0ff639e9de0561ba3ed6f281 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Mon, 18 Jan 2016 19:20:47 +0900 Subject: [PATCH] align: Reimplement d6086809 --- gnome-align.el | 66 ++++++++++++++++++++++++++++++++------------------ gnome-tests.el | 45 +++++++++++++++++++++++++--------- 2 files changed, 75 insertions(+), 36 deletions(-) diff --git a/gnome-align.el b/gnome-align.el index 7ac94aa6b..0b708d239 100644 --- a/gnome-align.el +++ b/gnome-align.el @@ -48,12 +48,14 @@ (cl-defstruct (gnome-align--argument (:constructor nil) (:constructor gnome-align--make-argument (type-start - 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) + (type-identifier-end nil :read-only t) (type-end nil :read-only t) (identifier-start nil :read-only t) (identifier-end nil :read-only t)) @@ -76,20 +78,24 @@ (- (gnome-align--marker-column (gnome-align--argument-type-end arg)) (gnome-align--marker-column (gnome-align--argument-type-start arg)))) +(defun gnome-align--argument-type-identifier-width (arg) + (- (gnome-align--marker-column + (gnome-align--argument-type-identifier-end arg)) + (gnome-align--marker-column + (gnome-align--argument-type-start arg)))) + (defun gnome-align--arglist-identifier-start-column (arglist start-column) - (let ((column start-column) - argument-column) - (dolist (argument arglist) - (setq argument-column (+ start-column - (gnome-align--argument-type-width argument))) - (when (gnome-align--argument-identifier-start argument) - (save-excursion - (goto-char (gnome-align--argument-identifier-start argument)) - (when (eq (preceding-char) ? ) - (setq argument-column (1+ argument-column))))) - (when (> argument-column column) - (setq column argument-column))) - column)) + (let ((max-type-identifier-width + (apply #'max + (mapcar #'gnome-align--argument-type-identifier-width arglist))) + (max-extra-width + (apply #'max + (mapcar + (lambda (argument) + (- (gnome-align--argument-type-end argument) + (gnome-align--argument-type-identifier-end argument))) + arglist)))) + (+ start-column max-type-identifier-width max-extra-width))) (defun gnome-align--argument-identifier-width (argument) (if (gnome-align--argument-identifier-start argument) @@ -133,6 +139,7 @@ (save-restriction (narrow-to-region beg end) (let (type-start + type-identifier-end type-end identifier-start identifier-end @@ -149,11 +156,14 @@ (setq identifier-start (point-marker)) (c-backward-syntactic-ws) (if (or (bobp) (eq (preceding-char) ?,)) - ;; Identifier is omitted, or '...'. - (setq type-start identifier-start - type-end identifier-end - identifier-start nil - identifier-end nil) + (progn + ;; Identifier is omitted, or '...'. + (setq type-start identifier-start + type-identifier-end identifier-end + type-end identifier-end + identifier-start nil + identifier-end nil) + (c-backward-token-2)) (setq type-end (point-marker) last-token-start type-end) (while (and (not (bobp)) @@ -162,9 +172,17 @@ (unless (eq (char-after) ?,) (setq last-token-start (point-marker))))) (c-backward-syntactic-ws)) - (setq type-start last-token-start)) - (push (gnome-align--make-argument type-start type-end - identifier-start identifier-end) + (setq type-start last-token-start) + (save-excursion + (goto-char type-end) + (skip-chars-backward "*" type-start) + (c-backward-syntactic-ws) + (setq type-identifier-end (point-marker)))) + (push (gnome-align--make-argument type-start + type-identifier-end + type-end + identifier-start + identifier-end) arglist)) arglist)))) diff --git a/gnome-tests.el b/gnome-tests.el index d4e311369..403e0b178 100644 --- a/gnome-tests.el +++ b/gnome-tests.el @@ -20,7 +20,7 @@ void g_gpg_ctx_clear_signers (GGpgCtx *ctx); ") (defconst gnome-test-program-1-aligned "\ -GGpgCtx *g_gpg_ctx_new (GError **error); +GGpgCtx *g_gpg_ctx_new (GError **error); typedef void (*GGpgProgressCallback) (gpointer user_data, const gchar *what, @@ -28,16 +28,16 @@ typedef void (*GGpgProgressCallback) (gpointer user_data, gint current, gint total); -void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx, - GGpgProgressCallback callback, - gpointer user_data, - GDestroyNotify destroy_data); -void g_gpg_ctx_add_signer (GGpgCtx *ctx, - GGpgKey *key); -guint g_gpg_ctx_get_n_signers (GGpgCtx *ctx); -GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx, - guint index); -void g_gpg_ctx_clear_signers (GGpgCtx *ctx); +void g_gpg_ctx_set_progress_callback (GGpgCtx *ctx, + GGpgProgressCallback callback, + gpointer user_data, + GDestroyNotify destroy_data); +void g_gpg_ctx_add_signer (GGpgCtx *ctx, + GGpgKey *key); +guint g_gpg_ctx_get_n_signers (GGpgCtx *ctx); +GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx, + guint index); +void g_gpg_ctx_clear_signers (GGpgCtx *ctx); ") (defconst gnome-test-program-2 "\ @@ -57,6 +57,18 @@ const gchar ** gtk_widget_list_action_prefixes (GtkWidget * GParamSpec *pspec); ") +(defconst gnome-test-program-4 "\ +FOO_AVAILABLE_IN_ALL +int foo (struct foo ***a, int b, ...) G_GNUC_CONST; +") + +(defconst gnome-test-program-4-aligned "\ +FOO_AVAILABLE_IN_ALL +int foo (struct foo ***a, + int b, + ...) G_GNUC_CONST; +") + (ert-deftest gnome-test-align--compute-optimal-columns () "Tests the `gnome-align--compute-optimal-columns'." (with-temp-buffer @@ -65,7 +77,7 @@ const gchar ** gtk_widget_list_action_prefixes (GtkWidget * (let ((columns (gnome-align--compute-optimal-columns (point-min) (point-max)))) (should (= (cdr (assq 'identifier-start-column columns)) 9)) (should (= (cdr (assq 'arglist-start-column columns)) 41)) - (should (= (cdr (assq 'arglist-identifier-start-column columns)) 63))))) + (should (= (cdr (assq 'arglist-identifier-start-column columns)) 64))))) (ert-deftest gnome-test-align-region () "Tests the `gnome-align-region'." @@ -76,6 +88,15 @@ const gchar ** gtk_widget_list_action_prefixes (GtkWidget * (gnome-align-region (point-min) (point-max)) (should (equal (buffer-string) gnome-test-program-1-aligned)))) +(ert-deftest gnome-test-align-region-2 () + "Tests the `gnome-align-region'." + (with-temp-buffer + (insert gnome-test-program-4) + (c-mode) + (gnome-align-compute-optimal-columns (point-min) (point-max)) + (gnome-align-region (point-min) (point-max)) + (should (equal (buffer-string) gnome-test-program-4-aligned)))) + (ert-deftest gnome-test-align-guess-columns-1 () "Tests the `gnome-align-guess-columns'." (with-temp-buffer -- 2.39.2