1use xur_ast::visit::{self, Visitor};
5use xur_ast::{Module, NodeId, Pat, PatKind, Path};
6use xur_span::{SourceMap, Span};
7
8use crate::{Res, Resolutions};
9
10#[derive(Debug, Clone, Copy)]
12pub struct NameRef {
13 pub id: NodeId,
14 pub span: Span,
15}
16
17pub fn names(module: &Module) -> Vec<NameRef> {
19 let mut collector = Collector(Vec::new());
20 collector.visit_module(module);
21 collector.0.sort_by_key(|name| name.span.start);
22 collector.0
23}
24
25struct Collector(Vec<NameRef>);
26
27impl<'ast> Visitor<'ast> for Collector {
28 fn visit_path(&mut self, path: &'ast Path) {
29 self.0.push(NameRef {
30 id: path.id,
31 span: path.span,
32 });
33 visit::walk_path(self, path);
34 }
35
36 fn visit_pat(&mut self, pat: &'ast Pat) {
37 if let PatKind::Ident(_) = pat.kind {
38 self.0.push(NameRef {
39 id: pat.id,
40 span: pat.span,
41 });
42 }
43 visit::walk_pat(self, pat);
44 }
45}
46
47impl Resolutions {
48 pub fn describe(&self, res: Res, source_map: &SourceMap) -> String {
51 match res {
52 Res::Def(kind, def) => format!("{} {}", kind.descr(), self.def_path(def)),
53 Res::Local(id) => {
54 let ident = self.locals[&id];
55 format!("local {ident} at {}", source_map.describe_pos(ident.span))
56 }
57 Res::PrimTy(ty) => format!("builtin type {}", ty.name()),
58 Res::TyParamAssoc(param, name) => {
59 format!("{}::{name}", self.def_path(param))
60 }
61 Res::Err => "error".to_owned(),
62 }
63 }
64}