3

Running hs-toggle-hiding converts a C-style multi-line comment from.

/*
 * This is a comment.
 */

To:

/* ...

Is there a way to collepse the comment to:

/* ... */

Instead?

Drew
  • 75,699
  • 9
  • 109
  • 225
ideasman42
  • 8,375
  • 1
  • 28
  • 105

1 Answers1

3

This can be done using advice on hs-make-overlay:

(defun my-c-comment-end-skip-backward (pos)
  (cond
    ((and (eq (char-before pos) ?/) (eq (char-before (- pos 1)) ?*))
      (- pos 2))
    (t
      pos)))

(defun my-c-comment-hs-make-overlay-advice-fn (old-fn b e kind &optional b-ofs e-ofs)
  (when
    (and
      (eq kind 'comment)
      ;; Check the language uses C/C++ comments.
      (member major-mode '(c-mode c++-mode objc-mode
                           glsl-mode rust-mode java-mode js-mode)))

    ;; Check for trailing "*/" at the beginning.
    ;; This can happen at the beginning position when multiple comments
    ;; are collapsed, and the first comment is a single-line comment.
    (setq b (my-c-comment-end-skip-backward b))
    (when b-ofs
      (setq b-ofs (max b b-ofs)))

    ;; Check for trailing "*/".
    (setq e (my-c-comment-end-skip-backward e))
    (when e-ofs
      (setq e-ofs (min e e-ofs))))
  (funcall old-fn b e kind b-ofs e-ofs))

(advice-add 'hs-make-overlay :around #'my-c-comment-hs-make-overlay-advice-fn))
ideasman42
  • 8,375
  • 1
  • 28
  • 105