aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src/lib.rs')
-rw-r--r--crates/ra_hir_expand/src/lib.rs31
1 files changed, 25 insertions, 6 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 5eac2605b..2e8d63691 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -88,6 +88,25 @@ impl HirFileId {
88 } 88 }
89 } 89 }
90 90
91 pub fn expansion_level(self, db: &dyn db::AstDatabase) -> u32 {
92 let mut level = 0;
93 let mut curr = self;
94 while let HirFileIdRepr::MacroFile(macro_file) = curr.0 {
95 level += 1;
96 curr = match macro_file.macro_call_id {
97 MacroCallId::LazyMacro(id) => {
98 let loc = db.lookup_intern_macro(id);
99 loc.kind.file_id()
100 }
101 MacroCallId::EagerMacro(id) => {
102 let loc = db.lookup_intern_eager_expansion(id);
103 loc.file_id
104 }
105 };
106 }
107 level
108 }
109
91 /// If this is a macro call, returns the syntax node of the call. 110 /// If this is a macro call, returns the syntax node of the call.
92 pub fn call_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> { 111 pub fn call_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> {
93 match self.0 { 112 match self.0 {
@@ -140,7 +159,7 @@ impl HirFileId {
140 } 159 }
141 160
142 /// Indicate it is macro file generated for builtin derive 161 /// Indicate it is macro file generated for builtin derive
143 pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::ModuleItem>> { 162 pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::Item>> {
144 match self.0 { 163 match self.0 {
145 HirFileIdRepr::FileId(_) => None, 164 HirFileIdRepr::FileId(_) => None,
146 HirFileIdRepr::MacroFile(macro_file) => { 165 HirFileIdRepr::MacroFile(macro_file) => {
@@ -155,7 +174,7 @@ impl HirFileId {
155 MacroDefKind::BuiltInDerive(_) => loc.kind.node(db), 174 MacroDefKind::BuiltInDerive(_) => loc.kind.node(db),
156 _ => return None, 175 _ => return None,
157 }; 176 };
158 Some(item.with_value(ast::ModuleItem::cast(item.value.clone())?)) 177 Some(item.with_value(ast::Item::cast(item.value.clone())?))
159 } 178 }
160 } 179 }
161 } 180 }
@@ -239,18 +258,18 @@ pub struct MacroCallLoc {
239#[derive(Debug, Clone, PartialEq, Eq, Hash)] 258#[derive(Debug, Clone, PartialEq, Eq, Hash)]
240pub enum MacroCallKind { 259pub enum MacroCallKind {
241 FnLike(AstId<ast::MacroCall>), 260 FnLike(AstId<ast::MacroCall>),
242 Attr(AstId<ast::ModuleItem>, String), 261 Attr(AstId<ast::Item>, String),
243} 262}
244 263
245impl MacroCallKind { 264impl MacroCallKind {
246 pub fn file_id(&self) -> HirFileId { 265 fn file_id(&self) -> HirFileId {
247 match self { 266 match self {
248 MacroCallKind::FnLike(ast_id) => ast_id.file_id, 267 MacroCallKind::FnLike(ast_id) => ast_id.file_id,
249 MacroCallKind::Attr(ast_id, _) => ast_id.file_id, 268 MacroCallKind::Attr(ast_id, _) => ast_id.file_id,
250 } 269 }
251 } 270 }
252 271
253 pub fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { 272 fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> {
254 match self { 273 match self {
255 MacroCallKind::FnLike(ast_id) => ast_id.with_value(ast_id.to_node(db).syntax().clone()), 274 MacroCallKind::FnLike(ast_id) => ast_id.with_value(ast_id.to_node(db).syntax().clone()),
256 MacroCallKind::Attr(ast_id, _) => { 275 MacroCallKind::Attr(ast_id, _) => {
@@ -259,7 +278,7 @@ impl MacroCallKind {
259 } 278 }
260 } 279 }
261 280
262 pub fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { 281 fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> {
263 match self { 282 match self {
264 MacroCallKind::FnLike(ast_id) => { 283 MacroCallKind::FnLike(ast_id) => {
265 Some(ast_id.to_node(db).token_tree()?.syntax().clone()) 284 Some(ast_id.to_node(db).token_tree()?.syntax().clone())