Skip to main content

ide_ssr/
matching.rs

1//! This module is responsible for matching a search pattern against a node in the AST. In the
2//! process of matching, placeholder values are recorded.
3
4use crate::{
5    SsrMatches,
6    parsing::{Constraint, NodeKind, Placeholder, Var},
7    resolving::{ResolvedPattern, ResolvedRule, UfcsCallInfo},
8};
9use hir::{FileRange, FindPathConfig, Semantics};
10use ide_db::{FxHashMap, base_db::all_crates};
11use std::{cell::Cell, iter::Peekable};
12use syntax::{
13    SmolStr, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken,
14    ast::{self, AstNode, AstToken, HasGenericArgs},
15};
16
17// Creates a match error. If we're currently attempting to match some code that we thought we were
18// going to match, as indicated by the --debug-snippet flag, then populate the reason field.
19macro_rules! match_error {
20    ($e:expr) => {{
21            MatchFailed {
22                reason: if recording_match_fail_reasons() {
23                    Some(format!("{}", $e))
24                } else {
25                    None
26                }
27            }
28    }};
29    ($fmt:expr, $($arg:tt)+) => {{
30        MatchFailed {
31            reason: if recording_match_fail_reasons() {
32                Some(format!($fmt, $($arg)+))
33            } else {
34                None
35            }
36        }
37    }};
38}
39
40// Fails the current match attempt, recording the supplied reason if we're recording match fail reasons.
41macro_rules! fail_match {
42    ($($args:tt)*) => {return Err(match_error!($($args)*))};
43}
44
45/// Information about a match that was found.
46#[derive(Debug)]
47pub struct Match {
48    pub(crate) range: FileRange,
49    pub(crate) matched_node: SyntaxNode,
50    pub(crate) placeholder_values: FxHashMap<Var, PlaceholderMatch>,
51    pub(crate) ignored_comments: Vec<ast::Comment>,
52    pub(crate) rule_index: usize,
53    /// The depth of matched_node.
54    pub(crate) depth: usize,
55    // Each path in the template rendered for the module in which the match was found.
56    pub(crate) rendered_template_paths: FxHashMap<SyntaxNode, hir::ModPath>,
57}
58
59/// Information about a placeholder bound in a match.
60#[derive(Debug)]
61pub(crate) struct PlaceholderMatch {
62    pub(crate) range: FileRange,
63    /// More matches, found within `node`.
64    pub(crate) inner_matches: SsrMatches,
65    /// How many times the code that the placeholder matched needed to be dereferenced. Will only be
66    /// non-zero if the placeholder matched to the receiver of a method call.
67    pub(crate) autoderef_count: usize,
68    pub(crate) autoref_kind: ast::SelfParamKind,
69}
70
71#[derive(Debug)]
72pub(crate) struct MatchFailureReason {
73    pub(crate) reason: String,
74}
75
76/// An "error" indicating that matching failed. Use the fail_match! macro to create and return this.
77#[derive(Clone)]
78pub(crate) struct MatchFailed {
79    /// The reason why we failed to match. Only present when debug_active true in call to
80    /// `get_match`.
81    pub(crate) reason: Option<String>,
82}
83
84/// Checks if `code` matches the search pattern found in `search_scope`, returning information about
85/// the match, if it does. Since we only do matching in this module and searching is done by the
86/// parent module, we don't populate nested matches.
87pub(crate) fn get_match<'db>(
88    debug_active: bool,
89    rule: &ResolvedRule<'db>,
90    code: &SyntaxNode,
91    restrict_range: &Option<FileRange>,
92    sema: &Semantics<'db, ide_db::RootDatabase>,
93) -> Result<Match, MatchFailed> {
94    record_match_fails_reasons_scope(debug_active, || {
95        Matcher::try_match(rule, code, restrict_range, sema)
96    })
97}
98
99/// Checks if our search pattern matches a particular node of the AST.
100struct Matcher<'db, 'sema> {
101    sema: &'sema Semantics<'db, ide_db::RootDatabase>,
102    /// If any placeholders come from anywhere outside of this range, then the match will be
103    /// rejected.
104    restrict_range: Option<FileRange>,
105    rule: &'sema ResolvedRule<'db>,
106}
107
108/// Which phase of matching we're currently performing. We do two phases because most attempted
109/// matches will fail and it means we can defer more expensive checks to the second phase.
110enum Phase<'a> {
111    /// On the first phase, we perform cheap checks. No state is mutated and nothing is recorded.
112    First,
113    /// On the second phase, we construct the `Match`. Things like what placeholders bind to is
114    /// recorded.
115    Second(&'a mut Match),
116}
117
118impl<'db, 'sema> Matcher<'db, 'sema> {
119    fn try_match(
120        rule: &ResolvedRule<'db>,
121        code: &SyntaxNode,
122        restrict_range: &Option<FileRange>,
123        sema: &'sema Semantics<'db, ide_db::RootDatabase>,
124    ) -> Result<Match, MatchFailed> {
125        let match_state = Matcher { sema, restrict_range: *restrict_range, rule };
126        // First pass at matching, where we check that node types and idents match.
127        match_state.attempt_match_node(&mut Phase::First, &rule.pattern.node, code)?;
128        let file_range = sema
129            .original_range_opt(code)
130            .ok_or(MatchFailed { reason: Some("def site definition".to_owned()) })?;
131        match_state.validate_range(&file_range)?;
132        let mut the_match = Match {
133            range: file_range,
134            matched_node: code.clone(),
135            placeholder_values: FxHashMap::default(),
136            ignored_comments: Vec::new(),
137            rule_index: rule.index,
138            depth: 0,
139            rendered_template_paths: FxHashMap::default(),
140        };
141        // Second matching pass, where we record placeholder matches, ignored comments and maybe do
142        // any other more expensive checks that we didn't want to do on the first pass.
143        match_state.attempt_match_node(
144            &mut Phase::Second(&mut the_match),
145            &rule.pattern.node,
146            code,
147        )?;
148        the_match.depth = sema.ancestors_with_macros(the_match.matched_node.clone()).count();
149        if let Some(template) = &rule.template {
150            the_match.render_template_paths(template, sema)?;
151        }
152        Ok(the_match)
153    }
154
155    /// Checks that `range` is within the permitted range if any. This is applicable when we're
156    /// processing a macro expansion and we want to fail the match if we're working with a node that
157    /// didn't originate from the token tree of the macro call.
158    fn validate_range(&self, range: &FileRange) -> Result<(), MatchFailed> {
159        if let Some(restrict_range) = &self.restrict_range
160            && (restrict_range.file_id != range.file_id
161                || !restrict_range.range.contains_range(range.range))
162        {
163            fail_match!("Node originated from a macro");
164        }
165        Ok(())
166    }
167
168    fn attempt_match_node(
169        &self,
170        phase: &mut Phase<'_>,
171        pattern: &SyntaxNode,
172        code: &SyntaxNode,
173    ) -> Result<(), MatchFailed> {
174        // Handle placeholders.
175        if let Some(placeholder) = self.get_placeholder_for_node(pattern) {
176            for constraint in &placeholder.constraints {
177                self.check_constraint(constraint, code)?;
178            }
179            if let Phase::Second(matches_out) = phase {
180                let original_range = self
181                    .sema
182                    .original_range_opt(code)
183                    .ok_or(MatchFailed { reason: Some("def site definition".to_owned()) })?;
184                // We validated the range for the node when we started the match, so the placeholder
185                // probably can't fail range validation, but just to be safe...
186                self.validate_range(&original_range)?;
187                matches_out.placeholder_values.insert(
188                    placeholder.ident.clone(),
189                    PlaceholderMatch::from_range(original_range),
190                );
191            }
192            return Ok(());
193        }
194        // We allow a UFCS call to match a method call, provided they resolve to the same function.
195        if let Some(pattern_ufcs) = self.rule.pattern.ufcs_function_calls.get(pattern) {
196            if let Some(code) = ast::MethodCallExpr::cast(code.clone()) {
197                return self.attempt_match_ufcs_to_method_call(phase, pattern_ufcs, &code);
198            }
199            if let Some(code) = ast::CallExpr::cast(code.clone()) {
200                return self.attempt_match_ufcs_to_ufcs(phase, pattern_ufcs, &code);
201            }
202        }
203        if pattern.kind() != code.kind() {
204            fail_match!(
205                "Pattern had `{}` ({:?}), code had `{}` ({:?})",
206                pattern.text(),
207                pattern.kind(),
208                code.text(),
209                code.kind()
210            );
211        }
212        // Some kinds of nodes have special handling. For everything else, we fall back to default
213        // matching.
214        match code.kind() {
215            SyntaxKind::RECORD_EXPR_FIELD_LIST => {
216                self.attempt_match_record_field_list(phase, pattern, code)
217            }
218            SyntaxKind::TOKEN_TREE => self.attempt_match_token_tree(phase, pattern, code),
219            SyntaxKind::PATH => self.attempt_match_path(phase, pattern, code),
220            _ => self.attempt_match_node_children(phase, pattern, code),
221        }
222    }
223
224    fn attempt_match_node_children(
225        &self,
226        phase: &mut Phase<'_>,
227        pattern: &SyntaxNode,
228        code: &SyntaxNode,
229    ) -> Result<(), MatchFailed> {
230        self.attempt_match_sequences(
231            phase,
232            PatternIterator::new(pattern),
233            code.children_with_tokens(),
234        )
235    }
236
237    fn attempt_match_sequences(
238        &self,
239        phase: &mut Phase<'_>,
240        pattern_it: PatternIterator,
241        mut code_it: SyntaxElementChildren,
242    ) -> Result<(), MatchFailed> {
243        let mut pattern_it = pattern_it.peekable();
244        loop {
245            match phase.next_non_trivial(&mut code_it) {
246                None => {
247                    if let Some(p) = pattern_it.next() {
248                        fail_match!("Part of the pattern was unmatched: {:?}", p);
249                    }
250                    return Ok(());
251                }
252                Some(SyntaxElement::Token(c)) => {
253                    self.attempt_match_token(phase, &mut pattern_it, &c)?;
254                }
255                Some(SyntaxElement::Node(c)) => match pattern_it.next() {
256                    Some(SyntaxElement::Node(p)) => {
257                        self.attempt_match_node(phase, &p, &c)?;
258                    }
259                    Some(p) => fail_match!("Pattern wanted '{}', code has {}", p, c.text()),
260                    None => fail_match!("Pattern reached end, code has {}", c.text()),
261                },
262            }
263        }
264    }
265
266    fn attempt_match_token(
267        &self,
268        phase: &mut Phase<'_>,
269        pattern: &mut Peekable<PatternIterator>,
270        code: &syntax::SyntaxToken,
271    ) -> Result<(), MatchFailed> {
272        phase.record_ignored_comments(code);
273        // Ignore whitespace and comments.
274        if code.kind().is_trivia() {
275            return Ok(());
276        }
277        if let Some(SyntaxElement::Token(p)) = pattern.peek() {
278            // If the code has a comma and the pattern is about to close something, then accept the
279            // comma without advancing the pattern. i.e. ignore trailing commas.
280            if code.kind() == SyntaxKind::COMMA && is_closing_token(p.kind()) {
281                return Ok(());
282            }
283            // Conversely, if the pattern has a comma and the code doesn't, skip that part of the
284            // pattern and continue to match the code.
285            if p.kind() == SyntaxKind::COMMA && is_closing_token(code.kind()) {
286                pattern.next();
287            }
288        }
289        // Consume an element from the pattern and make sure it matches.
290        match pattern.next() {
291            Some(SyntaxElement::Token(p)) => {
292                if p.kind() != code.kind() || p.text() != code.text() {
293                    fail_match!(
294                        "Pattern wanted token '{}' ({:?}), but code had token '{}' ({:?})",
295                        p.text(),
296                        p.kind(),
297                        code.text(),
298                        code.kind()
299                    )
300                }
301            }
302            Some(SyntaxElement::Node(p)) => {
303                // Not sure if this is actually reachable.
304                fail_match!(
305                    "Pattern wanted {:?}, but code had token '{}' ({:?})",
306                    p,
307                    code.text(),
308                    code.kind()
309                );
310            }
311            None => {
312                fail_match!("Pattern exhausted, while code remains: `{}`", code.text());
313            }
314        }
315        Ok(())
316    }
317
318    fn check_constraint(
319        &self,
320        constraint: &Constraint,
321        code: &SyntaxNode,
322    ) -> Result<(), MatchFailed> {
323        match constraint {
324            Constraint::Kind(kind) => {
325                kind.matches(code)?;
326            }
327            Constraint::Not(sub) => {
328                if self.check_constraint(sub, code).is_ok() {
329                    fail_match!("Constraint {:?} failed for '{}'", constraint, code.text());
330                }
331            }
332        }
333        Ok(())
334    }
335
336    /// Paths are matched based on whether they refer to the same thing, even if they're written
337    /// differently.
338    fn attempt_match_path(
339        &self,
340        phase: &mut Phase<'_>,
341        pattern: &SyntaxNode,
342        code: &SyntaxNode,
343    ) -> Result<(), MatchFailed> {
344        if let Some(pattern_resolved) = self.rule.pattern.resolved_paths.get(pattern) {
345            let pattern_path = ast::Path::cast(pattern.clone()).unwrap();
346            let code_path = ast::Path::cast(code.clone()).unwrap();
347            if let (Some(pattern_segment), Some(code_segment)) =
348                (pattern_path.segment(), code_path.segment())
349            {
350                // Match everything within the segment except for the name-ref, which is handled
351                // separately via comparing what the path resolves to below.
352                self.attempt_match_opt(
353                    phase,
354                    pattern_segment.generic_arg_list(),
355                    code_segment.generic_arg_list(),
356                )?;
357                self.attempt_match_opt(
358                    phase,
359                    pattern_segment.parenthesized_arg_list(),
360                    code_segment.parenthesized_arg_list(),
361                )?;
362            }
363            if matches!(phase, Phase::Second(_)) {
364                let resolution = self
365                    .sema
366                    .resolve_path(&code_path)
367                    .ok_or_else(|| match_error!("Failed to resolve path `{}`", code.text()))?;
368                if pattern_resolved.resolution != resolution {
369                    fail_match!("Pattern had path `{}` code had `{}`", pattern.text(), code.text());
370                }
371            }
372        } else {
373            return self.attempt_match_node_children(phase, pattern, code);
374        }
375        Ok(())
376    }
377
378    fn attempt_match_opt<T: AstNode>(
379        &self,
380        phase: &mut Phase<'_>,
381        pattern: Option<T>,
382        code: Option<T>,
383    ) -> Result<(), MatchFailed> {
384        match (pattern, code) {
385            (Some(p), Some(c)) => self.attempt_match_node(phase, p.syntax(), c.syntax()),
386            (None, None) => Ok(()),
387            (Some(p), None) => fail_match!("Pattern `{}` had nothing to match", p.syntax().text()),
388            (None, Some(c)) => {
389                fail_match!("Nothing in pattern to match code `{}`", c.syntax().text())
390            }
391        }
392    }
393
394    /// We want to allow the records to match in any order, so we have special matching logic for
395    /// them.
396    fn attempt_match_record_field_list(
397        &self,
398        phase: &mut Phase<'_>,
399        pattern: &SyntaxNode,
400        code: &SyntaxNode,
401    ) -> Result<(), MatchFailed> {
402        // Build a map keyed by field name.
403        let mut fields_by_name: FxHashMap<SmolStr, SyntaxNode> = FxHashMap::default();
404        for child in code.children() {
405            if let Some(record) = ast::RecordExprField::cast(child.clone())
406                && let Some(name) = record.field_name()
407            {
408                fields_by_name.insert(name.text().into(), child.clone());
409            }
410        }
411        for p in pattern.children_with_tokens() {
412            if let SyntaxElement::Node(p) = p
413                && let Some(name_element) = p.first_child_or_token()
414            {
415                if self.get_placeholder(&name_element).is_some() {
416                    // If the pattern is using placeholders for field names then order
417                    // independence doesn't make sense. Fall back to regular ordered
418                    // matching.
419                    return self.attempt_match_node_children(phase, pattern, code);
420                }
421                if let Some(ident) = only_ident(name_element) {
422                    let code_record = fields_by_name.remove(ident.text()).ok_or_else(|| {
423                        match_error!("Placeholder has record field '{}', but code doesn't", ident)
424                    })?;
425                    self.attempt_match_node(phase, &p, &code_record)?;
426                }
427            }
428        }
429        if let Some(unmatched_fields) = fields_by_name.keys().next() {
430            fail_match!(
431                "{} field(s) of a record literal failed to match, starting with {}",
432                fields_by_name.len(),
433                unmatched_fields
434            );
435        }
436        Ok(())
437    }
438
439    /// Outside of token trees, a placeholder can only match a single AST node, whereas in a token
440    /// tree it can match a sequence of tokens. Note, that this code will only be used when the
441    /// pattern matches the macro invocation. For matches within the macro call, we'll already have
442    /// expanded the macro.
443    fn attempt_match_token_tree(
444        &self,
445        phase: &mut Phase<'_>,
446        pattern: &SyntaxNode,
447        code: &syntax::SyntaxNode,
448    ) -> Result<(), MatchFailed> {
449        let mut pattern = PatternIterator::new(pattern).peekable();
450        let mut children = code.children_with_tokens();
451        while let Some(child) = children.next() {
452            if let Some(placeholder) = pattern.peek().and_then(|p| self.get_placeholder(p)) {
453                pattern.next();
454                let next_pattern_token = pattern
455                    .peek()
456                    .and_then(|p| match p {
457                        SyntaxElement::Token(t) => Some(t.clone()),
458                        SyntaxElement::Node(n) => n.first_token(),
459                    })
460                    .map(|p| p.text().to_owned());
461                let first_matched_token = child.clone();
462                let mut last_matched_token = child;
463                // Read code tokens util we reach one equal to the next token from our pattern
464                // or we reach the end of the token tree.
465                for next in &mut children {
466                    match &next {
467                        SyntaxElement::Token(t) => {
468                            if Some(t.to_string()) == next_pattern_token {
469                                pattern.next();
470                                break;
471                            }
472                        }
473                        SyntaxElement::Node(n) => {
474                            if let Some(first_token) = n.first_token()
475                                && Some(first_token.text()) == next_pattern_token.as_deref()
476                                && let Some(SyntaxElement::Node(p)) = pattern.next()
477                            {
478                                // We have a subtree that starts with the next token in our pattern.
479                                self.attempt_match_token_tree(phase, &p, n)?;
480                                break;
481                            }
482                        }
483                    };
484                    last_matched_token = next;
485                }
486                if let Phase::Second(match_out) = phase {
487                    match_out.placeholder_values.insert(
488                        placeholder.ident.clone(),
489                        PlaceholderMatch::from_range(FileRange {
490                            file_id: self
491                                .sema
492                                .original_range_opt(code)
493                                .ok_or(MatchFailed {
494                                    reason: Some("def site definition".to_owned()),
495                                })?
496                                .file_id,
497                            range: first_matched_token
498                                .text_range()
499                                .cover(last_matched_token.text_range()),
500                        }),
501                    );
502                }
503                continue;
504            }
505            // Match literal (non-placeholder) tokens.
506            match child {
507                SyntaxElement::Token(token) => {
508                    self.attempt_match_token(phase, &mut pattern, &token)?;
509                }
510                SyntaxElement::Node(node) => match pattern.next() {
511                    Some(SyntaxElement::Node(p)) => {
512                        self.attempt_match_token_tree(phase, &p, &node)?;
513                    }
514                    Some(SyntaxElement::Token(p)) => fail_match!(
515                        "Pattern has token '{}', code has subtree '{}'",
516                        p.text(),
517                        node.text()
518                    ),
519                    None => fail_match!("Pattern has nothing, code has '{}'", node.text()),
520                },
521            }
522        }
523        if let Some(p) = pattern.next() {
524            fail_match!("Reached end of token tree in code, but pattern still has {:?}", p);
525        }
526        Ok(())
527    }
528
529    fn attempt_match_ufcs_to_method_call(
530        &self,
531        phase: &mut Phase<'_>,
532        pattern_ufcs: &UfcsCallInfo<'db>,
533        code: &ast::MethodCallExpr,
534    ) -> Result<(), MatchFailed> {
535        use ast::HasArgList;
536        let code_resolved_function = self
537            .sema
538            .resolve_method_call(code)
539            .ok_or_else(|| match_error!("Failed to resolve method call"))?;
540        if pattern_ufcs.function != code_resolved_function {
541            fail_match!("Method call resolved to a different function");
542        }
543        // Check arguments.
544        let mut pattern_args = pattern_ufcs
545            .call_expr
546            .arg_list()
547            .ok_or_else(|| match_error!("Pattern function call has no args"))?
548            .args();
549        // If the function we're calling takes a self parameter, then we store additional
550        // information on the placeholder match about autoderef and autoref. This allows us to use
551        // the placeholder in a context where autoderef and autoref don't apply.
552        if code_resolved_function.self_param(self.sema.db).is_some() {
553            if let (Some(pattern_type), Some(expr)) =
554                (&pattern_ufcs.qualifier_type, &code.receiver())
555            {
556                let deref_count = self.check_expr_type(pattern_type, expr)?;
557                let pattern_receiver = pattern_args.next();
558                self.attempt_match_opt(phase, pattern_receiver.clone(), code.receiver())?;
559                if let Phase::Second(match_out) = phase
560                    && let Some(placeholder_value) = pattern_receiver
561                        .and_then(|n| self.get_placeholder_for_node(n.syntax()))
562                        .and_then(|placeholder| {
563                            match_out.placeholder_values.get_mut(&placeholder.ident)
564                        })
565                {
566                    placeholder_value.autoderef_count = deref_count;
567                    placeholder_value.autoref_kind = self
568                        .sema
569                        .resolve_method_call_as_callable(code)
570                        .and_then(|callable| {
571                            let (self_param, _) = callable.receiver_param(self.sema.db)?;
572                            Some(self.sema.source(self_param)?.value.kind())
573                        })
574                        .unwrap_or(ast::SelfParamKind::Owned);
575                }
576            }
577        } else {
578            self.attempt_match_opt(phase, pattern_args.next(), code.receiver())?;
579        }
580        let mut code_args =
581            code.arg_list().ok_or_else(|| match_error!("Code method call has no args"))?.args();
582        loop {
583            match (pattern_args.next(), code_args.next()) {
584                (None, None) => return Ok(()),
585                (p, c) => self.attempt_match_opt(phase, p, c)?,
586            }
587        }
588    }
589
590    fn attempt_match_ufcs_to_ufcs(
591        &self,
592        phase: &mut Phase<'_>,
593        pattern_ufcs: &UfcsCallInfo<'db>,
594        code: &ast::CallExpr,
595    ) -> Result<(), MatchFailed> {
596        use ast::HasArgList;
597        // Check that the first argument is the expected type.
598        if let (Some(pattern_type), Some(expr)) = (
599            &pattern_ufcs.qualifier_type,
600            &code.arg_list().and_then(|code_args| code_args.args().next()),
601        ) {
602            self.check_expr_type(pattern_type, expr)?;
603        }
604        self.attempt_match_node_children(phase, pattern_ufcs.call_expr.syntax(), code.syntax())
605    }
606
607    /// Verifies that `expr` matches `pattern_type`, possibly after dereferencing some number of
608    /// times. Returns the number of times it needed to be dereferenced.
609    fn check_expr_type(
610        &self,
611        pattern_type: &hir::Type<'db>,
612        expr: &ast::Expr,
613    ) -> Result<usize, MatchFailed> {
614        use hir::HirDisplay;
615        let code_type = self
616            .sema
617            .type_of_expr(expr)
618            .ok_or_else(|| {
619                match_error!("Failed to get receiver type for `{}`", expr.syntax().text())
620            })?
621            .original;
622        let krate = self.sema.scope(expr.syntax()).map(|it| it.krate()).unwrap_or_else(|| {
623            hir::Crate::from(*all_crates(self.sema.db).last().expect("no crate graph present"))
624        });
625
626        code_type
627            .autoderef(self.sema.db)
628            .enumerate()
629            .find(|(_, deref_code_type)| pattern_type == deref_code_type)
630            .map(|(count, _)| count)
631            .ok_or_else(|| {
632                let display_target = krate.to_display_target(self.sema.db);
633                // Temporary needed to make the borrow checker happy.
634                match_error!(
635                    "Pattern type `{}` didn't match code type `{}`",
636                    pattern_type.display(self.sema.db, display_target),
637                    code_type.display(self.sema.db, display_target)
638                )
639            })
640    }
641
642    fn get_placeholder_for_node(&self, node: &SyntaxNode) -> Option<&Placeholder> {
643        self.get_placeholder(&SyntaxElement::Node(node.clone()))
644    }
645
646    fn get_placeholder(&self, element: &SyntaxElement) -> Option<&Placeholder> {
647        only_ident(element.clone()).and_then(|ident| self.rule.get_placeholder(&ident))
648    }
649}
650
651impl Match {
652    fn render_template_paths<'db>(
653        &mut self,
654        template: &ResolvedPattern<'db>,
655        sema: &Semantics<'db, ide_db::RootDatabase>,
656    ) -> Result<(), MatchFailed> {
657        let module = sema
658            .scope(&self.matched_node)
659            .ok_or_else(|| match_error!("Matched node isn't in a module"))?
660            .module();
661        for (path, resolved_path) in &template.resolved_paths {
662            if let hir::PathResolution::Def(module_def) = resolved_path.resolution {
663                let cfg = FindPathConfig {
664                    prefer_no_std: false,
665                    prefer_prelude: true,
666                    prefer_absolute: false,
667                    allow_unstable: true,
668                };
669                let mod_path = module.find_path(sema.db, module_def, cfg).ok_or_else(|| {
670                    match_error!("Failed to render template path `{}` at match location")
671                })?;
672                self.rendered_template_paths.insert(path.clone(), mod_path);
673            }
674        }
675        Ok(())
676    }
677}
678
679impl Phase<'_> {
680    fn next_non_trivial(&mut self, code_it: &mut SyntaxElementChildren) -> Option<SyntaxElement> {
681        loop {
682            let c = code_it.next();
683            if let Some(SyntaxElement::Token(t)) = &c {
684                self.record_ignored_comments(t);
685                if t.kind().is_trivia() {
686                    continue;
687                }
688            }
689            return c;
690        }
691    }
692
693    fn record_ignored_comments(&mut self, token: &SyntaxToken) {
694        if token.kind() == SyntaxKind::COMMENT
695            && let Phase::Second(match_out) = self
696            && let Some(comment) = ast::Comment::cast(token.clone())
697        {
698            match_out.ignored_comments.push(comment);
699        }
700    }
701}
702
703fn is_closing_token(kind: SyntaxKind) -> bool {
704    kind == SyntaxKind::R_PAREN || kind == SyntaxKind::R_CURLY || kind == SyntaxKind::R_BRACK
705}
706
707pub(crate) fn record_match_fails_reasons_scope<F, T>(debug_active: bool, f: F) -> T
708where
709    F: Fn() -> T,
710{
711    RECORDING_MATCH_FAIL_REASONS.with(|c| c.set(debug_active));
712    let res = f();
713    RECORDING_MATCH_FAIL_REASONS.with(|c| c.set(false));
714    res
715}
716
717// For performance reasons, we don't want to record the reason why every match fails, only the bit
718// of code that the user indicated they thought would match. We use a thread local to indicate when
719// we are trying to match that bit of code. This saves us having to pass a boolean into all the bits
720// of code that can make the decision to not match.
721thread_local! {
722    pub static RECORDING_MATCH_FAIL_REASONS: Cell<bool> = const { Cell::new(false) };
723}
724
725fn recording_match_fail_reasons() -> bool {
726    RECORDING_MATCH_FAIL_REASONS.with(|c| c.get())
727}
728
729impl PlaceholderMatch {
730    fn from_range(range: FileRange) -> Self {
731        Self {
732            range,
733            inner_matches: SsrMatches::default(),
734            autoderef_count: 0,
735            autoref_kind: ast::SelfParamKind::Owned,
736        }
737    }
738}
739
740impl NodeKind {
741    fn matches(&self, node: &SyntaxNode) -> Result<(), MatchFailed> {
742        let ok = match self {
743            Self::Literal => {
744                cov_mark::hit!(literal_constraint);
745                ast::Literal::can_cast(node.kind())
746            }
747        };
748        if !ok {
749            fail_match!("Code '{}' isn't of kind {:?}", node.text(), self);
750        }
751        Ok(())
752    }
753}
754
755// If `node` contains nothing but an ident then return it, otherwise return None.
756fn only_ident(element: SyntaxElement) -> Option<SyntaxToken> {
757    match element {
758        SyntaxElement::Token(t) => {
759            if t.kind() == SyntaxKind::IDENT {
760                return Some(t);
761            }
762        }
763        SyntaxElement::Node(n) => {
764            let mut children = n.children_with_tokens();
765            if let (Some(only_child), None) = (children.next(), children.next()) {
766                return only_ident(only_child);
767            }
768        }
769    }
770    None
771}
772
773struct PatternIterator {
774    iter: SyntaxElementChildren,
775}
776
777impl Iterator for PatternIterator {
778    type Item = SyntaxElement;
779
780    fn next(&mut self) -> Option<SyntaxElement> {
781        self.iter.find(|element| !element.kind().is_trivia())
782    }
783}
784
785impl PatternIterator {
786    fn new(parent: &SyntaxNode) -> Self {
787        Self { iter: parent.children_with_tokens() }
788    }
789}
790
791#[cfg(test)]
792mod tests {
793    use crate::{MatchFinder, SsrRule};
794
795    #[test]
796    fn parse_match_replace() {
797        let rule: SsrRule = "foo($x) ==>> bar($x)".parse().unwrap();
798        let input = "fn foo() {} fn bar() {} fn main() { foo(1+2); }";
799
800        let (db, position, selections) = crate::tests::single_file(input);
801        hir::attach_db(&db, || {
802            let position = ide_db::FilePosition {
803                file_id: position.file_id.file_id(&db),
804                offset: position.offset,
805            };
806            let mut match_finder = MatchFinder::in_context(
807                &db,
808                position,
809                selections
810                    .into_iter()
811                    .map(|frange| ide_db::FileRange {
812                        file_id: frange.file_id.file_id(&db),
813                        range: frange.range,
814                    })
815                    .collect(),
816            )
817            .unwrap();
818            match_finder.add_rule(rule).unwrap();
819            let matches = match_finder.matches();
820            assert_eq!(matches.matches.len(), 1);
821            assert_eq!(matches.matches[0].matched_node.text(), "foo(1+2)");
822            assert_eq!(matches.matches[0].placeholder_values.len(), 1);
823
824            let edits = match_finder.edits();
825            assert_eq!(edits.len(), 1);
826            let edit = &edits[&position.file_id];
827            let mut after = input.to_owned();
828            edit.apply(&mut after);
829            assert_eq!(after, "fn foo() {} fn bar() {} fn main() { bar(1+2); }");
830        });
831    }
832}