1use either::Either;
4use hir_expand::{AstId, InFile};
5use la_arena::{Arena, ArenaMap, Idx};
6use syntax::{AstNode, AstPtr, ast};
7
8use crate::{
9 AstIdLoc, GenericDefId, LocalFieldId, LocalLifetimeParamId, LocalTypeOrConstParamId, Lookup,
10 UseId, VariantId, attr::Attrs, db::DefDatabase,
11};
12
13pub trait HasSource {
14 type Value: AstNode;
15 fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
16 let InFile { file_id, value } = self.ast_ptr(db);
17 InFile::new(file_id, value.to_node(&db.parse_or_expand(file_id)))
18 }
19 fn ast_ptr(&self, db: &dyn DefDatabase) -> InFile<AstPtr<Self::Value>>;
20}
21
22impl<T> HasSource for T
23where
24 T: AstIdLoc,
25{
26 type Value = T::Ast;
27 fn ast_ptr(&self, db: &dyn DefDatabase) -> InFile<AstPtr<Self::Value>> {
28 let id = self.ast_id();
29 let ast_id_map = db.ast_id_map(id.file_id);
30 InFile::new(id.file_id, ast_id_map.get(id.value))
31 }
32}
33
34pub trait HasChildSource<ChildId> {
35 type Value;
36 fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<ChildId, Self::Value>>;
37}
38
39pub fn use_tree_to_ast(
41 db: &dyn DefDatabase,
42 use_ast_id: AstId<ast::Use>,
43 index: Idx<ast::UseTree>,
44) -> ast::UseTree {
45 use_tree_source_map(db, use_ast_id)[index].clone()
46}
47
48fn use_tree_source_map(db: &dyn DefDatabase, use_ast_id: AstId<ast::Use>) -> Arena<ast::UseTree> {
50 let ast = use_ast_id.to_node(db);
53 let ast_use_tree = ast.use_tree().expect("missing `use_tree`");
54 let mut span_map = None;
55 crate::item_tree::lower_use_tree(db, ast_use_tree, &mut |range| {
56 span_map.get_or_insert_with(|| db.span_map(use_ast_id.file_id)).span_for_range(range).ctx
57 })
58 .expect("failed to lower use tree")
59 .1
60}
61
62impl HasChildSource<la_arena::Idx<ast::UseTree>> for UseId {
63 type Value = ast::UseTree;
64 fn child_source(
65 &self,
66 db: &dyn DefDatabase,
67 ) -> InFile<ArenaMap<la_arena::Idx<ast::UseTree>, Self::Value>> {
68 let loc = self.lookup(db);
69 InFile::new(loc.id.file_id, use_tree_source_map(db, loc.id).into_iter().collect())
70 }
71}
72
73impl HasChildSource<LocalTypeOrConstParamId> for GenericDefId {
74 type Value = Either<ast::TypeOrConstParam, ast::Trait>;
75 fn child_source(
76 &self,
77 db: &dyn DefDatabase,
78 ) -> InFile<ArenaMap<LocalTypeOrConstParamId, Self::Value>> {
79 let generic_params = db.generic_params(*self);
80 let mut idx_iter = generic_params.iter_type_or_consts().map(|(idx, _)| idx);
81
82 let (file_id, generic_params_list) = self.file_id_and_params_of(db);
83
84 let mut params = ArenaMap::default();
85
86 match *self {
89 GenericDefId::TraitId(id) => {
90 let trait_ref = id.lookup(db).source(db).value;
91 let idx = idx_iter.next().unwrap();
92 params.insert(idx, Either::Right(trait_ref));
93 }
94 _ => {}
95 }
96
97 if let Some(generic_params_list) = generic_params_list {
98 for (idx, ast_param) in idx_iter.zip(generic_params_list.type_or_const_params()) {
99 params.insert(idx, Either::Left(ast_param));
100 }
101 }
102
103 InFile::new(file_id, params)
104 }
105}
106
107impl HasChildSource<LocalLifetimeParamId> for GenericDefId {
108 type Value = ast::LifetimeParam;
109 fn child_source(
110 &self,
111 db: &dyn DefDatabase,
112 ) -> InFile<ArenaMap<LocalLifetimeParamId, Self::Value>> {
113 let generic_params = db.generic_params(*self);
114 let idx_iter = generic_params.iter_lt().map(|(idx, _)| idx);
115
116 let (file_id, generic_params_list) = self.file_id_and_params_of(db);
117
118 let mut params = ArenaMap::default();
119
120 if let Some(generic_params_list) = generic_params_list {
121 for (idx, ast_param) in idx_iter.zip(generic_params_list.lifetime_params()) {
122 params.insert(idx, ast_param);
123 }
124 }
125
126 InFile::new(file_id, params)
127 }
128}
129
130impl HasChildSource<LocalFieldId> for VariantId {
131 type Value = Either<ast::TupleField, ast::RecordField>;
132
133 fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<LocalFieldId, Self::Value>> {
134 let (src, container) = match *self {
135 VariantId::EnumVariantId(it) => {
136 let lookup = it.lookup(db);
137 (lookup.source(db).map(|it| it.kind()), lookup.parent.lookup(db).container)
138 }
139 VariantId::StructId(it) => {
140 let lookup = it.lookup(db);
141 (lookup.source(db).map(|it| it.kind()), lookup.container)
142 }
143 VariantId::UnionId(it) => {
144 let lookup = it.lookup(db);
145 (lookup.source(db).map(|it| it.kind()), lookup.container)
146 }
147 };
148 let span_map = db.span_map(src.file_id);
149 let mut map = ArenaMap::new();
150 match &src.value {
151 ast::StructKind::Tuple(fl) => {
152 let cfg_options = container.krate.cfg_options(db);
153 let mut idx = 0;
154 for fd in fl.fields() {
155 let enabled =
156 Attrs::is_cfg_enabled_for(db, &fd, span_map.as_ref(), cfg_options).is_ok();
157 if !enabled {
158 continue;
159 }
160 map.insert(
161 LocalFieldId::from_raw(la_arena::RawIdx::from(idx)),
162 Either::Left(fd.clone()),
163 );
164 idx += 1;
165 }
166 }
167 ast::StructKind::Record(fl) => {
168 let cfg_options = container.krate.cfg_options(db);
169 let mut idx = 0;
170 for fd in fl.fields() {
171 let enabled =
172 Attrs::is_cfg_enabled_for(db, &fd, span_map.as_ref(), cfg_options).is_ok();
173 if !enabled {
174 continue;
175 }
176 map.insert(
177 LocalFieldId::from_raw(la_arena::RawIdx::from(idx)),
178 Either::Right(fd.clone()),
179 );
180 idx += 1;
181 }
182 }
183 ast::StructKind::Unit => (),
184 }
185 InFile::new(src.file_id, map)
186 }
187}