Skip to content

Commit b77b1f2

Browse files
srawlinsCommit Queue
authored andcommitted
DAS plugins: Add docs for writing a plugin.
Here we provide docs specifically for writing a plugin, using a plugin, and we improve the text about writing rules. Change-Id: I3fcfbc50609054158e5bdf964499005a79b4fba8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410160 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent ca2d428 commit b77b1f2

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Using plugins
2+
3+
This document describes how to enable an analyzer plugin. An analyzer plugin
4+
can be enabled for a given package, so that the plugin can report diagnostics
5+
(lints and warnings) and offer quick fixes. Plugins are enabled via the
6+
`analysis_options.yaml` file under the top-level `plugins` section:
7+
8+
```
9+
plugins:
10+
my_plugin: ^1.0.0
11+
```
12+
13+
Note: This is similar to how analyzer plugins are enabled in the [legacy][]
14+
analyzer plugin system. However, in the legacy system, this `plugins` section
15+
is listed under the top-level `analyzer` section. In the new analyzer plugin
16+
system, `plugins` is a top-level section.
17+
18+
Individual plugins are listed similar to how dependencies are listed in a
19+
`pubspec.yaml` file; they are listed as a key-value pair, with the package name
20+
as the key. The value can either be
21+
22+
* a package version constraint, in which case the package is downloaded from
23+
https://pub.dev,
24+
* a git dependency,
25+
* an absolute path.
26+
27+
For example, while developing a plugin locally, it can be enabled as:
28+
29+
```
30+
plugins:
31+
my_plugin:
32+
path: /path/to/my_plugin
33+
```
34+
35+
Note: after any change is made to the `plugins` section of an
36+
`analysis_options.yaml` file, the Dart Analysis Server must be restarted to see
37+
the effects.
38+
39+
[legacy]: https://github.com/dart-lang/sdk/blob/main/pkg/analyzer_plugin/doc/tutorial/tutorial.md
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Writing a plugin
2+
3+
This document describes how to write an analyzer plugin, to provide custom
4+
static analysis, or to offer custom quick fixes in an IDE.
5+
6+
## The pubspec file
7+
8+
An analyzer plugin is a Dart package, so let's start with the `pubspec.yaml` file:
9+
10+
```yaml
11+
name: test_analyzer_plugin
12+
version: 0.0.1
13+
14+
environment:
15+
sdk: '>=3.6.0 <4.0.0'
16+
17+
dependencies:
18+
analysis_server_plugin: any
19+
analyzer: ^7.2.0
20+
```
21+
22+
There is nothing special about this pubspec; note that we need a dependency on
23+
the `analysis_server_plugin` package, and on the `analyzer` package, supporting
24+
at least 7.2.0. The version of the analyzer package needs to move lockstep with
25+
the Dart SDK. For Dart 3.8.0-70.1.beta, `^7.2.0` is a good version constraint.
26+
27+
## The main Dart file
28+
29+
One source file is required, at `lib/main.dart`. Here is the basic layout:
30+
31+
```dart
32+
import 'package:analysis_server_plugin/plugin.dart';
33+
import 'package:analysis_server_plugin/registry.dart';
34+
35+
final plugin = SimplePlugin();
36+
37+
class SimplePlugin extends Plugin {
38+
@override
39+
void register(PluginRegistry registry) {
40+
// Here we register analysis rules, and quick fixes.
41+
}
42+
}
43+
```
44+
45+
Here we have a class, `SimplePlugin`, which extends the `Plugin` class from the
46+
`analysis_server_plugin` package. This class has one method that we override:
47+
`register`. In the `register` method, we can register analysis rules and quick
48+
fixes (CorrectionProducers). See details in the [writing rules][] doc, and the
49+
writing quick fixes doc (TODO).
50+
51+
Additionally, we provide a top-level variable in this file called `plugin`,
52+
which is an instance of our `SimplePlugin` class. When a running instance of
53+
the Dart Analysis Server needs to use this analyzer plugin, it generates some
54+
code that needs to _import_ this `lib/main.dart` file, and that references this
55+
`plugin` top-level variable.
56+
57+
# Debugging a plugin
58+
59+
If a plugin is not behaving as expected (for example, warnings are not
60+
appearing in the IDE), you can check the [analyzer diagnostics pages][]. If the
61+
plugin isolate has crashed, the "plugins" screen will display the crash.
62+
63+
`print` cannot be used in plugin code to debug. Instead, writing to a log file
64+
can help in debugging plugin code.
65+
66+
[writing rules]: https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server_plugin/doc/writing_rules.md
67+
[analyzer diagnostics pages]: https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/doc/tutorial/instrumentation.md#open-the-analyzer-diagnostics-pages

pkg/analysis_server_plugin/doc/writing_rules.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MyRule extends AnalysisRule {
2626
2727
MyRule()
2828
: super(
29-
name: LintNames.prefer_void_to_null,
29+
name: 'my_rule',
3030
description: 'A longer description of the rule.',
3131
);
3232
@@ -124,6 +124,13 @@ Let's look at each declaration individually:
124124
analysis. Typically, a 'visit' method like this is where we perform some
125125
analysis and maybe report lint(s) or warning(s).
126126

127+
Some rules do not require complex logic in the visitor class, but rules may
128+
also need to walk up or down the syntax tree, or examine properties of nodes
129+
carefully and thoroughly. For many examples of analysis rules and their visitor
130+
classes, see the [lint rules] that ship with the Dart Analysis Server.
131+
132+
[lint rules]: https://github.com/dart-lang/sdk/tree/main/pkg/linter/lib/src/rules
133+
127134
## Registering an analysis rule
128135

129136
In order for an analysis rule to be used in an analyzer plugin, it must be
@@ -144,4 +151,6 @@ enabled by default. To register an analysis rule as a "lint rule," such that it
144151
must be specifically enabled from analysis options, use `registerLintRule`
145152
instead.
146153

147-
TODO(srawlins): Write up and link documentation for this Plugin subclass.
154+
See [writing a plugin][] for for information about the `Plugin` class.
155+
156+
[writing a plugin]: https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server_plugin/doc/writing_rules.md

0 commit comments

Comments
 (0)