OLD | NEW |
(Empty) | |
| 1 # Providing Navigation |
| 2 |
| 3 Navigation information is used by clients to allow users to navigate to the |
| 4 location at which an identifier is defined. |
| 5 |
| 6 ## Implementation details |
| 7 |
| 8 Navigation information can be requested both by an `analysis.getNavigation` |
| 9 request and by a subscription. If the server has subscribed for navigation |
| 10 information in some set of files, the the plugin should send the information in |
| 11 an `analysis.navigation` notification whenever the information needs to be |
| 12 updated. |
| 13 |
| 14 When an `analysis.getNavigation` request is received, the method |
| 15 `handleAnalysisGetNavigation` will be invoked. This method is responsible for |
| 16 returning a response that contains the available navigation information. |
| 17 |
| 18 The easiest way to implement the method `handleAnalysisGetNavigation` is by |
| 19 adding the classes `NavigationMixin` and `DartNavigationMixin` (from |
| 20 `package:analyzer_plugin/plugin/navigation_mixin.dart`) to the list of mixins |
| 21 for your subclass of `ServerPlugin`. This will leave you with one abstract |
| 22 method that you need to implement: `getNavigationContributors`. That method is |
| 23 responsible for returning a list of `NavigationContributor`s. It is the |
| 24 navigation contributors that produce the actual navigation information. (Most |
| 25 plugins will only need a single navigation contributor.) |
| 26 |
| 27 To write a navigation contributor, create a class that implements |
| 28 `NavigationContributor`. The interface defines a single method named |
| 29 `computeNavigation`. The method has two arguments: a `NavigationRequest` that |
| 30 describes the region of the file for which navigation is being requested and a |
| 31 `NavigationCollector` through which navigation information is to be added. |
| 32 |
| 33 ## Example |
| 34 |
| 35 Start by creating a class that implements `NavigationContributor`, then |
| 36 implement the method `computeNavigation`. This method is typically implemented |
| 37 by creating a visitor (such as an AstVisitor) that can visit the results of the |
| 38 analysis (such as a CompilationUnit) and extract the navigation information from |
| 39 the analysis result. |
| 40 |
| 41 For example, your contributor might look something like the following: |
| 42 |
| 43 ```dart |
| 44 class MyNavigationContributor implements NavigationContributor { |
| 45 @override |
| 46 void computeNavigation( |
| 47 NavigationRequest request, NavigationCollector collector) { |
| 48 if (request is DartNavigationRequest) { |
| 49 NavigationVisitor visitor = new NavigationVisitor(collector); |
| 50 request.result.unit.accept(visitor); |
| 51 } |
| 52 } |
| 53 } |
| 54 |
| 55 class NavigationVisitor extends RecursiveAstVisitor { |
| 56 final NavigationCollector collector; |
| 57 |
| 58 NavigationVisitor(this.collector); |
| 59 |
| 60 @override |
| 61 void visitSimpleIdentifier(SimpleIdentifier node) { |
| 62 // ... |
| 63 } |
| 64 } |
| 65 ``` |
| 66 |
| 67 Given a contributor like the one above, you can implement your plugin similar to |
| 68 the following: |
| 69 |
| 70 ```dart |
| 71 class MyPlugin extends ServerPlugin with NavigationMixin, DartNavigationMixin { |
| 72 // ... |
| 73 |
| 74 @override |
| 75 List<NavigationContributor> getNavigationContributors(String path) { |
| 76 return <NavigationContributor>[new MyNavigationContributor()]; |
| 77 } |
| 78 } |
| 79 ``` |
OLD | NEW |