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

Allow rustup doc books open subsections and paragraphs #3380

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1586,22 +1586,38 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
};

let topical_path: PathBuf;

let doc_url = if let Some(topic) = m.get_one::<String>("topic") {
topical_path = topical_doc::local_path(&toolchain.doc_path("").unwrap(), topic)?;
topical_path.to_str().unwrap()
} else if let Some((_, _, path)) = DOCS_DATA.iter().find(|(name, _, _)| m.get_flag(name)) {
path
} else {
"index.html"
};
let cached_path: String;
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to define this one here? Maybe we can put it into the if statement.

Copy link
Author

@CiderSlime CiderSlime Jun 9, 2023

Choose a reason for hiding this comment

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

This is the way to resolve &str borrowing issue. If we put this definition into if statement we will get error:
error[E0597]: cached_path does not live long enough. because source String must live as long as let doc_url lives.
TBH I just used the same workaround that is already used with let topical_path: PathBuf; one line above.
maybe there is a nicer way to solve this.

let mut has_docs_data_link = false;

let doc_url =
if let Some((short, _, path)) = DOCS_DATA.iter().find(|(name, _, _)| m.get_flag(name)) {
if let Some(topic) = m.get_one::<String>("topic") {
has_docs_data_link = true;
cached_path = format!("{}/{}", short, topic);
cached_path.as_str()
} else {
path
}
} else if let Some(topic) = m.get_one::<String>("topic") {
topical_path = topical_doc::local_path(&toolchain.doc_path("").unwrap(), topic)?;
topical_path.to_str().unwrap()
} else {
"index.html"
};

if m.get_flag("path") {
let doc_path = toolchain.doc_path(doc_url)?;
writeln!(process().stdout().lock(), "{}", doc_path.display())?;
Ok(utils::ExitCode(0))
} else {
toolchain.open_docs(doc_url)?;
if has_docs_data_link {
let url_path_buf = toolchain.doc_path(doc_url)?;
Copy link
Member

Choose a reason for hiding this comment

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

I think we can put it into open_docs. We don't need to leak it here. And the overall logic is very similar with the open_docs.

Copy link
Author

@CiderSlime CiderSlime Jun 9, 2023

Choose a reason for hiding this comment

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

I have tried to use open_docs(), but there was a problem with differentiating web links from file links when we use urls with anchors. Problem is caused by "#" symbol in url and located in the opener crate

So I had to use utils::open_browser() to prepend "file:///" to the url arbitrary, after generating url with toolchain.doc_path()

Or maybe we should go further and prepend "file:///" everywhere?

let url_path = format!("file:///{}", url_path_buf.to_str().unwrap());
let doc_path = Path::new(&url_path);
utils::open_browser(doc_path)?
} else {
toolchain.open_docs(doc_url)?;
}
Ok(utils::ExitCode(0))
}
}
Expand Down