rust-lang / rust-mode

Emacs configuration for Rust
Apache License 2.0
1.1k stars 178 forks source link

Support `beginning-of-line-text` and `(fill-paragraph 't)` in comments #549

Open jeberger opened 1 month ago

jeberger commented 1 month ago

I'm not sure if this should be two issues or one, but they feel related.

When programming in C/C++ with cc-mode, when the cursor is inside a comment and I hit M-<home> (which is bound to beginning-of-line-text), then the cursor moves to the first character of the comment text after the comments symbols, e.g.:

   // Comment text
      ^ `beginning-of-line-text` moves the cursor here

In rust-mode, the cursor moves to the first non-whitespace character (same as back-to-indentation).

Possibly related: when I hit C-u M-q (bound to (fill-paragraph 't)) to justify a paragraph in a comment, rust-mode adds extra spaces to the begnining of some lines:

   /// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
   ///  in reprehenderit  in  voluptate velit  esse  cillum dolore  eu
   /// fugiat nulla pariatur.

whereas cc-mode is able to justify the text correctly:

   /// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
   /// in  reprehenderit  in voluptate  velit  esse  cillum dolore  eu
   /// fugiat nulla pariatur.

I believe both issues may be related to identifying and setting the fill-prefix when these functions are called inside comments. It would be nice to have those working as in cc-mode.

psibi commented 1 month ago

Is the bug reproducible when you are using with tree-sitter enabled: https://github.com/rust-lang/rust-mode?tab=readme-ov-file#tree-sitter ?

jeberger commented 1 month ago

Is the bug reproducible when you are using with tree-sitter enabled: https://github.com/rust-lang/rust-mode?tab=readme-ov-file#tree-sitter ?

Actually I had tree-sitter enabled, but the behaviour is the same if I disable it.

psibi commented 1 month ago

Hmm, that's strange. This is what happens in my Rust buffer with tree sitter enabled:

Any chance you can run your Emacs only with the rust-mode and see what's the behavior is ? I'm wondering if some other third party package is interfering.

jeberger commented 1 month ago

I've removed my whole .emacs.d, installed just tree-sitter (20220212.1632), tree-sitter-langs (0.12.210), tree-sitter-indent (0.3) and rust-mode (20240520.749) and I have the same behaviour (emacs is v29.4 on ArchLinux).

psibi commented 1 month ago

Hmm, strange. Just to double check - does evaluating this show t for you:

(treesit-ready-p 'rust)
jeberger commented 1 month ago

(treesit-ready-p 'rust)

Gives: Debugger entered--Lisp error: (void-function treesit-ready-p)

But if I type M-x tree-sitter-query-builder, then type the following query:

(function_item (identifier) @func)

Then it correctly highlights the function names.

psibi commented 1 month ago

But if I type M-x tree-sitter-query-builder, then type the following query:

Does the (treesit-ready-p 'rust) work after that ?

jeberger commented 1 month ago

But if I type M-x tree-sitter-query-builder, then type the following query:

Does the (treesit-ready-p 'rust) work after that ?

I don't think so, but I can't check since I'm no longer in front of the work computer where that happened.

Here at home, I have a different behaviour:

Weird…

jeberger commented 1 month ago

OK I figured out why (treesit-ready-p 'rust) returned nil here: it was picking the system install of treesitter (which didn't include the Rust support) instead of the one from package.el. When I add the Rust support to the system install, I get the same behaviour as at work, except that here (treesit-ready-p 'rust) returns 't

jeberger commented 1 month ago

But if I type M-x tree-sitter-query-builder, then type the following query:

Does the (treesit-ready-p 'rust) work after that ?

I don't think so, but I can't check since I'm no longer in front of the work computer where that happened.

I can confirm that (treesit-ready-p 'rust) still fails with (void-function treesit-ready-p) after I run the query builder.

psibi commented 1 month ago

I can confirm that (treesit-ready-p 'rust) still fails with (void-function treesit-ready-p) after I run the query builder.

Ah, okay. That means that rust-mode is not using the tree sitter implementation for it. What happens if you do rust-ts-mode ? I believe even that won't be working ?

jeberger commented 1 month ago

I hadn't realized that tree-sitter was included in emacs >=29, so I was still installing it from melpa. And I hadn't realized that there were two distinct rust modes with or without tree-sitter.

In Emacs 29.2 on Linux using rust-ts-mode:

jeberger commented 1 month ago

FWIW, fill-paragraph works as expected for C in Emacs 29.2 on Linux using c-ts-mode.