diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 50 |
1 files changed, 16 insertions, 34 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 5db6647e3..4965f9c33 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -157,15 +157,16 @@ impl ExpandedRangeMap { | |||
157 | self.ranges.push((relative_range, token_id.clone())) | 157 | self.ranges.push((relative_range, token_id.clone())) |
158 | } | 158 | } |
159 | 159 | ||
160 | pub fn ranges(&self, to: &TokenMap) -> Vec<(TextRange, TextRange)> { | 160 | pub fn ranges(&self, to: &TokenMap, start: TextUnit) -> Vec<(TextRange, TextRange)> { |
161 | dbg!(&self.ranges); | ||
161 | self.ranges | 162 | self.ranges |
162 | .iter() | 163 | .iter() |
163 | .filter_map(|(r, tid)| { | 164 | .filter_map(|(r, tid)| { |
164 | if to.map_id == tid.map_id() { | 165 | if to.map_id != tid.map_id() { |
165 | return None; | 166 | return None; |
166 | } | 167 | } |
167 | if let Some(to_range) = to.relative_range_of(*tid) { | 168 | if let Some(to_range) = to.relative_range_of(*tid) { |
168 | Some((*r, to_range)) | 169 | Some((*r, TextRange::from_to(to_range.start() + start, to_range.end() + start))) |
169 | } else { | 170 | } else { |
170 | None | 171 | None |
171 | } | 172 | } |
@@ -320,7 +321,6 @@ struct TtTreeSink<'a> { | |||
320 | cursor: Cursor<'a>, | 321 | cursor: Cursor<'a>, |
321 | text_pos: TextUnit, | 322 | text_pos: TextUnit, |
322 | inner: SyntaxTreeBuilder, | 323 | inner: SyntaxTreeBuilder, |
323 | range_marker: Option<(TextRange, tt::TokenId)>, | ||
324 | range_map: &'a mut ExpandedRangeMap, | 324 | range_map: &'a mut ExpandedRangeMap, |
325 | 325 | ||
326 | // Number of roots | 326 | // Number of roots |
@@ -337,7 +337,6 @@ impl<'a> TtTreeSink<'a> { | |||
337 | inner: SyntaxTreeBuilder::default(), | 337 | inner: SyntaxTreeBuilder::default(), |
338 | roots: smallvec::SmallVec::new(), | 338 | roots: smallvec::SmallVec::new(), |
339 | range_map, | 339 | range_map, |
340 | range_marker: None, | ||
341 | } | 340 | } |
342 | } | 341 | } |
343 | } | 342 | } |
@@ -362,8 +361,6 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
362 | return; | 361 | return; |
363 | } | 362 | } |
364 | 363 | ||
365 | let mut last_ident = None; | ||
366 | |||
367 | for _ in 0..n_tokens { | 364 | for _ in 0..n_tokens { |
368 | if self.cursor.eof() { | 365 | if self.cursor.eof() { |
369 | break; | 366 | break; |
@@ -371,12 +368,20 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
371 | 368 | ||
372 | match self.cursor.token_tree() { | 369 | match self.cursor.token_tree() { |
373 | Some(tt::TokenTree::Leaf(leaf)) => { | 370 | Some(tt::TokenTree::Leaf(leaf)) => { |
374 | self.cursor = self.cursor.bump(); | 371 | // Mark the range if needed |
375 | self.buf += &format!("{}", leaf); | ||
376 | |||
377 | if let tt::Leaf::Ident(ident) = leaf { | 372 | if let tt::Leaf::Ident(ident) = leaf { |
378 | last_ident = Some(ident); | 373 | if kind == IDENT { |
374 | let range = TextRange::offset_len( | ||
375 | self.text_pos + TextUnit::of_str(&self.buf), | ||
376 | TextUnit::of_str(&ident.text), | ||
377 | ); | ||
378 | let token_id = ident.id; | ||
379 | self.range_map.set(range, &token_id); | ||
380 | } | ||
379 | } | 381 | } |
382 | |||
383 | self.cursor = self.cursor.bump(); | ||
384 | self.buf += &format!("{}", leaf); | ||
380 | } | 385 | } |
381 | Some(tt::TokenTree::Subtree(subtree)) => { | 386 | Some(tt::TokenTree::Subtree(subtree)) => { |
382 | self.cursor = self.cursor.subtree().unwrap(); | 387 | self.cursor = self.cursor.subtree().unwrap(); |
@@ -396,14 +401,6 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
396 | self.buf.clear(); | 401 | self.buf.clear(); |
397 | self.inner.token(kind, text); | 402 | self.inner.token(kind, text); |
398 | 403 | ||
399 | // Mark the range if needed | ||
400 | if let Some((range, token_id)) = self.range_marker.as_mut() { | ||
401 | if let Some(ident) = last_ident { | ||
402 | *range = TextRange::offset_len(range.start(), TextUnit::of_str(&ident.text)); | ||
403 | *token_id = ident.id; | ||
404 | } | ||
405 | } | ||
406 | |||
407 | // Add whitespace between adjoint puncts | 404 | // Add whitespace between adjoint puncts |
408 | let next = self.cursor.bump(); | 405 | let next = self.cursor.bump(); |
409 | if let ( | 406 | if let ( |
@@ -421,15 +418,6 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
421 | fn start_node(&mut self, kind: SyntaxKind) { | 418 | fn start_node(&mut self, kind: SyntaxKind) { |
422 | self.inner.start_node(kind); | 419 | self.inner.start_node(kind); |
423 | 420 | ||
424 | self.range_marker = if kind == IDENT { | ||
425 | Some(( | ||
426 | TextRange::offset_len(self.text_pos, TextUnit::from_usize(0)), | ||
427 | tt::TokenId::unspecified(), | ||
428 | )) | ||
429 | } else { | ||
430 | None | ||
431 | }; | ||
432 | |||
433 | match self.roots.last_mut() { | 421 | match self.roots.last_mut() { |
434 | None | Some(0) => self.roots.push(1), | 422 | None | Some(0) => self.roots.push(1), |
435 | Some(ref mut n) => **n += 1, | 423 | Some(ref mut n) => **n += 1, |
@@ -439,12 +427,6 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
439 | fn finish_node(&mut self) { | 427 | fn finish_node(&mut self) { |
440 | self.inner.finish_node(); | 428 | self.inner.finish_node(); |
441 | *self.roots.last_mut().unwrap() -= 1; | 429 | *self.roots.last_mut().unwrap() -= 1; |
442 | |||
443 | if let Some(range) = self.range_marker { | ||
444 | if range.1 != tt::TokenId::unspecified() { | ||
445 | self.range_map.set(range.0, &range.1) | ||
446 | } | ||
447 | } | ||
448 | } | 430 | } |
449 | 431 | ||
450 | fn error(&mut self, error: ParseError) { | 432 | fn error(&mut self, error: ParseError) { |