diff options
Diffstat (limited to 'crates/ra_ssr/src/lib.rs')
-rw-r--r-- | crates/ra_ssr/src/lib.rs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs index 6d578610b..a0a5c9762 100644 --- a/crates/ra_ssr/src/lib.rs +++ b/crates/ra_ssr/src/lib.rs | |||
@@ -13,11 +13,12 @@ mod errors; | |||
13 | #[cfg(test)] | 13 | #[cfg(test)] |
14 | mod tests; | 14 | mod tests; |
15 | 15 | ||
16 | use crate::errors::bail; | ||
16 | pub use crate::errors::SsrError; | 17 | pub use crate::errors::SsrError; |
17 | pub use crate::matching::Match; | 18 | pub use crate::matching::Match; |
18 | use crate::matching::MatchFailureReason; | 19 | use crate::matching::MatchFailureReason; |
19 | use hir::Semantics; | 20 | use hir::Semantics; |
20 | use ra_db::{FileId, FileRange}; | 21 | use ra_db::{FileId, FilePosition, FileRange}; |
21 | use ra_ide_db::source_change::SourceFileEdit; | 22 | use ra_ide_db::source_change::SourceFileEdit; |
22 | use ra_syntax::{ast, AstNode, SyntaxNode, TextRange}; | 23 | use ra_syntax::{ast, AstNode, SyntaxNode, TextRange}; |
23 | use rustc_hash::FxHashMap; | 24 | use rustc_hash::FxHashMap; |
@@ -51,10 +52,35 @@ pub struct MatchFinder<'db> { | |||
51 | } | 52 | } |
52 | 53 | ||
53 | impl<'db> MatchFinder<'db> { | 54 | impl<'db> MatchFinder<'db> { |
54 | pub fn new(db: &'db ra_ide_db::RootDatabase) -> MatchFinder<'db> { | 55 | /// Constructs a new instance where names will be looked up as if they appeared at |
56 | /// `lookup_context`. | ||
57 | pub fn in_context( | ||
58 | db: &'db ra_ide_db::RootDatabase, | ||
59 | _lookup_context: FilePosition, | ||
60 | ) -> MatchFinder<'db> { | ||
61 | // FIXME: Use lookup_context | ||
55 | MatchFinder { sema: Semantics::new(db), rules: Vec::new() } | 62 | MatchFinder { sema: Semantics::new(db), rules: Vec::new() } |
56 | } | 63 | } |
57 | 64 | ||
65 | /// Constructs an instance using the start of the first file in `db` as the lookup context. | ||
66 | pub fn at_first_file(db: &'db ra_ide_db::RootDatabase) -> Result<MatchFinder<'db>, SsrError> { | ||
67 | use ra_db::SourceDatabaseExt; | ||
68 | use ra_ide_db::symbol_index::SymbolsDatabase; | ||
69 | if let Some(first_file_id) = db | ||
70 | .local_roots() | ||
71 | .iter() | ||
72 | .next() | ||
73 | .and_then(|root| db.source_root(root.clone()).iter().next()) | ||
74 | { | ||
75 | Ok(MatchFinder::in_context( | ||
76 | db, | ||
77 | FilePosition { file_id: first_file_id, offset: 0.into() }, | ||
78 | )) | ||
79 | } else { | ||
80 | bail!("No files to search"); | ||
81 | } | ||
82 | } | ||
83 | |||
58 | /// Adds a rule to be applied. The order in which rules are added matters. Earlier rules take | 84 | /// Adds a rule to be applied. The order in which rules are added matters. Earlier rules take |
59 | /// precedence. If a node is matched by an earlier rule, then later rules won't be permitted to | 85 | /// precedence. If a node is matched by an earlier rule, then later rules won't be permitted to |
60 | /// match to it. | 86 | /// match to it. |