55import 'package:analysis_server/src/services/correction/organize_imports.dart' ;
66import 'package:analysis_server/src/utilities/extensions/range_factory.dart' ;
77import 'package:analysis_server/src/utilities/strings.dart' ;
8+ import 'package:analyzer/dart/analysis/code_style_options.dart' ;
89import 'package:analyzer/dart/ast/ast.dart' ;
910import 'package:analyzer/source/line_info.dart' ;
1011import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
1112import 'package:analyzer_plugin/utilities/range_factory.dart' ;
1213
1314/// Sorter for unit/class members.
1415class MemberSorter {
15- static final List <_PriorityItem > _PRIORITY_ITEMS = [
16- _PriorityItem (false , _MemberKind .UNIT_FUNCTION_MAIN , false ),
17- _PriorityItem (false , _MemberKind .UNIT_VARIABLE_CONST , false ),
18- _PriorityItem (false , _MemberKind .UNIT_VARIABLE_CONST , true ),
19- _PriorityItem (false , _MemberKind .UNIT_VARIABLE , false ),
20- _PriorityItem (false , _MemberKind .UNIT_VARIABLE , true ),
21- _PriorityItem (false , _MemberKind .UNIT_ACCESSOR , false ),
22- _PriorityItem (false , _MemberKind .UNIT_ACCESSOR , true ),
23- _PriorityItem (false , _MemberKind .UNIT_FUNCTION , false ),
24- _PriorityItem (false , _MemberKind .UNIT_FUNCTION , true ),
25- _PriorityItem (false , _MemberKind .UNIT_GENERIC_TYPE_ALIAS , false ),
26- _PriorityItem (false , _MemberKind .UNIT_GENERIC_TYPE_ALIAS , true ),
27- _PriorityItem (false , _MemberKind .UNIT_FUNCTION_TYPE , false ),
28- _PriorityItem (false , _MemberKind .UNIT_FUNCTION_TYPE , true ),
29- _PriorityItem (false , _MemberKind .UNIT_CLASS , false ),
30- _PriorityItem (false , _MemberKind .UNIT_CLASS , true ),
31- _PriorityItem (false , _MemberKind .UNIT_EXTENSION_TYPE , false ),
32- _PriorityItem (false , _MemberKind .UNIT_EXTENSION_TYPE , true ),
33- _PriorityItem (false , _MemberKind .UNIT_EXTENSION , false ),
34- _PriorityItem (false , _MemberKind .UNIT_EXTENSION , true ),
35- _PriorityItem (true , _MemberKind .CLASS_FIELD , false ),
36- _PriorityItem (true , _MemberKind .CLASS_ACCESSOR , false ),
37- _PriorityItem (true , _MemberKind .CLASS_ACCESSOR , true ),
38- _PriorityItem (false , _MemberKind .CLASS_FIELD , false ),
39- _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , false ),
40- _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , true ),
41- _PriorityItem (false , _MemberKind .CLASS_ACCESSOR , false ),
42- _PriorityItem (false , _MemberKind .CLASS_ACCESSOR , true ),
43- _PriorityItem (false , _MemberKind .CLASS_METHOD , false ),
44- _PriorityItem (false , _MemberKind .CLASS_METHOD , true ),
45- _PriorityItem (true , _MemberKind .CLASS_METHOD , false ),
46- _PriorityItem (true , _MemberKind .CLASS_METHOD , true )
47- ];
48-
4916 final String initialCode;
5017
5118 final CompilationUnit unit;
5219
20+ final CodeStyleOptions codeStyle;
21+
5322 final LineInfo lineInfo;
5423
5524 String code;
5625
5726 String endOfLine = '\n ' ;
5827
59- MemberSorter (this .initialCode, this .unit, this .lineInfo)
28+ MemberSorter (this .initialCode, this .unit, this .codeStyle, this . lineInfo)
6029 : code = initialCode {
6130 endOfLine = getEOL (code);
6231 }
@@ -78,6 +47,32 @@ class MemberSorter {
7847 return edits;
7948 }
8049
50+ int _getPriority (_PriorityItem item) {
51+ var priorityItems = _getPriorityItems (codeStyle);
52+ var priority = priorityItems.indexOf (item);
53+ return priority != - 1 ? priority : 0 ;
54+ }
55+
56+ List <_MemberInfo > _getSortedMembers (List <_MemberInfo > members) {
57+ var membersSorted = List <_MemberInfo >.from (members);
58+ membersSorted.sort ((_MemberInfo o1, _MemberInfo o2) {
59+ var priority1 = _getPriority (o1.item);
60+ var priority2 = _getPriority (o2.item);
61+ if (priority1 == priority2) {
62+ // don't reorder class fields
63+ if (o1.item.kind == _MemberKind .CLASS_FIELD ) {
64+ return o1.offset - o2.offset;
65+ }
66+ // sort all other members by name
67+ var name1 = o1.name.toLowerCase ();
68+ var name = o2.name.toLowerCase ();
69+ return name1.compareTo (name);
70+ }
71+ return priority1 - priority2;
72+ });
73+ return membersSorted;
74+ }
75+
8176 void _sortAndReorderMembers (List <_MemberInfo > members) {
8277 var membersSorted = _getSortedMembers (members);
8378 var size = membersSorted.length;
@@ -247,33 +242,46 @@ class MemberSorter {
247242 }
248243 }
249244
250- static int _getPriority (_PriorityItem item) {
251- for (var i = 0 ; i < _PRIORITY_ITEMS .length; i++ ) {
252- if (_PRIORITY_ITEMS [i] == item) {
253- return i;
254- }
255- }
256- return 0 ;
257- }
258-
259- static List <_MemberInfo > _getSortedMembers (List <_MemberInfo > members) {
260- var membersSorted = List <_MemberInfo >.from (members);
261- membersSorted.sort ((_MemberInfo o1, _MemberInfo o2) {
262- var priority1 = _getPriority (o1.item);
263- var priority2 = _getPriority (o2.item);
264- if (priority1 == priority2) {
265- // don't reorder class fields
266- if (o1.item.kind == _MemberKind .CLASS_FIELD ) {
267- return o1.offset - o2.offset;
268- }
269- // sort all other members by name
270- var name1 = o1.name.toLowerCase ();
271- var name = o2.name.toLowerCase ();
272- return name1.compareTo (name);
273- }
274- return priority1 - priority2;
275- });
276- return membersSorted;
245+ static List <_PriorityItem > _getPriorityItems (CodeStyleOptions codeStyle) {
246+ return [
247+ _PriorityItem (false , _MemberKind .UNIT_FUNCTION_MAIN , false ),
248+ _PriorityItem (false , _MemberKind .UNIT_VARIABLE_CONST , false ),
249+ _PriorityItem (false , _MemberKind .UNIT_VARIABLE_CONST , true ),
250+ _PriorityItem (false , _MemberKind .UNIT_VARIABLE , false ),
251+ _PriorityItem (false , _MemberKind .UNIT_VARIABLE , true ),
252+ _PriorityItem (false , _MemberKind .UNIT_ACCESSOR , false ),
253+ _PriorityItem (false , _MemberKind .UNIT_ACCESSOR , true ),
254+ _PriorityItem (false , _MemberKind .UNIT_FUNCTION , false ),
255+ _PriorityItem (false , _MemberKind .UNIT_FUNCTION , true ),
256+ _PriorityItem (false , _MemberKind .UNIT_GENERIC_TYPE_ALIAS , false ),
257+ _PriorityItem (false , _MemberKind .UNIT_GENERIC_TYPE_ALIAS , true ),
258+ _PriorityItem (false , _MemberKind .UNIT_FUNCTION_TYPE , false ),
259+ _PriorityItem (false , _MemberKind .UNIT_FUNCTION_TYPE , true ),
260+ _PriorityItem (false , _MemberKind .UNIT_CLASS , false ),
261+ _PriorityItem (false , _MemberKind .UNIT_CLASS , true ),
262+ _PriorityItem (false , _MemberKind .UNIT_EXTENSION_TYPE , false ),
263+ _PriorityItem (false , _MemberKind .UNIT_EXTENSION_TYPE , true ),
264+ _PriorityItem (false , _MemberKind .UNIT_EXTENSION , false ),
265+ _PriorityItem (false , _MemberKind .UNIT_EXTENSION , true ),
266+ if (codeStyle.sortConstructorsFirst)
267+ _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , false ),
268+ if (codeStyle.sortConstructorsFirst)
269+ _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , true ),
270+ _PriorityItem (true , _MemberKind .CLASS_FIELD , false ),
271+ _PriorityItem (true , _MemberKind .CLASS_ACCESSOR , false ),
272+ _PriorityItem (true , _MemberKind .CLASS_ACCESSOR , true ),
273+ _PriorityItem (false , _MemberKind .CLASS_FIELD , false ),
274+ if (! codeStyle.sortConstructorsFirst)
275+ _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , false ),
276+ if (! codeStyle.sortConstructorsFirst)
277+ _PriorityItem (false , _MemberKind .CLASS_CONSTRUCTOR , true ),
278+ _PriorityItem (false , _MemberKind .CLASS_ACCESSOR , false ),
279+ _PriorityItem (false , _MemberKind .CLASS_ACCESSOR , true ),
280+ _PriorityItem (false , _MemberKind .CLASS_METHOD , false ),
281+ _PriorityItem (false , _MemberKind .CLASS_METHOD , true ),
282+ _PriorityItem (true , _MemberKind .CLASS_METHOD , false ),
283+ _PriorityItem (true , _MemberKind .CLASS_METHOD , true )
284+ ];
277285 }
278286}
279287
0 commit comments