@@ -6,169 +6,150 @@ import '../back_end/code_writer.dart';
6
6
import '../constants.dart' ;
7
7
import 'piece.dart' ;
8
8
9
- /// An import or export directive.
9
+ /// An import or export directive and its `show` and `hide` combinators .
10
10
///
11
11
/// Contains pieces for the keyword and URI, the optional `as` clause for
12
- /// imports, the configurations (`if` clauses), and combinators (`show` and
13
- /// `hide` ).
12
+ /// imports, and the configurations (`if` clauses).
13
+ ///
14
+ /// Combinators can be split like so:
15
+ ///
16
+ /// [State.initial] All on one line:
17
+ ///
18
+ /// ```
19
+ /// import 'animals.dart' show Ant, Bat hide Cat, Dog;
20
+ /// ```
21
+ ///
22
+ /// [_beforeCombinators] Wrap before each keyword:
23
+ ///
24
+ /// ```
25
+ /// import 'animals.dart'
26
+ /// show Ant, Bat
27
+ /// hide Cat, Dog;
28
+ /// ```
29
+ ///
30
+ /// [_firstCombinator] Wrap before each keyword and split the first list of
31
+ /// names (only used when there are multiple combinators):
32
+ ///
33
+ /// ```
34
+ /// import 'animals.dart'
35
+ /// show
36
+ /// Ant,
37
+ /// Bat
38
+ /// hide Cat, Dog;
39
+ /// ```
40
+ ///
41
+ /// [_secondCombinator] : Wrap before each keyword and split the second list of
42
+ /// names (only used when there are multiple combinators):
43
+ ///
44
+ /// ```
45
+ /// import 'animals.dart'
46
+ /// show Ant, Bat
47
+ /// hide
48
+ /// Cat,
49
+ /// Dog;
50
+ /// ```
51
+ ///
52
+ /// [State.split] Wrap before each keyword and split both lists of names:
53
+ ///
54
+ /// ```
55
+ /// import 'animals.dart'
56
+ /// show
57
+ /// Ant,
58
+ /// Bat
59
+ /// hide
60
+ /// Cat,
61
+ /// Dog;
62
+ /// ```
63
+ ///
64
+ /// These are not allowed:
65
+ ///
66
+ /// ```
67
+ /// // Wrap list but not keyword:
68
+ /// import 'animals.dart' show
69
+ /// Ant,
70
+ /// Bat
71
+ /// hide Cat, Dog;
72
+ ///
73
+ /// // Wrap one keyword but not both:
74
+ /// import 'animals.dart'
75
+ /// show Ant, Bat hide Cat, Dog;
76
+ ///
77
+ /// import 'animals.dart' show Ant, Bat
78
+ /// hide Cat, Dog;
79
+ /// ```
80
+ ///
81
+ /// This ensures that when any wrapping occurs, the keywords are always at the
82
+ /// beginning of the line.
14
83
class ImportPiece extends Piece {
84
+ /// Split before combinator keywords.
85
+ static const _beforeCombinators = State (1 );
86
+
87
+ /// Split before each name in the first combinator.
88
+ static const _firstCombinator = State (2 );
89
+
90
+ /// Split before each name in the second combinator.
91
+ static const _secondCombinator = State (3 );
92
+
15
93
/// The main directive and its URI.
16
- final Piece directive ;
94
+ final Piece _directive ;
17
95
18
96
/// If the directive has `if` configurations, this is them.
19
- final Piece ? configurations ;
97
+ final Piece ? _configurations ;
20
98
21
99
/// The `as` clause for this directive.
22
100
///
23
101
/// Null if this is not an import or it has no library prefix.
24
- final Piece ? asClause;
25
-
26
- /// The piece for the `show` and/or `hide` combinators.
27
- final Piece ? combinator;
28
-
29
- ImportPiece (
30
- this .directive, this .configurations, this .asClause, this .combinator);
102
+ final Piece ? _asClause;
31
103
32
- @override
33
- int get stateCount => 1 ;
104
+ final List <ImportCombinator > _combinators;
34
105
35
- @override
36
- void format (CodeWriter writer, int state) {
37
- writer.format (directive);
38
- writer.formatOptional (configurations);
39
- writer.formatOptional (asClause);
40
- writer.formatOptional (combinator);
106
+ ImportPiece (this ._directive, this ._configurations, this ._asClause,
107
+ this ._combinators) {
108
+ assert (_combinators.length <= 2 );
41
109
}
42
110
43
111
@override
44
- void forEachChild (void Function (Piece piece) callback) {
45
- callback (directive);
46
- if (configurations case var configurations? ) callback (configurations);
47
- if (asClause case var asClause? ) callback (asClause);
48
- if (combinator case var combinator? ) callback (combinator);
49
- }
112
+ List <State > get states => [
113
+ _beforeCombinators,
114
+ if (_combinators.length > 1 ) ...[
115
+ _firstCombinator,
116
+ _secondCombinator,
117
+ ],
118
+ State .split
119
+ ];
50
120
51
121
@override
52
- String toString () => 'Directive' ;
53
- }
54
-
55
- /// The combinator on a directive with only one combinator. It can be split:
56
- ///
57
- /// // 0: All on one line:
58
- /// import 'animals.dart' show Ant, Bat, Cat;
59
- ///
60
- /// // 1: Split before the keyword:
61
- /// import 'animals.dart'
62
- /// show Ant, Bat, Cat;
63
- ///
64
- /// // 2: Split before the keyword and each name:
65
- /// import 'animals.dart'
66
- /// show
67
- /// Ant,
68
- /// Bat,
69
- /// Cat;
70
- class OneCombinatorPiece extends Piece {
71
- final ImportCombinator combinator;
72
-
73
- OneCombinatorPiece (this .combinator);
74
-
75
- /// 0: No splits anywhere.
76
- /// 1: Split before combinator keyword.
77
- /// 2: Split before combinator keyword and before each name.
78
- @override
79
- int get stateCount => 3 ;
122
+ void format (CodeWriter writer, State state) {
123
+ writer.format (_directive);
124
+ writer.formatOptional (_configurations);
125
+ writer.formatOptional (_asClause);
126
+
127
+ if (_combinators.isNotEmpty) {
128
+ _combinators[0 ].format (writer,
129
+ splitKeyword: state != State .initial,
130
+ splitNames: state == _firstCombinator || state == State .split);
131
+ }
80
132
81
- @override
82
- void format (CodeWriter writer, int state) {
83
- combinator.format (writer, splitKeyword: state != 0 , splitNames: state == 2 );
133
+ if (_combinators.length > 1 ) {
134
+ _combinators[1 ].format (writer,
135
+ splitKeyword: state != State .initial,
136
+ splitNames: state == _secondCombinator || state == State .split);
137
+ }
84
138
}
85
139
86
140
@override
87
141
void forEachChild (void Function (Piece piece) callback) {
88
- combinator.forEachChild (callback);
89
- }
90
-
91
- @override
92
- String toString () => '1Comb' ;
93
- }
94
-
95
- /// The combinators on a directive with two combinators. It can be split:
96
- ///
97
- /// // 0: All on one line:
98
- /// import 'animals.dart' show Ant, Bat hide Cat, Dog;
99
- ///
100
- /// // 1: Wrap before each keyword:
101
- /// import 'animals.dart'
102
- /// show Ant, Bat
103
- /// hide Cat, Dog;
104
- ///
105
- /// // 2: Wrap before each keyword and split the first list of names:
106
- /// import 'animals.dart'
107
- /// show
108
- /// Ant,
109
- /// Bat
110
- /// hide Cat, Dog;
111
- ///
112
- /// // 3: Wrap before each keyword and split the second list of names:
113
- /// import 'animals.dart'
114
- /// show Ant, Bat
115
- /// hide
116
- /// Cat,
117
- /// Dog;
118
- ///
119
- /// // 4: Wrap before each keyword and split both lists of names:
120
- /// import 'animals.dart'
121
- /// show
122
- /// Ant,
123
- /// Bat
124
- /// hide
125
- /// Cat,
126
- /// Dog;
127
- ///
128
- /// These are not allowed:
129
- ///
130
- /// // Wrap list but not keyword:
131
- /// import 'animals.dart' show
132
- /// Ant,
133
- /// Bat
134
- /// hide Cat, Dog;
135
- ///
136
- /// // Wrap one keyword but not both:
137
- /// import 'animals.dart'
138
- /// show Ant, Bat hide Cat, Dog;
139
- ///
140
- /// import 'animals.dart' show Ant, Bat
141
- /// hide Cat, Dog;
142
- ///
143
- /// This ensures that when any wrapping occurs, the keywords are always at
144
- /// the beginning of the line.
145
- class TwoCombinatorPiece extends Piece {
146
- final List <ImportCombinator > combinators;
147
-
148
- TwoCombinatorPiece (this .combinators);
149
-
150
- @override
151
- int get stateCount => 5 ;
152
-
153
- @override
154
- void format (CodeWriter writer, int state) {
155
- assert (combinators.length == 2 );
142
+ callback (_directive);
143
+ if (_configurations case var configurations? ) callback (configurations);
144
+ if (_asClause case var asClause? ) callback (asClause);
156
145
157
- combinators[0 ].format (writer,
158
- splitKeyword: state != 0 , splitNames: state == 2 || state == 4 );
159
- combinators[1 ].format (writer,
160
- splitKeyword: state != 0 , splitNames: state == 3 || state == 4 );
161
- }
162
-
163
- @override
164
- void forEachChild (void Function (Piece piece) callback) {
165
- for (var combinator in combinators) {
146
+ for (var combinator in _combinators) {
166
147
combinator.forEachChild (callback);
167
148
}
168
149
}
169
150
170
151
@override
171
- String toString () => '2Comb ' ;
152
+ String toString () => 'Import ' ;
172
153
}
173
154
174
155
/// A single `show` or `hide` combinator within an import or export directive.
0 commit comments