diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-18 11:47:50 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-18 11:47:50 +0000 |
commit | 242f0ae1d8e6766091a6050431c3d417a43a2a3e (patch) | |
tree | b45d7619b97cc6edaf6bcf7d1a42366879bdd703 /crates/ra_hir_expand | |
parent | 46ca40ccfced6945e05a25979a2703ad967d2fe0 (diff) | |
parent | 41544a40883874553f570e2999bf56d172bd6246 (diff) |
Merge #2545
2545: Add Token id to all tt::TokenTree r=matklad a=edwin0cheng
This PR try to add token id to all `tt::Leaf` and `tt::Delimiter`.
~~Some tests are failed now because of #2544~~
~~Still blocked by a test in goto-definition : see https://github.com/rust-analyzer/rust-analyzer/pull/2544#issuecomment-565572553~~
Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r-- | crates/ra_hir_expand/src/builtin_derive.rs | 28 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/quote.rs | 27 |
3 files changed, 45 insertions, 14 deletions
diff --git a/crates/ra_hir_expand/src/builtin_derive.rs b/crates/ra_hir_expand/src/builtin_derive.rs index b26441253..62c60e336 100644 --- a/crates/ra_hir_expand/src/builtin_derive.rs +++ b/crates/ra_hir_expand/src/builtin_derive.rs | |||
@@ -97,11 +97,24 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, mbe::ExpandError> { | |||
97 | 97 | ||
98 | fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> { | 98 | fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> { |
99 | let mut result = Vec::<tt::TokenTree>::new(); | 99 | let mut result = Vec::<tt::TokenTree>::new(); |
100 | result.push(tt::Leaf::Punct(tt::Punct { char: '<', spacing: tt::Spacing::Alone }).into()); | 100 | result.push( |
101 | tt::Leaf::Punct(tt::Punct { | ||
102 | char: '<', | ||
103 | spacing: tt::Spacing::Alone, | ||
104 | id: tt::TokenId::unspecified(), | ||
105 | }) | ||
106 | .into(), | ||
107 | ); | ||
101 | for i in 0..n { | 108 | for i in 0..n { |
102 | if i > 0 { | 109 | if i > 0 { |
103 | result | 110 | result.push( |
104 | .push(tt::Leaf::Punct(tt::Punct { char: ',', spacing: tt::Spacing::Alone }).into()); | 111 | tt::Leaf::Punct(tt::Punct { |
112 | char: ',', | ||
113 | spacing: tt::Spacing::Alone, | ||
114 | id: tt::TokenId::unspecified(), | ||
115 | }) | ||
116 | .into(), | ||
117 | ); | ||
105 | } | 118 | } |
106 | result.push( | 119 | result.push( |
107 | tt::Leaf::Ident(tt::Ident { | 120 | tt::Leaf::Ident(tt::Ident { |
@@ -112,7 +125,14 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> { | |||
112 | ); | 125 | ); |
113 | result.extend(bound.iter().cloned()); | 126 | result.extend(bound.iter().cloned()); |
114 | } | 127 | } |
115 | result.push(tt::Leaf::Punct(tt::Punct { char: '>', spacing: tt::Spacing::Alone }).into()); | 128 | result.push( |
129 | tt::Leaf::Punct(tt::Punct { | ||
130 | char: '>', | ||
131 | spacing: tt::Spacing::Alone, | ||
132 | id: tt::TokenId::unspecified(), | ||
133 | }) | ||
134 | .into(), | ||
135 | ); | ||
116 | result | 136 | result |
117 | } | 137 | } |
118 | 138 | ||
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index cb4e1950b..2fa5d5140 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -227,7 +227,7 @@ impl ExpansionInfo { | |||
227 | let token_id = self.macro_arg.1.token_by_range(range)?; | 227 | let token_id = self.macro_arg.1.token_by_range(range)?; |
228 | let token_id = self.macro_def.0.map_id_down(token_id); | 228 | let token_id = self.macro_def.0.map_id_down(token_id); |
229 | 229 | ||
230 | let range = self.exp_map.range_by_token(token_id)?; | 230 | let range = self.exp_map.range_by_token(token_id)?.by_kind(token.value.kind())?; |
231 | 231 | ||
232 | let token = algo::find_covering_element(&self.expanded.value, range).into_token()?; | 232 | let token = algo::find_covering_element(&self.expanded.value, range).into_token()?; |
233 | 233 | ||
@@ -248,7 +248,7 @@ impl ExpansionInfo { | |||
248 | } | 248 | } |
249 | }; | 249 | }; |
250 | 250 | ||
251 | let range = token_map.range_by_token(token_id)?; | 251 | let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; |
252 | let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start()) | 252 | let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start()) |
253 | .into_token()?; | 253 | .into_token()?; |
254 | Some((tt.with_value(token), origin)) | 254 | Some((tt.with_value(token), origin)) |
diff --git a/crates/ra_hir_expand/src/quote.rs b/crates/ra_hir_expand/src/quote.rs index aa8a5f23f..49155fe62 100644 --- a/crates/ra_hir_expand/src/quote.rs +++ b/crates/ra_hir_expand/src/quote.rs | |||
@@ -16,7 +16,10 @@ macro_rules! __quote { | |||
16 | { | 16 | { |
17 | let children = $crate::__quote!($($tt)*); | 17 | let children = $crate::__quote!($($tt)*); |
18 | let subtree = tt::Subtree { | 18 | let subtree = tt::Subtree { |
19 | delimiter: Some(tt::Delimiter::$delim), | 19 | delimiter: Some(tt::Delimiter { |
20 | kind: tt::DelimiterKind::$delim, | ||
21 | id: tt::TokenId::unspecified(), | ||
22 | }), | ||
20 | token_trees: $crate::quote::IntoTt::to_tokens(children), | 23 | token_trees: $crate::quote::IntoTt::to_tokens(children), |
21 | }; | 24 | }; |
22 | subtree | 25 | subtree |
@@ -29,6 +32,7 @@ macro_rules! __quote { | |||
29 | tt::Leaf::Punct(tt::Punct { | 32 | tt::Leaf::Punct(tt::Punct { |
30 | char: $first, | 33 | char: $first, |
31 | spacing: tt::Spacing::Alone, | 34 | spacing: tt::Spacing::Alone, |
35 | id: tt::TokenId::unspecified(), | ||
32 | }).into() | 36 | }).into() |
33 | ] | 37 | ] |
34 | } | 38 | } |
@@ -40,10 +44,12 @@ macro_rules! __quote { | |||
40 | tt::Leaf::Punct(tt::Punct { | 44 | tt::Leaf::Punct(tt::Punct { |
41 | char: $first, | 45 | char: $first, |
42 | spacing: tt::Spacing::Joint, | 46 | spacing: tt::Spacing::Joint, |
47 | id: tt::TokenId::unspecified(), | ||
43 | }).into(), | 48 | }).into(), |
44 | tt::Leaf::Punct(tt::Punct { | 49 | tt::Leaf::Punct(tt::Punct { |
45 | char: $sec, | 50 | char: $sec, |
46 | spacing: tt::Spacing::Alone, | 51 | spacing: tt::Spacing::Alone, |
52 | id: tt::TokenId::unspecified(), | ||
47 | }).into() | 53 | }).into() |
48 | ] | 54 | ] |
49 | } | 55 | } |
@@ -179,15 +185,15 @@ macro_rules! impl_to_to_tokentrees { | |||
179 | } | 185 | } |
180 | 186 | ||
181 | impl_to_to_tokentrees! { | 187 | impl_to_to_tokentrees! { |
182 | u32 => self { tt::Literal{text: self.to_string().into()} }; | 188 | u32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()} }; |
183 | usize => self { tt::Literal{text: self.to_string().into()}}; | 189 | usize => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; |
184 | i32 => self { tt::Literal{text: self.to_string().into()}}; | 190 | i32 => self { tt::Literal{text: self.to_string().into(), id: tt::TokenId::unspecified()}}; |
185 | tt::Leaf => self { self }; | 191 | tt::Leaf => self { self }; |
186 | tt::Literal => self { self }; | 192 | tt::Literal => self { self }; |
187 | tt::Ident => self { self }; | 193 | tt::Ident => self { self }; |
188 | tt::Punct => self { self }; | 194 | tt::Punct => self { self }; |
189 | &str => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into()}}; | 195 | &str => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into(), id: tt::TokenId::unspecified()}}; |
190 | String => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into()}} | 196 | String => self { tt::Literal{text: format!("{:?}", self.escape_default().to_string()).into(), id: tt::TokenId::unspecified()}} |
191 | } | 197 | } |
192 | 198 | ||
193 | #[cfg(test)] | 199 | #[cfg(test)] |
@@ -254,8 +260,13 @@ mod tests { | |||
254 | let fields = | 260 | let fields = |
255 | fields.iter().map(|it| quote!(#it: self.#it.clone(), ).token_trees.clone()).flatten(); | 261 | fields.iter().map(|it| quote!(#it: self.#it.clone(), ).token_trees.clone()).flatten(); |
256 | 262 | ||
257 | let list = | 263 | let list = tt::Subtree { |
258 | tt::Subtree { delimiter: Some(tt::Delimiter::Brace), token_trees: fields.collect() }; | 264 | delimiter: Some(tt::Delimiter { |
265 | kind: tt::DelimiterKind::Brace, | ||
266 | id: tt::TokenId::unspecified(), | ||
267 | }), | ||
268 | token_trees: fields.collect(), | ||
269 | }; | ||
259 | 270 | ||
260 | let quoted = quote! { | 271 | let quoted = quote! { |
261 | impl Clone for #struct_name { | 272 | impl Clone for #struct_name { |