-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Parse doc comment markdown only once #16144
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
Open
Alexendoo
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
Alexendoo:parse-markdown-once
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use std::mem; | ||
| use std::ops::Range; | ||
|
|
||
| use clippy_utils::diagnostics::span_lint_and_then; | ||
| use rustc_errors::Applicability; | ||
| use rustc_lint::LateContext; | ||
| use rustc_resolve::rustdoc::pulldown_cmark::{Event, Tag, TagEnd}; | ||
|
|
||
| use crate::doc::Fragments; | ||
|
|
||
| use super::DOC_LINK_CODE; | ||
|
|
||
| struct PendingLink { | ||
| range: Range<usize>, | ||
| seen_code: bool, | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| pub(super) struct LinkCode { | ||
| start: Option<usize>, | ||
| end: Option<usize>, | ||
| includes_link: bool, | ||
| pending_link: Option<PendingLink>, | ||
| } | ||
|
|
||
| impl LinkCode { | ||
| pub fn check( | ||
| &mut self, | ||
| cx: &LateContext<'_>, | ||
| event: &Event<'_>, | ||
| range: Range<usize>, | ||
| doc: &str, | ||
| fragments: Fragments<'_>, | ||
| ) { | ||
| match event { | ||
| Event::Start(Tag::Link { .. }) => { | ||
| self.pending_link = Some(PendingLink { | ||
| range, | ||
| seen_code: false, | ||
| }); | ||
| }, | ||
| Event::End(TagEnd::Link) => { | ||
| if let Some(PendingLink { range, seen_code: true }) = self.pending_link.take() { | ||
| if self.start.is_some() { | ||
| self.end = Some(range.end); | ||
| } else { | ||
| self.start = Some(range.start); | ||
| } | ||
| self.includes_link = true; | ||
| } | ||
| }, | ||
| _ if let Some(pending_link) = &mut self.pending_link => { | ||
| if matches!(event, Event::Code(_)) && !pending_link.seen_code { | ||
| pending_link.seen_code = true; | ||
| } else { | ||
| self.consume(cx, fragments, doc); | ||
| } | ||
| }, | ||
| Event::Code(_) => { | ||
| if self.start.is_some() { | ||
| self.end = Some(range.end); | ||
| } else { | ||
| self.start = Some(range.start); | ||
| } | ||
| }, | ||
| _ => self.consume(cx, fragments, doc), | ||
| } | ||
| } | ||
|
|
||
| fn consume(&mut self, cx: &LateContext<'_>, fragments: Fragments<'_>, doc: &str) { | ||
| if let LinkCode { | ||
| start: Some(start), | ||
| end: Some(end), | ||
| includes_link: true, | ||
| pending_link: _, | ||
| } = mem::take(self) | ||
| && let Some(span) = fragments.span(cx, start..end) | ||
| { | ||
| span_lint_and_then(cx, DOC_LINK_CODE, span, "code link adjacent to code text", |diag| { | ||
| diag.span_suggestion_verbose( | ||
| span, | ||
| "wrap the entire group in `<code>` tags", | ||
| format!("<code>{}</code>", doc[start..end].replace('`', "")), | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
| diag.help("separate code snippets will be shown with a gap"); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a bug in this new version that isn't in the current version. Unfortunately, there's no test case for them in the code, but here's a couple of test cases that catch it (they pass in the main branch, but fail in this PR's branch):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it now verifies that links contain a single code and nothing, which catches a FP on:
Separate to this PR but I think it'd be worth moving out of nursery unless there's a known issue I'm missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an aside what does the
arstat the end of the test file lines mean? As a brit I cannot help but read it as "arsed"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use the Colemak layout, and arst just happens to be the left hand home row keys.