aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-05-23 13:52:59 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-05-23 13:52:59 +0100
commitc290bb0749c14409cbab657e12123320176a33e5 (patch)
tree72f4f2b23092cebb2632ee430bf29ab69c6d53f1
parent44665685257f18d182ab116855dd9e8b054c49da (diff)
parentf1ec88cc56c0bb24c56e6f9f898ac567ce34c79f (diff)
Merge #1305
1305: Try to resolve name refs during highlighting r=matklad a=lnicola Preview: ![image](https://user-images.githubusercontent.com/308347/58253075-43464a80-7d70-11e9-84cc-e81990f2d3eb.png) This is probably not the cleanest implementation, but it's not clear to me what parts of `reference_definition` we don't want to run at this point. Also, is the `SourceAnalyzer` cheap enough to construct for each `NameRef`? Not like there's any alternative at this point, though. Co-authored-by: LaurenČ›iu Nicola <[email protected]>
-rw-r--r--Cargo.lock1
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__highlighting.snap128
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs49
-rw-r--r--editors/code/package.json47
-rw-r--r--editors/code/src/highlighting.ts7
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"
491dependencies = [ 491dependencies = [
492 "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 492 "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
493 "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", 493 "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
494 "flexi_logger 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)",
494 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", 495 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
495 "lsp-types 0.57.1 (registry+https://github.com/rust-lang/crates.io-index)", 496 "lsp-types 0.57.1 (registry+https://github.com/rust-lang/crates.io-index)",
496 "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", 497 "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 @@
1--- 1---
2created: "2019-03-23T16:20:31.394314144Z" 2created: "2019-05-23T12:10:32.628883358Z"
3creator: insta@0.7.1 3creator: insta@0.8.1
4source: crates/ra_ide_api/src/syntax_highlighting.rs 4source: crates/ra_ide_api/src/syntax_highlighting.rs
5expression: result 5expression: result
6--- 6---
7Ok( 7Ok(
8 [ 8 [
9 HighlightedRange { 9 HighlightedRange {
10 range: [1; 11), 10 range: [1; 24),
11 tag: "attribute"
12 },
13 HighlightedRange {
14 range: [25; 31),
15 tag: "keyword"
16 },
17 HighlightedRange {
18 range: [32; 35),
19 tag: "function"
20 },
21 HighlightedRange {
22 range: [42; 45),
23 tag: "keyword"
24 },
25 HighlightedRange {
26 range: [46; 47),
27 tag: "function"
28 },
29 HighlightedRange {
30 range: [49; 52),
31 tag: "text"
32 },
33 HighlightedRange {
34 range: [58; 61),
35 tag: "keyword"
36 },
37 HighlightedRange {
38 range: [62; 63),
39 tag: "function"
40 },
41 HighlightedRange {
42 range: [65; 68),
43 tag: "text"
44 },
45 HighlightedRange {
46 range: [73; 75),
47 tag: "keyword"
48 },
49 HighlightedRange {
50 range: [76; 79),
51 tag: "function"
52 },
53 HighlightedRange {
54 range: [80; 81),
55 tag: "type"
56 },
57 HighlightedRange {
58 range: [80; 81),
59 tag: "function"
60 },
61 HighlightedRange {
62 range: [88; 89),
63 tag: "type"
64 },
65 HighlightedRange {
66 range: [96; 110),
67 tag: "macro"
68 },
69 HighlightedRange {
70 range: [117; 127),
11 tag: "comment" 71 tag: "comment"
12 }, 72 },
13 HighlightedRange { 73 HighlightedRange {
14 range: [12; 14), 74 range: [128; 130),
15 tag: "keyword" 75 tag: "keyword"
16 }, 76 },
17 HighlightedRange { 77 HighlightedRange {
18 range: [15; 19), 78 range: [131; 135),
19 tag: "function" 79 tag: "function"
20 }, 80 },
21 HighlightedRange { 81 HighlightedRange {
22 range: [29; 37), 82 range: [145; 153),
23 tag: "macro" 83 tag: "macro"
24 }, 84 },
25 HighlightedRange { 85 HighlightedRange {
26 range: [38; 50), 86 range: [154; 166),
27 tag: "string" 87 tag: "string"
28 }, 88 },
29 HighlightedRange { 89 HighlightedRange {
30 range: [52; 54), 90 range: [168; 170),
91 tag: "literal"
92 },
93 HighlightedRange {
94 range: [178; 181),
95 tag: "keyword"
96 },
97 HighlightedRange {
98 range: [182; 185),
99 tag: "keyword"
100 },
101 HighlightedRange {
102 range: [186; 189),
103 tag: "macro"
104 },
105 HighlightedRange {
106 range: [197; 200),
107 tag: "macro"
108 },
109 HighlightedRange {
110 range: [192; 195),
111 tag: "text"
112 },
113 HighlightedRange {
114 range: [208; 211),
115 tag: "macro"
116 },
117 HighlightedRange {
118 range: [212; 216),
119 tag: "macro"
120 },
121 HighlightedRange {
122 range: [226; 227),
123 tag: "literal"
124 },
125 HighlightedRange {
126 range: [232; 233),
127 tag: "literal"
128 },
129 HighlightedRange {
130 range: [242; 248),
131 tag: "keyword.unsafe"
132 },
133 HighlightedRange {
134 range: [251; 254),
135 tag: "text"
136 },
137 HighlightedRange {
138 range: [255; 262),
139 tag: "text"
140 },
141 HighlightedRange {
142 range: [263; 264),
31 tag: "literal" 143 tag: "literal"
32 } 144 }
33 ] 145 ]
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)]
124struct Foo {
125 pub x: i32,
126 pub y: i32,
127}
128
129fn foo<T>() -> T {
130 unimplemented!();
131}
132
90// comment 133// comment
91fn main() {} 134fn 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);
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 @@
371 }, 371 },
372 { 372 {
373 "id": "ralsp.macro", 373 "id": "ralsp.macro",
374 "description": "Color for DFAF8F", 374 "description": "Color for macros",
375 "defaults": { 375 "defaults": {
376 "dark": "#BFEBBF", 376 "dark": "#BFEBBF",
377 "light": "#DD6718", 377 "light": "#DD6718",
378 "highContrast": "#ED7718" 378 "highContrast": "#ED7718"
379 } 379 }
380 },
381 {
382 "id": "ralsp.constant",
383 "description": "Color for constants",
384 "defaults": {
385 "dark": "#569cd6",
386 "light": "#267cb6",
387 "highContrast": "#569cd6"
388 }
389 },
390 {
391 "id": "ralsp.type",
392 "description": "Color for types",
393 "defaults": {
394 "dark": "#4EC9B0",
395 "light": "#267F99",
396 "highContrast": "#4EC9B0"
397 }
398 },
399 {
400 "id": "ralsp.field",
401 "description": "Color for fields",
402 "defaults": {
403 "dark": "#4EC9B0",
404 "light": "#267F99",
405 "highContrast": "#4EC9B0"
406 }
407 },
408 {
409 "id": "ralsp.variable",
410 "description": "Color for variables",
411 "defaults": {
412 "dark": "#4EC9B0",
413 "light": "#267F99",
414 "highContrast": "#4EC9B0"
415 }
416 },
417 {
418 "id": "ralsp.module",
419 "description": "Color for modules",
420 "defaults": {
421 "dark": "#D4D4D4",
422 "light": "#000000",
423 "highContrast": "#FFFFFF"
424 }
380 } 425 }
381 ] 426 ]
382 } 427 }
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 {
33 colorContrib('keyword.unsafe'), 33 colorContrib('keyword.unsafe'),
34 colorContrib('function'), 34 colorContrib('function'),
35 colorContrib('parameter'), 35 colorContrib('parameter'),
36 colorContrib('constant'),
37 colorContrib('type'),
36 colorContrib('builtin'), 38 colorContrib('builtin'),
37 colorContrib('text'), 39 colorContrib('text'),
38 colorContrib('attribute'), 40 colorContrib('attribute'),
39 colorContrib('literal'), 41 colorContrib('literal'),
40 colorContrib('macro') 42 colorContrib('macro'),
43 colorContrib('variable'),
44 colorContrib('field'),
45 colorContrib('module')
41 ]; 46 ];
42 47
43 return new Map<string, vscode.TextEditorDecorationType>(decorations); 48 return new Map<string, vscode.TextEditorDecorationType>(decorations);