Sitelet https://github.com/starkware-libs/cairo/pull/10151/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions crates/cairo-lang-doc/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ pub fn parse_documentation_comment(documentation_comment: &str) -> Vec<Documenta
.as_ref()
.and_then(|label_range| {
location_from_link_fields(
documentation_comment,
link.link_type,
&link.destination,
label_range,
Expand Down Expand Up @@ -370,24 +371,22 @@ fn span_from_relative_range(content: &str, range: Range<usize>) -> TextSpan {

/// Extracts a location link span and normalized destination text for the given link fields.
fn location_from_link_fields(
content: &str,
link_type: LinkType,
destination: &str,
label_range: &Range<usize>,
) -> Option<(Range<usize>, String)> {
let (destination_normalized, backticked) = normalize_location_text(destination)?;

match link_type {
LinkType::Inline => {
let range = find_inline_destination_range(label_range.end, destination);
Some((range, destination_normalized))
}
let range = match link_type {
LinkType::Inline => find_inline_destination_range(content, label_range.end, destination),
LinkType::Collapsed
| LinkType::CollapsedUnknown
| LinkType::Shortcut
| LinkType::ShortcutUnknown => Some((label_range.clone(), destination_normalized)),
_ => None,
}
.map(|(range, text)| (trim_backtick_range(range.clone(), backticked), text))
| LinkType::ShortcutUnknown => label_range.clone(),
_ => return None,
};
Some((trim_backtick_range(range, backticked), destination_normalized))
}

/// Returns true when the string looks like a location path (letters, digits, '_' or ':').
Expand Down Expand Up @@ -417,10 +416,17 @@ fn trim_backtick_range(range: Range<usize>, backticked: bool) -> Range<usize> {
}

/// Computes the range for an inline destination that follows a label.
fn find_inline_destination_range(label_last_end: usize, destination: &str) -> Range<usize> {
let destination_start = label_last_end + 2;
let destination_end = destination_start + destination.len();
destination_start..destination_end
fn find_inline_destination_range(
content: &str,
label_last_end: usize,
destination: &str,
) -> Range<usize> {
// Finds the actual `](` boundary after the label, or uses the label's end if not found.
// This handles cases where the label ends in markup (e.g. `[**bold**](path)`),
// so the `](` that opens the destination is not necessarily adjacent to it.
let destination_start =
label_last_end + content[label_last_end..].find("](").unwrap_or_default() + 2;
destination_start..(destination_start + destination.len())
}

/// Maps `HeadingLevel` to the correct markdown marker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,88 @@ MarkdownLink {
dest_span: None,
dest_text: None,
}

//! > ==========================================================================

//! > Inline link whose label ends in bare emphasis markers.

//! > test_runner_name
documentation_comment_parser_links_runner

//! > cairo_project.toml
[crate_roots]
hello = "src"

//! > cairo_code
/// See [**bold**](path) and [*em*](path2).

fn foo() {}

//! > expected_diagnostics

//! > Doc comment #1
/// See [**bold**](path) and [*em*](path2).

//! > Doc comment #1 links
MarkdownLink {
link_span: TextSpan {
start: TextOffset(
TextWidth(
8,
),
),
end: TextOffset(
TextWidth(
24,
),
),
},
dest_span: Some(
TextSpan {
start: TextOffset(
TextWidth(
19,
),
),
end: TextOffset(
TextWidth(
23,
),
),
},
),
dest_text: Some(
"path",
),
}
MarkdownLink {
link_span: TextSpan {
start: TextOffset(
TextWidth(
29,
),
),
end: TextOffset(
TextWidth(
42,
),
),
},
dest_span: Some(
TextSpan {
start: TextOffset(
TextWidth(
36,
),
),
end: TextOffset(
TextWidth(
41,
),
),
},
),
dest_text: Some(
"path2",
),
}
Loading