Auto merge of #69402 - GuillaumeGomez:extend-search, r=kinnison
Extend search I realized that when looking for "struct:String" in the rustdoc search for example, the "in arguments" and "returned" tabs were always empty. After some investigation, I realized it was because we only provided the name, and not the type, making it impossible to pass the "type filtering" check. To resolve this, I added the type alongside the name. Note for the future: we could improve this by instead only registering the path id and use the path dictionary directly. The only problem with that solution (which I already tested) is that it becomes complicated for types in other crates. It'd force us to handle both case with an id and a case with `(name, type)`. I found the current PR big enough to not want to provide it directly. However, I think this is definitely worth it to make it work this way in the future. About the two tests I added: they don't have much interest except checking that we actually have something returned in the search in the cases of a type filtering with and without literal search. I also had to update a bit the test script to add the new locally global (haha) variable I created (`NO_TYPE_FILTER`). I added this variable to make the code easier to read than just "-1". r? @kinnison cc @ollie27
This commit is contained in:
commit
f4c675c476
10 changed files with 237 additions and 108 deletions
|
@ -58,7 +58,7 @@ use rustc_span::symbol::{sym, Symbol};
|
|||
use serde::ser::SerializeSeq;
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
use crate::clean::{self, AttributesExt, Deprecation, GetDefId, SelfTy};
|
||||
use crate::clean::{self, AttributesExt, Deprecation, GetDefId, SelfTy, TypeKind};
|
||||
use crate::config::{OutputFormat, RenderOptions};
|
||||
use crate::docfs::{DocFS, ErrorStorage, PathError};
|
||||
use crate::doctree;
|
||||
|
@ -302,19 +302,25 @@ impl Serialize for IndexItem {
|
|||
|
||||
/// A type used for the search index.
|
||||
#[derive(Debug)]
|
||||
struct Type {
|
||||
struct RenderType {
|
||||
ty: Option<DefId>,
|
||||
idx: Option<usize>,
|
||||
name: Option<String>,
|
||||
generics: Option<Vec<String>>,
|
||||
generics: Option<Vec<Generic>>,
|
||||
}
|
||||
|
||||
impl Serialize for Type {
|
||||
impl Serialize for RenderType {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
if let Some(name) = &self.name {
|
||||
let mut seq = serializer.serialize_seq(None)?;
|
||||
seq.serialize_element(&name)?;
|
||||
if let Some(id) = self.idx {
|
||||
seq.serialize_element(&id)?;
|
||||
} else {
|
||||
seq.serialize_element(&name)?;
|
||||
}
|
||||
if let Some(generics) = &self.generics {
|
||||
seq.serialize_element(&generics)?;
|
||||
}
|
||||
|
@ -325,11 +331,32 @@ impl Serialize for Type {
|
|||
}
|
||||
}
|
||||
|
||||
/// A type used for the search index.
|
||||
#[derive(Debug)]
|
||||
struct Generic {
|
||||
name: String,
|
||||
defid: Option<DefId>,
|
||||
idx: Option<usize>,
|
||||
}
|
||||
|
||||
impl Serialize for Generic {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
if let Some(id) = self.idx {
|
||||
serializer.serialize_some(&id)
|
||||
} else {
|
||||
serializer.serialize_some(&self.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Full type of functions/methods in the search index.
|
||||
#[derive(Debug)]
|
||||
struct IndexItemFunctionType {
|
||||
inputs: Vec<Type>,
|
||||
output: Option<Vec<Type>>,
|
||||
inputs: Vec<TypeWithKind>,
|
||||
output: Option<Vec<TypeWithKind>>,
|
||||
}
|
||||
|
||||
impl Serialize for IndexItemFunctionType {
|
||||
|
@ -340,8 +367,8 @@ impl Serialize for IndexItemFunctionType {
|
|||
// If we couldn't figure out a type, just write `null`.
|
||||
let mut iter = self.inputs.iter();
|
||||
if match self.output {
|
||||
Some(ref output) => iter.chain(output.iter()).any(|ref i| i.name.is_none()),
|
||||
None => iter.any(|ref i| i.name.is_none()),
|
||||
Some(ref output) => iter.chain(output.iter()).any(|ref i| i.ty.name.is_none()),
|
||||
None => iter.any(|ref i| i.ty.name.is_none()),
|
||||
} {
|
||||
serializer.serialize_none()
|
||||
} else {
|
||||
|
@ -359,6 +386,31 @@ impl Serialize for IndexItemFunctionType {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TypeWithKind {
|
||||
ty: RenderType,
|
||||
kind: TypeKind,
|
||||
}
|
||||
|
||||
impl From<(RenderType, TypeKind)> for TypeWithKind {
|
||||
fn from(x: (RenderType, TypeKind)) -> TypeWithKind {
|
||||
TypeWithKind { ty: x.0, kind: x.1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for TypeWithKind {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut seq = serializer.serialize_seq(None)?;
|
||||
seq.serialize_element(&self.ty.name)?;
|
||||
let x: ItemType = self.kind.into();
|
||||
seq.serialize_element(&x)?;
|
||||
seq.end()
|
||||
}
|
||||
}
|
||||
|
||||
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
|
||||
thread_local!(pub static CURRENT_DEPTH: Cell<usize> = Cell::new(0));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue