Skip to content

Commit 3969348

Browse files
committed
Getting cargo workspace from file_id and refactoring
1 parent 0913809 commit 3969348

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

crates/ide/src/doc_links.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,38 +133,37 @@ pub(crate) fn external_docs(
133133
db: &RootDatabase,
134134
position: &FilePosition,
135135
target_dir: Option<&OsStr>,
136-
) -> DocumentationLinks {
136+
) -> Option<DocumentationLinks> {
137137
let sema = &Semantics::new(db);
138138
let file = sema.parse(position.file_id).syntax().clone();
139139
let token = pick_best_token(file.token_at_offset(position.offset), |kind| match kind {
140140
IDENT | INT_NUMBER | T![self] => 3,
141141
T!['('] | T![')'] => 2,
142142
kind if kind.is_trivia() => 0,
143143
_ => 1,
144-
});
145-
let Some(token) = token else { return Default::default() };
144+
})?;
146145
let token = sema.descend_into_macros_single(token);
147146

148-
let Some(node) = token.parent() else { return Default::default() };
147+
let node = token.parent()?;
149148
let definition = match_ast! {
150149
match node {
151150
ast::NameRef(name_ref) => match NameRefClass::classify(sema, &name_ref) {
152151
Some(NameRefClass::Definition(def)) => def,
153152
Some(NameRefClass::FieldShorthand { local_ref: _, field_ref }) => {
154153
Definition::Field(field_ref)
155154
}
156-
None => return Default::default(),
155+
None => return None,
157156
},
158157
ast::Name(name) => match NameClass::classify(sema, &name) {
159158
Some(NameClass::Definition(it) | NameClass::ConstReference(it)) => it,
160159
Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => Definition::Field(field_ref),
161-
None => return Default::default(),
160+
None => return None,
162161
},
163-
_ => return Default::default(),
162+
_ => return None
164163
}
165164
};
166165

167-
get_doc_links(db, definition, target_dir)
166+
Some(get_doc_links(db, definition, target_dir))
168167
}
169168

170169
/// Extracts all links from a given markdown text returning the definition text range, link-text
@@ -327,6 +326,10 @@ fn get_doc_links(
327326
def: Definition,
328327
target_dir: Option<&OsStr>,
329328
) -> DocumentationLinks {
329+
let join_url = |base_url: Option<Url>, path: &str| -> Option<Url> {
330+
base_url.and_then(|url| url.join(path).ok())
331+
};
332+
330333
let Some((target, file, frag)) = filename_and_frag_for_def(db, def) else { return Default::default(); };
331334

332335
let (mut web_url, mut local_url) = get_doc_base_urls(db, target, target_dir);
@@ -339,21 +342,13 @@ fn get_doc_links(
339342
web_url = join_url(web_url, &file);
340343
local_url = join_url(local_url, &file);
341344

342-
set_fragment_for_url(web_url.as_mut(), frag.as_deref());
343-
set_fragment_for_url(local_url.as_mut(), frag.as_deref());
345+
web_url.as_mut().map(|url| url.set_fragment(frag.as_deref()));
346+
local_url.as_mut().map(|url| url.set_fragment(frag.as_deref()));
344347

345348
return DocumentationLinks {
346349
web_url: web_url.map(|it| it.into()),
347350
local_url: local_url.map(|it| it.into()),
348351
};
349-
350-
fn join_url(base_url: Option<Url>, path: &str) -> Option<Url> {
351-
base_url.and_then(|url| url.join(path).ok())
352-
}
353-
354-
fn set_fragment_for_url(url: Option<&mut Url>, frag: Option<&str>) {
355-
url.map(|url| url.set_fragment(frag));
356-
}
357352
}
358353

359354
fn rewrite_intra_doc_link(
@@ -467,8 +462,14 @@ fn get_doc_base_urls(
467462
def: Definition,
468463
target_dir: Option<&OsStr>,
469464
) -> (Option<Url>, Option<Url>) {
470-
let local_doc_path =
471-
target_dir.and_then(create_url_from_os_str).and_then(|it| it.join("doc/").ok());
465+
let local_doc_path = target_dir
466+
.and_then(|path: &OsStr| -> Option<Url> {
467+
let mut with_prefix = OsStr::new("file:///").to_os_string();
468+
with_prefix.push(path);
469+
with_prefix.push("/");
470+
with_prefix.to_str().and_then(|s| Url::parse(s).ok())
471+
})
472+
.and_then(|it| it.join("doc/").ok());
472473
// special case base url of `BuiltinType` to core
473474
// https://github.com/rust-lang/rust-analyzer/issues/12250
474475
if let Definition::BuiltinType(..) = def {
@@ -533,14 +534,7 @@ fn get_doc_base_urls(
533534
.and_then(|it| it.join(&format!("{display_name}/")).ok());
534535
let local_base = local_base.and_then(|it| it.join(&format!("{display_name}/")).ok());
535536

536-
return (web_base, local_base);
537-
538-
fn create_url_from_os_str(path: &OsStr) -> Option<Url> {
539-
let mut with_prefix = OsStr::new("file:///").to_os_string();
540-
with_prefix.push(path);
541-
with_prefix.push("/");
542-
with_prefix.to_str().and_then(|s| Url::parse(s).ok())
543-
}
537+
(web_base, local_base)
544538
}
545539

546540
/// Get the filename and extension generated for a symbol by rustdoc.

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl Analysis {
476476
position: FilePosition,
477477
target_dir: Option<&OsStr>,
478478
) -> Cancellable<doc_links::DocumentationLinks> {
479-
self.with_db(|db| doc_links::external_docs(db, &position, target_dir))
479+
self.with_db(|db| doc_links::external_docs(db, &position, target_dir).unwrap_or_default())
480480
}
481481

482482
/// Computes parameter information at the given position.

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,14 +1537,16 @@ pub(crate) fn handle_open_docs(
15371537
params: lsp_types::TextDocumentPositionParams,
15381538
) -> Result<(Option<lsp_types::Url>, Option<lsp_types::Url>)> {
15391539
let _p = profile::span("handle_open_docs");
1540+
let file_uri = &params.text_document.uri;
1541+
let file_id = from_proto::file_id(&snap, file_uri)?;
15401542
let position = from_proto::file_position(&snap, params)?;
15411543

1542-
let cargo = match snap.workspaces.get(0) {
1543-
Some(ProjectWorkspace::Cargo { cargo, .. }) => Some(cargo),
1544+
let cargo = match &*snap.analysis.crates_for(file_id)? {
1545+
&[crate_id, ..] => snap.cargo_target_for_crate_root(crate_id).map(|(it, _)| it),
15441546
_ => None,
15451547
};
1546-
let target_dir =
1547-
cargo.and_then(|cargo| Some(cargo.target_directory())).and_then(|p| Some(p.as_os_str()));
1548+
1549+
let target_dir = cargo.map(|cargo| cargo.target_directory()).map(|p| p.as_os_str());
15481550
let Ok(remote_urls) = snap.analysis.external_docs(position, target_dir) else { return Ok((None, None)); };
15491551

15501552
let web_url = remote_urls.web_url.and_then(|it| Url::parse(&it).ok());

0 commit comments

Comments
 (0)