]> git.eshelyaron.com Git - emacs.git/commitdiff
cc-mode: add ‘c-lineup-ternary-bodies’ (bug#41061)
authorMichal Nazarewicz <mina86@mina86.com>
Sun, 3 May 2020 15:32:47 +0000 (16:32 +0100)
committerMichal Nazarewicz <mina86@mina86.com>
Sat, 9 May 2020 10:30:32 +0000 (11:30 +0100)
Introduce ‘c-lineup-ternary-bodies’ function which, when used as
a c lineup function, aligns question mark and colon of a ternary
operator.  For example:

    return arg % 2 == 0 ? arg / 2
                        : (3 * arg + 1);

* lisp/progmodes/cc-align.el (c-lineup-ternary-bodies): New function.
* doc/misc/cc-mode.texi (Operator Line-Up Functions): Document the
new function.
* test/lisp/progmodes/cc-mode-tests.el (c-lineup-ternary-bodies): New
test case.

doc/misc/cc-mode.texi
etc/NEWS
lisp/progmodes/cc-align.el
test/lisp/progmodes/cc-mode-tests.el

index f9c9f5e1830f683aeb3fb0dcf13320c41bf7d5aa..16eac4828c77b082ecf1cc30eaad0b4355616fde 100644 (file)
@@ -6395,6 +6395,26 @@ function is the same as specifying a list @code{(c-lineup-assignments
 
 @comment ------------------------------------------------------------
 
+@defun c-lineup-ternary-bodies
+@findex lineup-ternary-bodies @r{(c-)}
+Line up true and false branches of a ternary operator
+(i.e. @code{?:}).  More precisely, if the line starts with a colon
+which is a part of a said operator it with corresponding question
+mark.  For example:
+
+@example
+@group
+return arg % 2 == 0 ? arg / 2
+                    : (3 * arg + 1);    @hereFn{c-lineup-ternary-bodies}
+@end group
+@end example
+
+@workswith @code{arglist-cont}, @code{arglist-cont-nonempty} and
+@code{statement-cont}.
+@end defun
+
+@comment ------------------------------------------------------------
+
 @defun c-lineup-cascaded-calls
 @findex lineup-cascaded-calls @r{(c-)}
 Line up ``cascaded calls'' under each other.  If the line begins with
index 9c71752b621914530f0c664749850b77776c51e2..12406eea822f493ad508bb620e82b69a96ecd811 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -305,6 +305,23 @@ use ‘doxygen’ by default one might evaluate:
 
 or use it in a custom ‘c-style’.
 
+*** Added support to line up ‘?’ and ‘:’ of a ternary operator.
+The new ‘c-lineup-ternary-bodies’ function can be used as a lineup
+function to align question mark and colon which are part of a ternary
+operator (‘?:’).  For example:
+
+    return arg % 2 == 0 ? arg / 2
+                        : (3 * arg + 1);
+
+To enable, add it to appropriate entries in ‘c-offsets-alist’, e.g.:
+
+    (c-set-offset 'arglist-cont '(c-lineup-ternary-bodies
+                                  c-lineup-gcc-asm-reg))
+    (c-set-offset 'arglist-cont-nonempty '(c-lineup-ternary-bodies
+                                           c-lineup-gcc-asm-reg
+                                           c-lineup-arglist))
+    (c-set-offset 'statement-cont '(c-lineup-ternary-bodies +))
+
 ** browse-url
 
 *** Added support for custom URL handlers
index f30477dc787c56eb4d5801b7fd4dfc8336b0f861..c49bdc5c5185c1280f2eae22ede305dd833a88ff 100644 (file)
@@ -790,6 +790,38 @@ arglist-cont-nonempty."
   (or (c-lineup-assignments langelem)
       c-basic-offset))
 
+(defun c-lineup-ternary-bodies (langelem)
+  "Line up true and false branches of a ternary operator (i.e. ‘?:’).
+More precisely, if the line starts with a colon which is a part of
+a said operator it with corresponding question mark; otherwise return
+nil.  For example:
+
+    return arg % 2 == 0 ? arg / 2
+                        : (3 * arg + 1);    <- c-lineup-ternary-bodies
+
+Works with: arglist-cont, arglist-cont-nonempty and statement-cont.
+"
+  (save-excursion
+    (back-to-indentation)
+    (when (and (eq ?: (char-after))
+               (not (eq ?: (char-after (1+ (point))))))
+      (let ((limit (c-langelem-pos langelem)) (depth 1))
+        (catch 'done
+          (while (c-syntactic-skip-backward "^?:" limit t)
+            (goto-char (1- (point)))
+            (cond ((eq (char-after) ??)
+                   ;; If we’ve found a question mark, decrease depth.  If we’re
+                   ;; reached zero, we’ve found the one we were looking for.
+                   (when (zerop (setq depth (1- depth)))
+                     (throw 'done (vector (current-column)))))
+                  ((or (eq ?: (char-before)) (eq ?? (char-before)))
+                   ;; Step over ‘::’ and ‘?:’ operators.  We don’t have to
+                   ;; handle ‘?:’ here but doing so saves an iteration.
+                   (if (eq (point) limit)
+                       (throw 'done nil)
+                     (goto-char (1- (point)))))
+                  ((setq depth (1+ depth)))))))))) ; Otherwise increase depth.
+
 (defun c-lineup-cascaded-calls (langelem)
   "Line up \"cascaded calls\" under each other.
 If the line begins with \"->\" or \".\" and the preceding line ends
index 0729841ce6f21d59bc45f0f7db0ee322387fbef9..ad7a52b40d97152e9b33ace8808d7c867215c4c5 100644 (file)
       (insert macro-string)
       (c-mode))))
 
+(ert-deftest c-lineup-ternary-bodies ()
+  "Test for c-lineup-ternary-bodies function"
+  (with-temp-buffer
+    (c-mode)
+    (let* ((common-prefix "int value = condition ")
+           (expected-column (length common-prefix)))
+      (dolist (test '(("? a : \n b" . nil)
+                      ("? a \n ::b" . nil)
+                      ("a \n : b" . nil)
+                      ("? a \n : b" . t)
+                      ("? ::a \n : b" . t)
+                      ("? (p ? q : r) \n : b" . t)
+                      ("? p ?: q \n : b" . t)
+                      ("? p ? : q \n : b" . t)
+                      ("? p ? q : r \n : b" . t)))
+        (delete-region (point-min) (point-max))
+        (insert common-prefix (car test))
+        (should (equal
+                 (and (cdr test) (vector expected-column))
+                 (c-lineup-ternary-bodies '(statement-cont . 1))))))))
+
 ;;; cc-mode-tests.el ends here