aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.github/workflows/ci.yaml17
-rw-r--r--Cargo.lock53
-rw-r--r--crates/ra_assists/Cargo.toml1
-rw-r--r--crates/ra_assists/src/assist_ctx.rs40
-rw-r--r--crates/ra_assists/src/ast_transform.rs65
-rw-r--r--crates/ra_assists/src/handlers/add_explicit_type.rs5
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs29
-rw-r--r--crates/ra_assists/src/handlers/add_new.rs11
-rw-r--r--crates/ra_assists/src/handlers/auto_import.rs42
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs23
-rw-r--r--crates/ra_assists/src/handlers/inline_local_variable.rs3
-rw-r--r--crates/ra_assists/src/handlers/introduce_variable.rs60
-rw-r--r--crates/ra_assists/src/lib.rs123
-rw-r--r--crates/ra_assists/src/utils.rs24
-rw-r--r--crates/ra_cargo_watch/Cargo.toml5
-rw-r--r--crates/ra_hir/src/from_id.rs1
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_hir/src/semantics.rs412
-rw-r--r--crates/ra_hir/src/source_analyzer.rs222
-rw-r--r--crates/ra_hir/src/source_binder.rs217
-rw-r--r--crates/ra_hir_def/Cargo.toml1
-rw-r--r--crates/ra_ide/Cargo.toml4
-rw-r--r--crates/ra_ide/src/call_hierarchy.rs42
-rw-r--r--crates/ra_ide/src/call_info.rs27
-rw-r--r--crates/ra_ide/src/completion.rs4
-rw-r--r--crates/ra_ide/src/completion/complete_dot.rs4
-rw-r--r--crates/ra_ide/src/completion/complete_macro_in_item_position.rs2
-rw-r--r--crates/ra_ide/src/completion/complete_path.rs4
-rw-r--r--crates/ra_ide/src/completion/complete_pattern.rs2
-rw-r--r--crates/ra_ide/src/completion/complete_postfix.rs2
-rw-r--r--crates/ra_ide/src/completion/complete_record_literal.rs5
-rw-r--r--crates/ra_ide/src/completion/complete_record_pattern.rs5
-rw-r--r--crates/ra_ide/src/completion/complete_scope.rs4
-rw-r--r--crates/ra_ide/src/completion/complete_trait_impl.rs33
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs65
-rw-r--r--crates/ra_ide/src/diagnostics.rs9
-rw-r--r--crates/ra_ide/src/display/navigation_target.rs8
-rw-r--r--crates/ra_ide/src/expand.rs102
-rw-r--r--crates/ra_ide/src/expand_macro.rs29
-rw-r--r--crates/ra_ide/src/extend_selection.rs81
-rw-r--r--crates/ra_ide/src/goto_definition.rs39
-rw-r--r--crates/ra_ide/src/goto_type_definition.rs36
-rw-r--r--crates/ra_ide/src/hover.rs93
-rw-r--r--crates/ra_ide/src/impls.rs49
-rw-r--r--crates/ra_ide/src/inlay_hints.rs48
-rw-r--r--crates/ra_ide/src/lib.rs18
-rw-r--r--crates/ra_ide/src/marks.rs1
-rw-r--r--crates/ra_ide/src/parent_module.rs17
-rw-r--r--crates/ra_ide/src/references.rs130
-rw-r--r--crates/ra_ide/src/references/classify.rs30
-rw-r--r--crates/ra_ide/src/references/rename.rs28
-rw-r--r--crates/ra_ide/src/runnables.rs50
-rw-r--r--crates/ra_ide/src/snapshots/highlighting.html52
-rw-r--r--crates/ra_ide/src/snapshots/rainbow_highlighting.html24
-rw-r--r--crates/ra_ide/src/ssr.rs141
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs392
-rw-r--r--crates/ra_ide/src/syntax_highlighting/html.rs104
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tags.rs163
-rw-r--r--crates/ra_ide_db/Cargo.toml13
-rw-r--r--crates/ra_ide_db/src/defs.rs50
-rw-r--r--crates/ra_ide_db/src/imports_locator.rs26
-rw-r--r--crates/ra_prof/Cargo.toml1
-rw-r--r--crates/ra_syntax/src/algo.rs21
-rw-r--r--crates/ra_text_edit/Cargo.toml4
-rw-r--r--crates/rust-analyzer/Cargo.toml3
-rw-r--r--crates/rust-analyzer/src/caps.rs23
-rw-r--r--crates/rust-analyzer/src/conv.rs112
-rw-r--r--crates/rust-analyzer/src/main_loop.rs3
-rw-r--r--crates/rust-analyzer/src/main_loop/handlers.rs34
-rw-r--r--crates/rust-analyzer/src/req.rs9
-rw-r--r--crates/rust-analyzer/src/semantic_tokens.rs38
-rw-r--r--crates/test_utils/Cargo.toml2
-rw-r--r--crates/test_utils/src/lib.rs1
-rw-r--r--editors/code/package-lock.json11
-rw-r--r--editors/code/package.json50
-rw-r--r--editors/code/src/client.ts8
-rw-r--r--editors/code/src/config.ts2
-rw-r--r--editors/code/src/installation/download_artifact.ts21
-rw-r--r--editors/code/src/main.ts4
79 files changed, 2024 insertions, 1618 deletions
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 8ab47106d..974d18584 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -8,6 +8,16 @@ on:
8 - trying 8 - trying
9 9
10jobs: 10jobs:
11 rust-audit:
12 name: Audit Rust vulnerabilities
13 runs-on: ubuntu-latest
14 steps:
15 - name: Checkout repository
16 uses: actions/checkout@v1
17
18 - run: cargo install cargo-audit
19 - run: cargo audit
20
11 rust: 21 rust:
12 name: Rust 22 name: Rust
13 runs-on: ${{ matrix.os }} 23 runs-on: ${{ matrix.os }}
@@ -79,7 +89,7 @@ jobs:
79 if: matrix.os == 'windows-latest' 89 if: matrix.os == 'windows-latest'
80 run: Remove-Item ./target/debug/xtask.exe 90 run: Remove-Item ./target/debug/xtask.exe
81 91
82 type-script: 92 typescript:
83 name: TypeScript 93 name: TypeScript
84 runs-on: ubuntu-latest 94 runs-on: ubuntu-latest
85 env: 95 env:
@@ -96,7 +106,12 @@ jobs:
96 106
97 - run: npm ci 107 - run: npm ci
98 working-directory: ./editors/code 108 working-directory: ./editors/code
109
110 - run: npm audit
111 working-directory: ./editors/code
112
99 - run: npm run fmt 113 - run: npm run fmt
100 working-directory: ./editors/code 114 working-directory: ./editors/code
115
101 - run: npm run package --scripts-prepend-node-path 116 - run: npm run package --scripts-prepend-node-path
102 working-directory: ./editors/code 117 working-directory: ./editors/code
diff --git a/Cargo.lock b/Cargo.lock
index e5400f5eb..49fddef4b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -662,9 +662,9 @@ dependencies = [
662 662
663[[package]] 663[[package]]
664name = "lsp-types" 664name = "lsp-types"
665version = "0.70.2" 665version = "0.71.0"
666source = "registry+https://github.com/rust-lang/crates.io-index" 666source = "registry+https://github.com/rust-lang/crates.io-index"
667checksum = "6743fb3902ab3dfa6ce030daeac6ff492e20bb0fee840739d16f6bfb0efaf91c" 667checksum = "efa6b75633b0c3412ee36fc416e6d9c1e4ff576b536217f4ac3f34ac83d9e564"
668dependencies = [ 668dependencies = [
669 "base64", 669 "base64",
670 "bitflags", 670 "bitflags",
@@ -882,9 +882,9 @@ dependencies = [
882 882
883[[package]] 883[[package]]
884name = "proc-macro2" 884name = "proc-macro2"
885version = "1.0.8" 885version = "1.0.9"
886source = "registry+https://github.com/rust-lang/crates.io-index" 886source = "registry+https://github.com/rust-lang/crates.io-index"
887checksum = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" 887checksum = "6c09721c6781493a2a492a96b5a5bf19b65917fe6728884e7c44dd0c60ca3435"
888dependencies = [ 888dependencies = [
889 "unicode-xid", 889 "unicode-xid",
890] 890]
@@ -906,7 +906,6 @@ version = "0.1.0"
906name = "ra_assists" 906name = "ra_assists"
907version = "0.1.0" 907version = "0.1.0"
908dependencies = [ 908dependencies = [
909 "either",
910 "format-buf", 909 "format-buf",
911 "join_to_string", 910 "join_to_string",
912 "ra_db", 911 "ra_db",
@@ -927,10 +926,8 @@ dependencies = [
927 "cargo_metadata", 926 "cargo_metadata",
928 "crossbeam-channel", 927 "crossbeam-channel",
929 "insta", 928 "insta",
930 "jod-thread",
931 "log", 929 "log",
932 "lsp-types", 930 "lsp-types",
933 "parking_lot",
934 "serde_json", 931 "serde_json",
935] 932]
936 933
@@ -988,7 +985,6 @@ dependencies = [
988 "drop_bomb", 985 "drop_bomb",
989 "either", 986 "either",
990 "insta", 987 "insta",
991 "itertools",
992 "log", 988 "log",
993 "once_cell", 989 "once_cell",
994 "ra_arena", 990 "ra_arena",
@@ -1046,7 +1042,6 @@ version = "0.1.0"
1046dependencies = [ 1042dependencies = [
1047 "either", 1043 "either",
1048 "format-buf", 1044 "format-buf",
1049 "fst",
1050 "indexmap", 1045 "indexmap",
1051 "insta", 1046 "insta",
1052 "itertools", 1047 "itertools",
@@ -1063,29 +1058,17 @@ dependencies = [
1063 "ra_syntax", 1058 "ra_syntax",
1064 "ra_text_edit", 1059 "ra_text_edit",
1065 "rand", 1060 "rand",
1066 "rayon",
1067 "rustc-hash", 1061 "rustc-hash",
1068 "superslice",
1069 "test_utils", 1062 "test_utils",
1070 "unicase",
1071] 1063]
1072 1064
1073[[package]] 1065[[package]]
1074name = "ra_ide_db" 1066name = "ra_ide_db"
1075version = "0.1.0" 1067version = "0.1.0"
1076dependencies = [ 1068dependencies = [
1077 "either",
1078 "format-buf",
1079 "fst", 1069 "fst",
1080 "indexmap",
1081 "insta",
1082 "itertools",
1083 "join_to_string",
1084 "log", 1070 "log",
1085 "once_cell",
1086 "ra_cfg",
1087 "ra_db", 1071 "ra_db",
1088 "ra_fmt",
1089 "ra_hir", 1072 "ra_hir",
1090 "ra_prof", 1073 "ra_prof",
1091 "ra_syntax", 1074 "ra_syntax",
@@ -1093,8 +1076,6 @@ dependencies = [
1093 "rayon", 1076 "rayon",
1094 "rustc-hash", 1077 "rustc-hash",
1095 "superslice", 1078 "superslice",
1096 "test_utils",
1097 "unicase",
1098] 1079]
1099 1080
1100[[package]] 1081[[package]]
@@ -1122,7 +1103,6 @@ name = "ra_prof"
1122version = "0.1.0" 1103version = "0.1.0"
1123dependencies = [ 1104dependencies = [
1124 "backtrace", 1105 "backtrace",
1125 "itertools",
1126 "jemalloc-ctl", 1106 "jemalloc-ctl",
1127 "jemallocator", 1107 "jemallocator",
1128 "once_cell", 1108 "once_cell",
@@ -1165,7 +1145,6 @@ dependencies = [
1165name = "ra_text_edit" 1145name = "ra_text_edit"
1166version = "0.1.0" 1146version = "0.1.0"
1167dependencies = [ 1147dependencies = [
1168 "test_utils",
1169 "text_unit", 1148 "text_unit",
1170] 1149]
1171 1150
@@ -1324,7 +1303,6 @@ version = "0.1.0"
1324dependencies = [ 1303dependencies = [
1325 "anyhow", 1304 "anyhow",
1326 "crossbeam-channel", 1305 "crossbeam-channel",
1327 "either",
1328 "env_logger", 1306 "env_logger",
1329 "globset", 1307 "globset",
1330 "itertools", 1308 "itertools",
@@ -1534,9 +1512,9 @@ checksum = "ab16ced94dbd8a46c82fd81e3ed9a8727dac2977ea869d217bcc4ea1f122e81f"
1534 1512
1535[[package]] 1513[[package]]
1536name = "syn" 1514name = "syn"
1537version = "1.0.15" 1515version = "1.0.16"
1538source = "registry+https://github.com/rust-lang/crates.io-index" 1516source = "registry+https://github.com/rust-lang/crates.io-index"
1539checksum = "7a0294dc449adc58bb6592fff1a23d3e5e6e235afc6a0ffca2657d19e7bbffe5" 1517checksum = "123bd9499cfb380418d509322d7a6d52e5315f064fe4b3ad18a53d6b92c07859"
1540dependencies = [ 1518dependencies = [
1541 "proc-macro2", 1519 "proc-macro2",
1542 "quote", 1520 "quote",
@@ -1577,9 +1555,9 @@ dependencies = [
1577 1555
1578[[package]] 1556[[package]]
1579name = "text_unit" 1557name = "text_unit"
1580version = "0.1.9" 1558version = "0.1.10"
1581source = "registry+https://github.com/rust-lang/crates.io-index" 1559source = "registry+https://github.com/rust-lang/crates.io-index"
1582checksum = "e08bbcb7a3adbda0eb23431206b653bdad3d8dea311e72d36bf2215e27a42579" 1560checksum = "20431e104bfecc1a40872578dbc390e10290a0e9c35fffe3ce6f73c15a9dbfc2"
1583 1561
1584[[package]] 1562[[package]]
1585name = "thin-dst" 1563name = "thin-dst"
@@ -1606,15 +1584,6 @@ dependencies = [
1606] 1584]
1607 1585
1608[[package]] 1586[[package]]
1609name = "unicase"
1610version = "2.6.0"
1611source = "registry+https://github.com/rust-lang/crates.io-index"
1612checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
1613dependencies = [
1614 "version_check",
1615]
1616
1617[[package]]
1618name = "unicode-bidi" 1587name = "unicode-bidi"
1619version = "0.3.4" 1588version = "0.3.4"
1620source = "registry+https://github.com/rust-lang/crates.io-index" 1589source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1657,12 +1626,6 @@ dependencies = [
1657] 1626]
1658 1627
1659[[package]] 1628[[package]]
1660name = "version_check"
1661version = "0.9.1"
1662source = "registry+https://github.com/rust-lang/crates.io-index"
1663checksum = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"
1664
1665[[package]]
1666name = "walkdir" 1629name = "walkdir"
1667version = "2.3.1" 1630version = "2.3.1"
1668source = "registry+https://github.com/rust-lang/crates.io-index" 1631source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml
index 12a933645..d314dc8e6 100644
--- a/crates/ra_assists/Cargo.toml
+++ b/crates/ra_assists/Cargo.toml
@@ -11,7 +11,6 @@ doctest = false
11format-buf = "1.0.0" 11format-buf = "1.0.0"
12join_to_string = "0.1.3" 12join_to_string = "0.1.3"
13rustc-hash = "1.1.0" 13rustc-hash = "1.1.0"
14either = "1.5.3"
15 14
16ra_syntax = { path = "../ra_syntax" } 15ra_syntax = { path = "../ra_syntax" }
17ra_text_edit = { path = "../ra_text_edit" } 16ra_text_edit = { path = "../ra_text_edit" }
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index 5aab5fb8b..c25d2e323 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -1,6 +1,6 @@
1//! This module defines `AssistCtx` -- the API surface that is exposed to assists. 1//! This module defines `AssistCtx` -- the API surface that is exposed to assists.
2use hir::{InFile, SourceAnalyzer, SourceBinder}; 2use hir::Semantics;
3use ra_db::{FileRange, SourceDatabase}; 3use ra_db::FileRange;
4use ra_fmt::{leading_indent, reindent}; 4use ra_fmt::{leading_indent, reindent};
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::{ 6use ra_syntax::{
@@ -74,29 +74,23 @@ pub(crate) type AssistHandler = fn(AssistCtx) -> Option<Assist>;
74/// Note, however, that we don't actually use such two-phase logic at the 74/// Note, however, that we don't actually use such two-phase logic at the
75/// moment, because the LSP API is pretty awkward in this place, and it's much 75/// moment, because the LSP API is pretty awkward in this place, and it's much
76/// easier to just compute the edit eagerly :-) 76/// easier to just compute the edit eagerly :-)
77#[derive(Debug)] 77#[derive(Clone)]
78pub(crate) struct AssistCtx<'a> { 78pub(crate) struct AssistCtx<'a> {
79 pub(crate) sema: &'a Semantics<'a, RootDatabase>,
79 pub(crate) db: &'a RootDatabase, 80 pub(crate) db: &'a RootDatabase,
80 pub(crate) frange: FileRange, 81 pub(crate) frange: FileRange,
81 source_file: SourceFile, 82 source_file: SourceFile,
82 should_compute_edit: bool, 83 should_compute_edit: bool,
83} 84}
84 85
85impl Clone for AssistCtx<'_> {
86 fn clone(&self) -> Self {
87 AssistCtx {
88 db: self.db,
89 frange: self.frange,
90 source_file: self.source_file.clone(),
91 should_compute_edit: self.should_compute_edit,
92 }
93 }
94}
95
96impl<'a> AssistCtx<'a> { 86impl<'a> AssistCtx<'a> {
97 pub fn new(db: &RootDatabase, frange: FileRange, should_compute_edit: bool) -> AssistCtx { 87 pub fn new(
98 let parse = db.parse(frange.file_id); 88 sema: &'a Semantics<'a, RootDatabase>,
99 AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit } 89 frange: FileRange,
90 should_compute_edit: bool,
91 ) -> AssistCtx<'a> {
92 let source_file = sema.parse(frange.file_id);
93 AssistCtx { sema, db: sema.db, frange, source_file, should_compute_edit }
100 } 94 }
101 95
102 pub(crate) fn add_assist( 96 pub(crate) fn add_assist(
@@ -138,18 +132,6 @@ impl<'a> AssistCtx<'a> {
138 pub(crate) fn covering_element(&self) -> SyntaxElement { 132 pub(crate) fn covering_element(&self) -> SyntaxElement {
139 find_covering_element(self.source_file.syntax(), self.frange.range) 133 find_covering_element(self.source_file.syntax(), self.frange.range)
140 } 134 }
141 pub(crate) fn source_binder(&self) -> SourceBinder<'a, RootDatabase> {
142 SourceBinder::new(self.db)
143 }
144 pub(crate) fn source_analyzer(
145 &self,
146 node: &SyntaxNode,
147 offset: Option<TextUnit>,
148 ) -> SourceAnalyzer {
149 let src = InFile::new(self.frange.file_id.into(), node);
150 self.source_binder().analyze(src, offset)
151 }
152
153 pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement { 135 pub(crate) fn covering_node_for_range(&self, range: TextRange) -> SyntaxElement {
154 find_covering_element(self.source_file.syntax(), range) 136 find_covering_element(self.source_file.syntax(), range)
155 } 137 }
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs
index c6d15af5f..7846e9798 100644
--- a/crates/ra_assists/src/ast_transform.rs
+++ b/crates/ra_assists/src/ast_transform.rs
@@ -1,15 +1,12 @@
1//! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined. 1//! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined.
2use rustc_hash::FxHashMap; 2use rustc_hash::FxHashMap;
3 3
4use hir::{InFile, PathResolution}; 4use hir::{PathResolution, SemanticsScope};
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::ast::{self, AstNode}; 6use ra_syntax::ast::{self, AstNode};
7 7
8pub trait AstTransform<'a> { 8pub trait AstTransform<'a> {
9 fn get_substitution( 9 fn get_substitution(&self, node: &ra_syntax::SyntaxNode) -> Option<ra_syntax::SyntaxNode>;
10 &self,
11 node: InFile<&ra_syntax::SyntaxNode>,
12 ) -> Option<ra_syntax::SyntaxNode>;
13 10
14 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a>; 11 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a>;
15 fn or<T: AstTransform<'a> + 'a>(self, other: T) -> Box<dyn AstTransform<'a> + 'a> 12 fn or<T: AstTransform<'a> + 'a>(self, other: T) -> Box<dyn AstTransform<'a> + 'a>
@@ -23,10 +20,7 @@ pub trait AstTransform<'a> {
23struct NullTransformer; 20struct NullTransformer;
24 21
25impl<'a> AstTransform<'a> for NullTransformer { 22impl<'a> AstTransform<'a> for NullTransformer {
26 fn get_substitution( 23 fn get_substitution(&self, _node: &ra_syntax::SyntaxNode) -> Option<ra_syntax::SyntaxNode> {
27 &self,
28 _node: InFile<&ra_syntax::SyntaxNode>,
29 ) -> Option<ra_syntax::SyntaxNode> {
30 None 24 None
31 } 25 }
32 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> { 26 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> {
@@ -35,14 +29,16 @@ impl<'a> AstTransform<'a> for NullTransformer {
35} 29}
36 30
37pub struct SubstituteTypeParams<'a> { 31pub struct SubstituteTypeParams<'a> {
38 db: &'a RootDatabase, 32 source_scope: &'a SemanticsScope<'a, RootDatabase>,
39 substs: FxHashMap<hir::TypeParam, ast::TypeRef>, 33 substs: FxHashMap<hir::TypeParam, ast::TypeRef>,
40 previous: Box<dyn AstTransform<'a> + 'a>, 34 previous: Box<dyn AstTransform<'a> + 'a>,
41} 35}
42 36
43impl<'a> SubstituteTypeParams<'a> { 37impl<'a> SubstituteTypeParams<'a> {
44 pub fn for_trait_impl( 38 pub fn for_trait_impl(
39 source_scope: &'a SemanticsScope<'a, RootDatabase>,
45 db: &'a RootDatabase, 40 db: &'a RootDatabase,
41 // FIXME: there's implicit invariant that `trait_` and `source_scope` match...
46 trait_: hir::Trait, 42 trait_: hir::Trait,
47 impl_block: ast::ImplBlock, 43 impl_block: ast::ImplBlock,
48 ) -> SubstituteTypeParams<'a> { 44 ) -> SubstituteTypeParams<'a> {
@@ -56,7 +52,7 @@ impl<'a> SubstituteTypeParams<'a> {
56 .zip(substs.into_iter()) 52 .zip(substs.into_iter())
57 .collect(); 53 .collect();
58 return SubstituteTypeParams { 54 return SubstituteTypeParams {
59 db, 55 source_scope,
60 substs: substs_by_param, 56 substs: substs_by_param,
61 previous: Box::new(NullTransformer), 57 previous: Box::new(NullTransformer),
62 }; 58 };
@@ -80,15 +76,15 @@ impl<'a> SubstituteTypeParams<'a> {
80 } 76 }
81 fn get_substitution_inner( 77 fn get_substitution_inner(
82 &self, 78 &self,
83 node: InFile<&ra_syntax::SyntaxNode>, 79 node: &ra_syntax::SyntaxNode,
84 ) -> Option<ra_syntax::SyntaxNode> { 80 ) -> Option<ra_syntax::SyntaxNode> {
85 let type_ref = ast::TypeRef::cast(node.value.clone())?; 81 let type_ref = ast::TypeRef::cast(node.clone())?;
86 let path = match &type_ref { 82 let path = match &type_ref {
87 ast::TypeRef::PathType(path_type) => path_type.path()?, 83 ast::TypeRef::PathType(path_type) => path_type.path()?,
88 _ => return None, 84 _ => return None,
89 }; 85 };
90 let analyzer = hir::SourceAnalyzer::new(self.db, node, None); 86 let path = hir::Path::from_ast(path)?;
91 let resolution = analyzer.resolve_path(self.db, &path)?; 87 let resolution = self.source_scope.resolve_hir_path(&path)?;
92 match resolution { 88 match resolution {
93 hir::PathResolution::TypeParam(tp) => Some(self.substs.get(&tp)?.syntax().clone()), 89 hir::PathResolution::TypeParam(tp) => Some(self.substs.get(&tp)?.syntax().clone()),
94 _ => None, 90 _ => None,
@@ -97,10 +93,7 @@ impl<'a> SubstituteTypeParams<'a> {
97} 93}
98 94
99impl<'a> AstTransform<'a> for SubstituteTypeParams<'a> { 95impl<'a> AstTransform<'a> for SubstituteTypeParams<'a> {
100 fn get_substitution( 96 fn get_substitution(&self, node: &ra_syntax::SyntaxNode) -> Option<ra_syntax::SyntaxNode> {
101 &self,
102 node: InFile<&ra_syntax::SyntaxNode>,
103 ) -> Option<ra_syntax::SyntaxNode> {
104 self.get_substitution_inner(node).or_else(|| self.previous.get_substitution(node)) 97 self.get_substitution_inner(node).or_else(|| self.previous.get_substitution(node))
105 } 98 }
106 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> { 99 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> {
@@ -109,29 +102,34 @@ impl<'a> AstTransform<'a> for SubstituteTypeParams<'a> {
109} 102}
110 103
111pub struct QualifyPaths<'a> { 104pub struct QualifyPaths<'a> {
105 target_scope: &'a SemanticsScope<'a, RootDatabase>,
106 source_scope: &'a SemanticsScope<'a, RootDatabase>,
112 db: &'a RootDatabase, 107 db: &'a RootDatabase,
113 from: Option<hir::Module>,
114 previous: Box<dyn AstTransform<'a> + 'a>, 108 previous: Box<dyn AstTransform<'a> + 'a>,
115} 109}
116 110
117impl<'a> QualifyPaths<'a> { 111impl<'a> QualifyPaths<'a> {
118 pub fn new(db: &'a RootDatabase, from: Option<hir::Module>) -> Self { 112 pub fn new(
119 Self { db, from, previous: Box::new(NullTransformer) } 113 target_scope: &'a SemanticsScope<'a, RootDatabase>,
114 source_scope: &'a SemanticsScope<'a, RootDatabase>,
115 db: &'a RootDatabase,
116 ) -> Self {
117 Self { target_scope, source_scope, db, previous: Box::new(NullTransformer) }
120 } 118 }
121 119
122 fn get_substitution_inner( 120 fn get_substitution_inner(
123 &self, 121 &self,
124 node: InFile<&ra_syntax::SyntaxNode>, 122 node: &ra_syntax::SyntaxNode,
125 ) -> Option<ra_syntax::SyntaxNode> { 123 ) -> Option<ra_syntax::SyntaxNode> {
126 // FIXME handle value ns? 124 // FIXME handle value ns?
127 let from = self.from?; 125 let from = self.target_scope.module()?;
128 let p = ast::Path::cast(node.value.clone())?; 126 let p = ast::Path::cast(node.clone())?;
129 if p.segment().and_then(|s| s.param_list()).is_some() { 127 if p.segment().and_then(|s| s.param_list()).is_some() {
130 // don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway 128 // don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway
131 return None; 129 return None;
132 } 130 }
133 let analyzer = hir::SourceAnalyzer::new(self.db, node, None); 131 let hir_path = hir::Path::from_ast(p.clone());
134 let resolution = analyzer.resolve_path(self.db, &p)?; 132 let resolution = self.source_scope.resolve_hir_path(&hir_path?)?;
135 match resolution { 133 match resolution {
136 PathResolution::Def(def) => { 134 PathResolution::Def(def) => {
137 let found_path = from.find_use_path(self.db, def)?; 135 let found_path = from.find_use_path(self.db, def)?;
@@ -140,7 +138,7 @@ impl<'a> QualifyPaths<'a> {
140 let type_args = p 138 let type_args = p
141 .segment() 139 .segment()
142 .and_then(|s| s.type_arg_list()) 140 .and_then(|s| s.type_arg_list())
143 .map(|arg_list| apply(self, node.with_value(arg_list))); 141 .map(|arg_list| apply(self, arg_list));
144 if let Some(type_args) = type_args { 142 if let Some(type_args) = type_args {
145 let last_segment = path.segment().unwrap(); 143 let last_segment = path.segment().unwrap();
146 path = path.with_segment(last_segment.with_type_args(type_args)) 144 path = path.with_segment(last_segment.with_type_args(type_args))
@@ -157,11 +155,11 @@ impl<'a> QualifyPaths<'a> {
157 } 155 }
158} 156}
159 157
160pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: InFile<N>) -> N { 158pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N {
161 let syntax = node.value.syntax(); 159 let syntax = node.syntax();
162 let result = ra_syntax::algo::replace_descendants(syntax, &|element| match element { 160 let result = ra_syntax::algo::replace_descendants(syntax, &|element| match element {
163 ra_syntax::SyntaxElement::Node(n) => { 161 ra_syntax::SyntaxElement::Node(n) => {
164 let replacement = transformer.get_substitution(node.with_value(&n))?; 162 let replacement = transformer.get_substitution(&n)?;
165 Some(replacement.into()) 163 Some(replacement.into())
166 } 164 }
167 _ => None, 165 _ => None,
@@ -170,10 +168,7 @@ pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: InFile<N>
170} 168}
171 169
172impl<'a> AstTransform<'a> for QualifyPaths<'a> { 170impl<'a> AstTransform<'a> for QualifyPaths<'a> {
173 fn get_substitution( 171 fn get_substitution(&self, node: &ra_syntax::SyntaxNode) -> Option<ra_syntax::SyntaxNode> {
174 &self,
175 node: InFile<&ra_syntax::SyntaxNode>,
176 ) -> Option<ra_syntax::SyntaxNode> {
177 self.get_substitution_inner(node).or_else(|| self.previous.get_substitution(node)) 172 self.get_substitution_inner(node).or_else(|| self.previous.get_substitution(node))
178 } 173 }
179 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> { 174 fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> {
diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs
index 2cb9d2f48..a63ef48b1 100644
--- a/crates/ra_assists/src/handlers/add_explicit_type.rs
+++ b/crates/ra_assists/src/handlers/add_explicit_type.rs
@@ -51,14 +51,13 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option<Assist> {
51 } 51 }
52 } 52 }
53 // Infer type 53 // Infer type
54 let db = ctx.db; 54 let ty = ctx.sema.type_of_expr(&expr)?;
55 let analyzer = ctx.source_analyzer(stmt.syntax(), None);
56 let ty = analyzer.type_of(db, &expr)?;
57 // Assist not applicable if the type is unknown 55 // Assist not applicable if the type is unknown
58 if ty.contains_unknown() { 56 if ty.contains_unknown() {
59 return None; 57 return None;
60 } 58 }
61 59
60 let db = ctx.db;
62 ctx.add_assist( 61 ctx.add_assist(
63 AssistId("add_explicit_type"), 62 AssistId("add_explicit_type"),
64 format!("Insert explicit type '{}'", ty.display(db)), 63 format!("Insert explicit type '{}'", ty.display(db)),
diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
index ab21388c8..4005014bd 100644
--- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
@@ -1,4 +1,4 @@
1use hir::{HasSource, InFile}; 1use hir::HasSource;
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, edit, make, AstNode, NameOwner}, 3 ast::{self, edit, make, AstNode, NameOwner},
4 SmolStr, 4 SmolStr,
@@ -104,9 +104,7 @@ fn add_missing_impl_members_inner(
104 let impl_node = ctx.find_node_at_offset::<ast::ImplBlock>()?; 104 let impl_node = ctx.find_node_at_offset::<ast::ImplBlock>()?;
105 let impl_item_list = impl_node.item_list()?; 105 let impl_item_list = impl_node.item_list()?;
106 106
107 let analyzer = ctx.source_analyzer(impl_node.syntax(), None); 107 let trait_ = resolve_target_trait(&ctx.sema, &impl_node)?;
108
109 let trait_ = resolve_target_trait(ctx.db, &analyzer, &impl_node)?;
110 108
111 let def_name = |item: &ast::ImplItem| -> Option<SmolStr> { 109 let def_name = |item: &ast::ImplItem| -> Option<SmolStr> {
112 match item { 110 match item {
@@ -117,7 +115,7 @@ fn add_missing_impl_members_inner(
117 .map(|it| it.text().clone()) 115 .map(|it| it.text().clone())
118 }; 116 };
119 117
120 let missing_items = get_missing_impl_items(ctx.db, &analyzer, &impl_node) 118 let missing_items = get_missing_impl_items(&ctx.sema, &impl_node)
121 .iter() 119 .iter()
122 .map(|i| match i { 120 .map(|i| match i {
123 hir::AssocItem::Function(i) => ast::ImplItem::FnDef(i.source(ctx.db).value), 121 hir::AssocItem::Function(i) => ast::ImplItem::FnDef(i.source(ctx.db).value),
@@ -138,23 +136,17 @@ fn add_missing_impl_members_inner(
138 return None; 136 return None;
139 } 137 }
140 138
141 let db = ctx.db; 139 let sema = ctx.sema;
142 let file_id = ctx.frange.file_id;
143 let trait_file_id = trait_.source(db).file_id;
144 140
145 ctx.add_assist(AssistId(assist_id), label, |edit| { 141 ctx.add_assist(AssistId(assist_id), label, |edit| {
146 let n_existing_items = impl_item_list.impl_items().count(); 142 let n_existing_items = impl_item_list.impl_items().count();
147 let module = hir::SourceAnalyzer::new( 143 let source_scope = sema.scope_for_def(trait_);
148 db, 144 let target_scope = sema.scope(impl_item_list.syntax());
149 hir::InFile::new(file_id.into(), impl_node.syntax()), 145 let ast_transform = QualifyPaths::new(&target_scope, &source_scope, sema.db)
150 None, 146 .or(SubstituteTypeParams::for_trait_impl(&source_scope, sema.db, trait_, impl_node));
151 )
152 .module();
153 let ast_transform = QualifyPaths::new(db, module)
154 .or(SubstituteTypeParams::for_trait_impl(db, trait_, impl_node));
155 let items = missing_items 147 let items = missing_items
156 .into_iter() 148 .into_iter()
157 .map(|it| ast_transform::apply(&*ast_transform, InFile::new(trait_file_id, it))) 149 .map(|it| ast_transform::apply(&*ast_transform, it))
158 .map(|it| match it { 150 .map(|it| match it {
159 ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)), 151 ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)),
160 _ => it, 152 _ => it,
@@ -181,9 +173,10 @@ fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
181 173
182#[cfg(test)] 174#[cfg(test)]
183mod tests { 175mod tests {
184 use super::*;
185 use crate::helpers::{check_assist, check_assist_not_applicable}; 176 use crate::helpers::{check_assist, check_assist_not_applicable};
186 177
178 use super::*;
179
187 #[test] 180 #[test]
188 fn test_add_missing_impl_members() { 181 fn test_add_missing_impl_members() {
189 check_assist( 182 check_assist(
diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs
index dd070e8ec..166e907fb 100644
--- a/crates/ra_assists/src/handlers/add_new.rs
+++ b/crates/ra_assists/src/handlers/add_new.rs
@@ -1,5 +1,5 @@
1use format_buf::format; 1use format_buf::format;
2use hir::{Adt, InFile}; 2use hir::Adt;
3use join_to_string::join; 3use join_to_string::join;
4use ra_syntax::{ 4use ra_syntax::{
5 ast::{ 5 ast::{
@@ -133,16 +133,11 @@ fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<a
133 let module = strukt.syntax().ancestors().find(|node| { 133 let module = strukt.syntax().ancestors().find(|node| {
134 ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind()) 134 ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind())
135 })?; 135 })?;
136 let mut sb = ctx.source_binder();
137 136
138 let struct_def = { 137 let struct_def = ctx.sema.to_def(strukt)?;
139 let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() };
140 sb.to_def(src)?
141 };
142 138
143 let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| { 139 let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| {
144 let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() }; 140 let blk = ctx.sema.to_def(&impl_blk)?;
145 let blk = sb.to_def(src)?;
146 141
147 // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}` 142 // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}`
148 // (we currently use the wrong type parameter) 143 // (we currently use the wrong type parameter)
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs
index c4aea2a06..edf0cf6d0 100644
--- a/crates/ra_assists/src/handlers/auto_import.rs
+++ b/crates/ra_assists/src/handlers/auto_import.rs
@@ -3,8 +3,8 @@ use crate::{
3 insert_use_statement, AssistId, 3 insert_use_statement, AssistId,
4}; 4};
5use hir::{ 5use hir::{
6 db::HirDatabase, AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, 6 AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait,
7 SourceAnalyzer, Trait, Type, 7 Type,
8}; 8};
9use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; 9use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase};
10use ra_prof::profile; 10use ra_prof::profile;
@@ -78,14 +78,9 @@ impl AutoImportAssets {
78 78
79 fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistCtx) -> Option<Self> { 79 fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistCtx) -> Option<Self> {
80 let syntax_under_caret = method_call.syntax().to_owned(); 80 let syntax_under_caret = method_call.syntax().to_owned();
81 let source_analyzer = ctx.source_analyzer(&syntax_under_caret, None); 81 let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?;
82 let module_with_name_to_import = source_analyzer.module()?;
83 Some(Self { 82 Some(Self {
84 import_candidate: ImportCandidate::for_method_call( 83 import_candidate: ImportCandidate::for_method_call(&ctx.sema, &method_call)?,
85 &method_call,
86 &source_analyzer,
87 ctx.db,
88 )?,
89 module_with_name_to_import, 84 module_with_name_to_import,
90 syntax_under_caret, 85 syntax_under_caret,
91 }) 86 })
@@ -97,14 +92,9 @@ impl AutoImportAssets {
97 return None; 92 return None;
98 } 93 }
99 94
100 let source_analyzer = ctx.source_analyzer(&syntax_under_caret, None); 95 let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?;
101 let module_with_name_to_import = source_analyzer.module()?;
102 Some(Self { 96 Some(Self {
103 import_candidate: ImportCandidate::for_regular_path( 97 import_candidate: ImportCandidate::for_regular_path(&ctx.sema, &path_under_caret)?,
104 &path_under_caret,
105 &source_analyzer,
106 ctx.db,
107 )?,
108 module_with_name_to_import, 98 module_with_name_to_import,
109 syntax_under_caret, 99 syntax_under_caret,
110 }) 100 })
@@ -229,25 +219,23 @@ enum ImportCandidate {
229 219
230impl ImportCandidate { 220impl ImportCandidate {
231 fn for_method_call( 221 fn for_method_call(
222 sema: &Semantics<RootDatabase>,
232 method_call: &ast::MethodCallExpr, 223 method_call: &ast::MethodCallExpr,
233 source_analyzer: &SourceAnalyzer,
234 db: &impl HirDatabase,
235 ) -> Option<Self> { 224 ) -> Option<Self> {
236 if source_analyzer.resolve_method_call(method_call).is_some() { 225 if sema.resolve_method_call(method_call).is_some() {
237 return None; 226 return None;
238 } 227 }
239 Some(Self::TraitMethod( 228 Some(Self::TraitMethod(
240 source_analyzer.type_of(db, &method_call.expr()?)?, 229 sema.type_of_expr(&method_call.expr()?)?,
241 method_call.name_ref()?.syntax().to_string(), 230 method_call.name_ref()?.syntax().to_string(),
242 )) 231 ))
243 } 232 }
244 233
245 fn for_regular_path( 234 fn for_regular_path(
235 sema: &Semantics<RootDatabase>,
246 path_under_caret: &ast::Path, 236 path_under_caret: &ast::Path,
247 source_analyzer: &SourceAnalyzer,
248 db: &impl HirDatabase,
249 ) -> Option<Self> { 237 ) -> Option<Self> {
250 if source_analyzer.resolve_path(db, path_under_caret).is_some() { 238 if sema.resolve_path(path_under_caret).is_some() {
251 return None; 239 return None;
252 } 240 }
253 241
@@ -256,17 +244,15 @@ impl ImportCandidate {
256 let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; 244 let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?;
257 let qualifier_start_path = 245 let qualifier_start_path =
258 qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; 246 qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?;
259 if let Some(qualifier_start_resolution) = 247 if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) {
260 source_analyzer.resolve_path(db, &qualifier_start_path)
261 {
262 let qualifier_resolution = if qualifier_start_path == qualifier { 248 let qualifier_resolution = if qualifier_start_path == qualifier {
263 qualifier_start_resolution 249 qualifier_start_resolution
264 } else { 250 } else {
265 source_analyzer.resolve_path(db, &qualifier)? 251 sema.resolve_path(&qualifier)?
266 }; 252 };
267 if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution { 253 if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution {
268 Some(ImportCandidate::TraitAssocItem( 254 Some(ImportCandidate::TraitAssocItem(
269 assoc_item_path.ty(db), 255 assoc_item_path.ty(sema.db),
270 segment.syntax().to_string(), 256 segment.syntax().to_string(),
271 )) 257 ))
272 } else { 258 } else {
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index ae2437ed3..e5d8c639d 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -2,10 +2,11 @@
2 2
3use std::iter; 3use std::iter;
4 4
5use hir::{db::HirDatabase, Adt, HasSource}; 5use hir::{db::HirDatabase, Adt, HasSource, Semantics};
6use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner}; 6use ra_syntax::ast::{self, edit::IndentLevel, make, AstNode, NameOwner};
7 7
8use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
9use ra_ide_db::RootDatabase;
9 10
10// Assist: fill_match_arms 11// Assist: fill_match_arms
11// 12//
@@ -46,10 +47,9 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> {
46 }; 47 };
47 48
48 let expr = match_expr.expr()?; 49 let expr = match_expr.expr()?;
49 let (enum_def, module) = { 50 let enum_def = resolve_enum_def(&ctx.sema, &expr)?;
50 let analyzer = ctx.source_analyzer(expr.syntax(), None); 51 let module = ctx.sema.scope(expr.syntax()).module()?;
51 (resolve_enum_def(ctx.db, &analyzer, &expr)?, analyzer.module()?) 52
52 };
53 let variants = enum_def.variants(ctx.db); 53 let variants = enum_def.variants(ctx.db);
54 if variants.is_empty() { 54 if variants.is_empty() {
55 return None; 55 return None;
@@ -81,18 +81,11 @@ fn is_trivial(arm: &ast::MatchArm) -> bool {
81 } 81 }
82} 82}
83 83
84fn resolve_enum_def( 84fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> {
85 db: &impl HirDatabase, 85 sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() {
86 analyzer: &hir::SourceAnalyzer,
87 expr: &ast::Expr,
88) -> Option<hir::Enum> {
89 let expr_ty = analyzer.type_of(db, &expr)?;
90
91 let result = expr_ty.autoderef(db).find_map(|ty| match ty.as_adt() {
92 Some(Adt::Enum(e)) => Some(e), 86 Some(Adt::Enum(e)) => Some(e),
93 _ => None, 87 _ => None,
94 }); 88 })
95 result
96} 89}
97 90
98fn build_pat( 91fn build_pat(
diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs
index 91b588243..53a72309b 100644
--- a/crates/ra_assists/src/handlers/inline_local_variable.rs
+++ b/crates/ra_assists/src/handlers/inline_local_variable.rs
@@ -44,8 +44,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
44 } else { 44 } else {
45 let_stmt.syntax().text_range() 45 let_stmt.syntax().text_range()
46 }; 46 };
47 let analyzer = ctx.source_analyzer(bind_pat.syntax(), None); 47 let refs = ctx.sema.find_all_refs(&bind_pat);
48 let refs = analyzer.find_all_refs(&bind_pat);
49 if refs.is_empty() { 48 if refs.is_empty() {
50 return None; 49 return None;
51 }; 50 };
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs
index 7312ce687..b453c51fb 100644
--- a/crates/ra_assists/src/handlers/introduce_variable.rs
+++ b/crates/ra_assists/src/handlers/introduce_variable.rs
@@ -136,15 +136,13 @@ fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> {
136mod tests { 136mod tests {
137 use test_utils::covers; 137 use test_utils::covers;
138 138
139 use crate::helpers::{ 139 use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
140 check_assist_range, check_assist_range_not_applicable, check_assist_range_target,
141 };
142 140
143 use super::*; 141 use super::*;
144 142
145 #[test] 143 #[test]
146 fn test_introduce_var_simple() { 144 fn test_introduce_var_simple() {
147 check_assist_range( 145 check_assist(
148 introduce_variable, 146 introduce_variable,
149 " 147 "
150fn foo() { 148fn foo() {
@@ -161,16 +159,13 @@ fn foo() {
161 #[test] 159 #[test]
162 fn introduce_var_in_comment_is_not_applicable() { 160 fn introduce_var_in_comment_is_not_applicable() {
163 covers!(introduce_var_in_comment_is_not_applicable); 161 covers!(introduce_var_in_comment_is_not_applicable);
164 check_assist_range_not_applicable( 162 check_assist_not_applicable(introduce_variable, "fn main() { 1 + /* <|>comment<|> */ 1; }");
165 introduce_variable,
166 "fn main() { 1 + /* <|>comment<|> */ 1; }",
167 );
168 } 163 }
169 164
170 #[test] 165 #[test]
171 fn test_introduce_var_expr_stmt() { 166 fn test_introduce_var_expr_stmt() {
172 covers!(test_introduce_var_expr_stmt); 167 covers!(test_introduce_var_expr_stmt);
173 check_assist_range( 168 check_assist(
174 introduce_variable, 169 introduce_variable,
175 " 170 "
176fn foo() { 171fn foo() {
@@ -181,7 +176,7 @@ fn foo() {
181 let <|>var_name = 1 + 1; 176 let <|>var_name = 1 + 1;
182}", 177}",
183 ); 178 );
184 check_assist_range( 179 check_assist(
185 introduce_variable, 180 introduce_variable,
186 " 181 "
187fn foo() { 182fn foo() {
@@ -198,7 +193,7 @@ fn foo() {
198 193
199 #[test] 194 #[test]
200 fn test_introduce_var_part_of_expr_stmt() { 195 fn test_introduce_var_part_of_expr_stmt() {
201 check_assist_range( 196 check_assist(
202 introduce_variable, 197 introduce_variable,
203 " 198 "
204fn foo() { 199fn foo() {
@@ -215,7 +210,7 @@ fn foo() {
215 #[test] 210 #[test]
216 fn test_introduce_var_last_expr() { 211 fn test_introduce_var_last_expr() {
217 covers!(test_introduce_var_last_expr); 212 covers!(test_introduce_var_last_expr);
218 check_assist_range( 213 check_assist(
219 introduce_variable, 214 introduce_variable,
220 " 215 "
221fn foo() { 216fn foo() {
@@ -227,7 +222,7 @@ fn foo() {
227 bar(var_name) 222 bar(var_name)
228}", 223}",
229 ); 224 );
230 check_assist_range( 225 check_assist(
231 introduce_variable, 226 introduce_variable,
232 " 227 "
233fn foo() { 228fn foo() {
@@ -243,7 +238,7 @@ fn foo() {
243 238
244 #[test] 239 #[test]
245 fn test_introduce_var_in_match_arm_no_block() { 240 fn test_introduce_var_in_match_arm_no_block() {
246 check_assist_range( 241 check_assist(
247 introduce_variable, 242 introduce_variable,
248 " 243 "
249fn main() { 244fn main() {
@@ -268,7 +263,7 @@ fn main() {
268 263
269 #[test] 264 #[test]
270 fn test_introduce_var_in_match_arm_with_block() { 265 fn test_introduce_var_in_match_arm_with_block() {
271 check_assist_range( 266 check_assist(
272 introduce_variable, 267 introduce_variable,
273 " 268 "
274fn main() { 269fn main() {
@@ -300,7 +295,7 @@ fn main() {
300 295
301 #[test] 296 #[test]
302 fn test_introduce_var_in_closure_no_block() { 297 fn test_introduce_var_in_closure_no_block() {
303 check_assist_range( 298 check_assist(
304 introduce_variable, 299 introduce_variable,
305 " 300 "
306fn main() { 301fn main() {
@@ -317,7 +312,7 @@ fn main() {
317 312
318 #[test] 313 #[test]
319 fn test_introduce_var_in_closure_with_block() { 314 fn test_introduce_var_in_closure_with_block() {
320 check_assist_range( 315 check_assist(
321 introduce_variable, 316 introduce_variable,
322 " 317 "
323fn main() { 318fn main() {
@@ -334,7 +329,7 @@ fn main() {
334 329
335 #[test] 330 #[test]
336 fn test_introduce_var_path_simple() { 331 fn test_introduce_var_path_simple() {
337 check_assist_range( 332 check_assist(
338 introduce_variable, 333 introduce_variable,
339 " 334 "
340fn main() { 335fn main() {
@@ -352,7 +347,7 @@ fn main() {
352 347
353 #[test] 348 #[test]
354 fn test_introduce_var_path_method() { 349 fn test_introduce_var_path_method() {
355 check_assist_range( 350 check_assist(
356 introduce_variable, 351 introduce_variable,
357 " 352 "
358fn main() { 353fn main() {
@@ -370,7 +365,7 @@ fn main() {
370 365
371 #[test] 366 #[test]
372 fn test_introduce_var_return() { 367 fn test_introduce_var_return() {
373 check_assist_range( 368 check_assist(
374 introduce_variable, 369 introduce_variable,
375 " 370 "
376fn foo() -> u32 { 371fn foo() -> u32 {
@@ -388,7 +383,7 @@ fn foo() -> u32 {
388 383
389 #[test] 384 #[test]
390 fn test_introduce_var_does_not_add_extra_whitespace() { 385 fn test_introduce_var_does_not_add_extra_whitespace() {
391 check_assist_range( 386 check_assist(
392 introduce_variable, 387 introduce_variable,
393 " 388 "
394fn foo() -> u32 { 389fn foo() -> u32 {
@@ -407,7 +402,7 @@ fn foo() -> u32 {
407", 402",
408 ); 403 );
409 404
410 check_assist_range( 405 check_assist(
411 introduce_variable, 406 introduce_variable,
412 " 407 "
413fn foo() -> u32 { 408fn foo() -> u32 {
@@ -424,7 +419,7 @@ fn foo() -> u32 {
424", 419",
425 ); 420 );
426 421
427 check_assist_range( 422 check_assist(
428 introduce_variable, 423 introduce_variable,
429 " 424 "
430fn foo() -> u32 { 425fn foo() -> u32 {
@@ -452,7 +447,7 @@ fn foo() -> u32 {
452 447
453 #[test] 448 #[test]
454 fn test_introduce_var_break() { 449 fn test_introduce_var_break() {
455 check_assist_range( 450 check_assist(
456 introduce_variable, 451 introduce_variable,
457 " 452 "
458fn main() { 453fn main() {
@@ -474,7 +469,7 @@ fn main() {
474 469
475 #[test] 470 #[test]
476 fn test_introduce_var_for_cast() { 471 fn test_introduce_var_for_cast() {
477 check_assist_range( 472 check_assist(
478 introduce_variable, 473 introduce_variable,
479 " 474 "
480fn main() { 475fn main() {
@@ -492,27 +487,20 @@ fn main() {
492 487
493 #[test] 488 #[test]
494 fn test_introduce_var_for_return_not_applicable() { 489 fn test_introduce_var_for_return_not_applicable() {
495 check_assist_range_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } "); 490 check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } ");
496 } 491 }
497 492
498 #[test] 493 #[test]
499 fn test_introduce_var_for_break_not_applicable() { 494 fn test_introduce_var_for_break_not_applicable() {
500 check_assist_range_not_applicable( 495 check_assist_not_applicable(introduce_variable, "fn main() { loop { <|>break<|>; }; }");
501 introduce_variable,
502 "fn main() { loop { <|>break<|>; }; }",
503 );
504 } 496 }
505 497
506 // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic 498 // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic
507 #[test] 499 #[test]
508 fn introduce_var_target() { 500 fn introduce_var_target() {
509 check_assist_range_target( 501 check_assist_target(introduce_variable, "fn foo() -> u32 { <|>return 2 + 2<|>; }", "2 + 2");
510 introduce_variable,
511 "fn foo() -> u32 { <|>return 2 + 2<|>; }",
512 "2 + 2",
513 );
514 502
515 check_assist_range_target( 503 check_assist_target(
516 introduce_variable, 504 introduce_variable,
517 " 505 "
518fn main() { 506fn main() {
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index d7998b0d1..c28a9b92b 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -19,6 +19,7 @@ use ra_text_edit::TextEdit;
19 19
20pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler}; 20pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler};
21pub use crate::handlers::replace_qualified_name_with_use::insert_use_statement; 21pub use crate::handlers::replace_qualified_name_with_use::insert_use_statement;
22use hir::Semantics;
22 23
23/// Unique identifier of the assist, should not be shown to the user 24/// Unique identifier of the assist, should not be shown to the user
24/// directly. 25/// directly.
@@ -63,7 +64,8 @@ pub struct ResolvedAssist {
63/// Assists are returned in the "unresolved" state, that is only labels are 64/// Assists are returned in the "unresolved" state, that is only labels are
64/// returned, without actual edits. 65/// returned, without actual edits.
65pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> { 66pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> {
66 let ctx = AssistCtx::new(db, range, false); 67 let sema = Semantics::new(db);
68 let ctx = AssistCtx::new(&sema, range, false);
67 handlers::all() 69 handlers::all()
68 .iter() 70 .iter()
69 .filter_map(|f| f(ctx.clone())) 71 .filter_map(|f| f(ctx.clone()))
@@ -77,7 +79,8 @@ pub fn unresolved_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabe
77/// Assists are returned in the "resolved" state, that is with edit fully 79/// Assists are returned in the "resolved" state, that is with edit fully
78/// computed. 80/// computed.
79pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { 81pub fn resolved_assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
80 let ctx = AssistCtx::new(db, range, true); 82 let sema = Semantics::new(db);
83 let ctx = AssistCtx::new(&sema, range, true);
81 let mut a = handlers::all() 84 let mut a = handlers::all()
82 .iter() 85 .iter()
83 .filter_map(|f| f(ctx.clone())) 86 .filter_map(|f| f(ctx.clone()))
@@ -162,9 +165,10 @@ mod helpers {
162 use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; 165 use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
163 use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; 166 use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
164 use ra_syntax::TextRange; 167 use ra_syntax::TextRange;
165 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; 168 use test_utils::{add_cursor, assert_eq_text, extract_range_or_offset, RangeOrOffset};
166 169
167 use crate::{AssistCtx, AssistHandler}; 170 use crate::{AssistCtx, AssistHandler};
171 use hir::Semantics;
168 172
169 pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { 173 pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
170 let (mut db, file_id) = RootDatabase::with_single_file(text); 174 let (mut db, file_id) = RootDatabase::with_single_file(text);
@@ -176,81 +180,66 @@ mod helpers {
176 } 180 }
177 181
178 pub(crate) fn check_assist(assist: AssistHandler, before: &str, after: &str) { 182 pub(crate) fn check_assist(assist: AssistHandler, before: &str, after: &str) {
179 let (before_cursor_pos, before) = extract_offset(before); 183 check(assist, before, ExpectedResult::After(after));
180 let (db, file_id) = with_single_file(&before);
181 let frange =
182 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
183 let assist =
184 assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable");
185 let action = assist.0[0].action.clone().unwrap();
186
187 let actual = action.edit.apply(&before);
188 let actual_cursor_pos = match action.cursor_position {
189 None => action
190 .edit
191 .apply_to_offset(before_cursor_pos)
192 .expect("cursor position is affected by the edit"),
193 Some(off) => off,
194 };
195 let actual = add_cursor(&actual, actual_cursor_pos);
196 assert_eq_text!(after, &actual);
197 }
198
199 pub(crate) fn check_assist_range(assist: AssistHandler, before: &str, after: &str) {
200 let (range, before) = extract_range(before);
201 let (db, file_id) = with_single_file(&before);
202 let frange = FileRange { file_id, range };
203 let assist =
204 assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable");
205 let action = assist.0[0].action.clone().unwrap();
206
207 let mut actual = action.edit.apply(&before);
208 if let Some(pos) = action.cursor_position {
209 actual = add_cursor(&actual, pos);
210 }
211 assert_eq_text!(after, &actual);
212 } 184 }
213 185
186 // FIXME: instead of having a separate function here, maybe use
187 // `extract_ranges` and mark the target as `<target> </target>` in the
188 // fixuture?
214 pub(crate) fn check_assist_target(assist: AssistHandler, before: &str, target: &str) { 189 pub(crate) fn check_assist_target(assist: AssistHandler, before: &str, target: &str) {
215 let (before_cursor_pos, before) = extract_offset(before); 190 check(assist, before, ExpectedResult::Target(target));
216 let (db, file_id) = with_single_file(&before);
217 let frange =
218 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
219 let assist =
220 assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable");
221 let action = assist.0[0].action.clone().unwrap();
222
223 let range = action.target.expect("expected target on action");
224 assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
225 } 191 }
226 192
227 pub(crate) fn check_assist_range_target(assist: AssistHandler, before: &str, target: &str) { 193 pub(crate) fn check_assist_not_applicable(assist: AssistHandler, before: &str) {
228 let (range, before) = extract_range(before); 194 check(assist, before, ExpectedResult::NotApplicable);
229 let (db, file_id) = with_single_file(&before);
230 let frange = FileRange { file_id, range };
231 let assist =
232 assist(AssistCtx::new(&db, frange, true)).expect("code action is not applicable");
233 let action = assist.0[0].action.clone().unwrap();
234
235 let range = action.target.expect("expected target on action");
236 assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
237 } 195 }
238 196
239 pub(crate) fn check_assist_not_applicable(assist: AssistHandler, before: &str) { 197 enum ExpectedResult<'a> {
240 let (before_cursor_pos, before) = extract_offset(before); 198 NotApplicable,
241 let (db, file_id) = with_single_file(&before); 199 After(&'a str),
242 let frange = 200 Target(&'a str),
243 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
244 let assist = assist(AssistCtx::new(&db, frange, true));
245 assert!(assist.is_none());
246 } 201 }
247 202
248 pub(crate) fn check_assist_range_not_applicable(assist: AssistHandler, before: &str) { 203 fn check(assist: AssistHandler, before: &str, expected: ExpectedResult) {
249 let (range, before) = extract_range(before); 204 let (range_or_offset, before) = extract_range_or_offset(before);
205 let range: TextRange = range_or_offset.into();
206
250 let (db, file_id) = with_single_file(&before); 207 let (db, file_id) = with_single_file(&before);
251 let frange = FileRange { file_id, range }; 208 let frange = FileRange { file_id, range };
252 let assist = assist(AssistCtx::new(&db, frange, true)); 209 let sema = Semantics::new(&db);
253 assert!(assist.is_none()); 210 let assist_ctx = AssistCtx::new(&sema, frange, true);
211
212 match (assist(assist_ctx), expected) {
213 (Some(assist), ExpectedResult::After(after)) => {
214 let action = assist.0[0].action.clone().unwrap();
215
216 let mut actual = action.edit.apply(&before);
217 match action.cursor_position {
218 None => {
219 if let RangeOrOffset::Offset(before_cursor_pos) = range_or_offset {
220 let off = action
221 .edit
222 .apply_to_offset(before_cursor_pos)
223 .expect("cursor position is affected by the edit");
224 actual = add_cursor(&actual, off)
225 }
226 }
227 Some(off) => actual = add_cursor(&actual, off),
228 };
229
230 assert_eq_text!(after, &actual);
231 }
232 (Some(assist), ExpectedResult::Target(target)) => {
233 let action = assist.0[0].action.clone().unwrap();
234 let range = action.target.expect("expected target on action");
235 assert_eq_text!(&before[range.start().to_usize()..range.end().to_usize()], target);
236 }
237 (Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"),
238 (None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => {
239 panic!("code action is not applicable")
240 }
241 (None, ExpectedResult::NotApplicable) => (),
242 };
254 } 243 }
255} 244}
256 245
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs
index 6ff44c95c..92d3ed471 100644
--- a/crates/ra_assists/src/utils.rs
+++ b/crates/ra_assists/src/utils.rs
@@ -1,16 +1,15 @@
1//! Assorted functions shared by several assists. 1//! Assorted functions shared by several assists.
2 2
3use hir::Semantics;
4use ra_ide_db::RootDatabase;
3use ra_syntax::{ 5use ra_syntax::{
4 ast::{self, make, NameOwner}, 6 ast::{self, make, NameOwner},
5 AstNode, T, 7 AstNode, T,
6}; 8};
7
8use hir::db::HirDatabase;
9use rustc_hash::FxHashSet; 9use rustc_hash::FxHashSet;
10 10
11pub fn get_missing_impl_items( 11pub fn get_missing_impl_items(
12 db: &impl HirDatabase, 12 sema: &Semantics<RootDatabase>,
13 analyzer: &hir::SourceAnalyzer,
14 impl_block: &ast::ImplBlock, 13 impl_block: &ast::ImplBlock,
15) -> Vec<hir::AssocItem> { 14) -> Vec<hir::AssocItem> {
16 // Names must be unique between constants and functions. However, type aliases 15 // Names must be unique between constants and functions. However, type aliases
@@ -42,15 +41,17 @@ pub fn get_missing_impl_items(
42 } 41 }
43 } 42 }
44 43
45 resolve_target_trait(db, analyzer, impl_block).map_or(vec![], |target_trait| { 44 resolve_target_trait(sema, impl_block).map_or(vec![], |target_trait| {
46 target_trait 45 target_trait
47 .items(db) 46 .items(sema.db)
48 .iter() 47 .iter()
49 .filter(|i| match i { 48 .filter(|i| match i {
50 hir::AssocItem::Function(f) => !impl_fns_consts.contains(&f.name(db).to_string()), 49 hir::AssocItem::Function(f) => {
51 hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()), 50 !impl_fns_consts.contains(&f.name(sema.db).to_string())
51 }
52 hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(sema.db).to_string()),
52 hir::AssocItem::Const(c) => c 53 hir::AssocItem::Const(c) => c
53 .name(db) 54 .name(sema.db)
54 .map(|n| !impl_fns_consts.contains(&n.to_string())) 55 .map(|n| !impl_fns_consts.contains(&n.to_string()))
55 .unwrap_or_default(), 56 .unwrap_or_default(),
56 }) 57 })
@@ -60,8 +61,7 @@ pub fn get_missing_impl_items(
60} 61}
61 62
62pub(crate) fn resolve_target_trait( 63pub(crate) fn resolve_target_trait(
63 db: &impl HirDatabase, 64 sema: &Semantics<RootDatabase>,
64 analyzer: &hir::SourceAnalyzer,
65 impl_block: &ast::ImplBlock, 65 impl_block: &ast::ImplBlock,
66) -> Option<hir::Trait> { 66) -> Option<hir::Trait> {
67 let ast_path = impl_block 67 let ast_path = impl_block
@@ -70,7 +70,7 @@ pub(crate) fn resolve_target_trait(
70 .and_then(ast::PathType::cast)? 70 .and_then(ast::PathType::cast)?
71 .path()?; 71 .path()?;
72 72
73 match analyzer.resolve_path(db, &ast_path) { 73 match sema.resolve_path(&ast_path) {
74 Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def), 74 Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),
75 _ => None, 75 _ => None,
76 } 76 }
diff --git a/crates/ra_cargo_watch/Cargo.toml b/crates/ra_cargo_watch/Cargo.toml
index b09650d98..1fdbffea1 100644
--- a/crates/ra_cargo_watch/Cargo.toml
+++ b/crates/ra_cargo_watch/Cargo.toml
@@ -6,13 +6,10 @@ authors = ["rust-analyzer developers"]
6 6
7[dependencies] 7[dependencies]
8crossbeam-channel = "0.4.0" 8crossbeam-channel = "0.4.0"
9lsp-types = { version = "0.70.1", features = ["proposed"] } 9lsp-types = { version = "0.71.0", features = ["proposed"] }
10log = "0.4.8" 10log = "0.4.8"
11cargo_metadata = "0.9.1" 11cargo_metadata = "0.9.1"
12jod-thread = "0.1.0"
13parking_lot = "0.10.0"
14serde_json = "1.0.48" 12serde_json = "1.0.48"
15 13
16[dev-dependencies] 14[dev-dependencies]
17insta = "0.13.1" 15insta = "0.13.1"
18serde_json = "1.0.48"
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index c16c17072..3aa7c4870 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -40,6 +40,7 @@ from_id![
40 (hir_def::ConstId, crate::Const), 40 (hir_def::ConstId, crate::Const),
41 (hir_def::FunctionId, crate::Function), 41 (hir_def::FunctionId, crate::Function),
42 (hir_def::ImplId, crate::ImplBlock), 42 (hir_def::ImplId, crate::ImplBlock),
43 (hir_def::TypeParamId, crate::TypeParam),
43 (hir_expand::MacroDefId, crate::MacroDef) 44 (hir_expand::MacroDefId, crate::MacroDef)
44]; 45];
45 46
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 7a9745ebe..4a85e7e36 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -26,6 +26,7 @@ macro_rules! impl_froms {
26 } 26 }
27} 27}
28 28
29mod semantics;
29pub mod db; 30pub mod db;
30pub mod source_analyzer; 31pub mod source_analyzer;
31pub mod source_binder; 32pub mod source_binder;
@@ -45,8 +46,8 @@ pub use crate::{
45 StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, 46 StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
46 }, 47 },
47 has_source::HasSource, 48 has_source::HasSource,
48 source_analyzer::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, 49 semantics::{original_range, Semantics, SemanticsScope},
49 source_binder::SourceBinder, 50 source_analyzer::PathResolution,
50}; 51};
51 52
52pub use hir_def::{ 53pub use hir_def::{
diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs
new file mode 100644
index 000000000..c3d8ee1ae
--- /dev/null
+++ b/crates/ra_hir/src/semantics.rs
@@ -0,0 +1,412 @@
1//! See `Semantics`.
2
3use std::{cell::RefCell, fmt, iter::successors};
4
5use hir_def::{
6 resolver::{self, HasResolver, Resolver},
7 DefWithBodyId, TraitId,
8};
9use ra_db::{FileId, FileRange};
10use ra_syntax::{
11 algo::{find_covering_element, skip_trivia_token},
12 ast, match_ast, AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
13 TextRange, TextUnit,
14};
15use rustc_hash::{FxHashMap, FxHashSet};
16
17use crate::{
18 db::HirDatabase,
19 source_analyzer::{resolve_hir_path, ReferenceDescriptor, SourceAnalyzer},
20 source_binder::{ChildContainer, SourceBinder},
21 Function, HirFileId, InFile, Local, MacroDef, Module, Name, Origin, Path, PathResolution,
22 ScopeDef, StructField, Trait, Type, TypeParam, VariantDef,
23};
24use ra_prof::profile;
25
26/// Primary API to get semantic information, like types, from syntax trees.
27pub struct Semantics<'db, DB> {
28 pub db: &'db DB,
29 pub(crate) sb: RefCell<SourceBinder>,
30 cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
31}
32
33impl<DB> fmt::Debug for Semantics<'_, DB> {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 write!(f, "Semantics {{ ... }}")
36 }
37}
38
39impl<'db, DB: HirDatabase> Semantics<'db, DB> {
40 pub fn new(db: &DB) -> Semantics<DB> {
41 let sb = RefCell::new(SourceBinder::new());
42 Semantics { db, sb, cache: RefCell::default() }
43 }
44
45 pub fn parse(&self, file_id: FileId) -> ast::SourceFile {
46 let tree = self.db.parse(file_id).tree();
47 self.cache(tree.syntax().clone(), file_id.into());
48 tree
49 }
50
51 pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
52 let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
53 let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
54 let file_id = sa.expand(self.db, macro_call)?;
55 let node = self.db.parse_or_expand(file_id)?;
56 self.cache(node.clone(), file_id);
57 Some(node)
58 }
59
60 pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
61 let parent = token.parent();
62 let parent = self.find_file(parent);
63 let sa = self.analyze2(parent.as_ref(), None);
64
65 let token = successors(Some(parent.with_value(token)), |token| {
66 let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?;
67 let tt = macro_call.token_tree()?;
68 if !token.value.text_range().is_subrange(&tt.syntax().text_range()) {
69 return None;
70 }
71 let file_id = sa.expand(self.db, token.with_value(&macro_call))?;
72 let token = file_id.expansion_info(self.db)?.map_token_down(token.as_ref())?;
73
74 self.cache(find_root(&token.value.parent()), token.file_id);
75
76 Some(token)
77 })
78 .last()
79 .unwrap();
80
81 token.value
82 }
83
84 pub fn original_range(&self, node: &SyntaxNode) -> FileRange {
85 let node = self.find_file(node.clone());
86 original_range(self.db, node.as_ref())
87 }
88
89 pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
90 let node = self.find_file(node);
91 node.ancestors_with_macros(self.db).map(|it| it.value)
92 }
93
94 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
95 self.analyze(expr.syntax()).type_of(self.db, &expr)
96 }
97
98 pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
99 self.analyze(pat.syntax()).type_of_pat(self.db, &pat)
100 }
101
102 pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
103 self.analyze(call.syntax()).resolve_method_call(call)
104 }
105
106 pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<StructField> {
107 self.analyze(field.syntax()).resolve_field(field)
108 }
109
110 pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<StructField> {
111 self.analyze(field.syntax()).resolve_record_field(field)
112 }
113
114 pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<VariantDef> {
115 self.analyze(record_lit.syntax()).resolve_record_literal(record_lit)
116 }
117
118 pub fn resolve_record_pattern(&self, record_pat: &ast::RecordPat) -> Option<VariantDef> {
119 self.analyze(record_pat.syntax()).resolve_record_pattern(record_pat)
120 }
121
122 pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {
123 let sa = self.analyze(macro_call.syntax());
124 let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
125 sa.resolve_macro_call(self.db, macro_call)
126 }
127
128 pub fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
129 self.analyze(path.syntax()).resolve_path(self.db, path)
130 }
131
132 // FIXME: use this instead?
133 // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;
134
135 pub fn to_def<T: ToDef + Clone>(&self, src: &T) -> Option<T::Def> {
136 T::to_def(self, src)
137 }
138
139 pub fn to_module_def(&self, file: FileId) -> Option<Module> {
140 let mut sb = self.sb.borrow_mut();
141 sb.to_module_def(self.db, file)
142 }
143
144 pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db, DB> {
145 let node = self.find_file(node.clone());
146 let resolver = self.analyze2(node.as_ref(), None).resolver;
147 SemanticsScope { db: self.db, resolver }
148 }
149
150 pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextUnit) -> SemanticsScope<'db, DB> {
151 let node = self.find_file(node.clone());
152 let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver;
153 SemanticsScope { db: self.db, resolver }
154 }
155
156 pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db, DB> {
157 let resolver = def.id.resolver(self.db);
158 SemanticsScope { db: self.db, resolver }
159 }
160
161 // FIXME: we only use this in `inline_local_variable` assist, ideally, we
162 // should switch to general reference search infra there.
163 pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
164 self.analyze(pat.syntax()).find_all_refs(pat)
165 }
166
167 fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
168 let src = self.find_file(node.clone());
169 self.analyze2(src.as_ref(), None)
170 }
171
172 fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option<TextUnit>) -> SourceAnalyzer {
173 let _p = profile("Semantics::analyze2");
174
175 let container = match self.sb.borrow_mut().find_container(self.db, src) {
176 Some(it) => it,
177 None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
178 };
179
180 let resolver = match container {
181 ChildContainer::DefWithBodyId(def) => {
182 return SourceAnalyzer::new_for_body(self.db, def, src, offset)
183 }
184 ChildContainer::TraitId(it) => it.resolver(self.db),
185 ChildContainer::ImplId(it) => it.resolver(self.db),
186 ChildContainer::ModuleId(it) => it.resolver(self.db),
187 ChildContainer::EnumId(it) => it.resolver(self.db),
188 ChildContainer::VariantId(it) => it.resolver(self.db),
189 ChildContainer::GenericDefId(it) => it.resolver(self.db),
190 };
191 SourceAnalyzer::new_for_resolver(resolver, src)
192 }
193
194 fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {
195 assert!(root_node.parent().is_none());
196 let mut cache = self.cache.borrow_mut();
197 let prev = cache.insert(root_node, file_id);
198 assert!(prev == None || prev == Some(file_id))
199 }
200
201 pub fn assert_contains_node(&self, node: &SyntaxNode) {
202 self.find_file(node.clone());
203 }
204
205 fn lookup(&self, root_node: &SyntaxNode) -> Option<HirFileId> {
206 let cache = self.cache.borrow();
207 cache.get(root_node).copied()
208 }
209
210 fn find_file(&self, node: SyntaxNode) -> InFile<SyntaxNode> {
211 let root_node = find_root(&node);
212 let file_id = self.lookup(&root_node).unwrap_or_else(|| {
213 panic!(
214 "\n\nFailed to lookup {:?} in this Semantics.\n\
215 Make sure to use only query nodes, derived from this instance of Semantics.\n\
216 root node: {:?}\n\
217 known nodes: {}\n\n",
218 node,
219 root_node,
220 self.cache
221 .borrow()
222 .keys()
223 .map(|it| format!("{:?}", it))
224 .collect::<Vec<_>>()
225 .join(", ")
226 )
227 });
228 InFile::new(file_id, node)
229 }
230}
231
232pub trait ToDef: Sized + AstNode + 'static {
233 type Def;
234 fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self) -> Option<Self::Def>;
235}
236
237macro_rules! to_def_impls {
238 ($(($def:path, $ast:path)),* ,) => {$(
239 impl ToDef for $ast {
240 type Def = $def;
241 fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self)
242 -> Option<Self::Def>
243 {
244 let src = sema.find_file(src.syntax().clone()).with_value(src);
245 sema.sb.borrow_mut().to_id(sema.db, src.cloned()).map(Into::into)
246 }
247 }
248 )*}
249}
250
251to_def_impls![
252 (crate::Module, ast::Module),
253 (crate::Struct, ast::StructDef),
254 (crate::Enum, ast::EnumDef),
255 (crate::Union, ast::UnionDef),
256 (crate::Trait, ast::TraitDef),
257 (crate::ImplBlock, ast::ImplBlock),
258 (crate::TypeAlias, ast::TypeAliasDef),
259 (crate::Const, ast::ConstDef),
260 (crate::Static, ast::StaticDef),
261 (crate::Function, ast::FnDef),
262 (crate::StructField, ast::RecordFieldDef),
263 (crate::EnumVariant, ast::EnumVariant),
264 (crate::TypeParam, ast::TypeParam),
265 (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
266];
267
268impl ToDef for ast::BindPat {
269 type Def = Local;
270
271 fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self) -> Option<Local> {
272 let src = sema.find_file(src.syntax().clone()).with_value(src);
273 let file_id = src.file_id;
274 let mut sb = sema.sb.borrow_mut();
275 let db = sema.db;
276 let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| {
277 let res = match_ast! {
278 match it {
279 ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
280 ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
281 ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
282 _ => return None,
283 }
284 };
285 Some(res)
286 })?;
287 let (_body, source_map) = db.body_with_source_map(parent);
288 let src = src.cloned().map(ast::Pat::from);
289 let pat_id = source_map.node_pat(src.as_ref())?;
290 Some(Local { parent: parent.into(), pat_id })
291 }
292}
293
294fn find_root(node: &SyntaxNode) -> SyntaxNode {
295 node.ancestors().last().unwrap()
296}
297
298pub struct SemanticsScope<'a, DB> {
299 pub db: &'a DB,
300 resolver: Resolver,
301}
302
303impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> {
304 pub fn module(&self) -> Option<Module> {
305 Some(Module { id: self.resolver.module()? })
306 }
307
308 /// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type
309 // FIXME: rename to visible_traits to not repeat scope?
310 pub fn traits_in_scope(&self) -> FxHashSet<TraitId> {
311 let resolver = &self.resolver;
312 resolver.traits_in_scope(self.db)
313 }
314
315 pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
316 let resolver = &self.resolver;
317
318 resolver.process_all_names(self.db, &mut |name, def| {
319 let def = match def {
320 resolver::ScopeDef::PerNs(it) => it.into(),
321 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
322 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
323 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }),
324 resolver::ScopeDef::Local(pat_id) => {
325 let parent = resolver.body_owner().unwrap().into();
326 ScopeDef::Local(Local { parent, pat_id })
327 }
328 };
329 f(name, def)
330 })
331 }
332
333 pub fn resolve_hir_path(&self, path: &Path) -> Option<PathResolution> {
334 resolve_hir_path(self.db, &self.resolver, path)
335 }
336}
337
338// FIXME: Change `HasSource` trait to work with `Semantics` and remove this?
339pub fn original_range(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> FileRange {
340 let mut elem: InFile<SyntaxElement> = node.map(|n| n.clone().into());
341
342 while let Some((range, Origin::Call)) = original_range_and_origin(db, elem.as_ref()) {
343 let original_file = range.file_id.original_file(db);
344
345 if range.file_id == original_file.into() {
346 return FileRange { file_id: original_file, range: range.value };
347 }
348
349 if range.file_id != elem.file_id {
350 if let Some(root) = db.parse_or_expand(range.file_id) {
351 elem = range.with_value(find_covering_element(&root, range.value));
352 continue;
353 }
354 }
355
356 log::error!("Fail to mapping up more for {:?}", range);
357 return FileRange { file_id: range.file_id.original_file(db), range: range.value };
358 }
359
360 // Fall back to whole macro call
361 if let Some(expansion) = node.file_id.expansion_info(db) {
362 if let Some(call_node) = expansion.call_node() {
363 return FileRange {
364 file_id: call_node.file_id.original_file(db),
365 range: call_node.value.text_range(),
366 };
367 }
368 }
369
370 FileRange { file_id: node.file_id.original_file(db), range: node.value.text_range() }
371}
372
373fn original_range_and_origin(
374 db: &impl HirDatabase,
375 elem: InFile<&SyntaxElement>,
376) -> Option<(InFile<TextRange>, Origin)> {
377 let expansion = elem.file_id.expansion_info(db)?;
378
379 let node = match elem.as_ref().value {
380 NodeOrToken::Node(it) => elem.with_value(it),
381 NodeOrToken::Token(it) => {
382 let (tt, origin) = expansion.map_token_up(elem.with_value(it))?;
383 return Some((tt.map(|it| it.text_range()), origin));
384 }
385 };
386
387 // the input node has only one token ?
388 let single = skip_trivia_token(node.value.first_token()?, Direction::Next)?
389 == skip_trivia_token(node.value.last_token()?, Direction::Prev)?;
390
391 Some(node.value.descendants().find_map(|it| {
392 let first = skip_trivia_token(it.first_token()?, Direction::Next)?;
393 let last = skip_trivia_token(it.last_token()?, Direction::Prev)?;
394
395 if !single && first == last {
396 return None;
397 }
398
399 // Try to map first and last tokens of node, and, if success, return the union range of mapped tokens
400 let (first, first_origin) = expansion.map_token_up(node.with_value(&first))?;
401 let (last, last_origin) = expansion.map_token_up(node.with_value(&last))?;
402
403 if first.file_id != last.file_id || first_origin != last_origin {
404 return None;
405 }
406
407 Some((
408 first.with_value(first.value.text_range().extend_to(&last.value.text_range())),
409 first_origin,
410 ))
411 })?)
412}
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index efa3f8a79..b655e2c32 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -14,29 +14,27 @@ use hir_def::{
14 BodySourceMap, 14 BodySourceMap,
15 }, 15 },
16 expr::{ExprId, PatId}, 16 expr::{ExprId, PatId},
17 resolver::{self, resolver_for_scope, Resolver, TypeNs, ValueNs}, 17 resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs},
18 AsMacroCall, DefWithBodyId, TraitId, 18 AsMacroCall, DefWithBodyId,
19}; 19};
20use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile, MacroCallId}; 20use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
21use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment}; 21use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment};
22use ra_syntax::{ 22use ra_syntax::{
23 ast::{self, AstNode}, 23 ast::{self, AstNode},
24 AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextUnit, 24 AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
25}; 25};
26use rustc_hash::FxHashSet;
27 26
28use crate::{ 27use crate::{
29 db::HirDatabase, Adt, Const, DefWithBody, EnumVariant, Function, Local, MacroDef, Name, Path, 28 db::HirDatabase, Adt, Const, EnumVariant, Function, Local, MacroDef, Path, Static, Struct,
30 ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, 29 Trait, Type, TypeAlias, TypeParam,
31}; 30};
32 31
33/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of 32/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
34/// original source files. It should not be used inside the HIR itself. 33/// original source files. It should not be used inside the HIR itself.
35#[derive(Debug)] 34#[derive(Debug)]
36pub struct SourceAnalyzer { 35pub(crate) struct SourceAnalyzer {
37 file_id: HirFileId, 36 file_id: HirFileId,
38 resolver: Resolver, 37 pub(crate) resolver: Resolver,
39 body_owner: Option<DefWithBody>,
40 body_source_map: Option<Arc<BodySourceMap>>, 38 body_source_map: Option<Arc<BodySourceMap>>,
41 infer: Option<Arc<InferenceResult>>, 39 infer: Option<Arc<InferenceResult>>,
42 scopes: Option<Arc<ExprScopes>>, 40 scopes: Option<Arc<ExprScopes>>,
@@ -55,57 +53,13 @@ pub enum PathResolution {
55 AssocItem(crate::AssocItem), 53 AssocItem(crate::AssocItem),
56} 54}
57 55
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct ScopeEntryWithSyntax {
60 pub(crate) name: Name,
61 pub(crate) ptr: Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>,
62}
63
64impl ScopeEntryWithSyntax {
65 pub fn name(&self) -> &Name {
66 &self.name
67 }
68
69 pub fn ptr(&self) -> Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>> {
70 self.ptr
71 }
72}
73
74#[derive(Debug)] 56#[derive(Debug)]
75pub struct ReferenceDescriptor { 57pub struct ReferenceDescriptor {
76 pub range: TextRange, 58 pub range: TextRange,
77 pub name: String, 59 pub name: String,
78} 60}
79 61
80#[derive(Debug)]
81pub struct Expansion {
82 macro_call_id: MacroCallId,
83}
84
85impl Expansion {
86 pub fn map_token_down(
87 &self,
88 db: &impl HirDatabase,
89 token: InFile<&SyntaxToken>,
90 ) -> Option<InFile<SyntaxToken>> {
91 let exp_info = self.file_id().expansion_info(db)?;
92 exp_info.map_token_down(token)
93 }
94
95 pub fn file_id(&self) -> HirFileId {
96 self.macro_call_id.as_file()
97 }
98}
99
100impl SourceAnalyzer { 62impl SourceAnalyzer {
101 pub fn new(
102 db: &impl HirDatabase,
103 node: InFile<&SyntaxNode>,
104 offset: Option<TextUnit>,
105 ) -> SourceAnalyzer {
106 crate::source_binder::SourceBinder::new(db).analyze(node, offset)
107 }
108
109 pub(crate) fn new_for_body( 63 pub(crate) fn new_for_body(
110 db: &impl HirDatabase, 64 db: &impl HirDatabase,
111 def: DefWithBodyId, 65 def: DefWithBodyId,
@@ -121,7 +75,6 @@ impl SourceAnalyzer {
121 let resolver = resolver_for_scope(db, def, scope); 75 let resolver = resolver_for_scope(db, def, scope);
122 SourceAnalyzer { 76 SourceAnalyzer {
123 resolver, 77 resolver,
124 body_owner: Some(def.into()),
125 body_source_map: Some(source_map), 78 body_source_map: Some(source_map),
126 infer: Some(db.infer(def)), 79 infer: Some(db.infer(def)),
127 scopes: Some(scopes), 80 scopes: Some(scopes),
@@ -135,7 +88,6 @@ impl SourceAnalyzer {
135 ) -> SourceAnalyzer { 88 ) -> SourceAnalyzer {
136 SourceAnalyzer { 89 SourceAnalyzer {
137 resolver, 90 resolver,
138 body_owner: None,
139 body_source_map: None, 91 body_source_map: None,
140 infer: None, 92 infer: None,
141 scopes: None, 93 scopes: None,
@@ -143,10 +95,6 @@ impl SourceAnalyzer {
143 } 95 }
144 } 96 }
145 97
146 pub fn module(&self) -> Option<crate::code_model::Module> {
147 Some(crate::code_model::Module { id: self.resolver.module()? })
148 }
149
150 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> { 98 fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> {
151 let src = InFile { file_id: self.file_id, value: expr }; 99 let src = InFile { file_id: self.file_id, value: expr };
152 self.body_source_map.as_ref()?.node_expr(src) 100 self.body_source_map.as_ref()?.node_expr(src)
@@ -180,7 +128,7 @@ impl SourceAnalyzer {
180 TraitEnvironment::lower(db, &self.resolver) 128 TraitEnvironment::lower(db, &self.resolver)
181 } 129 }
182 130
183 pub fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { 131 pub(crate) fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> {
184 let expr_id = if let Some(expr) = self.expand_expr(db, InFile::new(self.file_id, expr)) { 132 let expr_id = if let Some(expr) = self.expand_expr(db, InFile::new(self.file_id, expr)) {
185 self.body_source_map.as_ref()?.node_expr(expr.as_ref())? 133 self.body_source_map.as_ref()?.node_expr(expr.as_ref())?
186 } else { 134 } else {
@@ -192,24 +140,27 @@ impl SourceAnalyzer {
192 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) 140 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } })
193 } 141 }
194 142
195 pub fn type_of_pat(&self, db: &impl HirDatabase, pat: &ast::Pat) -> Option<Type> { 143 pub(crate) fn type_of_pat(&self, db: &impl HirDatabase, pat: &ast::Pat) -> Option<Type> {
196 let pat_id = self.pat_id(pat)?; 144 let pat_id = self.pat_id(pat)?;
197 let ty = self.infer.as_ref()?[pat_id].clone(); 145 let ty = self.infer.as_ref()?[pat_id].clone();
198 let environment = self.trait_env(db); 146 let environment = self.trait_env(db);
199 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) 147 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } })
200 } 148 }
201 149
202 pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> { 150 pub(crate) fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
203 let expr_id = self.expr_id(&call.clone().into())?; 151 let expr_id = self.expr_id(&call.clone().into())?;
204 self.infer.as_ref()?.method_resolution(expr_id).map(Function::from) 152 self.infer.as_ref()?.method_resolution(expr_id).map(Function::from)
205 } 153 }
206 154
207 pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> { 155 pub(crate) fn resolve_field(&self, field: &ast::FieldExpr) -> Option<crate::StructField> {
208 let expr_id = self.expr_id(&field.clone().into())?; 156 let expr_id = self.expr_id(&field.clone().into())?;
209 self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into()) 157 self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into())
210 } 158 }
211 159
212 pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<crate::StructField> { 160 pub(crate) fn resolve_record_field(
161 &self,
162 field: &ast::RecordField,
163 ) -> Option<crate::StructField> {
213 let expr_id = match field.expr() { 164 let expr_id = match field.expr() {
214 Some(it) => self.expr_id(&it)?, 165 Some(it) => self.expr_id(&it)?,
215 None => { 166 None => {
@@ -220,17 +171,23 @@ impl SourceAnalyzer {
220 self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into()) 171 self.infer.as_ref()?.record_field_resolution(expr_id).map(|it| it.into())
221 } 172 }
222 173
223 pub fn resolve_record_literal(&self, record_lit: &ast::RecordLit) -> Option<crate::VariantDef> { 174 pub(crate) fn resolve_record_literal(
175 &self,
176 record_lit: &ast::RecordLit,
177 ) -> Option<crate::VariantDef> {
224 let expr_id = self.expr_id(&record_lit.clone().into())?; 178 let expr_id = self.expr_id(&record_lit.clone().into())?;
225 self.infer.as_ref()?.variant_resolution_for_expr(expr_id).map(|it| it.into()) 179 self.infer.as_ref()?.variant_resolution_for_expr(expr_id).map(|it| it.into())
226 } 180 }
227 181
228 pub fn resolve_record_pattern(&self, record_pat: &ast::RecordPat) -> Option<crate::VariantDef> { 182 pub(crate) fn resolve_record_pattern(
183 &self,
184 record_pat: &ast::RecordPat,
185 ) -> Option<crate::VariantDef> {
229 let pat_id = self.pat_id(&record_pat.clone().into())?; 186 let pat_id = self.pat_id(&record_pat.clone().into())?;
230 self.infer.as_ref()?.variant_resolution_for_pat(pat_id).map(|it| it.into()) 187 self.infer.as_ref()?.variant_resolution_for_pat(pat_id).map(|it| it.into())
231 } 188 }
232 189
233 pub fn resolve_macro_call( 190 pub(crate) fn resolve_macro_call(
234 &self, 191 &self,
235 db: &impl HirDatabase, 192 db: &impl HirDatabase,
236 macro_call: InFile<&ast::MacroCall>, 193 macro_call: InFile<&ast::MacroCall>,
@@ -240,52 +197,11 @@ impl SourceAnalyzer {
240 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into()) 197 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into())
241 } 198 }
242 199
243 pub fn resolve_hir_path( 200 pub(crate) fn resolve_path(
244 &self, 201 &self,
245 db: &impl HirDatabase, 202 db: &impl HirDatabase,
246 path: &crate::Path, 203 path: &ast::Path,
247 ) -> Option<PathResolution> { 204 ) -> Option<PathResolution> {
248 let types =
249 self.resolver.resolve_path_in_type_ns_fully(db, path.mod_path()).map(|ty| match ty {
250 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
251 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
252 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
253 PathResolution::Def(Adt::from(it).into())
254 }
255 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
256 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
257 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
258 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
259 });
260 let values =
261 self.resolver.resolve_path_in_value_ns_fully(db, path.mod_path()).and_then(|val| {
262 let res = match val {
263 ValueNs::LocalBinding(pat_id) => {
264 let var = Local { parent: self.body_owner?, pat_id };
265 PathResolution::Local(var)
266 }
267 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
268 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
269 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
270 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
271 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
272 };
273 Some(res)
274 });
275
276 let items = self
277 .resolver
278 .resolve_module_path_in_items(db, path.mod_path())
279 .take_types()
280 .map(|it| PathResolution::Def(it.into()));
281 types.or(values).or(items).or_else(|| {
282 self.resolver
283 .resolve_path_as_macro(db, path.mod_path())
284 .map(|def| PathResolution::Macro(def.into()))
285 })
286 }
287
288 pub fn resolve_path(&self, db: &impl HirDatabase, path: &ast::Path) -> Option<PathResolution> {
289 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { 205 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
290 let expr_id = self.expr_id(&path_expr.into())?; 206 let expr_id = self.expr_id(&path_expr.into())?;
291 if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) { 207 if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_expr(expr_id) {
@@ -300,40 +216,24 @@ impl SourceAnalyzer {
300 } 216 }
301 // This must be a normal source file rather than macro file. 217 // This must be a normal source file rather than macro file.
302 let hir_path = crate::Path::from_ast(path.clone())?; 218 let hir_path = crate::Path::from_ast(path.clone())?;
303 self.resolve_hir_path(db, &hir_path) 219 resolve_hir_path(db, &self.resolver, &hir_path)
304 } 220 }
305 221
306 fn resolve_local_name(&self, name_ref: &ast::NameRef) -> Option<ScopeEntryWithSyntax> { 222 fn resolve_local_name(
223 &self,
224 name_ref: &ast::NameRef,
225 ) -> Option<Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>> {
307 let name = name_ref.as_name(); 226 let name = name_ref.as_name();
308 let source_map = self.body_source_map.as_ref()?; 227 let source_map = self.body_source_map.as_ref()?;
309 let scopes = self.scopes.as_ref()?; 228 let scopes = self.scopes.as_ref()?;
310 let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?; 229 let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?;
311 let entry = scopes.resolve_name_in_scope(scope, &name)?; 230 let entry = scopes.resolve_name_in_scope(scope, &name)?;
312 Some(ScopeEntryWithSyntax { 231 Some(source_map.pat_syntax(entry.pat())?.value)
313 name: entry.name().clone(),
314 ptr: source_map.pat_syntax(entry.pat())?.value,
315 })
316 }
317
318 pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, ScopeDef)) {
319 self.resolver.process_all_names(db, &mut |name, def| {
320 let def = match def {
321 resolver::ScopeDef::PerNs(it) => it.into(),
322 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
323 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
324 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }),
325 resolver::ScopeDef::Local(pat_id) => {
326 let parent = self.resolver.body_owner().unwrap().into();
327 ScopeDef::Local(Local { parent, pat_id })
328 }
329 };
330 f(name, def)
331 })
332 } 232 }
333 233
334 // FIXME: we only use this in `inline_local_variable` assist, ideally, we 234 // FIXME: we only use this in `inline_local_variable` assist, ideally, we
335 // should switch to general reference search infra there. 235 // should switch to general reference search infra there.
336 pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> { 236 pub(crate) fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
337 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap(); 237 let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
338 let ptr = Either::Left(AstPtr::new(&ast::Pat::from(pat.clone()))); 238 let ptr = Either::Left(AstPtr::new(&ast::Pat::from(pat.clone())));
339 fn_def 239 fn_def
@@ -342,7 +242,7 @@ impl SourceAnalyzer {
342 .filter_map(ast::NameRef::cast) 242 .filter_map(ast::NameRef::cast)
343 .filter(|name_ref| match self.resolve_local_name(&name_ref) { 243 .filter(|name_ref| match self.resolve_local_name(&name_ref) {
344 None => false, 244 None => false,
345 Some(entry) => entry.ptr() == ptr, 245 Some(d_ptr) => d_ptr == ptr,
346 }) 246 })
347 .map(|name_ref| ReferenceDescriptor { 247 .map(|name_ref| ReferenceDescriptor {
348 name: name_ref.text().to_string(), 248 name: name_ref.text().to_string(),
@@ -351,19 +251,14 @@ impl SourceAnalyzer {
351 .collect() 251 .collect()
352 } 252 }
353 253
354 /// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type 254 pub(crate) fn expand(
355 pub fn traits_in_scope(&self, db: &impl HirDatabase) -> FxHashSet<TraitId> {
356 self.resolver.traits_in_scope(db)
357 }
358
359 pub fn expand(
360 &self, 255 &self,
361 db: &impl HirDatabase, 256 db: &impl HirDatabase,
362 macro_call: InFile<&ast::MacroCall>, 257 macro_call: InFile<&ast::MacroCall>,
363 ) -> Option<Expansion> { 258 ) -> Option<HirFileId> {
364 let macro_call_id = 259 let macro_call_id =
365 macro_call.as_call_id(db, |path| self.resolver.resolve_path_as_macro(db, &path))?; 260 macro_call.as_call_id(db, |path| self.resolver.resolve_path_as_macro(db, &path))?;
366 Some(Expansion { macro_call_id }) 261 Some(macro_call_id.as_file())
367 } 262 }
368} 263}
369 264
@@ -409,6 +304,47 @@ fn scope_for_offset(
409 }) 304 })
410} 305}
411 306
307pub(crate) fn resolve_hir_path(
308 db: &impl HirDatabase,
309 resolver: &Resolver,
310 path: &crate::Path,
311) -> Option<PathResolution> {
312 let types = resolver.resolve_path_in_type_ns_fully(db, path.mod_path()).map(|ty| match ty {
313 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
314 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
315 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
316 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
317 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
318 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
319 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
320 });
321 let body_owner = resolver.body_owner();
322 let values = resolver.resolve_path_in_value_ns_fully(db, path.mod_path()).and_then(|val| {
323 let res = match val {
324 ValueNs::LocalBinding(pat_id) => {
325 let var = Local { parent: body_owner?.into(), pat_id };
326 PathResolution::Local(var)
327 }
328 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
329 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
330 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
331 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
332 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
333 };
334 Some(res)
335 });
336
337 let items = resolver
338 .resolve_module_path_in_items(db, path.mod_path())
339 .take_types()
340 .map(|it| PathResolution::Def(it.into()));
341 types.or(values).or(items).or_else(|| {
342 resolver
343 .resolve_path_as_macro(db, path.mod_path())
344 .map(|def| PathResolution::Macro(def.into()))
345 })
346}
347
412// XXX: during completion, cursor might be outside of any particular 348// XXX: during completion, cursor might be outside of any particular
413// expression. Try to figure out the correct scope... 349// expression. Try to figure out the correct scope...
414fn adjust( 350fn adjust(
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index f3150f578..4353e25ac 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -5,112 +5,89 @@ use hir_def::{
5 child_by_source::ChildBySource, 5 child_by_source::ChildBySource,
6 dyn_map::DynMap, 6 dyn_map::DynMap,
7 keys::{self, Key}, 7 keys::{self, Key},
8 resolver::{HasResolver, Resolver},
9 ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, 8 ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId,
10 StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, 9 StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
11}; 10};
12use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; 11use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind};
12use ra_db::FileId;
13use ra_prof::profile; 13use ra_prof::profile;
14use ra_syntax::{ 14use ra_syntax::{
15 ast::{self, NameOwner}, 15 ast::{self, NameOwner},
16 match_ast, AstNode, SyntaxNode, TextUnit, 16 match_ast, AstNode, SyntaxNode,
17}; 17};
18use rustc_hash::FxHashMap; 18use rustc_hash::FxHashMap;
19 19
20use crate::{db::HirDatabase, Local, Module, SourceAnalyzer, TypeParam}; 20use crate::{db::HirDatabase, Module};
21use ra_db::FileId;
22 21
23pub struct SourceBinder<'a, DB> { 22pub(crate) struct SourceBinder {
24 pub db: &'a DB,
25 child_by_source_cache: FxHashMap<ChildContainer, DynMap>, 23 child_by_source_cache: FxHashMap<ChildContainer, DynMap>,
26} 24}
27 25
28impl<DB: HirDatabase> SourceBinder<'_, DB> { 26impl SourceBinder {
29 pub fn new(db: &DB) -> SourceBinder<DB> { 27 pub(crate) fn new() -> SourceBinder {
30 SourceBinder { db, child_by_source_cache: FxHashMap::default() } 28 SourceBinder { child_by_source_cache: FxHashMap::default() }
31 }
32
33 pub fn analyze(
34 &mut self,
35 src: InFile<&SyntaxNode>,
36 offset: Option<TextUnit>,
37 ) -> SourceAnalyzer {
38 let _p = profile("SourceBinder::analyzer");
39 let container = match self.find_container(src) {
40 Some(it) => it,
41 None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
42 };
43
44 let resolver = match container {
45 ChildContainer::DefWithBodyId(def) => {
46 return SourceAnalyzer::new_for_body(self.db, def, src, offset)
47 }
48 ChildContainer::TraitId(it) => it.resolver(self.db),
49 ChildContainer::ImplId(it) => it.resolver(self.db),
50 ChildContainer::ModuleId(it) => it.resolver(self.db),
51 ChildContainer::EnumId(it) => it.resolver(self.db),
52 ChildContainer::VariantId(it) => it.resolver(self.db),
53 ChildContainer::GenericDefId(it) => it.resolver(self.db),
54 };
55 SourceAnalyzer::new_for_resolver(resolver, src)
56 }
57
58 pub fn to_def<T: ToDef>(&mut self, src: InFile<T>) -> Option<T::Def> {
59 T::to_def(self, src)
60 } 29 }
61 30
62 pub fn to_module_def(&mut self, file: FileId) -> Option<Module> { 31 pub(crate) fn to_module_def(&mut self, db: &impl HirDatabase, file: FileId) -> Option<Module> {
63 let _p = profile("SourceBinder::to_module_def"); 32 let _p = profile("SourceBinder::to_module_def");
64 let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { 33 let (krate, local_id) = db.relevant_crates(file).iter().find_map(|&crate_id| {
65 let crate_def_map = self.db.crate_def_map(crate_id); 34 let crate_def_map = db.crate_def_map(crate_id);
66 let local_id = crate_def_map.modules_for_file(file).next()?; 35 let local_id = crate_def_map.modules_for_file(file).next()?;
67 Some((crate_id, local_id)) 36 Some((crate_id, local_id))
68 })?; 37 })?;
69 Some(Module { id: ModuleId { krate, local_id } }) 38 Some(Module { id: ModuleId { krate, local_id } })
70 } 39 }
71 40
72 fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> { 41 pub(crate) fn to_id<T: ToId>(
73 T::to_id(self, src) 42 &mut self,
43 db: &impl HirDatabase,
44 src: InFile<T>,
45 ) -> Option<T::ID> {
46 T::to_id(db, self, src)
74 } 47 }
75 48
76 fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> { 49 pub(crate) fn find_container(
77 for container in src.cloned().ancestors_with_macros(self.db).skip(1) { 50 &mut self,
51 db: &impl HirDatabase,
52 src: InFile<&SyntaxNode>,
53 ) -> Option<ChildContainer> {
54 for container in src.cloned().ancestors_with_macros(db).skip(1) {
78 let res: ChildContainer = match_ast! { 55 let res: ChildContainer = match_ast! {
79 match (container.value) { 56 match (container.value) {
80 ast::TraitDef(it) => { 57 ast::TraitDef(it) => {
81 let def: TraitId = self.to_id(container.with_value(it))?; 58 let def: TraitId = self.to_id(db, container.with_value(it))?;
82 def.into() 59 def.into()
83 }, 60 },
84 ast::ImplBlock(it) => { 61 ast::ImplBlock(it) => {
85 let def: ImplId = self.to_id(container.with_value(it))?; 62 let def: ImplId = self.to_id(db, container.with_value(it))?;
86 def.into() 63 def.into()
87 }, 64 },
88 ast::FnDef(it) => { 65 ast::FnDef(it) => {
89 let def: FunctionId = self.to_id(container.with_value(it))?; 66 let def: FunctionId = self.to_id(db, container.with_value(it))?;
90 DefWithBodyId::from(def).into() 67 DefWithBodyId::from(def).into()
91 }, 68 },
92 ast::StaticDef(it) => { 69 ast::StaticDef(it) => {
93 let def: StaticId = self.to_id(container.with_value(it))?; 70 let def: StaticId = self.to_id(db, container.with_value(it))?;
94 DefWithBodyId::from(def).into() 71 DefWithBodyId::from(def).into()
95 }, 72 },
96 ast::ConstDef(it) => { 73 ast::ConstDef(it) => {
97 let def: ConstId = self.to_id(container.with_value(it))?; 74 let def: ConstId = self.to_id(db, container.with_value(it))?;
98 DefWithBodyId::from(def).into() 75 DefWithBodyId::from(def).into()
99 }, 76 },
100 ast::EnumDef(it) => { 77 ast::EnumDef(it) => {
101 let def: EnumId = self.to_id(container.with_value(it))?; 78 let def: EnumId = self.to_id(db, container.with_value(it))?;
102 def.into() 79 def.into()
103 }, 80 },
104 ast::StructDef(it) => { 81 ast::StructDef(it) => {
105 let def: StructId = self.to_id(container.with_value(it))?; 82 let def: StructId = self.to_id(db, container.with_value(it))?;
106 VariantId::from(def).into() 83 VariantId::from(def).into()
107 }, 84 },
108 ast::UnionDef(it) => { 85 ast::UnionDef(it) => {
109 let def: UnionId = self.to_id(container.with_value(it))?; 86 let def: UnionId = self.to_id(db, container.with_value(it))?;
110 VariantId::from(def).into() 87 VariantId::from(def).into()
111 }, 88 },
112 ast::Module(it) => { 89 ast::Module(it) => {
113 let def: ModuleId = self.to_id(container.with_value(it))?; 90 let def: ModuleId = self.to_id(db, container.with_value(it))?;
114 def.into() 91 def.into()
115 }, 92 },
116 _ => { continue }, 93 _ => { continue },
@@ -119,12 +96,11 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
119 return Some(res); 96 return Some(res);
120 } 97 }
121 98
122 let c = self.to_module_def(src.file_id.original_file(self.db))?; 99 let c = self.to_module_def(db, src.file_id.original_file(db))?;
123 Some(c.id.into()) 100 Some(c.id.into())
124 } 101 }
125 102
126 fn child_by_source(&mut self, container: ChildContainer) -> &DynMap { 103 fn child_by_source(&mut self, db: &impl HirDatabase, container: ChildContainer) -> &DynMap {
127 let db = self.db;
128 self.child_by_source_cache.entry(container).or_insert_with(|| match container { 104 self.child_by_source_cache.entry(container).or_insert_with(|| match container {
129 ChildContainer::DefWithBodyId(it) => it.child_by_source(db), 105 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
130 ChildContainer::ModuleId(it) => it.child_by_source(db), 106 ChildContainer::ModuleId(it) => it.child_by_source(db),
@@ -137,49 +113,17 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
137 } 113 }
138} 114}
139 115
140pub trait ToId: Sized { 116pub(crate) trait ToId: Sized {
141 type ID: Sized + Copy + 'static; 117 type ID: Sized + Copy + 'static;
142 fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>) 118 fn to_id<DB: HirDatabase>(
143 -> Option<Self::ID>; 119 db: &DB,
144} 120 sb: &mut SourceBinder,
145
146pub trait ToDef: Sized + AstNode + 'static {
147 type Def;
148 fn to_def<DB: HirDatabase>(
149 sb: &mut SourceBinder<'_, DB>,
150 src: InFile<Self>, 121 src: InFile<Self>,
151 ) -> Option<Self::Def>; 122 ) -> Option<Self::ID>;
152}
153
154macro_rules! to_def_impls {
155 ($(($def:path, $ast:path)),* ,) => {$(
156 impl ToDef for $ast {
157 type Def = $def;
158 fn to_def<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
159 -> Option<Self::Def>
160 { sb.to_id(src).map(Into::into) }
161 }
162 )*}
163} 123}
164 124
165to_def_impls![
166 (crate::Module, ast::Module),
167 (crate::Struct, ast::StructDef),
168 (crate::Enum, ast::EnumDef),
169 (crate::Union, ast::UnionDef),
170 (crate::Trait, ast::TraitDef),
171 (crate::ImplBlock, ast::ImplBlock),
172 (crate::TypeAlias, ast::TypeAliasDef),
173 (crate::Const, ast::ConstDef),
174 (crate::Static, ast::StaticDef),
175 (crate::Function, ast::FnDef),
176 (crate::StructField, ast::RecordFieldDef),
177 (crate::EnumVariant, ast::EnumVariant),
178 (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
179];
180
181#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 125#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
182enum ChildContainer { 126pub(crate) enum ChildContainer {
183 DefWithBodyId(DefWithBodyId), 127 DefWithBodyId(DefWithBodyId),
184 ModuleId(ModuleId), 128 ModuleId(ModuleId),
185 TraitId(TraitId), 129 TraitId(TraitId),
@@ -201,7 +145,7 @@ impl_froms! {
201 GenericDefId 145 GenericDefId
202} 146}
203 147
204pub trait ToIdByKey: Sized + AstNode + 'static { 148pub(crate) trait ToIdByKey: Sized + AstNode + 'static {
205 type ID: Sized + Copy + 'static; 149 type ID: Sized + Copy + 'static;
206 const KEY: Key<Self, Self::ID>; 150 const KEY: Key<Self, Self::ID>;
207} 151}
@@ -209,11 +153,11 @@ pub trait ToIdByKey: Sized + AstNode + 'static {
209impl<T: ToIdByKey> ToId for T { 153impl<T: ToIdByKey> ToId for T {
210 type ID = <T as ToIdByKey>::ID; 154 type ID = <T as ToIdByKey>::ID;
211 fn to_id<DB: HirDatabase>( 155 fn to_id<DB: HirDatabase>(
212 sb: &mut SourceBinder<'_, DB>, 156 db: &DB,
157 sb: &mut SourceBinder,
213 src: InFile<Self>, 158 src: InFile<Self>,
214 ) -> Option<Self::ID> { 159 ) -> Option<Self::ID> {
215 let container = sb.find_container(src.as_ref().map(|it| it.syntax()))?; 160 let container = sb.find_container(db, src.as_ref().map(|it| it.syntax()))?;
216 let db = sb.db;
217 let dyn_map = 161 let dyn_map =
218 &*sb.child_by_source_cache.entry(container).or_insert_with(|| match container { 162 &*sb.child_by_source_cache.entry(container).or_insert_with(|| match container {
219 ChildContainer::DefWithBodyId(it) => it.child_by_source(db), 163 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
@@ -255,68 +199,44 @@ to_id_key_impls![
255impl ToId for ast::MacroCall { 199impl ToId for ast::MacroCall {
256 type ID = MacroDefId; 200 type ID = MacroDefId;
257 fn to_id<DB: HirDatabase>( 201 fn to_id<DB: HirDatabase>(
258 sb: &mut SourceBinder<'_, DB>, 202 db: &DB,
203 sb: &mut SourceBinder,
259 src: InFile<Self>, 204 src: InFile<Self>,
260 ) -> Option<Self::ID> { 205 ) -> Option<Self::ID> {
261 let kind = MacroDefKind::Declarative; 206 let kind = MacroDefKind::Declarative;
262 207
263 let krate = sb.to_module_def(src.file_id.original_file(sb.db))?.id.krate; 208 let krate = sb.to_module_def(db, src.file_id.original_file(db))?.id.krate;
264 209
265 let ast_id = 210 let ast_id = Some(AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)));
266 Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value)));
267 211
268 Some(MacroDefId { krate: Some(krate), ast_id, kind }) 212 Some(MacroDefId { krate: Some(krate), ast_id, kind })
269 } 213 }
270} 214}
271 215
272impl ToDef for ast::BindPat { 216impl ToId for ast::TypeParam {
273 type Def = Local; 217 type ID = TypeParamId;
274
275 fn to_def<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>) -> Option<Local> {
276 let file_id = src.file_id;
277 let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| {
278 let res = match_ast! {
279 match it {
280 ast::ConstDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
281 ast::StaticDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
282 ast::FnDef(value) => { sb.to_id(InFile { value, file_id})?.into() },
283 _ => return None,
284 }
285 };
286 Some(res)
287 })?;
288 let (_body, source_map) = sb.db.body_with_source_map(parent);
289 let src = src.map(ast::Pat::from);
290 let pat_id = source_map.node_pat(src.as_ref())?;
291 Some(Local { parent: parent.into(), pat_id })
292 }
293}
294
295impl ToDef for ast::TypeParam {
296 type Def = TypeParam;
297 218
298 fn to_def<DB: HirDatabase>( 219 fn to_id<DB: HirDatabase>(
299 sb: &mut SourceBinder<'_, DB>, 220 db: &DB,
300 src: InFile<ast::TypeParam>, 221 sb: &mut SourceBinder,
301 ) -> Option<TypeParam> { 222 src: InFile<Self>,
302 let mut sb = SourceBinder::new(sb.db); 223 ) -> Option<Self::ID> {
303 let file_id = src.file_id; 224 let file_id = src.file_id;
304 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { 225 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
305 let res = match_ast! { 226 let res = match_ast! {
306 match it { 227 match it {
307 ast::FnDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 228 ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
308 ast::StructDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 229 ast::StructDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
309 ast::EnumDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 230 ast::EnumDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
310 ast::TraitDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 231 ast::TraitDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
311 ast::TypeAliasDef(value) => { sb.to_id(InFile { value, file_id})?.into() }, 232 ast::TypeAliasDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
312 ast::ImplBlock(value) => { sb.to_id(InFile { value, file_id})?.into() }, 233 ast::ImplBlock(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
313 _ => return None, 234 _ => return None,
314 } 235 }
315 }; 236 };
316 Some(res) 237 Some(res)
317 })?; 238 })?;
318 let &id = sb.child_by_source(parent.into())[keys::TYPE_PARAM].get(&src)?; 239 sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src).copied()
319 Some(TypeParam { id })
320 } 240 }
321} 241}
322 242
@@ -324,7 +244,8 @@ impl ToId for ast::Module {
324 type ID = ModuleId; 244 type ID = ModuleId;
325 245
326 fn to_id<DB: HirDatabase>( 246 fn to_id<DB: HirDatabase>(
327 sb: &mut SourceBinder<'_, DB>, 247 db: &DB,
248 sb: &mut SourceBinder,
328 src: InFile<ast::Module>, 249 src: InFile<ast::Module>,
329 ) -> Option<ModuleId> { 250 ) -> Option<ModuleId> {
330 { 251 {
@@ -333,7 +254,7 @@ impl ToId for ast::Module {
333 .as_ref() 254 .as_ref()
334 .map(|it| it.syntax()) 255 .map(|it| it.syntax())
335 .cloned() 256 .cloned()
336 .ancestors_with_macros(sb.db) 257 .ancestors_with_macros(db)
337 .skip(1) 258 .skip(1)
338 .find_map(|it| { 259 .find_map(|it| {
339 let m = ast::Module::cast(it.value.clone())?; 260 let m = ast::Module::cast(it.value.clone())?;
@@ -341,15 +262,15 @@ impl ToId for ast::Module {
341 }); 262 });
342 263
343 let parent_module = match parent_declaration { 264 let parent_module = match parent_declaration {
344 Some(parent_declaration) => sb.to_id(parent_declaration)?, 265 Some(parent_declaration) => sb.to_id(db, parent_declaration)?,
345 None => { 266 None => {
346 let file_id = src.file_id.original_file(sb.db); 267 let file_id = src.file_id.original_file(db);
347 sb.to_module_def(file_id)?.id 268 sb.to_module_def(db, file_id)?.id
348 } 269 }
349 }; 270 };
350 271
351 let child_name = src.value.name()?.as_name(); 272 let child_name = src.value.name()?.as_name();
352 let def_map = sb.db.crate_def_map(parent_module.krate); 273 let def_map = db.crate_def_map(parent_module.krate);
353 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; 274 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
354 Some(ModuleId { krate: parent_module.krate, local_id: child_id }) 275 Some(ModuleId { krate: parent_module.krate, local_id: child_id })
355 } 276 }
diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml
index 5053d0688..fa25cc4fb 100644
--- a/crates/ra_hir_def/Cargo.toml
+++ b/crates/ra_hir_def/Cargo.toml
@@ -14,7 +14,6 @@ rustc-hash = "1.1.0"
14either = "1.5.3" 14either = "1.5.3"
15anymap = "0.12.1" 15anymap = "0.12.1"
16drop_bomb = "0.1.4" 16drop_bomb = "0.1.4"
17itertools = "0.8.2"
18 17
19ra_arena = { path = "../ra_arena" } 18ra_arena = { path = "../ra_arena" }
20ra_db = { path = "../ra_db" } 19ra_db = { path = "../ra_db" }
diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml
index 3407d2598..7625fc8c8 100644
--- a/crates/ra_ide/Cargo.toml
+++ b/crates/ra_ide/Cargo.toml
@@ -17,11 +17,7 @@ indexmap = "1.3.2"
17itertools = "0.8.2" 17itertools = "0.8.2"
18join_to_string = "0.1.3" 18join_to_string = "0.1.3"
19log = "0.4.8" 19log = "0.4.8"
20rayon = "1.3.0"
21fst = { version = "0.3.5", default-features = false }
22rustc-hash = "1.1.0" 20rustc-hash = "1.1.0"
23unicase = "2.6.0"
24superslice = "1.0.0"
25rand = { version = "0.7.3", features = ["small_rng"] } 21rand = { version = "0.7.3", features = ["small_rng"] }
26once_cell = "1.3.1" 22once_cell = "1.3.1"
27 23
diff --git a/crates/ra_ide/src/call_hierarchy.rs b/crates/ra_ide/src/call_hierarchy.rs
index 51ac59a71..b00b6d431 100644
--- a/crates/ra_ide/src/call_hierarchy.rs
+++ b/crates/ra_ide/src/call_hierarchy.rs
@@ -2,13 +2,13 @@
2 2
3use indexmap::IndexMap; 3use indexmap::IndexMap;
4 4
5use hir::db::AstDatabase; 5use hir::Semantics;
6use ra_ide_db::RootDatabase; 6use ra_ide_db::RootDatabase;
7use ra_syntax::{ast, match_ast, AstNode, TextRange}; 7use ra_syntax::{ast, match_ast, AstNode, TextRange};
8 8
9use crate::{ 9use crate::{
10 call_info::FnCallNode, display::ToNav, expand::descend_into_macros, goto_definition, 10 call_info::FnCallNode, display::ToNav, goto_definition, references, FilePosition,
11 references, FilePosition, NavigationTarget, RangeInfo, 11 NavigationTarget, RangeInfo,
12}; 12};
13 13
14#[derive(Debug, Clone)] 14#[derive(Debug, Clone)]
@@ -38,30 +38,31 @@ pub(crate) fn call_hierarchy(
38} 38}
39 39
40pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> { 40pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
41 let sema = Semantics::new(db);
41 // 1. Find all refs 42 // 1. Find all refs
42 // 2. Loop through refs and determine unique fndef. This will become our `from: CallHierarchyItem,` in the reply. 43 // 2. Loop through refs and determine unique fndef. This will become our `from: CallHierarchyItem,` in the reply.
43 // 3. Add ranges relative to the start of the fndef. 44 // 3. Add ranges relative to the start of the fndef.
44 let refs = references::find_all_refs(db, position, None)?; 45 let refs = references::find_all_refs(db, position, None)?;
45 46
46 let mut calls = CallLocations::default(); 47 let mut calls = CallLocations::default();
47 let mut sb = hir::SourceBinder::new(db);
48 48
49 for reference in refs.info.references() { 49 for reference in refs.info.references() {
50 let file_id = reference.file_range.file_id; 50 let file_id = reference.file_range.file_id;
51 let file = db.parse_or_expand(file_id.into())?; 51 let file = sema.parse(file_id);
52 let file = file.syntax();
52 let token = file.token_at_offset(reference.file_range.range.start()).next()?; 53 let token = file.token_at_offset(reference.file_range.range.start()).next()?;
53 let token = descend_into_macros(db, file_id, token); 54 let token = sema.descend_into_macros(token);
54 let syntax = token.value.parent(); 55 let syntax = token.parent();
55 56
56 // This target is the containing function 57 // This target is the containing function
57 if let Some(nav) = syntax.ancestors().find_map(|node| { 58 if let Some(nav) = syntax.ancestors().find_map(|node| {
58 match_ast! { 59 match_ast! {
59 match node { 60 match node {
60 ast::FnDef(it) => { 61 ast::FnDef(it) => {
61 let def = sb.to_def(token.with_value(it))?; 62 let def = sema.to_def(&it)?;
62 Some(def.to_nav(sb.db)) 63 Some(def.to_nav(sema.db))
63 }, 64 },
64 _ => { None }, 65 _ => None,
65 } 66 }
66 } 67 }
67 }) { 68 }) {
@@ -74,11 +75,13 @@ pub(crate) fn incoming_calls(db: &RootDatabase, position: FilePosition) -> Optio
74} 75}
75 76
76pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> { 77pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Option<Vec<CallItem>> {
78 let sema = Semantics::new(db);
77 let file_id = position.file_id; 79 let file_id = position.file_id;
78 let file = db.parse_or_expand(file_id.into())?; 80 let file = sema.parse(file_id);
81 let file = file.syntax();
79 let token = file.token_at_offset(position.offset).next()?; 82 let token = file.token_at_offset(position.offset).next()?;
80 let token = descend_into_macros(db, file_id, token); 83 let token = sema.descend_into_macros(token);
81 let syntax = token.value.parent(); 84 let syntax = token.parent();
82 85
83 let mut calls = CallLocations::default(); 86 let mut calls = CallLocations::default();
84 87
@@ -87,14 +90,11 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
87 .filter_map(|node| FnCallNode::with_node_exact(&node)) 90 .filter_map(|node| FnCallNode::with_node_exact(&node))
88 .filter_map(|call_node| { 91 .filter_map(|call_node| {
89 let name_ref = call_node.name_ref()?; 92 let name_ref = call_node.name_ref()?;
90 let name_ref = token.with_value(name_ref.syntax());
91
92 let analyzer = hir::SourceAnalyzer::new(db, name_ref, None);
93 93
94 if let Some(func_target) = match &call_node { 94 if let Some(func_target) = match &call_node {
95 FnCallNode::CallExpr(expr) => { 95 FnCallNode::CallExpr(expr) => {
96 //FIXME: Type::as_callable is broken 96 //FIXME: Type::as_callable is broken
97 let callable_def = analyzer.type_of(db, &expr.expr()?)?.as_callable()?; 97 let callable_def = sema.type_of_expr(&expr.expr()?)?.as_callable()?;
98 match callable_def { 98 match callable_def {
99 hir::CallableDef::FunctionId(it) => { 99 hir::CallableDef::FunctionId(it) => {
100 let fn_def: hir::Function = it.into(); 100 let fn_def: hir::Function = it.into();
@@ -105,15 +105,15 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
105 } 105 }
106 } 106 }
107 FnCallNode::MethodCallExpr(expr) => { 107 FnCallNode::MethodCallExpr(expr) => {
108 let function = analyzer.resolve_method_call(&expr)?; 108 let function = sema.resolve_method_call(&expr)?;
109 Some(function.to_nav(db)) 109 Some(function.to_nav(db))
110 } 110 }
111 FnCallNode::MacroCallExpr(expr) => { 111 FnCallNode::MacroCallExpr(macro_call) => {
112 let macro_def = analyzer.resolve_macro_call(db, name_ref.with_value(&expr))?; 112 let macro_def = sema.resolve_macro_call(&macro_call)?;
113 Some(macro_def.to_nav(db)) 113 Some(macro_def.to_nav(db))
114 } 114 }
115 } { 115 } {
116 Some((func_target, name_ref.value.text_range())) 116 Some((func_target, name_ref.syntax().text_range()))
117 } else { 117 } else {
118 None 118 None
119 } 119 }
diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs
index 7c6322cb4..9a1fc0d35 100644
--- a/crates/ra_ide/src/call_info.rs
+++ b/crates/ra_ide/src/call_info.rs
@@ -1,5 +1,5 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use hir::db::AstDatabase; 2use hir::Semantics;
3use ra_ide_db::RootDatabase; 3use ra_ide_db::RootDatabase;
4use ra_syntax::{ 4use ra_syntax::{
5 ast::{self, ArgListOwner}, 5 ast::{self, ArgListOwner},
@@ -7,24 +7,23 @@ use ra_syntax::{
7}; 7};
8use test_utils::tested_by; 8use test_utils::tested_by;
9 9
10use crate::{expand::descend_into_macros, CallInfo, FilePosition, FunctionSignature}; 10use crate::{CallInfo, FilePosition, FunctionSignature};
11 11
12/// Computes parameter information for the given call expression. 12/// Computes parameter information for the given call expression.
13pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { 13pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
14 let file = db.parse_or_expand(position.file_id.into())?; 14 let sema = Semantics::new(db);
15 let file = sema.parse(position.file_id);
16 let file = file.syntax();
15 let token = file.token_at_offset(position.offset).next()?; 17 let token = file.token_at_offset(position.offset).next()?;
16 let token = descend_into_macros(db, position.file_id, token); 18 let token = sema.descend_into_macros(token);
17 19
18 // Find the calling expression and it's NameRef 20 // Find the calling expression and it's NameRef
19 let calling_node = FnCallNode::with_node(&token.value.parent())?; 21 let calling_node = FnCallNode::with_node(&token.parent())?;
20 let name_ref = calling_node.name_ref()?;
21 let name_ref = token.with_value(name_ref.syntax());
22 22
23 let analyzer = hir::SourceAnalyzer::new(db, name_ref, None);
24 let (mut call_info, has_self) = match &calling_node { 23 let (mut call_info, has_self) = match &calling_node {
25 FnCallNode::CallExpr(expr) => { 24 FnCallNode::CallExpr(call) => {
26 //FIXME: Type::as_callable is broken 25 //FIXME: Type::as_callable is broken
27 let callable_def = analyzer.type_of(db, &expr.expr()?)?.as_callable()?; 26 let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?;
28 match callable_def { 27 match callable_def {
29 hir::CallableDef::FunctionId(it) => { 28 hir::CallableDef::FunctionId(it) => {
30 let fn_def = it.into(); 29 let fn_def = it.into();
@@ -36,12 +35,12 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal
36 } 35 }
37 } 36 }
38 } 37 }
39 FnCallNode::MethodCallExpr(expr) => { 38 FnCallNode::MethodCallExpr(method_call) => {
40 let function = analyzer.resolve_method_call(&expr)?; 39 let function = sema.resolve_method_call(&method_call)?;
41 (CallInfo::with_fn(db, function), function.has_self_param(db)) 40 (CallInfo::with_fn(db, function), function.has_self_param(db))
42 } 41 }
43 FnCallNode::MacroCallExpr(expr) => { 42 FnCallNode::MacroCallExpr(macro_call) => {
44 let macro_def = analyzer.resolve_macro_call(db, name_ref.with_value(&expr))?; 43 let macro_def = sema.resolve_macro_call(&macro_call)?;
45 (CallInfo::with_macro(db, macro_def)?, false) 44 (CallInfo::with_macro(db, macro_def)?, false)
46 } 45 }
47 }; 46 };
diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs
index 4bdc6ba23..c378c2c62 100644
--- a/crates/ra_ide/src/completion.rs
+++ b/crates/ra_ide/src/completion.rs
@@ -17,7 +17,6 @@ mod complete_postfix;
17mod complete_macro_in_item_position; 17mod complete_macro_in_item_position;
18mod complete_trait_impl; 18mod complete_trait_impl;
19 19
20use ra_db::SourceDatabase;
21use ra_ide_db::RootDatabase; 20use ra_ide_db::RootDatabase;
22 21
23#[cfg(test)] 22#[cfg(test)]
@@ -57,8 +56,7 @@ pub use crate::completion::completion_item::{
57/// identifier prefix/fuzzy match should be done higher in the stack, together 56/// identifier prefix/fuzzy match should be done higher in the stack, together
58/// with ordering of completions (currently this is done by the client). 57/// with ordering of completions (currently this is done by the client).
59pub(crate) fn completions(db: &RootDatabase, position: FilePosition) -> Option<Completions> { 58pub(crate) fn completions(db: &RootDatabase, position: FilePosition) -> Option<Completions> {
60 let original_parse = db.parse(position.file_id); 59 let ctx = CompletionContext::new(db, position)?;
61 let ctx = CompletionContext::new(db, &original_parse, position)?;
62 60
63 let mut acc = Completions::default(); 61 let mut acc = Completions::default();
64 62
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs
index 2ca78c927..a6e0158b2 100644
--- a/crates/ra_ide/src/completion/complete_dot.rs
+++ b/crates/ra_ide/src/completion/complete_dot.rs
@@ -16,7 +16,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
16 _ => return, 16 _ => return,
17 }; 17 };
18 18
19 let receiver_ty = match ctx.analyzer.type_of(ctx.db, &dot_receiver) { 19 let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) {
20 Some(ty) => ty, 20 Some(ty) => ty,
21 _ => return, 21 _ => return,
22 }; 22 };
@@ -55,7 +55,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Ty
55fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { 55fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) {
56 if let Some(krate) = ctx.module.map(|it| it.krate()) { 56 if let Some(krate) = ctx.module.map(|it| it.krate()) {
57 let mut seen_methods = FxHashSet::default(); 57 let mut seen_methods = FxHashSet::default();
58 let traits_in_scope = ctx.analyzer.traits_in_scope(ctx.db); 58 let traits_in_scope = ctx.scope().traits_in_scope();
59 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| { 59 receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
60 if func.has_self_param(ctx.db) && seen_methods.insert(func.name(ctx.db)) { 60 if func.has_self_param(ctx.db) && seen_methods.insert(func.name(ctx.db)) {
61 acc.add_function(ctx, func); 61 acc.add_function(ctx, func);
diff --git a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs
index faadd1e3f..1866d9e6c 100644
--- a/crates/ra_ide/src/completion/complete_macro_in_item_position.rs
+++ b/crates/ra_ide/src/completion/complete_macro_in_item_position.rs
@@ -5,7 +5,7 @@ use crate::completion::{CompletionContext, Completions};
5pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) { 5pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
6 // Show only macros in top level. 6 // Show only macros in top level.
7 if ctx.is_new_item { 7 if ctx.is_new_item {
8 ctx.analyzer.process_all_names(ctx.db, &mut |name, res| { 8 ctx.scope().process_all_names(&mut |name, res| {
9 if let hir::ScopeDef::MacroDef(mac) = res { 9 if let hir::ScopeDef::MacroDef(mac) = res {
10 acc.add_macro(ctx, Some(name.to_string()), mac); 10 acc.add_macro(ctx, Some(name.to_string()), mac);
11 } 11 }
diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs
index 2d7f09a6c..c626e90cc 100644
--- a/crates/ra_ide/src/completion/complete_path.rs
+++ b/crates/ra_ide/src/completion/complete_path.rs
@@ -11,7 +11,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
11 Some(path) => path.clone(), 11 Some(path) => path.clone(),
12 _ => return, 12 _ => return,
13 }; 13 };
14 let def = match ctx.analyzer.resolve_hir_path(ctx.db, &path) { 14 let def = match ctx.scope().resolve_hir_path(&path) {
15 Some(PathResolution::Def(def)) => def, 15 Some(PathResolution::Def(def)) => def,
16 _ => return, 16 _ => return,
17 }; 17 };
@@ -49,7 +49,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
49 // FIXME: complete T::AssocType 49 // FIXME: complete T::AssocType
50 let krate = ctx.module.map(|m| m.krate()); 50 let krate = ctx.module.map(|m| m.krate());
51 if let Some(krate) = krate { 51 if let Some(krate) = krate {
52 let traits_in_scope = ctx.analyzer.traits_in_scope(ctx.db); 52 let traits_in_scope = ctx.scope().traits_in_scope();
53 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| { 53 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
54 match item { 54 match item {
55 hir::AssocItem::Function(func) => { 55 hir::AssocItem::Function(func) => {
diff --git a/crates/ra_ide/src/completion/complete_pattern.rs b/crates/ra_ide/src/completion/complete_pattern.rs
index fd03b1c40..c2c6ca002 100644
--- a/crates/ra_ide/src/completion/complete_pattern.rs
+++ b/crates/ra_ide/src/completion/complete_pattern.rs
@@ -9,7 +9,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
9 } 9 }
10 // FIXME: ideally, we should look at the type we are matching against and 10 // FIXME: ideally, we should look at the type we are matching against and
11 // suggest variants + auto-imports 11 // suggest variants + auto-imports
12 ctx.analyzer.process_all_names(ctx.db, &mut |name, res| { 12 ctx.scope().process_all_names(&mut |name, res| {
13 let def = match &res { 13 let def = match &res {
14 hir::ScopeDef::ModuleDef(def) => def, 14 hir::ScopeDef::ModuleDef(def) => def,
15 _ => return, 15 _ => return,
diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs
index 5470dc291..8a74f993a 100644
--- a/crates/ra_ide/src/completion/complete_postfix.rs
+++ b/crates/ra_ide/src/completion/complete_postfix.rs
@@ -29,7 +29,7 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
29 dot_receiver.syntax().text().to_string() 29 dot_receiver.syntax().text().to_string()
30 }; 30 };
31 31
32 let receiver_ty = match ctx.analyzer.type_of(ctx.db, &dot_receiver) { 32 let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) {
33 Some(it) => it, 33 Some(it) => it,
34 None => return, 34 None => return,
35 }; 35 };
diff --git a/crates/ra_ide/src/completion/complete_record_literal.rs b/crates/ra_ide/src/completion/complete_record_literal.rs
index 577c394d2..f98353d76 100644
--- a/crates/ra_ide/src/completion/complete_record_literal.rs
+++ b/crates/ra_ide/src/completion/complete_record_literal.rs
@@ -5,10 +5,7 @@ use crate::completion::{CompletionContext, Completions};
5/// Complete fields in fields literals. 5/// Complete fields in fields literals.
6pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionContext) { 6pub(super) fn complete_record_lite