Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent example-code blocks in comments #563

Merged
merged 4 commits into from
Nov 12, 2024
Merged

Conversation

bemoody
Copy link

@bemoody bemoody commented Nov 3, 2024

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);
}

Benjamin Moody added 2 commits November 2, 2024 20:22
This can be used as the indent-line-function, as an alternative to
rust-mode-indent-line.

In addition to indenting the current line as rust-mode-indent-line
does, it also checks to see whether we are editing a Markdown code
block inside a comment (either a documentation comment or a normal
comment.)  If we are inside a Markdown code block, it extracts that
code block, treats it as Rust code, and indents the current line
accordingly.

Note that this behavior is only triggered if point is inside the
comment body: placing point at the start of the line and pressing tab
will only re-indent the comment line, but placing point at the end of
the line and pressing tab will re-indent the both comment line and its
contents.
@@ -1457,7 +1574,7 @@ whichever comes first."
(setq-local syntax-propertize-function #'rust-syntax-propertize)

;; Indentation
(setq-local indent-line-function 'rust-mode-indent-line)
(setq-local indent-line-function 'rust-doc-indent-line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this variable to rust-mode-indent-line and tweak the functionality in that function. 🤔

@bemoody
Copy link
Author

bemoody commented Nov 3, 2024

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
Copy link
Member

psibi commented Nov 7, 2024

@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
Copy link
Member

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
Copy link
Member

psibi commented Nov 7, 2024

Got it, Thanks!

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

  • rust-mode-indent-line to rust-mode--indent-line
  • rust-doc-indent-line to rust-mode-indent-line

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
Copy link
Author

bemoody commented Nov 11, 2024

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

@psibi psibi merged commit 542f175 into rust-lang:master Nov 12, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants