From ae9fdefa4fdc3501f3e6f2219a54e24da27fd001 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Wed, 28 Feb 2018 20:39:48 +0000 Subject: [PATCH] Fix example code just showing endless spinner when deployed from Windows (#14878) * If example code is not found, render a message instead of an endless spinner * Trim codeTags to avoid lookup issues with windows carriage returns * Trim \r on Windows off the end of code blocks so they're consistently \n * Add a test to ensure sample code parses if built on Windows * Add a comment about why we trimRight() Fixes #14820. --- examples/flutter_gallery/lib/gallery/demo.dart | 2 +- .../flutter_gallery/lib/gallery/example_code_parser.dart | 6 ++++-- examples/flutter_gallery/test/example_code_parser_test.dart | 5 +++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/examples/flutter_gallery/lib/gallery/demo.dart b/examples/flutter_gallery/lib/gallery/demo.dart index c6e477ce61..ff67cc9ce1 100644 --- a/examples/flutter_gallery/lib/gallery/demo.dart +++ b/examples/flutter_gallery/lib/gallery/demo.dart @@ -117,7 +117,7 @@ class FullScreenCodeDialogState extends State { getExampleCode(widget.exampleCodeTag, DefaultAssetBundle.of(context)).then((String code) { if (mounted) { setState(() { - _exampleCode = code; + _exampleCode = code ?? 'Example code not found'; }); } }); diff --git a/examples/flutter_gallery/lib/gallery/example_code_parser.dart b/examples/flutter_gallery/lib/gallery/example_code_parser.dart index dc5b79ba78..6a834bc40d 100644 --- a/examples/flutter_gallery/lib/gallery/example_code_parser.dart +++ b/examples/flutter_gallery/lib/gallery/example_code_parser.dart @@ -33,7 +33,7 @@ Future _parseExampleCode(AssetBundle bundle) async { if (line.startsWith(_kStartTag)) { // Starting a new code block. codeBlock = []; - codeTag = line.substring(_kStartTag.length); + codeTag = line.substring(_kStartTag.length).trim(); } else { // Just skipping the line. } @@ -46,7 +46,9 @@ Future _parseExampleCode(AssetBundle bundle) async { codeTag = null; } else { // Add to the current block - codeBlock.add(line); + // trimRight() to remove any \r on Windows + // without removing any useful indentation + codeBlock.add(line.trimRight()); } } } diff --git a/examples/flutter_gallery/test/example_code_parser_test.dart b/examples/flutter_gallery/test/example_code_parser_test.dart index c023169e77..0e583d9754 100644 --- a/examples/flutter_gallery/test/example_code_parser_test.dart +++ b/examples/flutter_gallery/test/example_code_parser_test.dart @@ -18,6 +18,9 @@ void main() { final String codeSnippet1 = await getExampleCode('test_1', bundle); expect(codeSnippet1, 'test 1 0\ntest 1 1'); + + final String codeSnippet3 = await getExampleCode('test_2_windows_breaks', bundle); + expect(codeSnippet3, 'windows test 2 0\nwindows test 2 1'); }); } @@ -32,6 +35,8 @@ test 0 1 test 1 0 test 1 1 // END + +// START test_2_windows_breaks\r\nwindows test 2 0\r\nwindows test 2 1\r\n// END '''; class TestAssetBundle extends AssetBundle {