OLD | NEW |
---|---|
(Empty) | |
1 # Providing Highlighting Information | |
2 | |
3 Highlighting information is used by clients to help users identify different | |
4 syntactic and semantic regions of their code. | |
5 | |
6 Syntactic highlighting is highlighting that is based completely on the syntax of | |
7 the code. For example, editors will often provide unique colors for comments, | |
8 string literals, and numeric literals. | |
9 | |
10 Semantic highlighting is highlighting that is based on semantic information. For | |
11 example, an editor could highlight references to fields differently than | |
12 references to local variables. Editors could also highlight references to | |
13 deprecated elements differently. | |
14 | |
15 ## Implementation details | |
16 | |
17 Highlighting information is available through a subscription. If the server has | |
18 subscribed for highlighting information in some set of files, then the plugin | |
19 should send the information in an `analysis.highlights` notification whenever | |
20 the information needs to be updated. | |
21 | |
22 When a notification needs to be sent, the method `sendHighlightsNotification` | |
23 will be invoked. This method is responsible for sending the notification. | |
24 | |
25 The easiest way to add support for this notification is by adding the classes | |
26 `HighlightsMixin` and `DartHighlightsMixin` (from | |
27 `package:analyzer_plugin/plugin/highlights_mixin.dart`) to the list of mixins | |
28 for your subclass of `ServerPlugin`. This will leave you with one abstract | |
29 method that you need to implement: `getHighlightsContributors`. That method is | |
30 responsible for returning a list of `HighlightsContributor`s. It is the | |
31 highlights contributors that produce the actual highlighting information. (Most | |
32 plugins will only need a single highlights contributor.) | |
33 | |
34 To write a highlights contributor, create a class that implements | |
35 `HighlightsContributor`. The interface defines a single method named | |
36 `computeHighlights`. The method has two arguments: an `HighlightsRequest` that | |
37 describes the file for which highlighting information is being requested and an | |
38 `HighlightsCollector` through which highlighting information is to be added. | |
39 | |
40 If you mix in the class `DartHighlightsMixin`, then the request will be an | |
41 instance of `DartHighlightsRequest`, which also has analysis results. | |
42 | |
43 ## Example | |
44 | |
45 Start by creating a class that implements `HighlightsContributor`, then | |
46 implement the method `computeOutline`. This method is typically implemented | |
scheglov
2017/08/25 16:15:42
Not Outline.
Brian Wilkerson
2017/08/25 16:21:24
Done
| |
47 by creating a visitor (such as an AstVisitor) that can visit the results of the | |
48 analysis (such as a CompilationUnit) and extract the highlighting information | |
49 from the analysis result. | |
50 | |
51 For example, your contributor might look something like the following: | |
52 | |
53 ```dart | |
54 class MyHighlightsContributor implements HighlightsContributor { | |
55 @override | |
56 void computeHighlights( | |
57 HighlightsRequest request, HighlightsCollector collector) { | |
58 if (request is DartHighlightsRequest) { | |
59 HighlightsVisitor visitor = new HighlightsVisitor(collector); | |
60 request.result.unit.accept(visitor); | |
61 } | |
62 } | |
63 } | |
64 | |
65 class HighlightsVisitor extends RecursiveAstVisitor { | |
66 final HighlightsCollector collector; | |
67 | |
68 HighlightsVisitor(this.collector); | |
69 | |
70 @override | |
71 void visitSimpleIdentifier(ClassDeclaration node) { | |
72 // ... | |
73 } | |
74 } | |
75 ``` | |
76 | |
77 Given a contributor like the one above, you can implement your plugin similar to | |
78 the following: | |
79 | |
80 ```dart | |
81 class MyPlugin extends ServerPlugin with HighlightsMixin, DartHighlightsMixin { | |
82 // ... | |
83 | |
84 @override | |
85 List<HighlightsContributor> getHighlightsContributors(String path) { | |
86 return <HighlightsContributor>[new MyHighlightsContributor()]; | |
87 } | |
88 } | |
89 ``` | |
OLD | NEW |