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 --- Cargo.lock | 1 + .../src/snapshots/tests__highlighting.snap | 128 +++++++++++++++++++-- crates/ra_ide_api/src/syntax_highlighting.rs | 49 +++++++- editors/code/package.json | 47 +++++++- editors/code/src/highlighting.ts | 7 +- 5 files changed, 221 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ecea2444e..9502fed45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -491,6 +491,7 @@ version = "0.2.0" dependencies = [ "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "flexi_logger 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lsp-types 0.57.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", 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); diff --git a/editors/code/package.json b/editors/code/package.json index 750c97bb1..cde5fbcb8 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -371,12 +371,57 @@ }, { "id": "ralsp.macro", - "description": "Color for DFAF8F", + "description": "Color for macros", "defaults": { "dark": "#BFEBBF", "light": "#DD6718", "highContrast": "#ED7718" } + }, + { + "id": "ralsp.constant", + "description": "Color for constants", + "defaults": { + "dark": "#569cd6", + "light": "#267cb6", + "highContrast": "#569cd6" + } + }, + { + "id": "ralsp.type", + "description": "Color for types", + "defaults": { + "dark": "#4EC9B0", + "light": "#267F99", + "highContrast": "#4EC9B0" + } + }, + { + "id": "ralsp.field", + "description": "Color for fields", + "defaults": { + "dark": "#4EC9B0", + "light": "#267F99", + "highContrast": "#4EC9B0" + } + }, + { + "id": "ralsp.variable", + "description": "Color for variables", + "defaults": { + "dark": "#4EC9B0", + "light": "#267F99", + "highContrast": "#4EC9B0" + } + }, + { + "id": "ralsp.module", + "description": "Color for modules", + "defaults": { + "dark": "#D4D4D4", + "light": "#000000", + "highContrast": "#FFFFFF" + } } ] } diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index e1a68544a..8389d94b8 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -33,11 +33,16 @@ export class Highlighter { colorContrib('keyword.unsafe'), colorContrib('function'), colorContrib('parameter'), + colorContrib('constant'), + colorContrib('type'), colorContrib('builtin'), colorContrib('text'), colorContrib('attribute'), colorContrib('literal'), - colorContrib('macro') + colorContrib('macro'), + colorContrib('variable'), + colorContrib('field'), + colorContrib('module') ]; return new Map(decorations); -- cgit v1.2.3