rust-lang / rust-mode

Emacs configuration for Rust
Apache License 2.0
1.13k stars 181 forks source link

Indent example-code blocks in comments #563

Closed bemoody closed 2 weeks ago

bemoody commented 3 weeks ago

Allow pressing Tab to indent example code blocks inside comments.

For example, if the buffer contains this:

/// Say hello
///
/// ```
/// fn main() {
/// greet("world");
/// }
/// ```
pub fn greet(whom: &str) {
    println!("Hello, {}", whom);
}

Move to the word "world" and press Tab, and you should see:

/// Say hello
///
/// ```
/// fn main() {
///     greet("world");
/// }
/// ```
pub fn greet(whom: &str) {
    println!("Hello, {}", whom);
}
bemoody commented 3 weeks ago

I'm not sure what you're saying.

I could rename the function rust-mode-indent-line to rust-simple-indent-line and rename rust-doc-indent-line to rust-mode-indent-line.

If you're suggesting to turn it into one massive function, I think that's unwise. There is very little overlap in logic/functionality between those two functions: rust-doc-indent-line is concerned with rustdoc syntax, not with rust syntax. It's almost completely orthogonal to the programming language.

psibi commented 2 weeks ago

@jcs090218 Do you agree with the comments of @bemoody ?

@bemoody Thanks for the PR, LGTM from a high level. Is it possible to have a test for this functionality ?

jcs090218 commented 2 weeks ago

I suggest renaming rust-mode-indent-line to rust-mode--indent-line (or name anything logical) and creating a new rust-mode-indent-line to call the other two functions, etc.

I think it doesn't really matter since it requires little to no refactoring right now. 🤔

psibi commented 2 weeks ago

Got it, Thanks!

@bemoody Do you think you can do the following renames:

Adding a test would be nice and I think there are already some test related to indentation in rust-mode-tests.el from which you can take inspiration from.

bemoody commented 2 weeks ago

Sure, sounds good! I haven't worked with ert before, but I think this should do the trick.