Skip to content

Commit 7aa627f

Browse files
committed
Move more stuff to SourceBinder
1 parent a3d6ddb commit 7aa627f

File tree

3 files changed

+70
-66
lines changed

3 files changed

+70
-66
lines changed

crates/ra_hir/src/from_source.rs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,13 @@
11
//! Finds a corresponding hir data structure for a syntax node in a specific
22
//! file.
33

4-
use hir_def::{
5-
child_by_source::ChildBySource, keys, nameres::ModuleSource, GenericDefId, ModuleId,
6-
};
4+
use hir_def::{nameres::ModuleSource, ModuleId};
75
use hir_expand::name::AsName;
86
use ra_db::FileId;
97
use ra_prof::profile;
10-
use ra_syntax::{
11-
ast::{self, AstNode, NameOwner},
12-
match_ast,
13-
};
8+
use ra_syntax::ast::{self, AstNode, NameOwner};
149

15-
use crate::{
16-
db::{DefDatabase, HirDatabase},
17-
DefWithBody, InFile, Local, Module, SourceBinder, TypeParam,
18-
};
19-
20-
impl Local {
21-
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::BindPat>) -> Option<Self> {
22-
let mut sb = SourceBinder::new(db);
23-
let file_id = src.file_id;
24-
let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| {
25-
let res = match_ast! {
26-
match it {
27-
ast::ConstDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
28-
ast::StaticDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
29-
ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
30-
_ => return None,
31-
}
32-
};
33-
Some(res)
34-
})?;
35-
let (_body, source_map) = db.body_with_source_map(parent.into());
36-
let src = src.map(ast::Pat::from);
37-
let pat_id = source_map.node_pat(src.as_ref())?;
38-
Some(Local { parent, pat_id })
39-
}
40-
}
41-
42-
impl TypeParam {
43-
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::TypeParam>) -> Option<Self> {
44-
let mut sb = SourceBinder::new(db);
45-
let file_id = src.file_id;
46-
let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
47-
let res = match_ast! {
48-
match it {
49-
ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
50-
ast::StructDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
51-
ast::EnumDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
52-
ast::TraitDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
53-
ast::TypeAliasDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
54-
ast::ImplBlock(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
55-
_ => return None,
56-
}
57-
};
58-
Some(res)
59-
})?;
60-
let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?;
61-
Some(TypeParam { id })
62-
}
63-
}
10+
use crate::{db::DefDatabase, InFile, Module};
6411

6512
impl Module {
6613
pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {

crates/ra_hir/src/source_binder.rs

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use hir_def::{
88
dyn_map::DynMap,
99
keys::{self, Key},
1010
resolver::{HasResolver, Resolver},
11-
ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, ImplId, ModuleId, StaticId,
12-
StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
11+
ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId,
12+
StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
1313
};
1414
use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind};
1515
use ra_prof::profile;
1616
use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit};
1717
use rustc_hash::FxHashMap;
1818

19-
use crate::{db::HirDatabase, ModuleSource, SourceAnalyzer};
19+
use crate::{db::HirDatabase, Local, ModuleSource, SourceAnalyzer, TypeParam};
2020

2121
pub struct SourceBinder<'a, DB> {
2222
pub db: &'a DB,
@@ -53,8 +53,7 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
5353
}
5454

5555
pub fn to_def<T: ToDef>(&mut self, src: InFile<T>) -> Option<T::Def> {
56-
let id: T::ID = self.to_id(src)?;
57-
Some(id.into())
56+
T::to_def(self, src)
5857
}
5958

6059
fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> {
@@ -110,20 +109,27 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
110109
}
111110
}
112111

113-
pub trait ToId: Sized + AstNode + 'static {
112+
pub trait ToId: Sized {
114113
type ID: Sized + Copy + 'static;
115114
fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
116115
-> Option<Self::ID>;
117116
}
118117

119-
pub trait ToDef: ToId {
120-
type Def: From<Self::ID>;
118+
pub trait ToDef: Sized + AstNode + 'static {
119+
type Def;
120+
fn to_def<DB: HirDatabase>(
121+
sb: &mut SourceBinder<'_, DB>,
122+
src: InFile<Self>,
123+
) -> Option<Self::Def>;
121124
}
122125

123126
macro_rules! to_def_impls {
124127
($(($def:path, $ast:path)),* ,) => {$(
125128
impl ToDef for $ast {
126129
type Def = $def;
130+
fn to_def<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
131+
-> Option<Self::Def>
132+
{ sb.to_id(src).map(Into::into) }
127133
}
128134
)*}
129135
}
@@ -230,3 +236,54 @@ impl ToId for ast::MacroCall {
230236
Some(MacroDefId { krate, ast_id, kind })
231237
}
232238
}
239+
240+
impl ToDef for ast::BindPat {
241+
type Def = Local;
242+
243+
fn to_def<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>) -> Option<Local> {
244+
let file_id = src.file_id;
245+
let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| {
246+
let res = match_ast! {
247+
match it {
248+
ast::ConstDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
249+
ast::StaticDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
250+
ast::FnDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
251+
_ => return None,
252+
}
253+
};
254+
Some(res)
255+
})?;
256+
let (_body, source_map) = sb.db.body_with_source_map(parent);
257+
let src = src.map(ast::Pat::from);
258+
let pat_id = source_map.node_pat(src.as_ref())?;
259+
Some(Local { parent: parent.into(), pat_id })
260+
}
261+
}
262+
263+
impl ToDef for ast::TypeParam {
264+
type Def = TypeParam;
265+
266+
fn to_def<DB: HirDatabase>(
267+
sb: &mut SourceBinder<'_, DB>,
268+
src: InFile<ast::TypeParam>,
269+
) -> Option<TypeParam> {
270+
let mut sb = SourceBinder::new(sb.db);
271+
let file_id = src.file_id;
272+
let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
273+
let res = match_ast! {
274+
match it {
275+
ast::FnDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
276+
ast::StructDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
277+
ast::EnumDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
278+
ast::TraitDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
279+
ast::TypeAliasDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
280+
ast::ImplBlock(value) => { sb.to_id(InFile { value, file_id})?.into() },
281+
_ => return None,
282+
}
283+
};
284+
Some(res)
285+
})?;
286+
let &id = parent.child_by_source(sb.db)[keys::TYPE_PARAM].get(&src)?;
287+
Some(TypeParam { id })
288+
}
289+
}

crates/ra_ide/src/references/classify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) fn classify_name(
2222
match parent {
2323
ast::BindPat(it) => {
2424
let src = name.with_value(it);
25-
let local = hir::Local::from_source(sb.db, src)?;
25+
let local = sb.to_def(src)?;
2626
Some(NameDefinition {
2727
visibility: None,
2828
container: local.module(sb.db),
@@ -114,7 +114,7 @@ pub(crate) fn classify_name(
114114
},
115115
ast::TypeParam(it) => {
116116
let src = name.with_value(it);
117-
let def = hir::TypeParam::from_source(sb.db, src)?;
117+
let def = sb.to_def(src)?;
118118
Some(NameDefinition {
119119
visibility: None,
120120
container: def.module(sb.db),

0 commit comments

Comments
 (0)