Skip to content

Commit 6ca8538

Browse files
committed
ssr: Keep track of text range _within_ matched SyntaxNode
1 parent 9e7fe56 commit 6ca8538

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

crates/ssr/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ impl SsrMatches {
327327

328328
impl Match {
329329
pub fn matched_text(&self) -> String {
330-
let range_inside_match = TextRange::up_to(self.range.range.len());
331-
self.matched_node.text().slice(range_inside_match).to_string()
330+
self.matched_node.text().slice(self.range_in_match).to_string()
332331
}
333332
}
334333

crates/ssr/src/matching.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use ide_db::base_db::FileRange;
1111
use rustc_hash::FxHashMap;
1212
use std::{cell::Cell, iter::Peekable};
1313
use syntax::ast::{AstNode, AstToken};
14-
use syntax::{ast, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken};
14+
use syntax::{
15+
ast, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken, TextRange,
16+
};
1517
use test_utils::mark;
1618

1719
// Creates a match error. If we're currently attempting to match some code that we thought we were
@@ -46,6 +48,7 @@ macro_rules! fail_match {
4648
#[derive(Debug)]
4749
pub struct Match {
4850
pub(crate) range: FileRange,
51+
pub(crate) range_in_match: TextRange,
4952
pub(crate) matched_node: SyntaxNode,
5053
pub(crate) placeholder_values: FxHashMap<Var, PlaceholderMatch>,
5154
pub(crate) ignored_comments: Vec<ast::Comment>,
@@ -154,8 +157,12 @@ impl<'db, 'sema> Matcher<'db, 'sema> {
154157

155158
dbg!(&matched_range, code);
156159

157-
let range = match matched_range {
158-
Ok(()) => sema.original_range(code),
160+
let (range, range_in_match) = match matched_range {
161+
Ok(()) => {
162+
let original_range = sema.original_range(code);
163+
let range_in_match = TextRange::up_to(original_range.range.len());
164+
(original_range, range_in_match)
165+
}
159166
// Err(MatchFailed::Partial(r)) => r
160167
Err(TokenMatchFailed::EarlyStop(end)) => {
161168
if code.kind() != SyntaxKind::LET_STMT {
@@ -177,14 +184,16 @@ impl<'db, 'sema> Matcher<'db, 'sema> {
177184
let text_range = original_range.range;
178185
assert!(text_range.contains(end));
179186
original_range.range = syntax::TextRange::new(text_range.start(), end);
180-
original_range
187+
let range_in_match = TextRange::up_to(original_range.range.len());
188+
(original_range, range_in_match)
181189
}
182190
Err(TokenMatchFailed::Failed(e)) => return Err(e),
183191
};
184192

185193
match_state.validate_range(&range)?;
186194
let mut the_match = Match {
187195
range,
196+
range_in_match,
188197
matched_node: code.clone(),
189198
placeholder_values: FxHashMap::default(),
190199
ignored_comments: Vec::new(),

0 commit comments

Comments
 (0)