Apply formatting to markdown code blocks#22470
Conversation
|
|
minimal working prototype using regex adapted from blacken-docs: |
|
Some notes on the regex adapted from blacken-docs:
Prototype also silently discards any formatting error, and those should either be warnings or get tracked somehow to re-raise them outside of the closure. |
|
Also need to decide on if/how to gate this behind |
f0fa410 to
18cc1b2
Compare
18cc1b2 to
169bed3
Compare
169bed3 to
42b2ed4
Compare
|
Not familiar enough with ty to know why this changed what files ty looks at, or how to make ty ignore the md files when loading the project. |
ntBre
left a comment
There was a problem hiding this comment.
Nice! I found a few nits, but this otherwise looks good to me!
|
|
||
| settings.add_filter(&tempdir_filter(project_dir.to_str().unwrap()), "[TMP]/"); | ||
| settings.add_filter( | ||
| &tempdir_filter(Self::crate_root().to_str().unwrap()), |
There was a problem hiding this comment.
Does this need to be tempdir_filtered? I don't think it's a big deal either way, but I wouldn't think this is needed.
There was a problem hiding this comment.
Micha suggested moving this into the base helper in https://github.com/astral-sh/ruff/pull/22470/changes/BASE..80911e2bbe05b87b1f58c43356f26c804c8d292a#r2711704566
There was a problem hiding this comment.
Yep, I saw his comment. I just thought it might have applied only to the crate_root part. I don't think the crate root should be in a temporary directory, so I think we could just leave off the tempdir_filter part. Again, it's okay to leave it since it's a no-op if the filter doesn't match, but the tests should also fail loudly if it was necessary, so I don't think it hurts to try removing the tempdir_filter call.
There was a problem hiding this comment.
tempdir_filter just transforms the path into a regex string, not sure why "tempdir" is in the name other than the expected use case:
ruff/crates/ruff/tests/cli/main.rs
Line 26 in 80911e2
There was a problem hiding this comment.
Ohhh, I see. I had always seen this used with actual temporary directories, so I thought it was doing something special with /tmp. Sorry for the noise!
| Ok(Some(source_kind)) => match source_kind { | ||
| SourceKind::Markdown(_) => return Ok(Diagnostics::default()), // skip linting markdown | ||
| _ => source_kind, | ||
| }, |
There was a problem hiding this comment.
Is this the same as the comment on crates/ruff/resources/test/fixtures/unformatted.md? (Did we open a follow-up issue for that?)
Wondered if this comment had gone unnoticed? Is there a way to include this? |
|
Amy may have different thoughts, but I would expect Quarto formatting support to follow more general Quarto support tracked in #6140 rather than as part of this feature. |
|
Why would that be? It seems that quarto support follows immediately from this PR or am I missing something? Is it because context is carried from one block to the next? Otherwise, wouldn't modifying this line be enough? |
|
Oh I may have misunderstood how Quarto notebooks are structured. They are just Markdown files where the For a demonstration of a line plot on a polar axis, see @fig-polar.
```{python}
#| label: fig-polar
#| fig-cap: "A line plot on a polar axis"
import numpy as np
import matplotlib.pyplot as plt
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(
subplot_kw = {'projection': 'polar'}
)
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()
```From https://quarto.org/docs/computations/python.html#code-blocks |
See [this comment](#22470 (comment)) from #22470. Also related to #6140. ## Summary Add support for formatting code blocks with curly brace syntax (e.g., \`\`\`{python}, \`\`\`{py}, \`\`\`{pyi}) in Markdown files. This syntax is commonly used in tools like Quarto and R Markdown. The regex pattern now matches both the standard syntax (\`\`\`python) and the curly brace variant (\`\`\`{python}). ## Test Plan Added test cases. Fix #22951 --------- Co-authored-by: Amethyst Reese <amethyst@n7.gg>
Adds initial support for formatting Python code blocks inside Markdown files.
Markdownsource types/kinds.mdfile extension toMarkdownby default```pyheaderPythonvsStuboptions based on language label on code block--preview, and otherwise emits formatting error when given a markdown fileextend-include = ["**/*.md"]if they want to format markdown files by defaultLimitations:
~~~fences, arbitrary fence lengths, or code blocks inside blockquotesIssue #3792