rust-lang / rust-mode

Emacs configuration for Rust
Apache License 2.0
1.11k stars 179 forks source link

`rust-beginning-of-defun` skips async functions #468

Closed cowile closed 5 months ago

cowile commented 1 year ago

rust-beginning-of-defun skips async functions.

The problem starts at rust-mode.el:1228

(defvar rust-top-item-beg-re
  (concat "^\\s-*\\(?:priv\\|pub\\)?\\s-*"
          (regexp-opt
           '("enum" "struct" "type" "mod" "use" "fn" "static" "impl"
             "extern" "trait"))))

(defun rust-beginning-of-defun (&optional arg)
  "Move backward to the beginning of the current defun.

With ARG, move backward multiple defuns.  Negative ARG means
move forward.

This is written mainly to be used as `beginning-of-defun-function' for Rust.
Don't move to the beginning of the line. `beginning-of-defun',
which calls this, does that afterwards."
  (interactive "p")
  (re-search-backward (concat "^\\(" rust-top-item-beg-re "\\)\\_>")
                      nil 'move (or arg 1)))

rust-top-item-beg-re does not include the async keyword anywhere and therefore does not match a function with the async keyword.

cowile commented 1 year ago

As this is is fairly minimal implementation of beginning-of-defun-function, perhaps it might be a better implementation to rely on the default definition for beginning-of-defun and control its behavior with defun-prompt-regex.

brotzeit commented 1 year ago

I hope we can use treesitter for this in the future.

psibi commented 7 months ago

Can you check if this works with tree sitter: https://github.com/rust-lang/rust-mode?tab=readme-ov-file#tree-sitter

treesit-beginning-of-defun is the treesitter alternative. I can confirm that it doesn't skip async functions for me.

psibi commented 5 months ago

I'm closing this issue for now as I haven't been able to reproduce it recently using tree sitter and haven't heard back. Please feel free to reopen it if you encounter the issue again in the future.