1 //! This module allows building an SSR MatchFinder by parsing the SSR rule
5 base_db::{FilePosition, FileRange, SourceDatabase},
9 ast::{self, AstNode, AstToken},
13 use crate::MatchFinder;
15 /// Attempts to build an SSR MatchFinder from a comment at the given file
16 /// range. If successful, returns the MatchFinder and a TextRange covering
18 pub fn ssr_from_comment(db: &RootDatabase, frange: FileRange) -> Option<(MatchFinder, TextRange)> {
20 let file = db.parse(frange.file_id);
21 file.tree().syntax().token_at_offset(frange.range.start()).find_map(ast::Comment::cast)
23 let comment_text_without_prefix = comment.text().strip_prefix(comment.prefix()).unwrap();
24 let ssr_rule = comment_text_without_prefix.parse().ok()?;
26 let lookup_context = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
28 let mut match_finder = MatchFinder::in_context(db, lookup_context, vec![]).ok()?;
29 match_finder.add_rule(ssr_rule).ok()?;
31 Some((match_finder, comment.syntax().text_range()))