pub struct Index { /* private fields */ }Expand description
The persistent definition/reference index of a resolved program.
§Examples
use hir_lang::{Builder, Name, Span};
use intern_lang::Interner;
// fn f(n) { n } — the use of `n` is a reference to the parameter.
let mut names = Interner::new();
let n = Name::new(names.intern("n"));
let mut b = Builder::new();
b.set_span(Span::new(5, 6));
let (param, _) = b.local_param(n);
b.set_span(Span::new(10, 11));
let use_n = b.name_expr(n);
let body = b.block(&[], Some(use_n));
let f = b.func(Name::new(names.intern("f")), &[param], body);
let root = b.module(None, &[f]);
let hir = b.finish(root)?;
let unit = hir.unit();
let res = resolve_lang::resolve(hir, &names)?;
let index = res.index();
let hit = index.at(unit, 10).unwrap();
let def = index.definition(hit.def).unwrap();
assert_eq!(def.location.unwrap().span, Span::new(5, 6));
assert_eq!(index.references(hit.def).len(), 1);Implementations§
Source§impl Index
impl Index
Sourcepub fn definitions(&self) -> &[Definition]
pub fn definitions(&self) -> &[Definition]
Every definition, in unit order, then preorder (externals last).
§Examples
use resolve_lang::Index;
assert!(Index::default().definitions().is_empty());Sourcepub fn references_all(&self) -> &[Reference]
pub fn references_all(&self) -> &[Reference]
Every reference, by unit, then path, then segment.
§Examples
use resolve_lang::Index;
assert!(Index::default().references_all().is_empty());Sourcepub fn definition(&self, def: DefRef) -> Option<&Definition>
pub fn definition(&self, def: DefRef) -> Option<&Definition>
One definition.
§Examples
use resolve_lang::{DefRef, Index};
assert!(Index::default().definition(DefRef::from_index(0)).is_none());Sourcepub fn references(&self, def: DefRef) -> References<'_> ⓘ
pub fn references(&self, def: DefRef) -> References<'_> ⓘ
The references to a definition, sorted by location (find-references). Allocation-free: an iterator over the definition’s slice.
§Examples
use resolve_lang::{DefRef, Index};
assert_eq!(Index::default().references(DefRef::from_index(0)).len(), 0);Sourcepub fn def_of(&self, target: Target) -> Option<DefRef>
pub fn def_of(&self, target: Target) -> Option<DefRef>
The definition of a target (an item, variant, binder, aliased import, or a referenced external symbol).
§Examples
use hir_lang::{BinderId, UnitId};
use resolve_lang::{Index, Target};
let t = Target::Local(UnitId::new(0), BinderId::from_index(0).unwrap());
assert!(Index::default().def_of(t).is_none());Sourcepub fn resolve_at(
&self,
unit: UnitId,
path: PathId,
segment: u32,
) -> Option<DefRef>
pub fn resolve_at( &self, unit: UnitId, path: PathId, segment: u32, ) -> Option<DefRef>
Go-to-definition by HIR id: what segment segment of path names.
§Examples
use hir_lang::{PathId, UnitId};
use resolve_lang::Index;
assert!(Index::default().resolve_at(UnitId::new(0), PathId::from_index(0).unwrap(), 0).is_none());Sourcepub fn path_references(&self, unit: UnitId, path: PathId) -> &[Reference]
pub fn path_references(&self, unit: UnitId, path: PathId) -> &[Reference]
The references inside one path, by segment.
§Examples
use hir_lang::{PathId, UnitId};
use resolve_lang::Index;
assert!(Index::default().path_references(UnitId::new(0), PathId::from_index(0).unwrap()).is_empty());Sourcepub fn at(&self, unit: UnitId, offset: u32) -> Option<Occurrence>
pub fn at(&self, unit: UnitId, offset: u32) -> Option<Occurrence>
Go-to-definition by position: the definition or reference whose name
contains byte offset of unit’s source.
§Examples
use hir_lang::UnitId;
use resolve_lang::Index;
assert!(Index::default().at(UnitId::new(0), 3).is_none());Sourcepub fn rename_set(&self, def: DefRef) -> RenameSet
pub fn rename_set(&self, def: DefRef) -> RenameSet
Every edit renaming a definition implies: its own name and every reference that spells the same name (references through an aliased import spell the alias and are left alone). Renaming an aliased import edits the alias and the references that went through it.
§Examples
use resolve_lang::{DefRef, Index};
assert!(Index::default().rename_set(DefRef::from_index(0)).edits.is_empty());Sourcepub fn document_symbols(&self, unit: UnitId) -> Vec<(DefRef, &Definition)>
pub fn document_symbols(&self, unit: UnitId) -> Vec<(DefRef, &Definition)>
The item definitions of a unit in preorder (with parent links), for
a document outline.
§Examples
use hir_lang::UnitId;
use resolve_lang::Index;
assert!(Index::default().document_symbols(UnitId::new(0)).is_empty());