diff options
Diffstat (limited to 'crates/ra_ide_api/src/syntax_highlighting.rs')
-rw-r--r-- | crates/ra_ide_api/src/syntax_highlighting.rs | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 2158291dc..89f20260f 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs | |||
@@ -40,8 +40,41 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
40 | COMMENT => "comment", | 40 | COMMENT => "comment", |
41 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", | 41 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", |
42 | ATTR => "attribute", | 42 | ATTR => "attribute", |
43 | NAME_REF => "text", | 43 | NAME_REF => { |
44 | if let Some(name_ref) = node.as_node().and_then(|n| ast::NameRef::cast(n)) { | ||
45 | use crate::name_ref_kind::{classify_name_ref, NameRefKind::*}; | ||
46 | use hir::{ModuleDef, ImplItem}; | ||
47 | |||
48 | // FIXME: try to reuse the SourceAnalyzers | ||
49 | let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); | ||
50 | match classify_name_ref(db, &analyzer, name_ref) { | ||
51 | Some(Method(_)) => "function", | ||
52 | Some(Macro(_)) => "macro", | ||
53 | Some(FieldAccess(_)) => "field", | ||
54 | Some(AssocItem(ImplItem::Method(_))) => "function", | ||
55 | Some(AssocItem(ImplItem::Const(_))) => "constant", | ||
56 | Some(AssocItem(ImplItem::TypeAlias(_))) => "type", | ||
57 | Some(Def(ModuleDef::Module(_))) => "module", | ||
58 | Some(Def(ModuleDef::Function(_))) => "function", | ||
59 | Some(Def(ModuleDef::Struct(_))) => "type", | ||
60 | Some(Def(ModuleDef::Enum(_))) => "type", | ||
61 | Some(Def(ModuleDef::EnumVariant(_))) => "constant", | ||
62 | Some(Def(ModuleDef::Const(_))) => "constant", | ||
63 | Some(Def(ModuleDef::Static(_))) => "constant", | ||
64 | Some(Def(ModuleDef::Trait(_))) => "type", | ||
65 | Some(Def(ModuleDef::TypeAlias(_))) => "type", | ||
66 | Some(SelfType(_)) => "type", | ||
67 | Some(Pat(_)) => "text", | ||
68 | Some(SelfParam(_)) => "type", | ||
69 | Some(GenericParam(_)) => "type", | ||
70 | None => "text", | ||
71 | } | ||
72 | } else { | ||
73 | "text" | ||
74 | } | ||
75 | } | ||
44 | NAME => "function", | 76 | NAME => "function", |
77 | TYPE_ALIAS_DEF | TYPE_ARG | TYPE_PARAM => "type", | ||
45 | INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", | 78 | INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", |
46 | LIFETIME => "parameter", | 79 | LIFETIME => "parameter", |
47 | T![unsafe] => "keyword.unsafe", | 80 | T![unsafe] => "keyword.unsafe", |
@@ -87,9 +120,23 @@ mod tests { | |||
87 | fn test_highlighting() { | 120 | fn test_highlighting() { |
88 | let (analysis, file_id) = single_file( | 121 | let (analysis, file_id) = single_file( |
89 | r#" | 122 | r#" |
123 | #[derive(Clone, Debug)] | ||
124 | struct Foo { | ||
125 | pub x: i32, | ||
126 | pub y: i32, | ||
127 | } | ||
128 | |||
129 | fn foo<T>() -> T { | ||
130 | unimplemented!(); | ||
131 | } | ||
132 | |||
90 | // comment | 133 | // comment |
91 | fn main() {} | 134 | fn main() {} |
92 | println!("Hello, {}!", 92); | 135 | println!("Hello, {}!", 92); |
136 | |||
137 | let mut vec = Vec::new(); | ||
138 | vec.push(Foo { x: 0, y: 1 }); | ||
139 | unsafe { vec.set_len(0); } | ||
93 | "#, | 140 | "#, |
94 | ); | 141 | ); |
95 | let result = analysis.highlight(file_id); | 142 | let result = analysis.highlight(file_id); |