From f1ec88cc56c0bb24c56e6f9f898ac567ce34c79f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Thu, 23 May 2019 13:26:38 +0300 Subject: Improve highlighting of name refs --- .../src/snapshots/tests__highlighting.snap | 128 +++++++++++++++++++-- crates/ra_ide_api/src/syntax_highlighting.rs | 49 +++++++- 2 files changed, 168 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/snapshots/tests__highlighting.snap b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap index 72029e0ed..9d4c04db3 100644 --- a/crates/ra_ide_api/src/snapshots/tests__highlighting.snap +++ b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap @@ -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), + range: [1; 24), + tag: "attribute" + }, + HighlightedRange { + range: [25; 31), + tag: "keyword" + }, + HighlightedRange { + range: [32; 35), + tag: "function" + }, + HighlightedRange { + 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: [117; 127), tag: "comment" }, HighlightedRange { - range: [12; 14), + range: [128; 130), tag: "keyword" }, HighlightedRange { - range: [15; 19), + range: [131; 135), tag: "function" }, HighlightedRange { - range: [29; 37), + range: [145; 153), tag: "macro" }, HighlightedRange { - range: [38; 50), + 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" } ] 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 "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 { + 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); -- cgit v1.2.3