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?

0

1 Answer 1

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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.