aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-22 14:32:10 +0000
committerAleksey Kladov <[email protected]>2019-11-22 14:32:10 +0000
commitb315f05cf160a11b9012fcde2a9aefc240e39135 (patch)
treeced460568980b0bd71ad95267c89f320ce324ba4 /crates
parent78f3b0627cf4a5d34aaf63c7b5a2e1b744a11b14 (diff)
Move data to a single file
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/data.rs206
-rw-r--r--crates/ra_hir_def/src/db.rs5
-rw-r--r--crates/ra_hir_def/src/function.rs61
-rw-r--r--crates/ra_hir_def/src/impls.rs86
-rw-r--r--crates/ra_hir_def/src/lib.rs5
-rw-r--r--crates/ra_hir_def/src/traits.rs66
-rw-r--r--crates/ra_hir_def/src/type_alias.rs27
-rw-r--r--crates/ra_ide_api/src/completion/presentation.rs11
8 files changed, 213 insertions, 254 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
new file mode 100644
index 000000000..b73e0d0a7
--- /dev/null
+++ b/crates/ra_hir_def/src/data.rs
@@ -0,0 +1,206 @@
1use std::sync::Arc;
2
3use hir_expand::{
4 name::{self, AsName, Name},
5 AstId,
6};
7use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
8
9use crate::{
10 db::DefDatabase2,
11 type_ref::{Mutability, TypeRef},
12 AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionId, FunctionLoc, HasSource, ImplId,
13 Intern, Lookup, TraitId, TypeAliasId, TypeAliasLoc,
14};
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct FunctionData {
18 pub name: Name,
19 pub params: Vec<TypeRef>,
20 pub ret_type: TypeRef,
21 /// True if the first param is `self`. This is relevant to decide whether this
22 /// can be called as a method.
23 pub has_self_param: bool,
24}
25
26impl FunctionData {
27 pub(crate) fn fn_data_query(db: &impl DefDatabase2, func: FunctionId) -> Arc<FunctionData> {
28 let src = func.lookup(db).source(db);
29 let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
30 let mut params = Vec::new();
31 let mut has_self_param = false;
32 if let Some(param_list) = src.value.param_list() {
33 if let Some(self_param) = param_list.self_param() {
34 let self_type = if let Some(type_ref) = self_param.ascribed_type() {
35 TypeRef::from_ast(type_ref)
36 } else {
37 let self_type = TypeRef::Path(name::SELF_TYPE.into());
38 match self_param.kind() {
39 ast::SelfParamKind::Owned => self_type,
40 ast::SelfParamKind::Ref => {
41 TypeRef::Reference(Box::new(self_type), Mutability::Shared)
42 }
43 ast::SelfParamKind::MutRef => {
44 TypeRef::Reference(Box::new(self_type), Mutability::Mut)
45 }
46 }
47 };
48 params.push(self_type);
49 has_self_param = true;
50 }
51 for param in param_list.params() {
52 let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
53 params.push(type_ref);
54 }
55 }
56 let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) {
57 TypeRef::from_ast(type_ref)
58 } else {
59 TypeRef::unit()
60 };
61
62 let sig = FunctionData { name, params, ret_type, has_self_param };
63 Arc::new(sig)
64 }
65}
66
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct TypeAliasData {
69 pub name: Name,
70 pub type_ref: Option<TypeRef>,
71}
72
73impl TypeAliasData {
74 pub(crate) fn type_alias_data_query(
75 db: &impl DefDatabase2,
76 typ: TypeAliasId,
77 ) -> Arc<TypeAliasData> {
78 let node = typ.lookup(db).source(db).value;
79 let name = node.name().map_or_else(Name::missing, |n| n.as_name());
80 let type_ref = node.type_ref().map(TypeRef::from_ast);
81 Arc::new(TypeAliasData { name, type_ref })
82 }
83}
84
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub struct TraitData {
87 pub name: Option<Name>,
88 pub items: Vec<AssocItemId>,
89 pub auto: bool,
90}
91
92impl TraitData {
93 pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> {
94 let src = tr.source(db);
95 let name = src.value.name().map(|n| n.as_name());
96 let auto = src.value.is_auto();
97 let ast_id_map = db.ast_id_map(src.file_id);
98 let items = if let Some(item_list) = src.value.item_list() {
99 item_list
100 .impl_items()
101 .map(|item_node| match item_node {
102 ast::ImplItem::FnDef(it) => FunctionLoc {
103 container: ContainerId::TraitId(tr),
104 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
105 }
106 .intern(db)
107 .into(),
108 ast::ImplItem::ConstDef(it) => ConstLoc {
109 container: ContainerId::TraitId(tr),
110 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
111 }
112 .intern(db)
113 .into(),
114 ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc {
115 container: ContainerId::TraitId(tr),
116 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
117 }
118 .intern(db)
119 .into(),
120 })
121 .collect()
122 } else {
123 Vec::new()
124 };
125 Arc::new(TraitData { name, items, auto })
126 }
127
128 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
129 self.items.iter().filter_map(|item| match item {
130 AssocItemId::TypeAliasId(t) => Some(*t),
131 _ => None,
132 })
133 }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq)]
137pub struct ImplData {
138 target_trait: Option<TypeRef>,
139 target_type: TypeRef,
140 items: Vec<AssocItemId>,
141 negative: bool,
142}
143
144impl ImplData {
145 pub(crate) fn impl_data_query(db: &impl DefDatabase2, id: ImplId) -> Arc<ImplData> {
146 let src = id.source(db);
147 let items = db.ast_id_map(src.file_id);
148
149 let target_trait = src.value.target_trait().map(TypeRef::from_ast);
150 let target_type = TypeRef::from_ast_opt(src.value.target_type());
151 let negative = src.value.is_negative();
152
153 let items = if let Some(item_list) = src.value.item_list() {
154 item_list
155 .impl_items()
156 .map(|item_node| match item_node {
157 ast::ImplItem::FnDef(it) => {
158 let def = FunctionLoc {
159 container: ContainerId::ImplId(id),
160 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
161 }
162 .intern(db);
163 def.into()
164 }
165 ast::ImplItem::ConstDef(it) => {
166 let def = ConstLoc {
167 container: ContainerId::ImplId(id),
168 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
169 }
170 .intern(db);
171 def.into()
172 }
173 ast::ImplItem::TypeAliasDef(it) => {
174 let def = TypeAliasLoc {
175 container: ContainerId::ImplId(id),
176 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
177 }
178 .intern(db);
179 def.into()
180 }
181 })
182 .collect()
183 } else {
184 Vec::new()
185 };
186
187 let res = ImplData { target_trait, target_type, items, negative };
188 Arc::new(res)
189 }
190
191 pub fn target_trait(&self) -> Option<&TypeRef> {
192 self.target_trait.as_ref()
193 }
194
195 pub fn target_type(&self) -> &TypeRef {
196 &self.target_type
197 }
198
199 pub fn items(&self) -> &[AssocItemId] {
200 &self.items
201 }
202
203 pub fn is_negative(&self) -> bool {
204 self.negative
205 }
206}
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 02e649cc7..8c1784ec9 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -8,15 +8,12 @@ use ra_syntax::ast;
8use crate::{ 8use crate::{
9 adt::{EnumData, StructData}, 9 adt::{EnumData, StructData},
10 body::{scope::ExprScopes, Body, BodySourceMap}, 10 body::{scope::ExprScopes, Body, BodySourceMap},
11 function::FunctionData, 11 data::{FunctionData, ImplData, TraitData, TypeAliasData},
12 generics::GenericParams, 12 generics::GenericParams,
13 impls::ImplData,
14 nameres::{ 13 nameres::{
15 raw::{ImportSourceMap, RawItems}, 14 raw::{ImportSourceMap, RawItems},
16 CrateDefMap, 15 CrateDefMap,
17 }, 16 },
18 traits::TraitData,
19 type_alias::TypeAliasData,
20 DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, 17 DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId,
21 TypeAliasId, 18 TypeAliasId,
22}; 19};
diff --git a/crates/ra_hir_def/src/function.rs b/crates/ra_hir_def/src/function.rs
deleted file mode 100644
index 33265275e..000000000
--- a/crates/ra_hir_def/src/function.rs
+++ /dev/null
@@ -1,61 +0,0 @@
1use std::sync::Arc;
2
3use hir_expand::name::{self, AsName, Name};
4use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
5
6use crate::{
7 db::DefDatabase2,
8 type_ref::{Mutability, TypeRef},
9 FunctionId, HasSource, Lookup,
10};
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct FunctionData {
14 pub name: Name,
15 pub params: Vec<TypeRef>,
16 pub ret_type: TypeRef,
17 /// True if the first param is `self`. This is relevant to decide whether this
18 /// can be called as a method.
19 pub has_self_param: bool,
20}
21
22impl FunctionData {
23 pub(crate) fn fn_data_query(db: &impl DefDatabase2, func: FunctionId) -> Arc<FunctionData> {
24 let src = func.lookup(db).source(db);
25 let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
26 let mut params = Vec::new();
27 let mut has_self_param = false;
28 if let Some(param_list) = src.value.param_list() {
29 if let Some(self_param) = param_list.self_param() {
30 let self_type = if let Some(type_ref) = self_param.ascribed_type() {
31 TypeRef::from_ast(type_ref)
32 } else {
33 let self_type = TypeRef::Path(name::SELF_TYPE.into());
34 match self_param.kind() {
35 ast::SelfParamKind::Owned => self_type,
36 ast::SelfParamKind::Ref => {
37 TypeRef::Reference(Box::new(self_type), Mutability::Shared)
38 }
39 ast::SelfParamKind::MutRef => {
40 TypeRef::Reference(Box::new(self_type), Mutability::Mut)
41 }
42 }
43 };
44 params.push(self_type);
45 has_self_param = true;
46 }
47 for param in param_list.params() {
48 let type_ref = TypeRef::from_ast_opt(param.ascribed_type());
49 params.push(type_ref);
50 }
51 }
52 let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) {
53 TypeRef::from_ast(type_ref)
54 } else {
55 TypeRef::unit()
56 };
57
58 let sig = FunctionData { name, params, ret_type, has_self_param };
59 Arc::new(sig)
60 }
61}
diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs
deleted file mode 100644
index 750a869f2..000000000
--- a/crates/ra_hir_def/src/impls.rs
+++ /dev/null
@@ -1,86 +0,0 @@
1//! Defines hir-level representation of impls.
2//!
3//! The handling is similar, but is not quite the same as for other items,
4//! because `impl`s don't have names.
5
6use std::sync::Arc;
7
8use hir_expand::AstId;
9use ra_syntax::ast;
10
11use crate::{
12 db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstLoc, ContainerId,
13 FunctionLoc, ImplId, Intern, TypeAliasLoc,
14};
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct ImplData {
18 target_trait: Option<TypeRef>,
19 target_type: TypeRef,
20 items: Vec<AssocItemId>,
21 negative: bool,
22}
23
24impl ImplData {
25 pub(crate) fn impl_data_query(db: &impl DefDatabase2, id: ImplId) -> Arc<ImplData> {
26 let src = id.source(db);
27 let items = db.ast_id_map(src.file_id);
28
29 let target_trait = src.value.target_trait().map(TypeRef::from_ast);
30 let target_type = TypeRef::from_ast_opt(src.value.target_type());
31 let negative = src.value.is_negative();
32
33 let items = if let Some(item_list) = src.value.item_list() {
34 item_list
35 .impl_items()
36 .map(|item_node| match item_node {
37 ast::ImplItem::FnDef(it) => {
38 let def = FunctionLoc {
39 container: ContainerId::ImplId(id),
40 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
41 }
42 .intern(db);
43 def.into()
44 }
45 ast::ImplItem::ConstDef(it) => {
46 let def = ConstLoc {
47 container: ContainerId::ImplId(id),
48 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
49 }
50 .intern(db);
51 def.into()
52 }
53 ast::ImplItem::TypeAliasDef(it) => {
54 let def = TypeAliasLoc {
55 container: ContainerId::ImplId(id),
56 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
57 }
58 .intern(db);
59 def.into()
60 }
61 })
62 .collect()
63 } else {
64 Vec::new()
65 };
66
67 let res = ImplData { target_trait, target_type, items, negative };
68 Arc::new(res)
69 }
70
71 pub fn target_trait(&self) -> Option<&TypeRef> {
72 self.target_trait.as_ref()
73 }
74
75 pub fn target_type(&self) -> &TypeRef {
76 &self.target_type
77 }
78
79 pub fn items(&self) -> &[AssocItemId] {
80 &self.items
81 }
82
83 pub fn is_negative(&self) -> bool {
84 self.negative
85 }
86}
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 14ccad043..3a0420da0 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -13,15 +13,12 @@ pub mod path;
13pub mod type_ref; 13pub mod type_ref;
14pub mod builtin_type; 14pub mod builtin_type;
15pub mod adt; 15pub mod adt;
16pub mod impls;
17pub mod diagnostics; 16pub mod diagnostics;
18pub mod expr; 17pub mod expr;
19pub mod body; 18pub mod body;
20pub mod generics; 19pub mod generics;
21pub mod traits;
22pub mod resolver; 20pub mod resolver;
23pub mod type_alias; 21pub mod data;
24pub mod function;
25 22
26#[cfg(test)] 23#[cfg(test)]
27mod test_db; 24mod test_db;
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs
deleted file mode 100644
index 6c2d5b2a9..000000000
--- a/crates/ra_hir_def/src/traits.rs
+++ /dev/null
@@ -1,66 +0,0 @@
1//! HIR for trait definitions.
2
3use std::sync::Arc;
4
5use hir_expand::{
6 name::{AsName, Name},
7 AstId,
8};
9
10use ra_syntax::ast::{self, NameOwner};
11
12use crate::{
13 db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId,
14 TypeAliasId, TypeAliasLoc,
15};
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct TraitData {
19 pub name: Option<Name>,
20 pub items: Vec<AssocItemId>,
21 pub auto: bool,
22}
23
24impl TraitData {
25 pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> {
26 let src = tr.source(db);
27 let name = src.value.name().map(|n| n.as_name());
28 let auto = src.value.is_auto();
29 let ast_id_map = db.ast_id_map(src.file_id);
30 let items = if let Some(item_list) = src.value.item_list() {
31 item_list
32 .impl_items()
33 .map(|item_node| match item_node {
34 ast::ImplItem::FnDef(it) => FunctionLoc {
35 container: ContainerId::TraitId(tr),
36 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
37 }
38 .intern(db)
39 .into(),
40 ast::ImplItem::ConstDef(it) => ConstLoc {
41 container: ContainerId::TraitId(tr),
42 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
43 }
44 .intern(db)
45 .into(),
46 ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc {
47 container: ContainerId::TraitId(tr),
48 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
49 }
50 .intern(db)
51 .into(),
52 })
53 .collect()
54 } else {
55 Vec::new()
56 };
57 Arc::new(TraitData { name, items, auto })
58 }
59
60 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
61 self.items.iter().filter_map(|item| match item {
62 AssocItemId::TypeAliasId(t) => Some(*t),
63 _ => None,
64 })
65 }
66}
diff --git a/crates/ra_hir_def/src/type_alias.rs b/crates/ra_hir_def/src/type_alias.rs
deleted file mode 100644
index c0b49aa7c..000000000
--- a/crates/ra_hir_def/src/type_alias.rs
+++ /dev/null
@@ -1,27 +0,0 @@
1//! HIR for type aliases (i.e. the `type` keyword).
2
3use std::sync::Arc;
4
5use hir_expand::name::{AsName, Name};
6
7use ra_syntax::ast::NameOwner;
8
9use crate::{db::DefDatabase2, type_ref::TypeRef, HasSource, Lookup, TypeAliasId};
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct TypeAliasData {
13 pub name: Name,
14 pub type_ref: Option<TypeRef>,
15}
16
17impl TypeAliasData {
18 pub(crate) fn type_alias_data_query(
19 db: &impl DefDatabase2,
20 typ: TypeAliasId,
21 ) -> Arc<TypeAliasData> {
22 let node = typ.lookup(db).source(db).value;
23 let name = node.name().map_or_else(Name::missing, |n| n.as_name());
24 let type_ref = node.type_ref().map(TypeRef::from_ast);
25 Arc::new(TypeAliasData { name, type_ref })
26 }
27}
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs
index 50fdb0043..896ad1517 100644
--- a/crates/ra_ide_api/src/completion/presentation.rs
+++ b/crates/ra_ide_api/src/completion/presentation.rs
@@ -224,12 +224,11 @@ impl Completions {
224 && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") 224 && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
225 { 225 {
226 tested_by!(inserts_parens_for_function_calls); 226 tested_by!(inserts_parens_for_function_calls);
227 let (snippet, label) = 227 let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
228 if params.is_empty() || has_self_param && params.len() == 1 { 228 (format!("{}()$0", func_name), format!("{}()", name))
229 (format!("{}()$0", func_name), format!("{}()", name)) 229 } else {
230 } else { 230 (format!("{}($0)", func_name), format!("{}(…)", name))
231 (format!("{}($0)", func_name), format!("{}(…)", name)) 231 };
232 };
233 builder = builder.lookup_by(name).label(label).insert_snippet(snippet); 232 builder = builder.lookup_by(name).label(label).insert_snippet(snippet);
234 } 233 }
235 234