diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-20 06:44:37 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-20 06:44:37 +0000 |
commit | 4dae39a609fb58b42cbe152e66d30fe6471a34d7 (patch) | |
tree | 166ee3c45f99611f7c20740c2a246f5b3bed41a3 /crates/ra_hir_expand/src/lib.rs | |
parent | eec68e6f451ee7675ce2cb29b696df091e6aed13 (diff) | |
parent | 36e3fc9d5413f7e6e17e82867aae1318645880a3 (diff) |
Merge #2319
2319: Rename Source::ast -> Source::value r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_expand/src/lib.rs')
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index cfe7e6d15..6ca4bc7a3 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -166,19 +166,19 @@ impl ExpansionInfo { | |||
166 | pub fn map_token_down(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { | 166 | pub fn map_token_down(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { |
167 | assert_eq!(token.file_id, self.arg.file_id); | 167 | assert_eq!(token.file_id, self.arg.file_id); |
168 | let range = | 168 | let range = |
169 | token.ast.text_range().checked_sub(self.arg.ast.syntax().text_range().start())?; | 169 | token.value.text_range().checked_sub(self.arg.value.syntax().text_range().start())?; |
170 | let token_id = self.macro_arg.1.token_by_range(range)?; | 170 | let token_id = self.macro_arg.1.token_by_range(range)?; |
171 | let token_id = self.macro_def.0.map_id_down(token_id); | 171 | let token_id = self.macro_def.0.map_id_down(token_id); |
172 | 172 | ||
173 | let range = self.exp_map.range_by_token(token_id)?; | 173 | let range = self.exp_map.range_by_token(token_id)?; |
174 | 174 | ||
175 | let token = algo::find_covering_element(&self.expanded.ast, range).into_token()?; | 175 | let token = algo::find_covering_element(&self.expanded.value, range).into_token()?; |
176 | 176 | ||
177 | Some(self.expanded.with_ast(token)) | 177 | Some(self.expanded.with_ast(token)) |
178 | } | 178 | } |
179 | 179 | ||
180 | pub fn map_token_up(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { | 180 | pub fn map_token_up(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { |
181 | let token_id = self.exp_map.token_by_range(token.ast.text_range())?; | 181 | let token_id = self.exp_map.token_by_range(token.value.text_range())?; |
182 | 182 | ||
183 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); | 183 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); |
184 | let (token_map, tt) = match origin { | 184 | let (token_map, tt) = match origin { |
@@ -188,8 +188,8 @@ impl ExpansionInfo { | |||
188 | 188 | ||
189 | let range = token_map.range_by_token(token_id)?; | 189 | let range = token_map.range_by_token(token_id)?; |
190 | let token = algo::find_covering_element( | 190 | let token = algo::find_covering_element( |
191 | tt.ast.syntax(), | 191 | tt.value.syntax(), |
192 | range + tt.ast.syntax().text_range().start(), | 192 | range + tt.value.syntax().text_range().start(), |
193 | ) | 193 | ) |
194 | .into_token()?; | 194 | .into_token()?; |
195 | Some(tt.with_ast(token)) | 195 | Some(tt.with_ast(token)) |
@@ -240,30 +240,34 @@ impl<N: AstNode> AstId<N> { | |||
240 | } | 240 | } |
241 | } | 241 | } |
242 | 242 | ||
243 | /// FIXME: https://github.com/matklad/with ? | 243 | /// `Source<T>` stores a value of `T` inside a particular file/syntax tree. |
244 | /// | ||
245 | /// Typical usages are: | ||
246 | /// | ||
247 | /// * `Source<SyntaxNode>` -- syntax node in a file | ||
248 | /// * `Source<ast::FnDef>` -- ast node in a file | ||
249 | /// * `Source<TextUnit>` -- offset in a file | ||
244 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] | 250 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] |
245 | pub struct Source<T> { | 251 | pub struct Source<T> { |
246 | pub file_id: HirFileId, | 252 | pub file_id: HirFileId, |
247 | // FIXME: this stores all kind of things, not only `ast`. | 253 | pub value: T, |
248 | // There should be a better name... | ||
249 | pub ast: T, | ||
250 | } | 254 | } |
251 | 255 | ||
252 | impl<T> Source<T> { | 256 | impl<T> Source<T> { |
253 | pub fn new(file_id: HirFileId, ast: T) -> Source<T> { | 257 | pub fn new(file_id: HirFileId, value: T) -> Source<T> { |
254 | Source { file_id, ast } | 258 | Source { file_id, value } |
255 | } | 259 | } |
256 | 260 | ||
257 | // Similarly, naming here is stupid... | 261 | // Similarly, naming here is stupid... |
258 | pub fn with_ast<U>(&self, ast: U) -> Source<U> { | 262 | pub fn with_ast<U>(&self, value: U) -> Source<U> { |
259 | Source::new(self.file_id, ast) | 263 | Source::new(self.file_id, value) |
260 | } | 264 | } |
261 | 265 | ||
262 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { | 266 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { |
263 | Source::new(self.file_id, f(self.ast)) | 267 | Source::new(self.file_id, f(self.value)) |
264 | } | 268 | } |
265 | pub fn as_ref(&self) -> Source<&T> { | 269 | pub fn as_ref(&self) -> Source<&T> { |
266 | self.with_ast(&self.ast) | 270 | self.with_ast(&self.value) |
267 | } | 271 | } |
268 | pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode { | 272 | pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode { |
269 | db.parse_or_expand(self.file_id).expect("source created from invalid file") | 273 | db.parse_or_expand(self.file_id).expect("source created from invalid file") |