aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs4
-rw-r--r--crates/ra_hir/src/code_model/attrs.rs25
-rw-r--r--crates/ra_hir/src/db.rs12
-rw-r--r--crates/ra_hir/src/lib.rs3
-rw-r--r--crates/ra_hir_def/src/attr.rs36
-rw-r--r--crates/ra_hir_def/src/db.rs6
-rw-r--r--crates/ra_hir_def/src/lib.rs1
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs20
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs18
-rw-r--r--crates/ra_hir_def/src/type_alias.rs (renamed from crates/ra_hir/src/type_alias.rs)16
-rw-r--r--crates/ra_ide_api/src/completion/presentation.rs9
-rw-r--r--crates/ra_ide_api/src/expand_macro.rs34
-rw-r--r--crates/ra_ide_api/src/typing.rs34
-rw-r--r--crates/ra_lsp_server/src/lib.rs24
-rw-r--r--crates/ra_lsp_server/src/main.rs3
15 files changed, 165 insertions, 80 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 5690040a7..72c9b466f 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -937,7 +937,7 @@ impl TypeAlias {
937 } 937 }
938 938
939 pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> { 939 pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> {
940 db.type_alias_data(self).type_ref.clone() 940 db.type_alias_data(self.id).type_ref.clone()
941 } 941 }
942 942
943 pub fn ty(self, db: &impl HirDatabase) -> Ty { 943 pub fn ty(self, db: &impl HirDatabase) -> Ty {
@@ -945,7 +945,7 @@ impl TypeAlias {
945 } 945 }
946 946
947 pub fn name(self, db: &impl DefDatabase) -> Name { 947 pub fn name(self, db: &impl DefDatabase) -> Name {
948 db.type_alias_data(self).name.clone() 948 db.type_alias_data(self.id).name.clone()
949 } 949 }
950} 950}
951 951
diff --git a/crates/ra_hir/src/code_model/attrs.rs b/crates/ra_hir/src/code_model/attrs.rs
index 9e304217c..96da8c88c 100644
--- a/crates/ra_hir/src/code_model/attrs.rs
+++ b/crates/ra_hir/src/code_model/attrs.rs
@@ -5,10 +5,9 @@ use crate::{
5 Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static, 5 Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
6 Struct, StructField, Trait, TypeAlias, Union, 6 Struct, StructField, Trait, TypeAlias, Union,
7}; 7};
8use hir_def::attr::Attr; 8use hir_def::attr::{Attr, Attrs};
9use hir_expand::hygiene::Hygiene; 9use hir_expand::hygiene::Hygiene;
10use ra_syntax::ast; 10use ra_syntax::ast;
11use std::sync::Arc;
12 11
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum AttrDef { 13pub enum AttrDef {
@@ -37,17 +36,17 @@ impl_froms!(
37 MacroDef 36 MacroDef
38); 37);
39 38
40pub trait Attrs { 39pub trait HasAttrs {
41 fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>>; 40 fn attrs(&self, db: &impl HirDatabase) -> Attrs;
42} 41}
43 42
44pub(crate) fn attributes_query( 43pub(crate) fn attributes_query(db: &(impl DefDatabase + AstDatabase), def: AttrDef) -> Attrs {
45 db: &(impl DefDatabase + AstDatabase),
46 def: AttrDef,
47) -> Option<Arc<[Attr]>> {
48 match def { 44 match def {
49 AttrDef::Module(it) => { 45 AttrDef::Module(it) => {
50 let src = it.declaration_source(db)?; 46 let src = match it.declaration_source(db) {
47 Some(it) => it,
48 None => return Attrs::default(),
49 };
51 let hygiene = Hygiene::new(db, src.file_id); 50 let hygiene = Hygiene::new(db, src.file_id);
52 Attr::from_attrs_owner(&src.value, &hygiene) 51 Attr::from_attrs_owner(&src.value, &hygiene)
53 } 52 }
@@ -57,7 +56,7 @@ pub(crate) fn attributes_query(
57 let hygiene = Hygiene::new(db, src.file_id); 56 let hygiene = Hygiene::new(db, src.file_id);
58 Attr::from_attrs_owner(&named, &hygiene) 57 Attr::from_attrs_owner(&named, &hygiene)
59 } 58 }
60 FieldSource::Pos(..) => None, 59 FieldSource::Pos(..) => Attrs::default(),
61 }, 60 },
62 AttrDef::Adt(it) => match it { 61 AttrDef::Adt(it) => match it {
63 Adt::Struct(it) => attrs_from_ast(it, db), 62 Adt::Struct(it) => attrs_from_ast(it, db),
@@ -74,7 +73,7 @@ pub(crate) fn attributes_query(
74 } 73 }
75} 74}
76 75
77fn attrs_from_ast<T, D>(node: T, db: &D) -> Option<Arc<[Attr]>> 76fn attrs_from_ast<T, D>(node: T, db: &D) -> Attrs
78where 77where
79 T: HasSource, 78 T: HasSource,
80 T::Ast: ast::AttrsOwner, 79 T::Ast: ast::AttrsOwner,
@@ -85,8 +84,8 @@ where
85 Attr::from_attrs_owner(&src.value, &hygiene) 84 Attr::from_attrs_owner(&src.value, &hygiene)
86} 85}
87 86
88impl<T: Into<AttrDef> + Copy> Attrs for T { 87impl<T: Into<AttrDef> + Copy> HasAttrs for T {
89 fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>> { 88 fn attrs(&self, db: &impl HirDatabase) -> Attrs {
90 db.attrs((*self).into()) 89 db.attrs((*self).into())
91 } 90 }
92} 91}
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index ed0d68001..1cfcb2fd2 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -2,7 +2,7 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_def::attr::Attr; 5use hir_def::attr::Attrs;
6use ra_db::salsa; 6use ra_db::salsa;
7use ra_syntax::SmolStr; 7use ra_syntax::SmolStr;
8 8
@@ -16,16 +16,15 @@ use crate::{
16 CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef, 16 CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef,
17 TypeCtor, 17 TypeCtor,
18 }, 18 },
19 type_alias::TypeAliasData,
20 Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static, 19 Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static,
21 StructField, Trait, TypeAlias, 20 StructField, Trait,
22}; 21};
23 22
24pub use hir_def::db::{ 23pub use hir_def::db::{
25 BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, 24 BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage,
26 EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, 25 EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase,
27 InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, 26 InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery,
28 TraitDataQuery, 27 TraitDataQuery, TypeAliasDataQuery,
29}; 28};
30pub use hir_expand::db::{ 29pub use hir_expand::db::{
31 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, 30 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
@@ -39,9 +38,6 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
39 #[salsa::invoke(FnData::fn_data_query)] 38 #[salsa::invoke(FnData::fn_data_query)]
40 fn fn_data(&self, func: Function) -> Arc<FnData>; 39 fn fn_data(&self, func: Function) -> Arc<FnData>;
41 40
42 #[salsa::invoke(TypeAliasData::type_alias_data_query)]
43 fn type_alias_data(&self, typ: TypeAlias) -> Arc<TypeAliasData>;
44
45 #[salsa::invoke(ConstData::const_data_query)] 41 #[salsa::invoke(ConstData::const_data_query)]
46 fn const_data(&self, konst: Const) -> Arc<ConstData>; 42 fn const_data(&self, konst: Const) -> Arc<ConstData>;
47 43
@@ -61,7 +57,7 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
61 fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>; 57 fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
62 58
63 #[salsa::invoke(crate::code_model::attrs::attributes_query)] 59 #[salsa::invoke(crate::code_model::attrs::attributes_query)]
64 fn attrs(&self, def: crate::AttrDef) -> Option<Arc<[Attr]>>; 60 fn attrs(&self, def: crate::AttrDef) -> Attrs;
65} 61}
66 62
67#[salsa::query_group(HirDatabaseStorage)] 63#[salsa::query_group(HirDatabaseStorage)]
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 8c6834392..8535629ca 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -32,7 +32,6 @@ pub mod db;
32pub mod source_binder; 32pub mod source_binder;
33 33
34mod ids; 34mod ids;
35mod type_alias;
36mod ty; 35mod ty;
37mod impl_block; 36mod impl_block;
38mod expr; 37mod expr;
@@ -52,7 +51,7 @@ mod marks;
52 51
53pub use crate::{ 52pub use crate::{
54 code_model::{ 53 code_model::{
55 attrs::{AttrDef, Attrs}, 54 attrs::{AttrDef, HasAttrs},
56 docs::{DocDef, Docs, Documentation}, 55 docs::{DocDef, Docs, Documentation},
57 src::{HasBodySource, HasSource}, 56 src::{HasBodySource, HasSource},
58 Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, 57 Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum,
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 0e961ca12..7a9d0fdf4 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -1,6 +1,6 @@
1//! A higher level attributes based on TokenTree, with also some shortcuts. 1//! A higher level attributes based on TokenTree, with also some shortcuts.
2 2
3use std::sync::Arc; 3use std::{ops, sync::Arc};
4 4
5use hir_expand::hygiene::Hygiene; 5use hir_expand::hygiene::Hygiene;
6use mbe::ast_to_token_tree; 6use mbe::ast_to_token_tree;
@@ -13,6 +13,28 @@ use tt::Subtree;
13 13
14use crate::path::Path; 14use crate::path::Path;
15 15
16#[derive(Default, Debug, Clone, PartialEq, Eq)]
17pub struct Attrs {
18 entries: Option<Arc<[Attr]>>,
19}
20
21impl ops::Deref for Attrs {
22 type Target = [Attr];
23
24 fn deref(&self) -> &[Attr] {
25 match &self.entries {
26 Some(it) => &*it,
27 None => &[],
28 }
29 }
30}
31
32impl Attrs {
33 pub fn has_atom(&self, atom: &str) -> bool {
34 self.iter().any(|it| it.is_simple_atom(atom))
35 }
36}
37
16#[derive(Debug, Clone, PartialEq, Eq)] 38#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Attr { 39pub struct Attr {
18 pub(crate) path: Path, 40 pub(crate) path: Path,
@@ -43,13 +65,15 @@ impl Attr {
43 Some(Attr { path, input }) 65 Some(Attr { path, input })
44 } 66 }
45 67
46 pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Option<Arc<[Attr]>> { 68 pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs {
47 let mut attrs = owner.attrs().peekable(); 69 let mut attrs = owner.attrs().peekable();
48 if attrs.peek().is_none() { 70 let entries = if attrs.peek().is_none() {
49 // Avoid heap allocation 71 // Avoid heap allocation
50 return None; 72 None
51 } 73 } else {
52 Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect()) 74 Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect())
75 };
76 Attrs { entries }
53 } 77 }
54 78
55 pub fn is_simple_atom(&self, name: &str) -> bool { 79 pub fn is_simple_atom(&self, name: &str) -> bool {
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 844f8bbe8..5bbdaa4b2 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -15,7 +15,8 @@ use crate::{
15 CrateDefMap, 15 CrateDefMap,
16 }, 16 },
17 traits::TraitData, 17 traits::TraitData,
18 DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, 18 type_alias::TypeAliasData,
19 DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, TypeAliasId,
19}; 20};
20 21
21#[salsa::query_group(InternDatabaseStorage)] 22#[salsa::query_group(InternDatabaseStorage)]
@@ -64,6 +65,9 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
64 #[salsa::invoke(TraitData::trait_data_query)] 65 #[salsa::invoke(TraitData::trait_data_query)]
65 fn trait_data(&self, e: TraitId) -> Arc<TraitData>; 66 fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
66 67
68 #[salsa::invoke(TypeAliasData::type_alias_data_query)]
69 fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
70
67 #[salsa::invoke(Body::body_with_source_map_query)] 71 #[salsa::invoke(Body::body_with_source_map_query)]
68 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); 72 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
69 73
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index d579f5c7e..268144462 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -20,6 +20,7 @@ pub mod body;
20pub mod generics; 20pub mod generics;
21pub mod traits; 21pub mod traits;
22pub mod resolver; 22pub mod resolver;
23pub mod type_alias;
23 24
24#[cfg(test)] 25#[cfg(test)]
25mod test_db; 26mod test_db;
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index aae3dcadf..7902293e8 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -12,7 +12,7 @@ use rustc_hash::FxHashMap;
12use test_utils::tested_by; 12use test_utils::tested_by;
13 13
14use crate::{ 14use crate::{
15 attr::Attr, 15 attr::Attrs,
16 db::DefDatabase2, 16 db::DefDatabase2,
17 nameres::{ 17 nameres::{
18 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, 18 diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint,
@@ -549,7 +549,7 @@ where
549 // `#[macro_use] extern crate` is hoisted to imports macros before collecting 549 // `#[macro_use] extern crate` is hoisted to imports macros before collecting
550 // any other items. 550 // any other items.
551 for item in items { 551 for item in items {
552 if self.is_cfg_enabled(item.attrs()) { 552 if self.is_cfg_enabled(&item.attrs) {
553 if let raw::RawItemKind::Import(import_id) = item.kind { 553 if let raw::RawItemKind::Import(import_id) = item.kind {
554 let import = self.raw_items[import_id].clone(); 554 let import = self.raw_items[import_id].clone();
555 if import.is_extern_crate && import.is_macro_use { 555 if import.is_extern_crate && import.is_macro_use {
@@ -560,10 +560,10 @@ where
560 } 560 }
561 561
562 for item in items { 562 for item in items {
563 if self.is_cfg_enabled(item.attrs()) { 563 if self.is_cfg_enabled(&item.attrs) {
564 match item.kind { 564 match item.kind {
565 raw::RawItemKind::Module(m) => { 565 raw::RawItemKind::Module(m) => {
566 self.collect_module(&self.raw_items[m], item.attrs()) 566 self.collect_module(&self.raw_items[m], &item.attrs)
567 } 567 }
568 raw::RawItemKind::Import(import_id) => self 568 raw::RawItemKind::Import(import_id) => self
569 .def_collector 569 .def_collector
@@ -585,9 +585,9 @@ where
585 } 585 }
586 } 586 }
587 587
588 fn collect_module(&mut self, module: &raw::ModuleData, attrs: &[Attr]) { 588 fn collect_module(&mut self, module: &raw::ModuleData, attrs: &Attrs) {
589 let path_attr = self.path_attr(attrs); 589 let path_attr = self.path_attr(attrs);
590 let is_macro_use = self.is_macro_use(attrs); 590 let is_macro_use = attrs.has_atom("macro_use");
591 match module { 591 match module {
592 // inline module, just recurse 592 // inline module, just recurse
593 raw::ModuleData::Definition { name, items, ast_id } => { 593 raw::ModuleData::Definition { name, items, ast_id } => {
@@ -779,17 +779,13 @@ where
779 } 779 }
780 } 780 }
781 781
782 fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { 782 fn is_cfg_enabled(&self, attrs: &Attrs) -> bool {
783 attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) 783 attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false))
784 } 784 }
785 785
786 fn path_attr<'a>(&self, attrs: &'a [Attr]) -> Option<&'a SmolStr> { 786 fn path_attr<'a>(&self, attrs: &'a Attrs) -> Option<&'a SmolStr> {
787 attrs.iter().find_map(|attr| attr.as_path()) 787 attrs.iter().find_map(|attr| attr.as_path())
788 } 788 }
789
790 fn is_macro_use<'a>(&self, attrs: &'a [Attr]) -> bool {
791 attrs.iter().any(|attr| attr.is_simple_atom("macro_use"))
792 }
793} 789}
794 790
795fn is_macro_rules(path: &Path) -> bool { 791fn is_macro_rules(path: &Path) -> bool {
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index 7c68fd638..55a9634f8 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -16,7 +16,12 @@ use ra_syntax::{
16}; 16};
17use test_utils::tested_by; 17use test_utils::tested_by;
18 18
19use crate::{attr::Attr, db::DefDatabase2, path::Path, FileAstId, HirFileId, ModuleSource, Source}; 19use crate::{
20 attr::{Attr, Attrs},
21 db::DefDatabase2,
22 path::Path,
23 FileAstId, HirFileId, ModuleSource, Source,
24};
20 25
21/// `RawItems` is a set of top-level items in a file (except for impls). 26/// `RawItems` is a set of top-level items in a file (except for impls).
22/// 27///
@@ -129,21 +134,12 @@ impl Index<Impl> for RawItems {
129 } 134 }
130} 135}
131 136
132// Avoid heap allocation on items without attributes.
133type Attrs = Option<Arc<[Attr]>>;
134
135#[derive(Debug, PartialEq, Eq, Clone)] 137#[derive(Debug, PartialEq, Eq, Clone)]
136pub(super) struct RawItem { 138pub(super) struct RawItem {
137 attrs: Attrs, 139 pub(super) attrs: Attrs,
138 pub(super) kind: RawItemKind, 140 pub(super) kind: RawItemKind,
139} 141}
140 142
141impl RawItem {
142 pub(super) fn attrs(&self) -> &[Attr] {
143 self.attrs.as_ref().map_or(&[], |it| &*it)
144 }
145}
146
147#[derive(Debug, PartialEq, Eq, Clone, Copy)] 143#[derive(Debug, PartialEq, Eq, Clone, Copy)]
148pub(super) enum RawItemKind { 144pub(super) enum RawItemKind {
149 Module(Module), 145 Module(Module),
diff --git a/crates/ra_hir/src/type_alias.rs b/crates/ra_hir_def/src/type_alias.rs
index 392f244cf..c0b49aa7c 100644
--- a/crates/ra_hir/src/type_alias.rs
+++ b/crates/ra_hir_def/src/type_alias.rs
@@ -2,28 +2,24 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_def::type_ref::TypeRef;
6use hir_expand::name::{AsName, Name}; 5use hir_expand::name::{AsName, Name};
7 6
8use ra_syntax::ast::NameOwner; 7use ra_syntax::ast::NameOwner;
9 8
10use crate::{ 9use crate::{db::DefDatabase2, type_ref::TypeRef, HasSource, Lookup, TypeAliasId};
11 db::{AstDatabase, DefDatabase},
12 HasSource, TypeAlias,
13};
14 10
15#[derive(Debug, Clone, PartialEq, Eq)] 11#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct TypeAliasData { 12pub struct TypeAliasData {
17 pub(crate) name: Name, 13 pub name: Name,
18 pub(crate) type_ref: Option<TypeRef>, 14 pub type_ref: Option<TypeRef>,
19} 15}
20 16
21impl TypeAliasData { 17impl TypeAliasData {
22 pub(crate) fn type_alias_data_query( 18 pub(crate) fn type_alias_data_query(
23 db: &(impl DefDatabase + AstDatabase), 19 db: &impl DefDatabase2,
24 typ: TypeAlias, 20 typ: TypeAliasId,
25 ) -> Arc<TypeAliasData> { 21 ) -> Arc<TypeAliasData> {
26 let node = typ.source(db).value; 22 let node = typ.lookup(db).source(db).value;
27 let name = node.name().map_or_else(Name::missing, |n| n.as_name()); 23 let name = node.name().map_or_else(Name::missing, |n| n.as_name());
28 let type_ref = node.type_ref().map(TypeRef::from_ast); 24 let type_ref = node.type_ref().map(TypeRef::from_ast);
29 Arc::new(TypeAliasData { name, type_ref }) 25 Arc::new(TypeAliasData { name, type_ref })
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs
index b20329459..bd464d193 100644
--- a/crates/ra_ide_api/src/completion/presentation.rs
+++ b/crates/ra_ide_api/src/completion/presentation.rs
@@ -1,6 +1,6 @@
1//! This modules takes care of rendering various definitions as completion items. 1//! This modules takes care of rendering various definitions as completion items.
2 2
3use hir::{db::HirDatabase, Attrs, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; 3use hir::{db::HirDatabase, Docs, HasAttrs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk};
4use join_to_string::join; 4use join_to_string::join;
5use ra_syntax::ast::NameOwner; 5use ra_syntax::ast::NameOwner;
6use test_utils::tested_by; 6use test_utils::tested_by;
@@ -285,11 +285,8 @@ impl Completions {
285 } 285 }
286} 286}
287 287
288fn is_deprecated(node: impl Attrs, db: &impl HirDatabase) -> bool { 288fn is_deprecated(node: impl HasAttrs, db: &impl HirDatabase) -> bool {
289 match node.attrs(db) { 289 node.attrs(db).has_atom("deprecated")
290 None => false,
291 Some(attrs) => attrs.iter().any(|x| x.is_simple_atom("deprecated")),
292 }
293} 290}
294 291
295fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool { 292fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool {
diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs
index 7dbf33a16..673301b10 100644
--- a/crates/ra_ide_api/src/expand_macro.rs
+++ b/crates/ra_ide_api/src/expand_macro.rs
@@ -40,7 +40,7 @@ fn expand_macro_recur(
40 let analyzer = hir::SourceAnalyzer::new(db, source, None); 40 let analyzer = hir::SourceAnalyzer::new(db, source, None);
41 let expansion = analyzer.expand(db, macro_call)?; 41 let expansion = analyzer.expand(db, macro_call)?;
42 let macro_file_id = expansion.file_id(); 42 let macro_file_id = expansion.file_id();
43 let expanded: SyntaxNode = db.parse_or_expand(macro_file_id)?; 43 let mut expanded: SyntaxNode = db.parse_or_expand(macro_file_id)?;
44 44
45 let children = expanded.descendants().filter_map(ast::MacroCall::cast); 45 let children = expanded.descendants().filter_map(ast::MacroCall::cast);
46 let mut replaces = FxHashMap::default(); 46 let mut replaces = FxHashMap::default();
@@ -49,7 +49,14 @@ fn expand_macro_recur(
49 let node = hir::Source::new(macro_file_id, &child); 49 let node = hir::Source::new(macro_file_id, &child);
50 let new_node = expand_macro_recur(db, source, node)?; 50 let new_node = expand_macro_recur(db, source, node)?;
51 51
52 replaces.insert(child.syntax().clone().into(), new_node.into()); 52 // Replace the whole node if it is root
53 // `replace_descendants` will not replace the parent node
54 // but `SyntaxNode::descendants include itself
55 if expanded == *child.syntax() {
56 expanded = new_node;
57 } else {
58 replaces.insert(child.syntax().clone().into(), new_node.into());
59 }
53 } 60 }
54 61
55 Some(replace_descendants(&expanded, &replaces)) 62 Some(replace_descendants(&expanded, &replaces))
@@ -217,4 +224,27 @@ fn some_thing() -> u32 {
217} 224}
218"###); 225"###);
219 } 226 }
227
228 #[test]
229 fn macro_expand_match_ast_inside_let_statement() {
230 let res = check_expand_macro(
231 r#"
232 //- /lib.rs
233 macro_rules! match_ast {
234 (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
235 (match ($node:expr) {}) => {{}};
236 }
237
238 fn main() {
239 let p = f(|it| {
240 let res = mat<|>ch_ast! { match c {}};
241 Some(res)
242 })?;
243 }
244 "#,
245 );
246
247 assert_eq!(res.name, "match_ast");
248 assert_snapshot!(res.expansion, @r###"{}"###);
249 }
220} 250}
diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs
index d51132f73..21e5be9b3 100644
--- a/crates/ra_ide_api/src/typing.rs
+++ b/crates/ra_ide_api/src/typing.rs
@@ -40,9 +40,13 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<Sour
40 } 40 }
41 41
42 let prefix = comment.prefix(); 42 let prefix = comment.prefix();
43 if position.offset 43 let comment_range = comment.syntax().text_range();
44 < comment.syntax().text_range().start() + TextUnit::of_str(prefix) + TextUnit::from(1) 44 if position.offset < comment_range.start() + TextUnit::of_str(prefix) + TextUnit::from(1) {
45 { 45 return None;
46 }
47
48 // Continuing non-doc line comments (like this one :) ) is annoying
49 if prefix == "//" && comment_range.end() == position.offset {
46 return None; 50 return None;
47 } 51 }
48 52
@@ -247,6 +251,30 @@ impl S {
247} 251}
248", 252",
249 ); 253 );
254 do_check(
255 r"
256fn main() {
257 // Fix<|> me
258 let x = 1 + 1;
259}
260",
261 r"
262fn main() {
263 // Fix
264 // <|> me
265 let x = 1 + 1;
266}
267",
268 );
269 do_check_noop(
270 r"
271fn main() {
272 // Fix me<|>
273 let x = 1 + 1;
274}
275",
276 );
277
250 do_check_noop(r"<|>//! docz"); 278 do_check_noop(r"<|>//! docz");
251 } 279 }
252 280
diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs
index 0e5dbbbd5..9c36402b0 100644
--- a/crates/ra_lsp_server/src/lib.rs
+++ b/crates/ra_lsp_server/src/lib.rs
@@ -1,6 +1,26 @@
1//! FIXME: write short doc here 1//! Implementation of the LSP for rust-analyzer.
2 2//!
3//! This crate takes Rust-specific analysis results from ra_ide_api and
4//! translates into LSP types.
5//!
6//! It also is the root of all state. `world` module defines the bulk of the
7//! state, and `main_loop` module defines the rules for modifying it.
3#![recursion_limit = "512"] 8#![recursion_limit = "512"]
9
10#[allow(unused)]
11macro_rules! println {
12 ($($tt:tt)*) => {
13 compile_error!("stdout is locked, use eprintln")
14 };
15}
16
17#[allow(unused)]
18macro_rules! print {
19 ($($tt:tt)*) => {
20 compile_error!("stdout is locked, use eprint")
21 };
22}
23
4mod caps; 24mod caps;
5mod cargo_target_spec; 25mod cargo_target_spec;
6mod conv; 26mod conv;
diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs
index 7d9a1d054..e13c8ca14 100644
--- a/crates/ra_lsp_server/src/main.rs
+++ b/crates/ra_lsp_server/src/main.rs
@@ -1,8 +1,7 @@
1//! FIXME: write short doc here 1//! `ra_lsp_server` binary
2 2
3use flexi_logger::{Duplicate, Logger}; 3use flexi_logger::{Duplicate, Logger};
4use lsp_server::Connection; 4use lsp_server::Connection;
5
6use ra_lsp_server::{show_message, Result, ServerConfig}; 5use ra_lsp_server::{show_message, Result, ServerConfig};
7use ra_prof; 6use ra_prof;
8 7