aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ssr/src/resolving.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ssr/src/resolving.rs')
-rw-r--r--crates/ra_ssr/src/resolving.rs39
1 files changed, 33 insertions, 6 deletions
diff --git a/crates/ra_ssr/src/resolving.rs b/crates/ra_ssr/src/resolving.rs
index 75f556785..d53981737 100644
--- a/crates/ra_ssr/src/resolving.rs
+++ b/crates/ra_ssr/src/resolving.rs
@@ -3,10 +3,16 @@
3use crate::errors::error; 3use crate::errors::error;
4use crate::{parsing, SsrError}; 4use crate::{parsing, SsrError};
5use parsing::Placeholder; 5use parsing::Placeholder;
6use ra_db::FilePosition;
6use ra_syntax::{ast, SmolStr, SyntaxKind, SyntaxNode, SyntaxToken}; 7use ra_syntax::{ast, SmolStr, SyntaxKind, SyntaxNode, SyntaxToken};
7use rustc_hash::{FxHashMap, FxHashSet}; 8use rustc_hash::{FxHashMap, FxHashSet};
8use test_utils::mark; 9use test_utils::mark;
9 10
11pub(crate) struct ResolutionScope<'db> {
12 scope: hir::SemanticsScope<'db>,
13 hygiene: hir::Hygiene,
14}
15
10pub(crate) struct ResolvedRule { 16pub(crate) struct ResolvedRule {
11 pub(crate) pattern: ResolvedPattern, 17 pub(crate) pattern: ResolvedPattern,
12 pub(crate) template: Option<ResolvedPattern>, 18 pub(crate) template: Option<ResolvedPattern>,
@@ -30,12 +36,11 @@ pub(crate) struct ResolvedPath {
30impl ResolvedRule { 36impl ResolvedRule {
31 pub(crate) fn new( 37 pub(crate) fn new(
32 rule: parsing::ParsedRule, 38 rule: parsing::ParsedRule,
33 scope: &hir::SemanticsScope, 39 resolution_scope: &ResolutionScope,
34 hygiene: &hir::Hygiene,
35 index: usize, 40 index: usize,
36 ) -> Result<ResolvedRule, SsrError> { 41 ) -> Result<ResolvedRule, SsrError> {
37 let resolver = 42 let resolver =
38 Resolver { scope, hygiene, placeholders_by_stand_in: rule.placeholders_by_stand_in }; 43 Resolver { resolution_scope, placeholders_by_stand_in: rule.placeholders_by_stand_in };
39 let resolved_template = if let Some(template) = rule.template { 44 let resolved_template = if let Some(template) = rule.template {
40 Some(resolver.resolve_pattern_tree(template)?) 45 Some(resolver.resolve_pattern_tree(template)?)
41 } else { 46 } else {
@@ -57,8 +62,7 @@ impl ResolvedRule {
57} 62}
58 63
59struct Resolver<'a, 'db> { 64struct Resolver<'a, 'db> {
60 scope: &'a hir::SemanticsScope<'db>, 65 resolution_scope: &'a ResolutionScope<'db>,
61 hygiene: &'a hir::Hygiene,
62 placeholders_by_stand_in: FxHashMap<SmolStr, parsing::Placeholder>, 66 placeholders_by_stand_in: FxHashMap<SmolStr, parsing::Placeholder>,
63} 67}
64 68
@@ -104,6 +108,7 @@ impl Resolver<'_, '_> {
104 && !self.path_contains_placeholder(&path) 108 && !self.path_contains_placeholder(&path)
105 { 109 {
106 let resolution = self 110 let resolution = self
111 .resolution_scope
107 .resolve_path(&path) 112 .resolve_path(&path)
108 .ok_or_else(|| error!("Failed to resolve path `{}`", node.text()))?; 113 .ok_or_else(|| error!("Failed to resolve path `{}`", node.text()))?;
109 resolved_paths.insert(node, ResolvedPath { resolution, depth }); 114 resolved_paths.insert(node, ResolvedPath { resolution, depth });
@@ -131,9 +136,31 @@ impl Resolver<'_, '_> {
131 } 136 }
132 false 137 false
133 } 138 }
139}
140
141impl<'db> ResolutionScope<'db> {
142 pub(crate) fn new(
143 sema: &hir::Semantics<'db, ra_ide_db::RootDatabase>,
144 lookup_context: FilePosition,
145 ) -> ResolutionScope<'db> {
146 use ra_syntax::ast::AstNode;
147 let file = sema.parse(lookup_context.file_id);
148 // Find a node at the requested position, falling back to the whole file.
149 let node = file
150 .syntax()
151 .token_at_offset(lookup_context.offset)
152 .left_biased()
153 .map(|token| token.parent())
154 .unwrap_or_else(|| file.syntax().clone());
155 let scope = sema.scope(&node);
156 ResolutionScope {
157 scope,
158 hygiene: hir::Hygiene::new(sema.db, lookup_context.file_id.into()),
159 }
160 }
134 161
135 fn resolve_path(&self, path: &ast::Path) -> Option<hir::PathResolution> { 162 fn resolve_path(&self, path: &ast::Path) -> Option<hir::PathResolution> {
136 let hir_path = hir::Path::from_src(path.clone(), self.hygiene)?; 163 let hir_path = hir::Path::from_src(path.clone(), &self.hygiene)?;
137 // First try resolving the whole path. This will work for things like 164 // First try resolving the whole path. This will work for things like
138 // `std::collections::HashMap`, but will fail for things like 165 // `std::collections::HashMap`, but will fail for things like
139 // `std::collections::HashMap::new`. 166 // `std::collections::HashMap::new`.