flutter/dev/snippets/config/templates
Greg Spencer 202b045b50
Rewrite the analyze-sample-code script to also analyze snippets (#23893)
This rewrites the sample code analysis script to be a little less of a hack (but still not pretty), and to handle snippets as well.

It also changes the semantics of how sample code is handled: the namespace for the sample code is now limited to the file that it appears in, so some additional "Examples can assume:" blocks were added. The upside of this is that there will be far fewer name collisions.

I fixed the output too: no longer will you get 4000 lines of numbered output with the error at the top and have to grep for the actual problem. It gives the filename and line number of the original location of the code (in the comment in the tree), and prints out the source code on the line that caused the problem along with the error.

For snippets, it prints out the location of the start of the snippet and the source code line that causes the problem. It can't print out the original line, because snippets get formatted when they are written, so the line might not be in the same place.
2018-11-05 07:31:35 -08:00
..
README.md Dartdoc snippet extension to inject full featured code snippets in to API docs. (#23281) 2018-10-23 13:50:24 -07:00
stateful_widget.tmpl Rewrite the analyze-sample-code script to also analyze snippets (#23893) 2018-11-05 07:31:35 -08:00
stateless_widget.tmpl Rewrite the analyze-sample-code script to also analyze snippets (#23893) 2018-11-05 07:31:35 -08:00

Creating Code Snippets

In general, creating application snippets can be accomplished with the following syntax inside of the dartdoc comment for a Flutter class/variable/enum/etc.:

/// {@tool snippet --template=stateful_widget}
/// Any text outside of the code blocks will be accumulated and placed at the
/// top of the snippet box as a description. Don't try and say "see the code
/// above" or "see the code below", since the location of the description may
/// change in the future. You can use dartdoc [Linking] in the description, and
/// __Markdown__ too.
/// ```dart preamble
/// class Foo extends StatelessWidget {
///   const Foo({this.value = ''});
/// 
///   String value; 
/// 
///   @override
///   Widget build(BuildContext context) {
///     return Text(value);
///   }
/// }
/// ```
/// This will get tacked on to the end of the description above, and shown above
/// the snippet.  These two code blocks will be separated by `///...` in the
/// short version of the snippet code sample.
/// ```dart
/// String myValue = 'Foo';
/// 
/// @override
/// Widget build(BuildContext) {
///   return const Foo(myValue);
/// }
/// ```
/// {@end-tool}

This will result in the template having the section that's inside "```dart" interpolated into the template's stateful widget's state object body.

All code within a code block in a snippet needs to be able to be run through dartfmt without errors, so it needs to be valid code (This shouldn't be an additional burden, since all code will also be compiled to be sure it compiles).

Available Templates

The templates available for using as an argument to the snippets tool are as follows:

  • stateful_widget : Takes a preamble in addition to the default code block, which will be placed at the top level of the Dart file, so bare function calls are not allowed in the preamble. The default code block is placed as the body of a stateful widget, so you will need to implement the build() function, and any state variables.