Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Providing Outlines | |
| 2 | |
| 3 Outline information is used by clients to allow users to see the structure of | |
| 4 their code. | |
| 5 | |
| 6 ## Implementation details | |
| 7 | |
| 8 Outline information can is available through a subscription. If the server has | |
|
scheglov
2017/08/24 18:29:20
"can" or "is"?
Brian Wilkerson
2017/08/24 18:52:46
Done ('is')
| |
| 9 subscribed for outline information in some set of files, the the plugin should | |
|
scheglov
2017/08/24 18:29:20
"the the" => "then the"?
Brian Wilkerson
2017/08/24 18:52:46
Done
| |
| 10 send the information in an `analysis.outline` notification whenever the | |
| 11 information needs to be updated. | |
| 12 | |
| 13 When a notification needs to be sent, the method`sendOutlineNotification` will | |
| 14 be invoked. This method is responsible for sending the notification. | |
| 15 | |
| 16 The easiest way to add support for this notification is by adding the classes | |
| 17 `OutlineMixin` and `DartOutlineMixin` (from | |
| 18 `package:analyzer_plugin/plugin/outline_mixin.dart`) to the list of mixins | |
| 19 for your subclass of `ServerPlugin`. This will leave you with one abstract | |
| 20 method that you need to implement: `getOutlineContributors`. That method is | |
| 21 responsible for returning a list of `OutlineContributor`s. It is the outline | |
| 22 contributors that produce the actual outline information. (Most plugins will | |
| 23 only need a single outline contributor.) | |
| 24 | |
| 25 To write an outline contributor, create a class that implements | |
| 26 `OutlineContributor`. The interface defines a single method named | |
| 27 `computeOutline`. The method has two arguments: an `OutlineRequest` that | |
| 28 describes the file for which outline information is being requested and an | |
| 29 `OutlineCollector` through which outline information is to be added. | |
| 30 | |
| 31 If you mix in the class `DartOutlineMixin`, then the request will be an instance | |
| 32 of `DartOutlineRequest`, which also has analysis results. | |
| 33 | |
| 34 ## Example | |
| 35 | |
| 36 Start by creating a class that implements `OutlineContributor`, then | |
| 37 implement the method `computeOutline`. This method is typically implemented | |
| 38 by creating a visitor (such as an AstVisitor) that can visit the results of the | |
| 39 analysis (such as a CompilationUnit) and extract the outline information from | |
| 40 the analysis result. | |
| 41 | |
| 42 For example, your contributor might look something like the following: | |
| 43 | |
| 44 ```dart | |
| 45 class MyOutlineContributor implements OutlineContributor { | |
| 46 @override | |
| 47 void computeOutline( | |
| 48 OutlineRequest request, OutlineCollector collector) { | |
| 49 if (request is DartOutlineRequest) { | |
| 50 OutlineVisitor visitor = new OutlineVisitor(collector); | |
| 51 request.result.unit.accept(visitor); | |
| 52 } | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 class OutlineVisitor extends RecursiveAstVisitor { | |
| 57 final OutlineCollector collector; | |
| 58 | |
| 59 OutlineVisitor(this.collector); | |
| 60 | |
| 61 @override | |
| 62 void visitClassDeclaration(ClassDeclaration node) { | |
| 63 // ... | |
| 64 } | |
| 65 } | |
| 66 ``` | |
| 67 | |
| 68 Given a contributor like the one above, you can implement your plugin similar to | |
| 69 the following: | |
| 70 | |
| 71 ```dart | |
| 72 class MyPlugin extends ServerPlugin with OutlineMixin, DartOutlineMixin { | |
| 73 // ... | |
| 74 | |
| 75 @override | |
| 76 List<OutlineContributor> getOutlineContributors(String path) { | |
| 77 return <OutlineContributor>[new MyOutlineContributor()]; | |
| 78 } | |
| 79 } | |
| 80 ``` | |
| OLD | NEW |