OLD | NEW |
(Empty) | |
| 1 # Providing Occurrences Information |
| 2 |
| 3 Occurrences information is used by clients to help users identify all of the |
| 4 references to a single program element, such as a class, field, or local |
| 5 variable, within a single file. |
| 6 |
| 7 ## Implementation details |
| 8 |
| 9 Occurrences information is available through a subscription. If the server has |
| 10 subscribed for occurrences information in some set of files, then the plugin |
| 11 should send the information in an `analysis.occurrences` notification whenever |
| 12 the information needs to be updated. |
| 13 |
| 14 When a notification needs to be sent, the method `sendOccurrencesNotification` |
| 15 will be invoked. This method is responsible for sending the notification. |
| 16 |
| 17 The easiest way to add support for this notification is by adding the classes |
| 18 `OccurrencesMixin` and `DartOccurrencesMixin` (from |
| 19 `package:analyzer_plugin/plugin/occurrences_mixin.dart`) to the list of mixins |
| 20 for your subclass of `ServerPlugin`. This will leave you with one abstract |
| 21 method that you need to implement: `getOccurrencesContributors`. That method is |
| 22 responsible for returning a list of `OccurrencesContributor`s. It is the |
| 23 occurrences contributors that produce the actual occurrences information. (Most |
| 24 plugins will only need a single occurrences contributor.) |
| 25 |
| 26 To write an occurrences contributor, create a class that implements |
| 27 `OccurrencesContributor`. The interface defines a single method named |
| 28 `computeOccurrences`. The method has two arguments: an `OccurrencesRequest` that |
| 29 describes the file for which occurrences information is being requested and an |
| 30 `OccurrencesCollector` through which occurrences information is to be added. |
| 31 |
| 32 If you mix in the class `DartOccurrencesMixin`, then the request will be an |
| 33 instance of `DartOccurrencesRequest`, which also has analysis results. |
| 34 |
| 35 ## Example |
| 36 |
| 37 Start by creating a class that implements `OccurrencesContributor`, then |
| 38 implement the method `computeOccurrences`. This method is typically implemented |
| 39 by creating a visitor (such as an AstVisitor) that can visit the results of the |
| 40 analysis (such as a CompilationUnit) and extract the occurrences information |
| 41 from the analysis result. |
| 42 |
| 43 For example, your contributor might look something like the following: |
| 44 |
| 45 ```dart |
| 46 class MyOccurrencesContributor implements OccurrencesContributor { |
| 47 @override |
| 48 void computeOccurrences( |
| 49 OccurrencesRequest request, OccurrencesCollector collector) { |
| 50 if (request is DartOccurrencesRequest) { |
| 51 OccurrencesVisitor visitor = new OccurrencesVisitor(collector); |
| 52 request.result.unit.accept(visitor); |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 class OccurrencesVisitor extends RecursiveAstVisitor { |
| 58 final OccurrencesCollector collector; |
| 59 |
| 60 OccurrencesVisitor(this.collector); |
| 61 |
| 62 @override |
| 63 void visitSimpleIdentifier(SimpleIdentifier node) { |
| 64 // ... |
| 65 } |
| 66 } |
| 67 ``` |
| 68 |
| 69 Given a contributor like the one above, you can implement your plugin similar to |
| 70 the following: |
| 71 |
| 72 ```dart |
| 73 class MyPlugin extends ServerPlugin with OccurrencesMixin, DartOccurrencesMixin
{ |
| 74 // ... |
| 75 |
| 76 @override |
| 77 List<OccurrencesContributor> getOccurrencesContributors(String path) { |
| 78 return <OccurrencesContributor>[new MyOccurrencesContributor()]; |
| 79 } |
| 80 } |
| 81 ``` |
OLD | NEW |