Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1061)

Side by Side Diff: pkg/analyzer_plugin/doc/tutorial/assists.md

Issue 2973753003: Initial documentation for the plugin package (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer_plugin/README.md ('k') | pkg/analyzer_plugin/doc/tutorial/completion.md » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Providing Quick Assists
2
3 A quick assist is used by clients to provide a set of possible changes to code
4 that are based on the structure of the code. Quick assists are intended to help
5 users safely make local changes to code when those changes do not require any
6 user interaction. (Modifications that require interaction with users or that
7 touch multiple files are usually implemented as refactorings.)
8
9 For example, if the user has a function whose body consists of a single return
10 statement in a block, server will provide an assist to convert the function body
11 from a block to an expression (`=>`).
12
13 Assists have a priority associated with them. The priority allows the client to
14 display the assists that are most likely to be of use closer to the top of the
15 list when there are multiple assists available.
16
17 ## Implementation details
18
19 When appropriate, the analysis server will send your plugin an `edit.getAssists`
20 request. The request includes the `file`, `offset` and `length` associated with
21 the selected region of code.
22
23 When an `edit.getAssists` request is received, the method `handleEditGetAssists`
24 will be invoked. This method is responsible for returning a response that
25 contains the available assists.
26
27 The easiest way to implement this method is by adding the classes `AssistsMixin`
28 and `DartAssistsMixin` (from `package:analyzer_plugin/plugin/assist_mixin.dart`)
29 to the list of mixins for your subclass of `ServerPlugin`. This will leave you
30 with one abstract method that you need to implement: `getAssistContributors`.
31 That method is responsible for returning a list of `AssistContributor`s. It is
32 the assist contributors that produce the actual assists. (Most plugins will only
33 need a single assist contributor.)
34
35 To write an assist contributor, create a class that implements
36 `AssistContributor`. The interface defines a single method named
37 `computeAssists`. The method has two arguments: an `AssistRequest` that
38 describes the location at which assists were requested and an `AssistCollector`
39 through which assists are to be added.
40
41 The class `AssistContributorMixin` defines a support method that makes it easier
42 to implement `computeAssists`.
43
44 ## Example
45
46 Start by creating a class that implements `AssistContributor` and that mixes in
47 the class `AssistContributorMixin`, then implement the method `computeAssists`.
48 This method is typically implemented as a sequence of invocations of methods
49 that check to see whether a given assist is appropriate in the context of the
50 request
51
52 To learn about the support available for creating the edits, see
53 [Creating Edits][creatingEdits].
54
55 For example, your contributor might look something like the following:
56
57 ```dart
58 class MyAssistContributor extends Object
59 with AssistContributorMixin
60 implements AssistContributor {
61 static AssistKind wrapInIf =
62 new AssistKind('wrapInIf', 100, "Wrap in an 'if' statement");
63
64 DartAssistRequest request;
65
66 AssistCollector collector;
67
68 AnalysisSession get session => request.result.session;
69
70 @override
71 void computeAssists(DartAssistRequest request, AssistCollector collector) {
72 this.request = request;
73 this.collector = collector;
74 _wrapInIf();
75 _wrapInWhile();
76 // ...
77 }
78
79 void _wrapInIf() {
80 ChangeBuilder builder = new DartChangeBuilder(session);
81 // TODO Build the edit to wrap the selection in a 'if' statement.
82 addAssist(wrapInIf, builder);
83 }
84
85 void _wrapInWhile() {
86 // ...
87 }
88 }
89 ```
90
91 Given a contributor like the one above, you can implement your plugin similar to
92 the following:
93
94 ```dart
95 class MyPlugin extends ServerPlugin with AssistsMixin, DartAssistsMixin {
96 // ...
97
98 @override
99 List<AssistContributor> getAssistContributors(
100 covariant AnalysisDriver driver) {
mfairhurst 2017/07/07 13:45:46 I think in this case "covariant" is not needed
101 return <AssistContributor>[new MyAssistContributor()];
102 }
103 }
104 ```
105
106 [creatingEdits]: creating_edits.md
OLDNEW
« no previous file with comments | « pkg/analyzer_plugin/README.md ('k') | pkg/analyzer_plugin/doc/tutorial/completion.md » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698