aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs20
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs2
-rw-r--r--crates/ra_ide_api/src/completion/presentation.rs7
-rw-r--r--crates/ra_ide_api/src/display/navigation_target.rs13
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs5
-rw-r--r--crates/ra_ide_api/src/marks.rs1
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs88
7 files changed, 81 insertions, 55 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index c41752ae7..99da24142 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -17,6 +17,12 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
17 hir::ModuleDef::Module(module) => { 17 hir::ModuleDef::Module(module) => {
18 let module_scope = module.scope(ctx.db); 18 let module_scope = module.scope(ctx.db);
19 for (name, res) in module_scope.entries() { 19 for (name, res) in module_scope.entries() {
20 if let Some(hir::ModuleDef::BuiltinType(..)) = res.def.as_ref().take_types() {
21 if ctx.use_item_syntax.is_some() {
22 tested_by!(dont_complete_primitive_in_use);
23 continue;
24 }
25 }
20 if Some(module) == ctx.module { 26 if Some(module) == ctx.module {
21 if let Some(import) = res.import { 27 if let Some(import) = res.import {
22 if let Either::A(use_tree) = module.import_source(ctx.db, import) { 28 if let Either::A(use_tree) = module.import_source(ctx.db, import) {
@@ -89,6 +95,20 @@ mod tests {
89 } 95 }
90 96
91 #[test] 97 #[test]
98 fn dont_complete_primitive_in_use() {
99 covers!(dont_complete_primitive_in_use);
100 let completions = do_completion(r"use self::<|>;", CompletionKind::BuiltinType);
101 assert!(completions.is_empty());
102 }
103
104 #[test]
105 fn completes_primitives() {
106 let completions =
107 do_completion(r"fn main() { let _: <|> = 92; }", CompletionKind::BuiltinType);
108 assert_eq!(completions.len(), 17);
109 }
110
111 #[test]
92 fn completes_mod_with_docs() { 112 fn completes_mod_with_docs() {
93 check_reference_completion( 113 check_reference_completion(
94 "mod_with_docs", 114 "mod_with_docs",
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index 6f1392231..6f2a60640 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -78,6 +78,7 @@ pub enum CompletionItemKind {
78 Keyword, 78 Keyword,
79 Module, 79 Module,
80 Function, 80 Function,
81 BuiltinType,
81 Struct, 82 Struct,
82 Enum, 83 Enum,
83 EnumVariant, 84 EnumVariant,
@@ -102,6 +103,7 @@ pub(crate) enum CompletionKind {
102 Magic, 103 Magic,
103 Snippet, 104 Snippet,
104 Postfix, 105 Postfix,
106 BuiltinType,
105} 107}
106 108
107#[derive(Debug, PartialEq, Eq, Copy, Clone)] 109#[derive(Debug, PartialEq, Eq, Copy, Clone)]
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs
index 064d379a4..d405161d6 100644
--- a/crates/ra_ide_api/src/completion/presentation.rs
+++ b/crates/ra_ide_api/src/completion/presentation.rs
@@ -57,6 +57,7 @@ impl Completions {
57 } 57 }
58 Some(it) => it, 58 Some(it) => it,
59 }; 59 };
60 let mut completion_kind = CompletionKind::Reference;
60 let (kind, docs) = match def { 61 let (kind, docs) = match def {
61 Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), 62 Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)),
62 Resolution::Def(Function(func)) => { 63 Resolution::Def(Function(func)) => {
@@ -70,6 +71,10 @@ impl Completions {
70 Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), 71 Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)),
71 Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), 72 Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)),
72 Resolution::Def(TypeAlias(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), 73 Resolution::Def(TypeAlias(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)),
74 Resolution::Def(BuiltinType(..)) => {
75 completion_kind = CompletionKind::BuiltinType;
76 (CompletionItemKind::BuiltinType, None)
77 }
73 Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None), 78 Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None),
74 Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None), 79 Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None),
75 Resolution::SelfType(..) => ( 80 Resolution::SelfType(..) => (
@@ -77,7 +82,7 @@ impl Completions {
77 None, 82 None,
78 ), 83 ),
79 }; 84 };
80 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) 85 CompletionItem::new(completion_kind, ctx.source_range(), local_name)
81 .kind(kind) 86 .kind(kind)
82 .set_documentation(docs) 87 .set_documentation(docs)
83 .add_to(self) 88 .add_to(self)
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs
index ae729614f..e19c071b0 100644
--- a/crates/ra_ide_api/src/display/navigation_target.rs
+++ b/crates/ra_ide_api/src/display/navigation_target.rs
@@ -165,8 +165,11 @@ impl NavigationTarget {
165 } 165 }
166 } 166 }
167 167
168 pub(crate) fn from_def(db: &RootDatabase, module_def: hir::ModuleDef) -> NavigationTarget { 168 pub(crate) fn from_def(
169 match module_def { 169 db: &RootDatabase,
170 module_def: hir::ModuleDef,
171 ) -> Option<NavigationTarget> {
172 let nav = match module_def {
170 hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), 173 hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module),
171 hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func), 174 hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func),
172 hir::ModuleDef::Struct(s) => { 175 hir::ModuleDef::Struct(s) => {
@@ -201,7 +204,11 @@ impl NavigationTarget {
201 let (file_id, node) = e.source(db); 204 let (file_id, node) = e.source(db);
202 NavigationTarget::from_named(file_id.original_file(db), &*node) 205 NavigationTarget::from_named(file_id.original_file(db), &*node)
203 } 206 }
204 } 207 hir::ModuleDef::BuiltinType(..) => {
208 return None;
209 }
210 };
211 Some(nav)
205 } 212 }
206 213
207 pub(crate) fn from_impl_block( 214 pub(crate) fn from_impl_block(
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 4f8554625..97b367115 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -62,7 +62,10 @@ pub(crate) fn reference_definition(
62 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), 62 Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
63 Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)), 63 Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)),
64 Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_impl_item(db, assoc)), 64 Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_impl_item(db, assoc)),
65 Some(Def(def)) => return Exact(NavigationTarget::from_def(db, def)), 65 Some(Def(def)) => match NavigationTarget::from_def(db, def) {
66 Some(nav) => return Exact(nav),
67 None => return Approximate(vec![]),
68 },
66 Some(SelfType(ty)) => { 69 Some(SelfType(ty)) => {
67 if let Some((def_id, _)) = ty.as_adt() { 70 if let Some((def_id, _)) = ty.as_adt() {
68 return Exact(NavigationTarget::from_adt_def(db, def_id)); 71 return Exact(NavigationTarget::from_adt_def(db, def_id));
diff --git a/crates/ra_ide_api/src/marks.rs b/crates/ra_ide_api/src/marks.rs
index cc894a7df..9cb991de5 100644
--- a/crates/ra_ide_api/src/marks.rs
+++ b/crates/ra_ide_api/src/marks.rs
@@ -6,4 +6,5 @@ test_utils::marks!(
6 goto_definition_works_for_named_fields 6 goto_definition_works_for_named_fields
7 call_info_bad_offset 7 call_info_bad_offset
8 dont_complete_current_use 8 dont_complete_current_use
9 dont_complete_primitive_in_use
9); 10);
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index 4b24754a8..3a04a51cd 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -30,14 +30,6 @@ fn is_control_keyword(kind: SyntaxKind) -> bool {
30 } 30 }
31} 31}
32 32
33fn is_prim_type(node: &ast::NameRef) -> bool {
34 match node.text().as_str() {
35 "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "usize"
36 | "isize" | "f32" | "f64" | "bool" | "char" | "str" => true,
37 _ => false,
38 }
39}
40
41pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { 33pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
42 let _p = profile("highlight"); 34 let _p = profile("highlight");
43 let source_file = db.parse(file_id).tree; 35 let source_file = db.parse(file_id).tree;
@@ -71,51 +63,47 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
71 NAME_REF => { 63 NAME_REF => {
72 if let Some(name_ref) = node.as_node().and_then(ast::NameRef::cast) { 64 if let Some(name_ref) = node.as_node().and_then(ast::NameRef::cast) {
73 // FIXME: revisit this after #1340 65 // FIXME: revisit this after #1340
74 if is_prim_type(name_ref) { 66 use crate::name_ref_kind::{classify_name_ref, NameRefKind::*};
75 "type" 67 use hir::{ModuleDef, ImplItem};
76 } else {
77 use crate::name_ref_kind::{classify_name_ref, NameRefKind::*};
78 use hir::{ModuleDef, ImplItem};
79 68
80 // FIXME: try to reuse the SourceAnalyzers 69 // FIXME: try to reuse the SourceAnalyzers
81 let analyzer = 70 let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
82 hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); 71 match classify_name_ref(db, &analyzer, name_ref) {
83 match classify_name_ref(db, &analyzer, name_ref) { 72 Some(Method(_)) => "function",
84 Some(Method(_)) => "function", 73 Some(Macro(_)) => "macro",
85 Some(Macro(_)) => "macro", 74 Some(FieldAccess(_)) => "field",
86 Some(FieldAccess(_)) => "field", 75 Some(AssocItem(ImplItem::Method(_))) => "function",
87 Some(AssocItem(ImplItem::Method(_))) => "function", 76 Some(AssocItem(ImplItem::Const(_))) => "constant",
88 Some(AssocItem(ImplItem::Const(_))) => "constant", 77 Some(AssocItem(ImplItem::TypeAlias(_))) => "type",
89 Some(AssocItem(ImplItem::TypeAlias(_))) => "type", 78 Some(Def(ModuleDef::Module(_))) => "module",
90 Some(Def(ModuleDef::Module(_))) => "module", 79 Some(Def(ModuleDef::Function(_))) => "function",
91 Some(Def(ModuleDef::Function(_))) => "function", 80 Some(Def(ModuleDef::Struct(_))) => "type",
92 Some(Def(ModuleDef::Struct(_))) => "type", 81 Some(Def(ModuleDef::Union(_))) => "type",
93 Some(Def(ModuleDef::Union(_))) => "type", 82 Some(Def(ModuleDef::Enum(_))) => "type",
94 Some(Def(ModuleDef::Enum(_))) => "type", 83 Some(Def(ModuleDef::EnumVariant(_))) => "constant",
95 Some(Def(ModuleDef::EnumVariant(_))) => "constant", 84 Some(Def(ModuleDef::Const(_))) => "constant",
96 Some(Def(ModuleDef::Const(_))) => "constant", 85 Some(Def(ModuleDef::Static(_))) => "constant",
97 Some(Def(ModuleDef::Static(_))) => "constant", 86 Some(Def(ModuleDef::Trait(_))) => "type",
98 Some(Def(ModuleDef::Trait(_))) => "type", 87 Some(Def(ModuleDef::TypeAlias(_))) => "type",
99 Some(Def(ModuleDef::TypeAlias(_))) => "type", 88 Some(Def(ModuleDef::BuiltinType(_))) => "type",
100 Some(SelfType(_)) => "type", 89 Some(SelfType(_)) => "type",
101 Some(Pat(ptr)) => { 90 Some(Pat(ptr)) => {
102 binding_hash = Some({ 91 binding_hash = Some({
103 let text = ptr 92 let text = ptr
104 .syntax_node_ptr() 93 .syntax_node_ptr()
105 .to_node(&source_file.syntax()) 94 .to_node(&source_file.syntax())
106 .text() 95 .text()
107 .to_smol_string(); 96 .to_smol_string();
108 let shadow_count = 97 let shadow_count =
109 bindings_shadow_count.entry(text.clone()).or_default(); 98 bindings_shadow_count.entry(text.clone()).or_default();
110 calc_binding_hash(file_id, &text, *shadow_count) 99 calc_binding_hash(file_id, &text, *shadow_count)
111 }); 100 });
112 101
113 "variable" 102 "variable"
114 }
115 Some(SelfParam(_)) => "type",
116 Some(GenericParam(_)) => "type",
117 None => "text",
118 } 103 }
104 Some(SelfParam(_)) => "type",
105 Some(GenericParam(_)) => "type",
106 None => "text",
119 } 107 }
120 } else { 108 } else {
121 "text" 109 "text"