Skip to content

Commit

Permalink
Configure flutter_code_editor options with Hugo shortcode (#23926) (#…
Browse files Browse the repository at this point in the history
…24031)

* Configure flutter_code_editor options with Hugo shortcode (#23926)

* Minor fixes (#23926)

* Refactor after review (#23926)
  • Loading branch information
alexeyinkin authored Nov 15, 2022
1 parent 0f4ca63 commit f349f41
Show file tree
Hide file tree
Showing 31 changed files with 735 additions and 100 deletions.
2 changes: 1 addition & 1 deletion learning/tour-of-beam/frontend/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ packages:
name: flutter_code_editor
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1"
version: "0.1.4"
flutter_driver:
dependency: transitive
description: flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ExampleItemActions extends StatelessWidget {
return Row(
children: [
if (example.isMultiFile) multifilePopover,
if (example.complexity != Complexity.unspecified)
ComplexityWidget(complexity: example.complexity),
if (example.complexity != null)
ComplexityWidget(complexity: example.complexity!),
descriptionPopover,
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:playground/constants/params.dart';
import 'package:playground/modules/examples/models/example_token_type.dart';
import 'package:playground_components/playground_components.dart';
Expand Down Expand Up @@ -115,7 +116,7 @@ class ExamplesLoadingDescriptorFactory {
return null;
}

return _parseSingleExample(token);
return _parseSingleExample(token, params);
}

static ExamplesLoadingDescriptor? _tryParseOfCatalogDefaultExamples(
Expand All @@ -135,15 +136,32 @@ class ExamplesLoadingDescriptorFactory {
);
}

static ExampleLoadingDescriptor _parseSingleExample(String token) {
static ExampleLoadingDescriptor _parseSingleExample(
String token,
Map<String, dynamic> params,
) {
final viewOptions = ExampleViewOptions.fromShortMap(params);
final tokenType = ExampleTokenType.fromToken(token);

switch (tokenType) {
case ExampleTokenType.http:
return HttpExampleLoadingDescriptor(
sdk: Sdk.parseOrCreate(params['sdk']),
uri: Uri.parse(token),
viewOptions: viewOptions,
);

case ExampleTokenType.standard:
return StandardExampleLoadingDescriptor(path: token);
return StandardExampleLoadingDescriptor(
path: token,
viewOptions: viewOptions,
);

case ExampleTokenType.userShared:
return UserSharedExampleLoadingDescriptor(snippetId: token);
return UserSharedExampleLoadingDescriptor(
snippetId: token,
viewOptions: viewOptions,
);
}
}

Expand All @@ -166,22 +184,23 @@ class ExamplesLoadingDescriptorFactory {
return _emptyLazyLoadDescriptors;
}

return _defaultLazyLoadDescriptors;
return defaultLazyLoadDescriptors;
}

static Map<Sdk, List<ExampleLoadingDescriptor>>
get _emptyLazyLoadDescriptors {
return {
for (final sdk in Sdk.known)
sdk: [EmptyExampleLoadingDescriptor(sdk: sdk)]
sdk: [EmptyExampleLoadingDescriptor(sdk: sdk)],
};
}

@visibleForTesting
static Map<Sdk, List<ExampleLoadingDescriptor>>
get _defaultLazyLoadDescriptors {
get defaultLazyLoadDescriptors {
return {
for (final sdk in Sdk.known)
sdk: [CatalogDefaultExampleLoadingDescriptor(sdk: sdk)]
sdk: [CatalogDefaultExampleLoadingDescriptor(sdk: sdk)],
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
import 'package:playground_components/playground_components.dart';

enum ExampleTokenType {
/// A URI to load the plain content from.
http,

standard,
userShared,
;

static ExampleTokenType fromToken(String token) {
if (token.startsWith(RegExp('http(s)?://'))) {
return http;
}

final sdk = Sdk.tryParseExamplePath(token);
if (sdk != null) {
return standard;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ export 'src/models/example_loading_descriptors/content_example_loading_descripto
export 'src/models/example_loading_descriptors/empty_example_loading_descriptor.dart';
export 'src/models/example_loading_descriptors/example_loading_descriptor.dart';
export 'src/models/example_loading_descriptors/examples_loading_descriptor.dart';
export 'src/models/example_loading_descriptors/http_example_loading_descriptor.dart';
export 'src/models/example_loading_descriptors/standard_example_loading_descriptor.dart';
export 'src/models/example_loading_descriptors/user_shared_example_loading_descriptor.dart';
export 'src/models/example_view_options.dart';
export 'src/models/intents.dart';
export 'src/models/outputs.dart';
export 'src/models/sdk.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

import '../../cache/example_cache.dart';
import '../../enums/complexity.dart';
import '../../models/example.dart';
import '../../models/example_base.dart';
import '../../models/example_loading_descriptors/empty_example_loading_descriptor.dart';
Expand All @@ -34,14 +33,11 @@ class EmptyExampleLoader extends ExampleLoader {

@override
Future<Example> get future async => Example(
sdk: descriptor.sdk,
name: 'Embedded_Example',
path: '',
description: '',
sdk: descriptor.sdk,
source: '',
tags: [],
type: ExampleType.example,
source: '',
pipelineOptions: '',
complexity: Complexity.unspecified,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'catalog_default_example_loader.dart';
import 'content_example_loader.dart';
import 'empty_example_loader.dart';
import 'example_loader_factory.dart';
import 'http_example_loader.dart';
import 'standard_example_loader.dart';
import 'user_shared_example_loader.dart';

Expand All @@ -38,6 +39,7 @@ class ExamplesLoader {
defaultFactory.add(CatalogDefaultExampleLoader.new);
defaultFactory.add(ContentExampleLoader.new);
defaultFactory.add(EmptyExampleLoader.new);
defaultFactory.add(HttpExampleLoader.new);
defaultFactory.add(StandardExampleLoader.new);
defaultFactory.add(UserSharedExampleLoader.new);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'package:collection/collection.dart';
import 'package:http/http.dart' as http;

import '../../cache/example_cache.dart';
import '../../models/example.dart';
import '../../models/example_base.dart';
import '../../models/example_loading_descriptors/http_example_loading_descriptor.dart';
import 'example_loader.dart';

class HttpExampleLoader extends ExampleLoader {
final HttpExampleLoadingDescriptor descriptor;

const HttpExampleLoader({
required this.descriptor,
// TODO(alexeyinkin): Remove when this lands: https://github.com/dart-lang/language/issues/1813
required ExampleCache exampleCache,
});

@override
Future<Example> get future async {
final response = await http.get(descriptor.uri);

return Example(
name: descriptor.uri.path.split('/').lastOrNull ?? 'HTTP Example',
path: descriptor.uri.toString(),
sdk: descriptor.sdk,
source: response.body,
type: ExampleType.example,
viewOptions: descriptor.viewOptions,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import 'example_loader.dart';

/// Loads a given example from the local cache, then adds info from network.
///
/// This loader assumes that [ExampleState] is loading all examples to
/// This loader assumes that [ExampleCache] is loading all examples to
/// its cache. So it only completes if this is successful.
class StandardExampleLoader extends ExampleLoader {
final StandardExampleLoadingDescriptor descriptor;
Expand All @@ -41,7 +41,7 @@ class StandardExampleLoader extends ExampleLoader {
required this.descriptor,
required this.exampleCache,
}) {
_load();
unawaited(_load());
}

Future<void> _load() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class PlaygroundController with ChangeNotifier {
}
}

// TODO(alexeyinkin): Remove, used only in tests, refactor them.
void setSource(String source) {
final controller = requireSnippetEditingController();
controller.setSource(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,57 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_code_editor/flutter_code_editor.dart';

import '../enums/complexity.dart';
import '../models/example.dart';
import '../models/example_loading_descriptors/content_example_loading_descriptor.dart';
import '../models/example_loading_descriptors/example_loading_descriptor.dart';
import '../models/example_view_options.dart';
import '../models/sdk.dart';

class SnippetEditingController extends ChangeNotifier {
final Sdk sdk;
final CodeController codeController;
Example? _selectedExample;
String _pipelineOptions;
String _pipelineOptions = '';

SnippetEditingController({
required this.sdk,
Example? selectedExample,
String pipelineOptions = '',
}) : codeController = CodeController(
}) : codeController = CodeController(
language: sdk.highlightMode,
namedSectionParser: const BracketsStartEndNamedSectionParser(),
webSpaceFix: false,
),
_selectedExample = selectedExample,
_pipelineOptions = pipelineOptions;
);

set selectedExample(Example? value) {
_selectedExample = value;
setSource(_selectedExample?.source ?? '');

final viewOptions = value?.viewOptions;
if (viewOptions != null) {
_applyViewOptions(viewOptions);
}

_pipelineOptions = _selectedExample?.pipelineOptions ?? '';
notifyListeners();
}

void _applyViewOptions(ExampleViewOptions options) {
codeController.readOnlySectionNames = options.readOnlySectionNames.toSet();
codeController.visibleSectionNames = options.showSectionNames.toSet();

if (options.foldCommentAtLineZero) {
codeController.foldCommentAtLineZero();
}

if (options.foldImports) {
codeController.foldImports();
}

final unfolded = options.unfoldSectionNames;
if (unfolded.isNotEmpty) {
codeController.foldOutsideSections(unfolded);
}
}

Example? get selectedExample => _selectedExample;

set pipelineOptions(String value) {
Expand Down Expand Up @@ -82,15 +103,18 @@ class SnippetEditingController extends ChangeNotifier {
// user-shared examples, and an empty editor,
// https://github.com/apache/beam/issues/23252
return ContentExampleLoadingDescriptor(
complexity: _selectedExample?.complexity,
content: codeController.fullText,
name: _selectedExample?.name,
complexity: _selectedExample?.complexity ?? Complexity.unspecified,
sdk: sdk,
);
}

void setSource(String source) {
codeController.text = source;
codeController.readOnlySectionNames = const {};
codeController.visibleSectionNames = const {};

codeController.fullText = source;
codeController.historyController.deleteHistory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import 'package:json_annotation/json_annotation.dart';

enum Complexity {
@JsonValue('UNSPECIFIED')
unspecified,
@JsonValue('BASIC')
basic,
@JsonValue('MEDIUM')
Expand All @@ -29,17 +27,16 @@ enum Complexity {
advanced,
;

static Complexity fromString(String complexity) {
static Complexity? fromString(String? complexity) {
switch (complexity) {
case 'basic':
return Complexity.basic;
case 'medium':
return Complexity.medium;
case 'advanced':
return Complexity.advanced;
case 'unspecified':
return Complexity.unspecified;
}
throw Exception('Unknown complexity: $complexity');

return null;
}
}
Loading

0 comments on commit f349f41

Please sign in to comment.