aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-03-07 14:27:03 +0000
committerFlorian Diebold <[email protected]>2020-03-07 14:48:06 +0000
commit24e98121d81b75bafcd9c6005548776c00de8401 (patch)
tree976841b2501ab4501613a5f66b1004cd67f7e369 /crates/ra_hir_expand/src
parentaff82cf7ac172f213cb5dcca637cb2c5332294c1 (diff)
Try to complete within macros
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r--crates/ra_hir_expand/src/db.rs53
-rw-r--r--crates/ra_hir_expand/src/lib.rs9
2 files changed, 53 insertions, 9 deletions
diff --git a/crates/ra_hir_expand/src/db.rs b/crates/ra_hir_expand/src/db.rs
index f3a84cacc..a7aa60fc9 100644
--- a/crates/ra_hir_expand/src/db.rs
+++ b/crates/ra_hir_expand/src/db.rs
@@ -130,15 +130,42 @@ pub(crate) fn macro_expand(
130 db: &dyn AstDatabase, 130 db: &dyn AstDatabase,
131 id: MacroCallId, 131 id: MacroCallId,
132) -> Result<Arc<tt::Subtree>, String> { 132) -> Result<Arc<tt::Subtree>, String> {
133 macro_expand_with_arg(db, id, None)
134}
135
136// TODO hack
137pub fn expander(
138 db: &dyn AstDatabase,
139 id: MacroCallId,
140) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
141 let lazy_id = match id {
142 MacroCallId::LazyMacro(id) => id,
143 MacroCallId::EagerMacro(_id) => {
144 // TODO
145 unimplemented!()
146 }
147 };
148
149 let loc = db.lookup_intern_macro(lazy_id);
150 let macro_rules = db.macro_def(loc.def)?;
151 Some(macro_rules)
152}
153
154pub(crate) fn macro_expand_with_arg(
155 db: &dyn AstDatabase,
156 id: MacroCallId,
157 arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
158) -> Result<Arc<tt::Subtree>, String> {
133 let lazy_id = match id { 159 let lazy_id = match id {
134 MacroCallId::LazyMacro(id) => id, 160 MacroCallId::LazyMacro(id) => id,
135 MacroCallId::EagerMacro(id) => { 161 MacroCallId::EagerMacro(id) => {
162 // TODO
136 return Ok(db.lookup_intern_eager_expansion(id).subtree); 163 return Ok(db.lookup_intern_eager_expansion(id).subtree);
137 } 164 }
138 }; 165 };
139 166
140 let loc = db.lookup_intern_macro(lazy_id); 167 let loc = db.lookup_intern_macro(lazy_id);
141 let macro_arg = db.macro_arg(id).ok_or("Fail to args in to tt::TokenTree")?; 168 let macro_arg = arg.or_else(|| db.macro_arg(id)).ok_or("Fail to args in to tt::TokenTree")?;
142 169
143 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; 170 let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
144 let tt = macro_rules.0.expand(db, lazy_id, &macro_arg.0).map_err(|err| format!("{:?}", err))?; 171 let tt = macro_rules.0.expand(db, lazy_id, &macro_arg.0).map_err(|err| format!("{:?}", err))?;
@@ -163,11 +190,23 @@ pub(crate) fn parse_macro(
163 db: &dyn AstDatabase, 190 db: &dyn AstDatabase,
164 macro_file: MacroFile, 191 macro_file: MacroFile,
165) -> Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> { 192) -> Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
193 parse_macro_with_arg(db, macro_file, None)
194}
195
196pub fn parse_macro_with_arg(
197 db: &dyn AstDatabase,
198 macro_file: MacroFile,
199 arg: Option<Arc<(tt::Subtree, mbe::TokenMap)>>,
200) -> Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)> {
166 let _p = profile("parse_macro_query"); 201 let _p = profile("parse_macro_query");
167 202
168 let macro_call_id = macro_file.macro_call_id; 203 let macro_call_id = macro_file.macro_call_id;
169 let tt = db 204 let expansion = if let Some(arg) = arg {
170 .macro_expand(macro_call_id) 205 macro_expand_with_arg(db, macro_call_id, Some(arg))
206 } else {
207 db.macro_expand(macro_call_id)
208 };
209 let tt = expansion
171 .map_err(|err| { 210 .map_err(|err| {
172 // Note: 211 // Note:
173 // The final goal we would like to make all parse_macro success, 212 // The final goal we would like to make all parse_macro success,
@@ -185,15 +224,13 @@ pub(crate) fn parse_macro(
185 .collect::<Vec<_>>() 224 .collect::<Vec<_>>()
186 .join("\n"); 225 .join("\n");
187 226
188 log::warn!( 227 eprintln!(
189 "fail on macro_parse: (reason: {} macro_call: {:#}) parents: {}", 228 "fail on macro_parse: (reason: {} macro_call: {:#}) parents: {}",
190 err, 229 err, node.value, parents
191 node.value,
192 parents
193 ); 230 );
194 } 231 }
195 _ => { 232 _ => {
196 log::warn!("fail on macro_parse: (reason: {})", err); 233 eprintln!("fail on macro_parse: (reason: {})", err);
197 } 234 }
198 } 235 }
199 }) 236 })
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 3fce73e8a..92f3902dd 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -157,6 +157,13 @@ impl HirFileId {
157 } 157 }
158 } 158 }
159 } 159 }
160
161 pub fn macro_file(self) -> Option<MacroFile> {
162 match self.0 {
163 HirFileIdRepr::FileId(_) => None,
164 HirFileIdRepr::MacroFile(m) => Some(m),
165 }
166 }
160} 167}
161 168
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 169#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -296,7 +303,7 @@ pub struct ExpansionInfo {
296 exp_map: Arc<mbe::TokenMap>, 303 exp_map: Arc<mbe::TokenMap>,
297} 304}
298 305
299pub use mbe::Origin; 306pub use mbe::{syntax_node_to_token_tree, Origin};
300use ra_parser::FragmentKind; 307use ra_parser::FragmentKind;
301 308
302impl ExpansionInfo { 309impl ExpansionInfo {