OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:analyzer/dart/analysis/results.dart'; |
| 6 import 'package:analyzer/file_system/file_system.dart'; |
| 7 import 'package:analyzer/src/generated/source.dart'; |
| 8 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 9 import 'package:analyzer_plugin/protocol/protocol_common.dart' |
| 10 hide AnalysisError; |
| 11 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
| 12 import 'package:analyzer_plugin/src/utilities/folding/folding.dart'; |
| 13 import 'package:analyzer_plugin/utilities/generator.dart'; |
| 14 |
| 15 /** |
| 16 * The information about a requested set of folding regions when computing |
| 17 * folding regions in a `.dart` file. |
| 18 * |
| 19 * Clients may not extend, implement or mix-in this class. |
| 20 */ |
| 21 abstract class DartFoldingRequest implements FoldingRequest { |
| 22 /** |
| 23 * The analysis result for the file for which the folding regions are being |
| 24 * requested. |
| 25 */ |
| 26 ResolveResult get result; |
| 27 } |
| 28 |
| 29 /** |
| 30 * An object that [FoldingContributor]s use to record folding regions. |
| 31 * |
| 32 * Clients may not extend, implement or mix-in this class. |
| 33 */ |
| 34 abstract class FoldingCollector { |
| 35 /** |
| 36 * Record a new folding region corresponding to the given [range] that has the |
| 37 * given [kind]. |
| 38 */ |
| 39 void addRange(SourceRange range, FoldingKind kind); |
| 40 |
| 41 /** |
| 42 * Record a new folding region with the given [offset] and [length] that has |
| 43 * the given [kind]. |
| 44 */ |
| 45 void addRegion(int offset, int length, FoldingKind kind); |
| 46 } |
| 47 |
| 48 /** |
| 49 * An object used to produce folding regions. |
| 50 * |
| 51 * Clients may implement this class when implementing plugins. |
| 52 */ |
| 53 abstract class FoldingContributor { |
| 54 /** |
| 55 * Contribute folding regions into the given [collector]. |
| 56 */ |
| 57 void computeFolding(FoldingRequest request, FoldingCollector collector); |
| 58 } |
| 59 |
| 60 /** |
| 61 * A generator that will generate an 'analysis.folding' notification. |
| 62 * |
| 63 * Clients may not extend, implement or mix-in this class. |
| 64 */ |
| 65 class FoldingGenerator { |
| 66 /** |
| 67 * The contributors to be used to generate the folding regions. |
| 68 */ |
| 69 final List<FoldingContributor> contributors; |
| 70 |
| 71 /** |
| 72 * Initialize a newly created folding generator to use the given |
| 73 * [contributors]. |
| 74 */ |
| 75 FoldingGenerator(this.contributors); |
| 76 |
| 77 /** |
| 78 * Create an 'analysis.folding' notification. If any of the contributors |
| 79 * throws an exception, also create a non-fatal 'plugin.error' notification. |
| 80 */ |
| 81 GeneratorResult generateFoldingNotification(FoldingRequest request) { |
| 82 List<Notification> notifications = <Notification>[]; |
| 83 FoldingCollectorImpl collector = new FoldingCollectorImpl(); |
| 84 for (FoldingContributor contributor in contributors) { |
| 85 try { |
| 86 contributor.computeFolding(request, collector); |
| 87 } catch (exception, stackTrace) { |
| 88 notifications.add(new PluginErrorParams( |
| 89 false, exception.toString(), stackTrace.toString()) |
| 90 .toNotification()); |
| 91 } |
| 92 } |
| 93 notifications.add(new AnalysisFoldingParams(request.path, collector.regions) |
| 94 .toNotification()); |
| 95 return new GeneratorResult(null, notifications); |
| 96 } |
| 97 } |
| 98 |
| 99 /** |
| 100 * The information about a requested set of folding regions. |
| 101 * |
| 102 * Clients may not extend, implement or mix-in this class. |
| 103 */ |
| 104 abstract class FoldingRequest { |
| 105 /** |
| 106 * Return the path of the file for which folding regions are being requested. |
| 107 */ |
| 108 String get path; |
| 109 |
| 110 /** |
| 111 * Return the resource provider associated with this request. |
| 112 */ |
| 113 ResourceProvider get resourceProvider; |
| 114 } |
OLD | NEW |