From e8714b18fc65c7d7dc03b5595f1c1d7c9d51f78e Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Sun, 17 Jan 2016 09:14:54 +0900 Subject: [PATCH] align: -guess-columns -> -compute-optimal-columns --- README.md | 23 ++++++++++---------- gobject-align.el | 49 ++++++++++++++++++++++++++++++++++++++----- gobject-minor-mode.el | 2 ++ gobject-tests.el | 23 ++++++++++++++++---- 4 files changed, 77 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 22c6b98a1..7d1139d18 100644 --- a/README.md +++ b/README.md @@ -25,16 +25,17 @@ Install Usage ------ -| Key | Command | ---------------|---------------------------------------------------| -| C-c C-g g | Compute alignment columns from the current region | -| C-c C-g s | Set alignment column to the current point | -| C-c C-g a | Align argument list at the current point | -| C-c C-g r | Align function declarations in the current region | -| C-c C-g c | Insert ```module_object``` | -| C-c C-g C | Insert ```MODULE_OBJECT``` | -| C-c C-g C-c | Insert ```ModuleObject``` | -| C-c C-g s | Insert custom snippet | +| Key | Command | +--------------|-----------------------------------------------------------| +| C-c C-g C-g | Compute optimal alignment columns from the current region | +| C-c C-g s | Set alignment column to the current point | +| C-c C-g g | Guess alignment columns from the current region | +| C-c C-g a | Align argument list at the current point | +| C-c C-g r | Align function declarations in the current region | +| C-c C-g c | Insert ```module_object``` | +| C-c C-g C | Insert ```MODULE_OBJECT``` | +| C-c C-g C-c | Insert ```ModuleObject``` | +| C-c C-g s | Insert custom snippet | Example ------ @@ -59,7 +60,7 @@ GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx, guint index); void g_gpg_ctx_clear_signers (GGpgCtx *ctx); ``` -Mark the region, type ```C-c C-g g```, and you will see the optimum +Mark the region, type ```C-c C-g C-g```, and you will see the optimum alignment columns: ``` diff --git a/gobject-align.el b/gobject-align.el index 1bb3f57b5..3042fedf2 100644 --- a/gobject-align.el +++ b/gobject-align.el @@ -59,6 +59,7 @@ (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? (let (indent-tabs-mode) (indent-to-column column))) @@ -354,7 +355,7 @@ (push decl decls)))) decls)))) -(defun gobject-align--guess-columns (beg end) +(defun gobject-align--compute-optimal-columns (beg end) (let ((buffer (current-buffer)) decls) (with-temp-buffer @@ -379,10 +380,14 @@ arglist-identifier-start-column)))))) ;;;###autoload -(defun gobject-align-guess-columns (beg end) - "Guess the alignment rule from the function declarations in BEG and END" +(defun gobject-align-compute-optimal-columns (beg end) + "Compute the optimal alignment rule from the declarations in BEG and END. + +This sets `gobject-align-identifier-start-column', +`gobject-align-arglist-start-column', and +`gobject-align-arglist-identifier-start-column'." (interactive "r") - (let ((columns (gobject-align--guess-columns beg end))) + (let ((columns (gobject-align--compute-optimal-columns beg end))) (setq gobject-align-identifier-start-column (cdr (assq 'identifier-start-column columns)) gobject-align-arglist-start-column @@ -395,6 +400,40 @@ gobject-align-arglist-start-column gobject-align-arglist-identifier-start-column))) +;;;###autoload +(defun gobject-align-guess-columns (beg end) + "Guess the existing alignment rule from the declarations in BEG and END. + +This sets `gobject-align-identifier-start-column', +`gobject-align-arglist-start-column', and +`gobject-align-arglist-identifier-start-column'." + (interactive "r") + (let ((decls (gobject-align--scan-decls beg end)) + arglist) + (unless decls + (error "No function declaration in the region")) + (setq arglist (gobject-align--parse-arglist + (1+ (gobject-align--decl-arglist-start (car decls))) + (1- (gobject-align--decl-arglist-end (car decls))))) + (unless arglist + (error "Empty argument list")) + (unless (gobject-align--argument-identifier-start (car arglist)) + (error "No identifier in the argument list")) + (setq gobject-align-identifier-start-column + (gobject-align--marker-column + (gobject-align--decl-identifier-start (car decls))) + gobject-align-arglist-start-column + (gobject-align--marker-column + (gobject-align--decl-arglist-start (car decls))) + gobject-align-arglist-identifier-start-column + (gobject-align--marker-column + (gobject-align--argument-identifier-start (car arglist)))) + (message + "identifier-start: %d, arglist-start: %d, arglist-identifier-start: %d" + gobject-align-identifier-start-column + gobject-align-arglist-start-column + gobject-align-arglist-identifier-start-column))) + ;;;###autoload (defun gobject-align-region (beg end) "Reformat function declarations in the region between BEG and END." @@ -406,7 +445,7 @@ (unless (and gobject-align-identifier-start-column gobject-align-arglist-start-column gobject-align-arglist-identifier-start-column) - (let ((columns (gobject-align--guess-columns beg end))) + (let ((columns (gobject-align--compute-optimal-columns beg end))) (unless gobject-align-identifier-start-column (setq gobject-align-identifier-start-column (cdr (assq 'identifier-start-column columns)))) diff --git a/gobject-minor-mode.el b/gobject-minor-mode.el index dee08a821..d3a310da8 100644 --- a/gobject-minor-mode.el +++ b/gobject-minor-mode.el @@ -26,6 +26,7 @@ (autoload 'gobject-align-region "gobject-align") (autoload 'gobject-align-set-column "gobject-align") (autoload 'gobject-align-guess-columns "gobject-align") +(autoload 'gobject-align-compute-optimal-columns "gobject-align") (autoload 'gobject-snippet-insert-package_class "gobject-snippet") (autoload 'gobject-snippet-insert-PACKAGE_CLASS "gobject-snippet") (autoload 'gobject-snippet-insert-PackageClass "gobject-snippet") @@ -45,6 +46,7 @@ (define-key keymap "\C-c\C-gr" 'gobject-align-region) (define-key keymap "\C-c\C-gf" 'gobject-align-set-column) (define-key keymap "\C-c\C-gg" 'gobject-align-guess-columns) + (define-key keymap "\C-c\C-g\C-g" 'gobject-align-compute-optimal-columns) (define-key keymap "\C-c\C-gc" 'gobject-snippet-insert-package_class) (define-key keymap "\C-c\C-gC" 'gobject-snippet-insert-PACKAGE_CLASS) (define-key keymap "\C-c\C-g\C-c" 'gobject-snippet-insert-PackageClass) diff --git a/gobject-tests.el b/gobject-tests.el index 500b64165..c06c0a5ef 100644 --- a/gobject-tests.el +++ b/gobject-tests.el @@ -40,12 +40,17 @@ GGpgKey *g_gpg_ctx_get_signer (GGpgCtx *ctx, void g_gpg_ctx_clear_signers (GGpgCtx *ctx); ") -(ert-deftest gobject-test-align--guess-columns () - "Tests the `gobject-align--guess-columns'." +(defconst gobject-test-program-2 "\ +GDK_AVAILABLE_IN_3_16 +const gchar ** gtk_widget_list_action_prefixes (GtkWidget *widget); +") + +(ert-deftest gobject-test-align--compute-optimal-columns () + "Tests the `gobject-align--compute-optimal-columns'." (with-temp-buffer (insert gobject-test-program-1) (c-mode) - (let ((columns (gobject-align--guess-columns (point-min) (point-max)))) + (let ((columns (gobject-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))))) @@ -55,6 +60,16 @@ void g_gpg_ctx_clear_signers (GGpgCtx *ctx); (with-temp-buffer (insert gobject-test-program-1) (c-mode) - (gobject-align-guess-columns (point-min) (point-max)) + (gobject-align-compute-optimal-columns (point-min) (point-max)) (gobject-align-region (point-min) (point-max)) (should (equal (buffer-string) gobject-test-program-1-aligned)))) + +(ert-deftest gobject-test-align-guess-columns () + "Tests the `gobject-align-guess-columns'." + (with-temp-buffer + (insert gobject-test-program-2) + (c-mode) + (gobject-align-guess-columns (point-min) (point-max)) + (should (= gobject-align-identifier-start-column 24)) + (should (= gobject-align-arglist-start-column 56)) + (should (= gobject-align-arglist-identifier-start-column 80)))) -- 2.39.2