diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-09-19 19:18:41 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-09-19 19:18:41 +0100 |
commit | cd9b222ba0555424d73d549eac43d9aaf1765d7d (patch) | |
tree | 88eea12084b65ba7b0f808cc2c626c7b57c7ac53 /crates/ra_ide_api | |
parent | 44bab3621d74f1aec922300a3b3f4476907ba2ac (diff) | |
parent | 2867c40925e8f7b440ff50a421a2d3726b9ff334 (diff) |
Merge #1853
1853: Introduce FromSource trait r=matklad a=viorina
The idea is to provide an ability to get HIR from AST in a more general way than it's possible using `source_binder`.
It also could help with #1622 fixing.
Co-authored-by: Ekaterina Babshukova <[email protected]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/completion/completion_context.rs | 7 | ||||
-rw-r--r-- | crates/ra_ide_api/src/diagnostics.rs | 10 | ||||
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 5 | ||||
-rw-r--r-- | crates/ra_ide_api/src/impls.rs | 25 | ||||
-rw-r--r-- | crates/ra_ide_api/src/parent_module.rs | 16 | ||||
-rw-r--r-- | crates/ra_ide_api/src/references.rs | 7 | ||||
-rw-r--r-- | crates/ra_ide_api/src/runnables.rs | 4 |
7 files changed, 47 insertions, 27 deletions
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 7139947b3..59bd3689b 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs | |||
@@ -1,4 +1,3 @@ | |||
1 | use hir::source_binder; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | algo::{find_covering_element, find_node_at_offset}, | 2 | algo::{find_covering_element, find_node_at_offset}, |
4 | ast, AstNode, Parse, SourceFile, | 3 | ast, AstNode, Parse, SourceFile, |
@@ -47,7 +46,11 @@ impl<'a> CompletionContext<'a> { | |||
47 | original_parse: &'a Parse<ast::SourceFile>, | 46 | original_parse: &'a Parse<ast::SourceFile>, |
48 | position: FilePosition, | 47 | position: FilePosition, |
49 | ) -> Option<CompletionContext<'a>> { | 48 | ) -> Option<CompletionContext<'a>> { |
50 | let module = source_binder::module_from_position(db, position); | 49 | let src = hir::ModuleSource::from_position(db, position); |
50 | let module = hir::Module::from_definition( | ||
51 | db, | ||
52 | hir::Source { file_id: position.file_id.into(), ast: src }, | ||
53 | ); | ||
51 | let token = | 54 | let token = |
52 | original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?; | 55 | original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?; |
53 | let analyzer = | 56 | let analyzer = |
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index c448aa8c5..1ae152e5b 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs | |||
@@ -1,9 +1,6 @@ | |||
1 | use std::cell::RefCell; | 1 | use std::cell::RefCell; |
2 | 2 | ||
3 | use hir::{ | 3 | use hir::diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink}; |
4 | diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink}, | ||
5 | source_binder, | ||
6 | }; | ||
7 | use itertools::Itertools; | 4 | use itertools::Itertools; |
8 | use ra_assists::ast_editor::{AstBuilder, AstEditor}; | 5 | use ra_assists::ast_editor::{AstBuilder, AstEditor}; |
9 | use ra_db::SourceDatabase; | 6 | use ra_db::SourceDatabase; |
@@ -89,7 +86,10 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> | |||
89 | fix: Some(fix), | 86 | fix: Some(fix), |
90 | }) | 87 | }) |
91 | }); | 88 | }); |
92 | if let Some(m) = source_binder::module_from_file_id(db, file_id) { | 89 | let source_file = db.parse(file_id).tree().to_owned(); |
90 | let src = | ||
91 | hir::Source { file_id: file_id.into(), ast: hir::ModuleSource::SourceFile(source_file) }; | ||
92 | if let Some(m) = hir::Module::from_definition(db, src) { | ||
93 | m.diagnostics(db, &mut sink); | 93 | m.diagnostics(db, &mut sink); |
94 | }; | 94 | }; |
95 | drop(sink); | 95 | drop(sink); |
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 503dcacff..bc8863dad 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -96,9 +96,8 @@ pub(crate) fn name_definition( | |||
96 | 96 | ||
97 | if let Some(module) = ast::Module::cast(parent.clone()) { | 97 | if let Some(module) = ast::Module::cast(parent.clone()) { |
98 | if module.has_semi() { | 98 | if module.has_semi() { |
99 | if let Some(child_module) = | 99 | let src = hir::Source { file_id: file_id.into(), ast: module }; |
100 | hir::source_binder::module_from_declaration(db, file_id, module) | 100 | if let Some(child_module) = hir::Module::from_declaration(db, src) { |
101 | { | ||
102 | let nav = NavigationTarget::from_module(db, child_module); | 101 | let nav = NavigationTarget::from_module(db, child_module); |
103 | return Some(vec![nav]); | 102 | return Some(vec![nav]); |
104 | } | 103 | } |
diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index c5620dd52..f57f9a21b 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use hir::{db::HirDatabase, source_binder, ApplicationTy, Ty, TypeCtor}; | 1 | use hir::{db::HirDatabase, ApplicationTy, FromSource, Ty, TypeCtor}; |
2 | use ra_db::SourceDatabase; | 2 | use ra_db::SourceDatabase; |
3 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; | 3 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; |
4 | 4 | ||
@@ -11,17 +11,21 @@ pub(crate) fn goto_implementation( | |||
11 | let parse = db.parse(position.file_id); | 11 | let parse = db.parse(position.file_id); |
12 | let syntax = parse.tree().syntax().clone(); | 12 | let syntax = parse.tree().syntax().clone(); |
13 | 13 | ||
14 | let module = source_binder::module_from_position(db, position)?; | 14 | let src = hir::ModuleSource::from_position(db, position); |
15 | let module = hir::Module::from_definition( | ||
16 | db, | ||
17 | hir::Source { file_id: position.file_id.into(), ast: src }, | ||
18 | )?; | ||
15 | 19 | ||
16 | if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) { | 20 | if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) { |
17 | return Some(RangeInfo::new( | 21 | return Some(RangeInfo::new( |
18 | nominal_def.syntax().text_range(), | 22 | nominal_def.syntax().text_range(), |
19 | impls_for_def(db, &nominal_def, module)?, | 23 | impls_for_def(db, position, &nominal_def, module)?, |
20 | )); | 24 | )); |
21 | } else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) { | 25 | } else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) { |
22 | return Some(RangeInfo::new( | 26 | return Some(RangeInfo::new( |
23 | trait_def.syntax().text_range(), | 27 | trait_def.syntax().text_range(), |
24 | impls_for_trait(db, &trait_def, module)?, | 28 | impls_for_trait(db, position, &trait_def, module)?, |
25 | )); | 29 | )); |
26 | } | 30 | } |
27 | 31 | ||
@@ -30,14 +34,19 @@ pub(crate) fn goto_implementation( | |||
30 | 34 | ||
31 | fn impls_for_def( | 35 | fn impls_for_def( |
32 | db: &RootDatabase, | 36 | db: &RootDatabase, |
37 | position: FilePosition, | ||
33 | node: &ast::NominalDef, | 38 | node: &ast::NominalDef, |
34 | module: hir::Module, | 39 | module: hir::Module, |
35 | ) -> Option<Vec<NavigationTarget>> { | 40 | ) -> Option<Vec<NavigationTarget>> { |
36 | let ty = match node { | 41 | let ty = match node { |
37 | ast::NominalDef::StructDef(def) => { | 42 | ast::NominalDef::StructDef(def) => { |
38 | source_binder::struct_from_module(db, module, &def).ty(db) | 43 | let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() }; |
44 | hir::Struct::from_source(db, src)?.ty(db) | ||
45 | } | ||
46 | ast::NominalDef::EnumDef(def) => { | ||
47 | let src = hir::Source { file_id: position.file_id.into(), ast: def.clone() }; | ||
48 | hir::Enum::from_source(db, src)?.ty(db) | ||
39 | } | 49 | } |
40 | ast::NominalDef::EnumDef(def) => source_binder::enum_from_module(db, module, &def).ty(db), | ||
41 | }; | 50 | }; |
42 | 51 | ||
43 | let krate = module.krate(db)?; | 52 | let krate = module.krate(db)?; |
@@ -54,10 +63,12 @@ fn impls_for_def( | |||
54 | 63 | ||
55 | fn impls_for_trait( | 64 | fn impls_for_trait( |
56 | db: &RootDatabase, | 65 | db: &RootDatabase, |
66 | position: FilePosition, | ||
57 | node: &ast::TraitDef, | 67 | node: &ast::TraitDef, |
58 | module: hir::Module, | 68 | module: hir::Module, |
59 | ) -> Option<Vec<NavigationTarget>> { | 69 | ) -> Option<Vec<NavigationTarget>> { |
60 | let tr = source_binder::trait_from_module(db, module, node); | 70 | let src = hir::Source { file_id: position.file_id.into(), ast: node.clone() }; |
71 | let tr = hir::Trait::from_source(db, src)?; | ||
61 | 72 | ||
62 | let krate = module.krate(db)?; | 73 | let krate = module.krate(db)?; |
63 | let impls = db.impls_in_crate(krate); | 74 | let impls = db.impls_in_crate(krate); |
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs index 7d5f9ea2c..3668da8d7 100644 --- a/crates/ra_ide_api/src/parent_module.rs +++ b/crates/ra_ide_api/src/parent_module.rs | |||
@@ -5,7 +5,11 @@ use crate::{db::RootDatabase, NavigationTarget}; | |||
5 | /// This returns `Vec` because a module may be included from several places. We | 5 | /// This returns `Vec` because a module may be included from several places. We |
6 | /// don't handle this case yet though, so the Vec has length at most one. | 6 | /// don't handle this case yet though, so the Vec has length at most one. |
7 | pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { | 7 | pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { |
8 | let module = match hir::source_binder::module_from_position(db, position) { | 8 | let src = hir::ModuleSource::from_position(db, position); |
9 | let module = match hir::Module::from_definition( | ||
10 | db, | ||
11 | hir::Source { file_id: position.file_id.into(), ast: src }, | ||
12 | ) { | ||
9 | None => return Vec::new(), | 13 | None => return Vec::new(), |
10 | Some(it) => it, | 14 | Some(it) => it, |
11 | }; | 15 | }; |
@@ -15,10 +19,12 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na | |||
15 | 19 | ||
16 | /// Returns `Vec` for the same reason as `parent_module` | 20 | /// Returns `Vec` for the same reason as `parent_module` |
17 | pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { | 21 | pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { |
18 | let module = match hir::source_binder::module_from_file_id(db, file_id) { | 22 | let src = hir::ModuleSource::from_file_id(db, file_id); |
19 | Some(it) => it, | 23 | let module = |
20 | None => return Vec::new(), | 24 | match hir::Module::from_definition(db, hir::Source { file_id: file_id.into(), ast: src }) { |
21 | }; | 25 | Some(it) => it, |
26 | None => return Vec::new(), | ||
27 | }; | ||
22 | let krate = match module.krate(db) { | 28 | let krate = match module.krate(db) { |
23 | Some(it) => it, | 29 | Some(it) => it, |
24 | None => return Vec::new(), | 30 | None => return Vec::new(), |
diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 5f1f0efc3..acca71f2a 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use hir::{source_binder, Either, ModuleSource}; | 1 | use hir::{Either, ModuleSource}; |
2 | use ra_db::SourceDatabase; | 2 | use ra_db::SourceDatabase; |
3 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode}; | 3 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode}; |
4 | use relative_path::{RelativePath, RelativePathBuf}; | 4 | use relative_path::{RelativePath, RelativePathBuf}; |
@@ -135,9 +135,8 @@ fn rename_mod( | |||
135 | ) -> Option<SourceChange> { | 135 | ) -> Option<SourceChange> { |
136 | let mut source_file_edits = Vec::new(); | 136 | let mut source_file_edits = Vec::new(); |
137 | let mut file_system_edits = Vec::new(); | 137 | let mut file_system_edits = Vec::new(); |
138 | if let Some(module) = | 138 | let module_src = hir::Source { file_id: position.file_id.into(), ast: ast_module.clone() }; |
139 | source_binder::module_from_declaration(db, position.file_id, ast_module.clone()) | 139 | if let Some(module) = hir::Module::from_declaration(db, module_src) { |
140 | { | ||
141 | let src = module.definition_source(db); | 140 | let src = module.definition_source(db); |
142 | let file_id = src.file_id.as_original_file(); | 141 | let file_id = src.file_id.as_original_file(); |
143 | match src.ast { | 142 | match src.ast { |
diff --git a/crates/ra_ide_api/src/runnables.rs b/crates/ra_ide_api/src/runnables.rs index 9d84b3d08..095ca56c4 100644 --- a/crates/ra_ide_api/src/runnables.rs +++ b/crates/ra_ide_api/src/runnables.rs | |||
@@ -63,7 +63,9 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti | |||
63 | return None; | 63 | return None; |
64 | } | 64 | } |
65 | let range = module.syntax().text_range(); | 65 | let range = module.syntax().text_range(); |
66 | let module = hir::source_binder::module_from_child_node(db, file_id, module.syntax())?; | 66 | let src = hir::ModuleSource::from_child_node(db, file_id, &module.syntax()); |
67 | let module = | ||
68 | hir::Module::from_definition(db, hir::Source { file_id: file_id.into(), ast: src })?; | ||
67 | 69 | ||
68 | let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::"); | 70 | let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::"); |
69 | Some(Runnable { range, kind: RunnableKind::TestMod { path } }) | 71 | Some(Runnable { range, kind: RunnableKind::TestMod { path } }) |