@@ -13,28 +13,25 @@ import 'package:analyzer_plugin/utilities/range_factory.dart';
1313
1414/// Sorter for unit/class members.
1515class MemberSorter {
16- final String initialCode ;
16+ final String _initialCode ;
1717
18- final CompilationUnit unit ;
18+ final CompilationUnit _unit ;
1919
20- final LineInfo lineInfo;
21-
22- final String endOfLine;
20+ final LineInfo _lineInfo;
2321
2422 final List <_PriorityItem > _priorityItems;
2523
2624 String code;
2725
2826 MemberSorter (
29- this .initialCode ,
30- this .unit ,
27+ this ._initialCode ,
28+ this ._unit ,
3129 CodeStyleOptions codeStyle,
32- this .lineInfo,
33- ) : endOfLine = getEOL (initialCode),
34- code = initialCode,
35- _priorityItems = _getPriorityItems (codeStyle);
30+ this ._lineInfo,
31+ ) : _priorityItems = _getPriorityItems (codeStyle),
32+ code = _initialCode;
3633
37- /// Return the [SourceEdit] s that sort [unit ] .
34+ /// Returns the [SourceEdit] s that sort [_unit ] .
3835 List <SourceEdit > sort () {
3936 _sortClassesMembers ();
4037 _sortUnitMembers ();
@@ -43,8 +40,8 @@ class MemberSorter {
4340 _sortUnitDirectives ();
4441 // prepare edits
4542 var edits = < SourceEdit > [];
46- if (code != initialCode ) {
47- var diff = computeSimpleDiff (initialCode , code);
43+ if (code != _initialCode ) {
44+ var diff = computeSimpleDiff (_initialCode , code);
4845 var edit = SourceEdit (diff.offset, diff.length, diff.replacement);
4946 edits.add (edit);
5047 }
@@ -63,7 +60,7 @@ class MemberSorter {
6360 var priority2 = _getPriority (o2.item);
6461 if (priority1 == priority2) {
6562 // don't reorder class fields
66- if (o1.item.kind == _MemberKind .CLASS_FIELD ) {
63+ if (o1.item.kind == _MemberKind .classField ) {
6764 return o1.offset - o2.offset;
6865 }
6966 // sort all other members by name
@@ -100,7 +97,7 @@ class MemberSorter {
10097
10198 /// Sorts all class members.
10299 void _sortClassesMembers () {
103- for (var unitMember in unit .declarations) {
100+ for (var unitMember in _unit .declarations) {
104101 if (unitMember is ClassDeclaration ) {
105102 _sortClassMembers (unitMember.members);
106103 } else if (unitMember is EnumDeclaration ) {
@@ -123,13 +120,13 @@ class MemberSorter {
123120 var isStatic = false ;
124121 String name;
125122 if (member is ConstructorDeclaration ) {
126- kind = _MemberKind .CLASS_CONSTRUCTOR ;
123+ kind = _MemberKind .classConstructor ;
127124 name = member.name? .lexeme ?? '' ;
128125 } else if (member is FieldDeclaration ) {
129126 var fieldDeclaration = member;
130127 List <VariableDeclaration > fields = fieldDeclaration.fields.variables;
131128 if (fields.isNotEmpty) {
132- kind = _MemberKind .CLASS_FIELD ;
129+ kind = _MemberKind .classField ;
133130 isStatic = fieldDeclaration.isStatic;
134131 name = fields[0 ].name.lexeme;
135132 } else {
@@ -141,19 +138,19 @@ class MemberSorter {
141138 isStatic = method.isStatic;
142139 name = method.name.lexeme;
143140 if (method.isGetter) {
144- kind = _MemberKind .CLASS_ACCESSOR ;
141+ kind = _MemberKind .classAccessor ;
145142 name += ' getter' ;
146143 } else if (method.isSetter) {
147- kind = _MemberKind .CLASS_ACCESSOR ;
144+ kind = _MemberKind .classAccessor ;
148145 name += ' setter' ;
149146 } else {
150- kind = _MemberKind .CLASS_METHOD ;
147+ kind = _MemberKind .classMethod ;
151148 }
152149 } else {
153150 throw StateError ('Unsupported class of member: ${member .runtimeType }' );
154151 }
155152 var item = _PriorityItem .forName (isStatic, name, kind);
156- var nodeRange = range.nodeWithComments (lineInfo , member);
153+ var nodeRange = range.nodeWithComments (_lineInfo , member);
157154 var offset = nodeRange.offset;
158155 var length = nodeRange.length;
159156 var text = code.substring (offset, offset + length);
@@ -165,76 +162,77 @@ class MemberSorter {
165162
166163 /// Sorts all [Directive] s.
167164 void _sortUnitDirectives () {
168- var importOrganizer = ImportOrganizer (code, unit , [], removeUnused: false );
165+ var importOrganizer = ImportOrganizer (code, _unit , [], removeUnused: false );
169166 importOrganizer.organize ();
170167 code = importOrganizer.code;
171168 }
172169
173170 /// Sorts all [CompilationUnitMember] s.
174171 void _sortUnitMembers () {
175172 var members = < _MemberInfo > [];
176- for (var member in unit .declarations) {
173+ for (var member in _unit .declarations) {
177174 _MemberKind kind;
178175 String name;
179- if (member is ClassDeclaration ) {
180- kind = _MemberKind .UNIT_CLASS ;
181- name = member.name.lexeme;
182- } else if (member is ClassTypeAlias ) {
183- kind = _MemberKind .UNIT_CLASS ;
184- name = member.name.lexeme;
185- } else if (member is EnumDeclaration ) {
186- kind = _MemberKind .UNIT_CLASS ;
187- name = member.name.lexeme;
188- } else if (member is ExtensionTypeDeclaration ) {
189- kind = _MemberKind .UNIT_EXTENSION_TYPE ;
190- name = member.name.lexeme;
191- } else if (member is ExtensionDeclaration ) {
192- kind = _MemberKind .UNIT_EXTENSION ;
193- name = member.name? .lexeme ?? '' ;
194- } else if (member is FunctionDeclaration ) {
195- name = member.name.lexeme;
196- if (member.isGetter) {
197- kind = _MemberKind .UNIT_ACCESSOR ;
198- name += ' getter' ;
199- } else if (member.isSetter) {
200- kind = _MemberKind .UNIT_ACCESSOR ;
201- name += ' setter' ;
202- } else {
203- if (name == 'main' ) {
204- kind = _MemberKind .UNIT_FUNCTION_MAIN ;
176+ switch (member) {
177+ case ClassDeclaration ():
178+ kind = _MemberKind .unitClass;
179+ name = member.name.lexeme;
180+ case ClassTypeAlias ():
181+ kind = _MemberKind .unitClass;
182+ name = member.name.lexeme;
183+ case EnumDeclaration ():
184+ kind = _MemberKind .unitClass;
185+ name = member.name.lexeme;
186+ case ExtensionTypeDeclaration ():
187+ kind = _MemberKind .unitExtensionType;
188+ name = member.name.lexeme;
189+ case ExtensionDeclaration ():
190+ kind = _MemberKind .unitExtension;
191+ name = member.name? .lexeme ?? '' ;
192+ case FunctionDeclaration ():
193+ name = member.name.lexeme;
194+ if (member.isGetter) {
195+ kind = _MemberKind .unitAccessor;
196+ name += ' getter' ;
197+ } else if (member.isSetter) {
198+ kind = _MemberKind .unitAccessor;
199+ name += ' setter' ;
205200 } else {
206- kind = _MemberKind .UNIT_FUNCTION ;
201+ if (name == 'main' ) {
202+ kind = _MemberKind .unitFunctionMain;
203+ } else {
204+ kind = _MemberKind .unitFunction;
205+ }
207206 }
208- }
209- } else if (member is FunctionTypeAlias ) {
210- kind = _MemberKind .UNIT_FUNCTION_TYPE ;
211- name = member.name.lexeme;
212- } else if (member is GenericTypeAlias ) {
213- kind = _MemberKind .UNIT_GENERIC_TYPE_ALIAS ;
214- name = member.name.lexeme;
215- } else if (member is MixinDeclaration ) {
216- kind = _MemberKind .UNIT_CLASS ;
217- name = member.name.lexeme;
218- } else if (member is TopLevelVariableDeclaration ) {
219- var variableDeclaration = member;
220- List <VariableDeclaration > variables =
221- variableDeclaration.variables.variables;
222- if (variables.isNotEmpty) {
223- if (variableDeclaration.variables.isConst) {
224- kind = _MemberKind .UNIT_VARIABLE_CONST ;
207+ case FunctionTypeAlias ():
208+ kind = _MemberKind .unitFunctionType;
209+ name = member.name.lexeme;
210+ case GenericTypeAlias ():
211+ kind = _MemberKind .unitGenericTypeAlias;
212+ name = member.name.lexeme;
213+ case MixinDeclaration ():
214+ kind = _MemberKind .unitClass;
215+ name = member.name.lexeme;
216+ case TopLevelVariableDeclaration ():
217+ var variableDeclaration = member;
218+ List <VariableDeclaration > variables =
219+ variableDeclaration.variables.variables;
220+ if (variables.isNotEmpty) {
221+ if (variableDeclaration.variables.isConst) {
222+ kind = _MemberKind .unitVariableConst;
223+ } else {
224+ kind = _MemberKind .unitVariable;
225+ }
226+ name = variables[0 ].name.lexeme;
225227 } else {
226- kind = _MemberKind .UNIT_VARIABLE ;
228+ // Don't sort members if there are errors in the code.
229+ return ;
227230 }
228- name = variables[0 ].name.lexeme;
229- } else {
230- // Don't sort members if there are errors in the code.
231- return ;
232- }
233- } else {
234- throw StateError ('Unsupported class of member: ${member .runtimeType }' );
231+ default :
232+ throw StateError ('Unsupported member type: ${member .runtimeType }' );
235233 }
236234 var item = _PriorityItem .forName (false , name, kind);
237- var nodeRange = range.nodeWithComments (lineInfo , member);
235+ var nodeRange = range.nodeWithComments (_lineInfo , member);
238236 var offset = nodeRange.offset;
239237 var length = nodeRange.length;
240238 var text = code.substring (offset, offset + length);
@@ -244,54 +242,45 @@ class MemberSorter {
244242 _sortAndReorderMembers (members);
245243 }
246244
247- /// Return the EOL to use for [code] .
248- static String getEOL (String code) {
249- if (code.contains ('\r\n ' )) {
250- return '\r\n ' ;
251- } else {
252- return '\n ' ;
253- }
254- }
255-
256245 static List <_PriorityItem > _getPriorityItems (CodeStyleOptions codeStyle) {
257246 return [
258- _PriorityItem (false , _MemberKind .UNIT_FUNCTION_MAIN , false ),
259- _PriorityItem (false , _MemberKind .UNIT_VARIABLE_CONST , false ),
260- _PriorityItem (false , _MemberKind .UNIT_VARIABLE_CONST , true ),
261- _PriorityItem (false , _MemberKind .UNIT_VARIABLE , false ),
262- _PriorityItem (false , _MemberKind .UNIT_VARIABLE , true ),
263- _PriorityItem (false , _MemberKind .UNIT_ACCESSOR , false ),
264- _PriorityItem (false , _MemberKind .UNIT_ACCESSOR , true ),
265- _PriorityItem (false , _MemberKind .UNIT_FUNCTION , false ),
266- _PriorityItem (false , _MemberKind .UNIT_FUNCTION , true ),
267- _PriorityItem (false , _MemberKind .UNIT_GENERIC_TYPE_ALIAS , false ),
268- _PriorityItem (false , _MemberKind .UNIT_GENERIC_TYPE_ALIAS , true ),
269- _PriorityItem (false , _MemberKind .UNIT_FUNCTION_TYPE , false ),
270- _PriorityItem (false , _MemberKind .UNIT_FUNCTION_TYPE , true ),
271- _PriorityItem (false , _MemberKind .UNIT_CLASS , false ),
272- _PriorityItem (false , _MemberKind .UNIT_CLASS , true ),
273- _PriorityItem (false , _MemberKind .UNIT_EXTENSION_TYPE , false ),
274- _PriorityItem (false , _MemberKind .UNIT_EXTENSION_TYPE , true ),
275- _PriorityItem (false , _MemberKind .UNIT_EXTENSION , false ),
276- _PriorityItem (false , _MemberKind .UNIT_EXTENSION , true ),
247+ _PriorityItem (false , _MemberKind .unitFunctionMain , false ),
248+ _PriorityItem (false , _MemberKind .unitVariableConst , false ),
249+ _PriorityItem (false , _MemberKind .unitVariableConst , true ),
250+ _PriorityItem (false , _MemberKind .unitVariable , false ),
251+ _PriorityItem (false , _MemberKind .unitVariable , true ),
252+ _PriorityItem (false , _MemberKind .unitAccessor , false ),
253+ _PriorityItem (false , _MemberKind .unitAccessor , true ),
254+ _PriorityItem (false , _MemberKind .unitFunction , false ),
255+ _PriorityItem (false , _MemberKind .unitFunction , true ),
256+ _PriorityItem (false , _MemberKind .unitGenericTypeAlias , false ),
257+ _PriorityItem (false , _MemberKind .unitGenericTypeAlias , true ),
258+ _PriorityItem (false , _MemberKind .unitFunctionType , false ),
259+ _PriorityItem (false , _MemberKind .unitFunctionType , true ),
260+ _PriorityItem (false , _MemberKind .unitClass , false ),
261+ _PriorityItem (false , _MemberKind .unitClass , true ),
262+ _PriorityItem (false , _MemberKind .unitExtensionType , false ),
263+ _PriorityItem (false , _MemberKind .unitExtensionType , true ),
264+ _PriorityItem (false , _MemberKind .unitExtension , false ),
265+ _PriorityItem (false , _MemberKind .unitExtension , true ),
277266 if (codeStyle.sortConstructorsFirst)
278- _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , false ),
267+ _PriorityItem (false , _MemberKind .classConstructor , false ),
279268 if (codeStyle.sortConstructorsFirst)
280- _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , true ),
281- _PriorityItem (true , _MemberKind .CLASS_FIELD , false ),
282- _PriorityItem (true , _MemberKind .CLASS_ACCESSOR , false ),
283- _PriorityItem (true , _MemberKind .CLASS_ACCESSOR , true ),
284- _PriorityItem (false , _MemberKind .CLASS_FIELD , false ),
269+ _PriorityItem (false , _MemberKind .classConstructor , true ),
270+ _PriorityItem (true , _MemberKind .classField , false ),
271+ _PriorityItem (true , _MemberKind .classAccessor , false ),
272+ _PriorityItem (true , _MemberKind .classAccessor , true ),
273+ _PriorityItem (false , _MemberKind .classField , false ),
285274 if (! codeStyle.sortConstructorsFirst)
286- _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , false ),
275+ _PriorityItem (false , _MemberKind .classConstructor , false ),
287276 if (! codeStyle.sortConstructorsFirst)
288- _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , true ),
289- _PriorityItem (false , _MemberKind .CLASS_ACCESSOR , false ),
290- _PriorityItem (false , _MemberKind .CLASS_ACCESSOR , true ),
291- _PriorityItem (false , _MemberKind .CLASS_METHOD , false ),
292- _PriorityItem (false , _MemberKind .CLASS_METHOD , true ),
293- _PriorityItem (true , _MemberKind .CLASS_METHOD , false ),
294- _PriorityItem (true , _MemberKind .CLASS_METHOD , true ),
277+ _PriorityItem (false , _MemberKind .classConstructor , true ),
278+ _PriorityItem (false , _MemberKind .classAccessor , false ),
279+ _PriorityItem (false , _MemberKind .classAccessor , true ),
280+ _PriorityItem (false , _MemberKind .classMethod , false ),
281+ _PriorityItem (false , _MemberKind .classMethod , true ),
282+ _PriorityItem (true , _MemberKind .classMethod , false ),
283+ _PriorityItem (true , _MemberKind .classMethod , true ),
295284 ];
296285 }
297286}
@@ -314,20 +303,20 @@ class _MemberInfo {
314303}
315304
316305enum _MemberKind {
317- CLASS_ACCESSOR ,
318- CLASS_CONSTRUCTOR ,
319- CLASS_FIELD ,
320- CLASS_METHOD ,
321- UNIT_ACCESSOR ,
322- UNIT_CLASS ,
323- UNIT_EXTENSION ,
324- UNIT_EXTENSION_TYPE ,
325- UNIT_FUNCTION ,
326- UNIT_FUNCTION_MAIN ,
327- UNIT_FUNCTION_TYPE ,
328- UNIT_GENERIC_TYPE_ALIAS ,
329- UNIT_VARIABLE ,
330- UNIT_VARIABLE_CONST ,
306+ classAccessor ,
307+ classConstructor ,
308+ classField ,
309+ classMethod ,
310+ unitAccessor ,
311+ unitClass ,
312+ unitExtension ,
313+ unitExtensionType ,
314+ unitFunction ,
315+ unitFunctionMain ,
316+ unitFunctionType ,
317+ unitGenericTypeAlias ,
318+ unitVariable ,
319+ unitVariableConst ,
331320}
332321
333322class _PriorityItem {
@@ -348,7 +337,7 @@ class _PriorityItem {
348337 @override
349338 bool operator == (Object obj) {
350339 var other = obj as _PriorityItem ;
351- if (kind == _MemberKind .CLASS_FIELD ) {
340+ if (kind == _MemberKind .classField ) {
352341 return other.kind == kind && other.isStatic == isStatic;
353342 }
354343 return other.kind == kind &&
0 commit comments