Support formatting pycon markdown code blocks#23112
Conversation
|
Still needs tests |
|
ntBre
left a comment
There was a problem hiding this comment.
I wan to take a closer look at how this works in the docstring formatting, but this looks plausible to me in general.
Typing conformance resultsNo changes detected ✅ |
|
ntBre
left a comment
There was a problem hiding this comment.
I managed to come up with a few nits, but this looks good to me overall. The main things I'm worried about are the trailing whitespace and the ruff_formatter dependency (sorry again about that one!).
|
|
||
| let offset = FIRST_LINE.text_len(); | ||
| let mut changed = false; | ||
| let mut result = String::with_capacity(source.len()); |
There was a problem hiding this comment.
We might actually want to skip this with_capacity call since String::new() won't allocate at all in the case that nothing changes and we're able to return None.
There was a problem hiding this comment.
Should I do the same thing in format_code_blocks above? Is there a way to "initialize with full capacity only once needed"?
There was a problem hiding this comment.
It's probably not a big deal either way, but yeah I guess you could do the same in format_code_blocks. I don't think there's a nice way to initialize only if needed. There's reserve, but that's not quite the same.
I think it's fine to leave this as-is.
crates/ruff_markdown/src/lib.rs
Outdated
| while let Some(next_line) = lines.peek() { | ||
| if next_line.starts_with(CONTINUATION) { | ||
| end = next_line.full_end(); | ||
| unformatted.push_str(&source[TextRange::new(next_line.start() + offset, end)]); | ||
| lines.next(); | ||
| } else { | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
| while let Some(next_line) = lines.peek() { | |
| if next_line.starts_with(CONTINUATION) { | |
| end = next_line.full_end(); | |
| unformatted.push_str(&source[TextRange::new(next_line.start() + offset, end)]); | |
| lines.next(); | |
| } else { | |
| break; | |
| } | |
| } | |
| while let Some(next_line) = lines.next_if(|line| line.starts_with(CONTINUATION)) { | |
| end = next_line.full_end(); | |
| unformatted.push_str(&source[TextRange::new(next_line.start() + offset, end)]); | |
| } |
I think you can simplify this slightly with the next_if helper.
There was a problem hiding this comment.
I'm not sure if I like this as much once I had to add logic to deal with empty continuation lines (ie, no space after the "...") 🤔
What do you think? 257b0ae
There was a problem hiding this comment.
That commit looks fine to me! What don't you like about it?
There was a problem hiding this comment.
mostly that it didn't feel "simpler" at this point, but ¯_(ツ)_/¯
* main: (45 commits) [ty] Fix wrong inlay hints for overloaded function arguments (astral-sh#23179) [ty] Respect `@no_type_check` when combined with other decorators (astral-sh#23177) [ty] Use type context when inferring constructor argument types (astral-sh#23139) [`airflow`] Add ruff rules to catch deprecated attribute access from context key for Airflow 3.0 (`AIR301`) (astral-sh#22850) Support formatting `pycon` markdown code blocks (astral-sh#23112) Markdown formatting in LSP (astral-sh#23063) Instruct Claude to use comments more sparingly (astral-sh#23181) [`flake8-gettext`] Fix false negatives for plural argument of ngettext (`INT001`, `INT002`, `INT003`) (astral-sh#21078) [ty] Invoking goto-def on parentheses of a class constructor call takes you too constructor method [ty] Make goto definition on class constructor always go to class definition [ty] Assign lower completions ranking to deprecated functions and classes (astral-sh#23089) [ty] Fix parameter references across files via keyword args (astral-sh#23012) [ty] Exclude enclosing class for base completions (astral-sh#23141) [`pyupgrade`] Fix syntax error on string with newline escape and comment (`UP037`) (astral-sh#22968) [ty] Improve documentation for `expect_single_definition` method (astral-sh#23175) [ty] Configure check mode for all projects Add `home-assistant` to ecosystem projects (astral-sh#23132) Add tabbed shell completion documentation (astral-sh#23169) Bump typing conformance-suite pin (astral-sh#23174) [ty] Fix invalid diagnostic location for a sub-call to a specialized ParamSpec (astral-sh#23036) ...
Fix #23078