Improve highlighting of name refs

This commit is contained in:
Laurențiu Nicola 2019-05-23 13:26:38 +03:00
parent 4466568525
commit f1ec88cc56
5 changed files with 222 additions and 12 deletions

View file

@ -1,33 +1,145 @@
---
created: "2019-03-23T16:20:31.394314144Z"
creator: insta@0.7.1
created: "2019-05-23T12:10:32.628883358Z"
creator: insta@0.8.1
source: crates/ra_ide_api/src/syntax_highlighting.rs
expression: result
---
Ok(
[
HighlightedRange {
range: [1; 11),
tag: "comment"
range: [1; 24),
tag: "attribute"
},
HighlightedRange {
range: [12; 14),
range: [25; 31),
tag: "keyword"
},
HighlightedRange {
range: [15; 19),
range: [32; 35),
tag: "function"
},
HighlightedRange {
range: [29; 37),
range: [42; 45),
tag: "keyword"
},
HighlightedRange {
range: [46; 47),
tag: "function"
},
HighlightedRange {
range: [49; 52),
tag: "text"
},
HighlightedRange {
range: [58; 61),
tag: "keyword"
},
HighlightedRange {
range: [62; 63),
tag: "function"
},
HighlightedRange {
range: [65; 68),
tag: "text"
},
HighlightedRange {
range: [73; 75),
tag: "keyword"
},
HighlightedRange {
range: [76; 79),
tag: "function"
},
HighlightedRange {
range: [80; 81),
tag: "type"
},
HighlightedRange {
range: [80; 81),
tag: "function"
},
HighlightedRange {
range: [88; 89),
tag: "type"
},
HighlightedRange {
range: [96; 110),
tag: "macro"
},
HighlightedRange {
range: [38; 50),
range: [117; 127),
tag: "comment"
},
HighlightedRange {
range: [128; 130),
tag: "keyword"
},
HighlightedRange {
range: [131; 135),
tag: "function"
},
HighlightedRange {
range: [145; 153),
tag: "macro"
},
HighlightedRange {
range: [154; 166),
tag: "string"
},
HighlightedRange {
range: [52; 54),
range: [168; 170),
tag: "literal"
},
HighlightedRange {
range: [178; 181),
tag: "keyword"
},
HighlightedRange {
range: [182; 185),
tag: "keyword"
},
HighlightedRange {
range: [186; 189),
tag: "macro"
},
HighlightedRange {
range: [197; 200),
tag: "macro"
},
HighlightedRange {
range: [192; 195),
tag: "text"
},
HighlightedRange {
range: [208; 211),
tag: "macro"
},
HighlightedRange {
range: [212; 216),
tag: "macro"
},
HighlightedRange {
range: [226; 227),
tag: "literal"
},
HighlightedRange {
range: [232; 233),
tag: "literal"
},
HighlightedRange {
range: [242; 248),
tag: "keyword.unsafe"
},
HighlightedRange {
range: [251; 254),
tag: "text"
},
HighlightedRange {
range: [255; 262),
tag: "text"
},
HighlightedRange {
range: [263; 264),
tag: "literal"
}
]

View file

@ -40,8 +40,41 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
COMMENT => "comment",
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
ATTR => "attribute",
NAME_REF => "text",
NAME_REF => {
if let Some(name_ref) = node.as_node().and_then(|n| ast::NameRef::cast(n)) {
use crate::name_ref_kind::{classify_name_ref, NameRefKind::*};
use hir::{ModuleDef, ImplItem};
// FIXME: try to reuse the SourceAnalyzers
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
match classify_name_ref(db, &analyzer, name_ref) {
Some(Method(_)) => "function",
Some(Macro(_)) => "macro",
Some(FieldAccess(_)) => "field",
Some(AssocItem(ImplItem::Method(_))) => "function",
Some(AssocItem(ImplItem::Const(_))) => "constant",
Some(AssocItem(ImplItem::TypeAlias(_))) => "type",
Some(Def(ModuleDef::Module(_))) => "module",
Some(Def(ModuleDef::Function(_))) => "function",
Some(Def(ModuleDef::Struct(_))) => "type",
Some(Def(ModuleDef::Enum(_))) => "type",
Some(Def(ModuleDef::EnumVariant(_))) => "constant",
Some(Def(ModuleDef::Const(_))) => "constant",
Some(Def(ModuleDef::Static(_))) => "constant",
Some(Def(ModuleDef::Trait(_))) => "type",
Some(Def(ModuleDef::TypeAlias(_))) => "type",
Some(SelfType(_)) => "type",
Some(Pat(_)) => "text",
Some(SelfParam(_)) => "type",
Some(GenericParam(_)) => "type",
None => "text",
}
} else {
"text"
}
}
NAME => "function",
TYPE_ALIAS_DEF | TYPE_ARG | TYPE_PARAM => "type",
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
LIFETIME => "parameter",
T![unsafe] => "keyword.unsafe",
@ -87,9 +120,23 @@ mod tests {
fn test_highlighting() {
let (analysis, file_id) = single_file(
r#"
#[derive(Clone, Debug)]
struct Foo {
pub x: i32,
pub y: i32,
}
fn foo<T>() -> T {
unimplemented!();
}
// comment
fn main() {}
println!("Hello, {}!", 92);
let mut vec = Vec::new();
vec.push(Foo { x: 0, y: 1 });
unsafe { vec.set_len(0); }
"#,
);
let result = analysis.highlight(file_id);