diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-23 13:52:59 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-23 13:52:59 +0100 |
commit | c290bb0749c14409cbab657e12123320176a33e5 (patch) | |
tree | 72f4f2b23092cebb2632ee430bf29ab69c6d53f1 /crates/ra_ide_api | |
parent | 44665685257f18d182ab116855dd9e8b054c49da (diff) | |
parent | f1ec88cc56c0bb24c56e6f9f898ac567ce34c79f (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]>
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/snapshots/tests__highlighting.snap | 128 | ||||
-rw-r--r-- | crates/ra_ide_api/src/syntax_highlighting.rs | 49 |
2 files changed, 168 insertions, 9 deletions
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 | --- |
2 | created: "2019-03-23T16:20:31.394314144Z" | 2 | created: "2019-05-23T12:10:32.628883358Z" |
3 | creator: insta@0.7.1 | 3 | creator: insta@0.8.1 |
4 | source: crates/ra_ide_api/src/syntax_highlighting.rs | 4 | source: crates/ra_ide_api/src/syntax_highlighting.rs |
5 | expression: result | 5 | expression: result |
6 | --- | 6 | --- |
7 | Ok( | 7 | Ok( |
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)] | ||
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); |