[已解决]有推荐的文档注释方案吗

:smiley: 折腾了很久,终于能用Emacs工作了。

平常写注释的时候 用的是 M-;

如果要多行注解,就要手敲了,还有个办法就是 写个函数,绑定一个键位,让其插入个多行注解。但感觉不是解决办法。

诸位有没有什么推荐的 文档注释的好方案。

image

M-x comment-region

comment-dwim 不行吗?

好像没有多行的,能用,但是,不是文档注释。

听起来不是想要注释,而是想要tempel

1 个赞

先写内容,然后选中所有注释。用comment-dwim 。

comment-dwim 没法成文档注释,有没有类似的,一键成文档注释的 类似于js-doc,我目前主要写ts

谢谢,我好好研究下这个插件

3 个赞

这是我抄袭并用了很久的注释函数

(setq comment-style 'extra-line)       ;多行注释
;; (define-key global-map (kbd "M-;") 'comment-line)     ; 多行注释
;; (define-key global-map (kbd "C-x C-m") 'comment-dwim) ; 单行行尾注释
(defun qiang-comment-dwim-line (&optional arg)
  "Replacement for the comment-dwim command.
If no region is selected and current line is not blank and
we are not at the end of the line, then comment current line.
Replaces default behaviour of comment-dwim,
when it inserts comment at the end of the line. "
  (interactive "*P")
  (comment-normalize-vars)
  (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
      (comment-or-uncomment-region (line-beginning-position) (line-end-position))
    (comment-dwim arg)))
(global-set-key "\M-;" 'qiang-comment-dwim-line)
1 个赞
1 个赞

不清楚你用的哪个major-mode,如果只是想要为ts添加多行注释,直接写一个函数即可,不必专门引入一个包

(defun maple-ts-comment()
  (interactive)
  (if (use-region-p)
      (let ((comment-style 'extra-line)
            (comment-start "/** ")
            (comment-end " */"))
        (comment-or-uncomment-region (region-beginning) (region-end)))
    (comment-or-uncomment-region (line-beginning-position) (line-end-position))))
1 个赞

如果使用了 yasnippet 这个 package 的话,直接输入 /** 然后调用 yas-expand 就可以了

绝大部分 mode 都支持的

1 个赞

方案有很多。

除了 yasnippetts-docstr 之外,还可以用 smartparens,它默认就支持 c/c++ 的 /* 补全,js 可以这样设置:

(with-eval-after-load 'js
  (require 'smartparens)
  (add-hook 'js-mode-hook #'smartparens-mode)
  (sp-local-pair 'js-mode "/*" "*/"
                 :post-handlers '(("| " "SPC")
                                  ("* ||\n[i]" "RET"))))

使用:

  1. 输入 /* 得到:

    /*|*/
    
  2. 继续输入回车得到:

    /*
     * |
     */
    
  3. 输入任意注释内容,然后调用 c-indent-new-comment-line (可以绑定到 M-return) 得到带星号的新行:

    /*
     * 注释
     * |
     */
    

如果想在注释内写大段文档以及代码混合的内容,可以考虑我的这个包: separedit

1 个赞

一直用的杀哥写的:

(defun xah-comment-dwim ()
  "Like `comment-dwim', but toggle comment if cursor is not at end of line.

URL `http://ergoemacs.org/emacs/emacs_toggle_comment_by_line.html'
Version 2016-10-25"
  (interactive)
  (if (region-active-p)
      (comment-dwim nil)
    (let (($lbp (line-beginning-position))
          ($lep (line-end-position)))
      (if (eq $lbp $lep)
          (progn
            (comment-dwim nil))
        (if (eq (point) $lep)
            (progn
              (comment-dwim nil))
          (progn
            (comment-or-uncomment-region $lbp $lep)
            (forward-line )))))))
1 个赞

大佬,这个能分享下吗?

在网上插了 c-indent-new-comment-line 没找到怎么执行,自己尝试了下每次都报错。。