rust-lang / rust-lang/rust-mode

Add the possibility to bind keys to functions for doc comments

Open
#385 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Emacs Lisp
Stars
1.3k
Forks
198
PR merge metrics
No merged PRs in 30d

Description

As of now using comment-line or comment-dwim will always open a comment with //.

I needed some doc comments (/// or //!) so I implemented mine (inspired by http://ergoemacs.org/emacs/elisp_comment_command.html, thanks a lot!) hoping it would help:

(defun rust-doc-comment-dwim (c)
  "Comment or uncomment the current line or text selection."
  (interactive)

  ;; If there's no text selection, comment or uncomment the line
  ;; depending whether the WHOLE line is a comment. If there is a text
  ;; selection, using the first line to determine whether to
  ;; comment/uncomment.
  (let (p1 p2)
    (if (use-region-p)
        (save-excursion
          (setq p1 (region-beginning) p2 (region-end))
          (goto-char p1)
          (if (wholeLineIsCmt-p c)
              (my-uncomment-region p1 p2 c)
            (my-comment-region p1 p2 c)
            ))
      (progn
        (if (wholeLineIsCmt-p c)
            (my-uncomment-current-line c)
          (my-comment-current-line c)
          )) )))

(defun wholeLineIsCmt-p (c)
  (save-excursion
    (beginning-of-line 1)
    (looking-at (concat "[ \t]*//" c))
    ))

(defun my-comment-current-line (c)
  (interactive)
  (beginning-of-line 1)
  (insert (concat "//" c))
  )

(defun my-uncomment-current-line (c)
  "Remove “//c” (if any) in the beginning of current line."
  (interactive)
  (when (wholeLineIsCmt-p c)
    (beginning-of-line 1)
    (search-forward (concat "//" c))
    (delete-backward-char 3)
    ))

(defun my-comment-region (p1 p2 c)
  "Add “//c” to the beginning of each line of selected text."
  (interactive "r")
  (let ((deactivate-mark nil))
    (save-excursion
      (goto-char p2)
      (while (>= (point) p1)
        (my-comment-current-line c)
        (previous-line)
        ))))

(defun my-uncomment-region (p1 p2 c)
  "Remove “//c” (if any) in the beginning of each line of selected text."
  (interactive "r")
  (let ((deactivate-mark nil))
    (save-excursion
      (goto-char p2)
      (while (>= (point) p1)
        (my-uncomment-current-line c)
        (previous-line) )) ))

(use-package rust-mode
  :mode "\\.rs'"
  :bind ("C-M-;" . rust-doc-comment-dwim-following)
  :bind ("C-M-," . rust-doc-comment-dwim-enclosing)
  (defun rust-doc-comment-dwim-following ()
    (interactive)
    (rust-doc-comment-dwim "/"))
  (defun rust-doc-comment-dwim-enclosing ()
    (interactive)
    (rust-doc-comment-dwim "!"))
  )

Which allows to comment as /// when pressing Ctrl-Alt-; and //! when pressing Ctrl-Alt-,

It would actually be better to have these two functions available in the mode to bind them to some keys but I'm actually really bad at writing Lisp to do it

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing comment-line and comment-dwim behavior in rust-mode, then review the proposed rust-doc-comment-dwim entry points. Check how rust-mode exposes commands and whether commenting behavior has tests; done means doc-comment commands can be bound to keys and handle the documented line and region cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
emacs-lisp, rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.