@@ -6,13 +6,10 @@ import 'dart:io';
6
6
import 'dart:math' ;
7
7
8
8
import 'package:_fe_analyzer_shared/src/scanner/token.dart' ;
9
- import 'package:analysis_server/src/protocol_server.dart'
10
- show doSourceChange_addElementEdit;
11
9
import 'package:analysis_server/src/utilities/extensions/ast.dart' ;
12
10
import 'package:analysis_server/src/utilities/strings.dart' ;
13
11
import 'package:analyzer/dart/analysis/features.dart' ;
14
12
import 'package:analyzer/dart/analysis/results.dart' ;
15
- import 'package:analyzer/dart/analysis/session.dart' ;
16
13
import 'package:analyzer/dart/ast/precedence.dart' ;
17
14
import 'package:analyzer/dart/ast/visitor.dart' ;
18
15
import 'package:analyzer/dart/element/element.dart' ;
@@ -24,125 +21,10 @@ import 'package:analyzer/src/dart/ast/extensions.dart';
24
21
import 'package:analyzer/src/dart/ast/utilities.dart' ;
25
22
import 'package:analyzer/src/dart/scanner/reader.dart' ;
26
23
import 'package:analyzer/src/dart/scanner/scanner.dart' ;
27
- import 'package:analyzer_plugin/protocol/protocol_common.dart'
28
- show SourceChange, SourceEdit;
29
24
import 'package:analyzer_plugin/src/utilities/string_utilities.dart' ;
30
25
import 'package:analyzer_plugin/utilities/range_factory.dart' ;
31
26
import 'package:path/path.dart' as path;
32
27
33
- /// Adds edits to the given [change] that ensure that all the [libraries] are
34
- /// imported into the given [targetLibrary] .
35
- Future <void > addLibraryImports (AnalysisSession session, SourceChange change,
36
- LibraryElement targetLibrary, Set <Source > libraries) async {
37
- var libraryPath = targetLibrary.source.fullName;
38
-
39
- var resolveResult = await session.getResolvedUnit (libraryPath);
40
- if (resolveResult is ! ResolvedUnitResult ) {
41
- return ;
42
- }
43
-
44
- var libUtils = CorrectionUtils (resolveResult);
45
- var eol = libUtils.endOfLine;
46
- // Prepare information about existing imports.
47
- LibraryDirective ? libraryDirective;
48
- var importDirectives = < _ImportDirectiveInfo > [];
49
- var directives = < NamespaceDirective > [];
50
- for (var directive in libUtils.unit.directives) {
51
- if (directive is LibraryDirective ) {
52
- libraryDirective = directive;
53
- } else if (directive is NamespaceDirective ) {
54
- directives.add (directive);
55
- if (directive is ImportDirective ) {
56
- var uriStr = directive.uri.stringValue;
57
- if (uriStr != null ) {
58
- importDirectives.add (
59
- _ImportDirectiveInfo (uriStr, directive.offset, directive.end),
60
- );
61
- }
62
- }
63
- }
64
- }
65
-
66
- // Prepare all URIs to import.
67
- var uriList = libraries
68
- .map ((library) => getLibrarySourceUri (
69
- session.resourceProvider.pathContext, targetLibrary, library.uri))
70
- .toList ();
71
- uriList.sort ((a, b) => a.compareTo (b));
72
-
73
- var analysisOptions =
74
- session.analysisContext.getAnalysisOptionsForFile (resolveResult.file);
75
- var quote =
76
- analysisOptions.codeStyleOptions.preferredQuoteForUris (directives);
77
-
78
- // Insert imports: between existing imports.
79
- if (importDirectives.isNotEmpty) {
80
- var isFirstPackage = true ;
81
- for (var importUri in uriList) {
82
- var inserted = false ;
83
- var isPackage = importUri.startsWith ('package:' );
84
- var isAfterDart = false ;
85
- for (var existingImport in importDirectives) {
86
- if (existingImport.uri.startsWith ('dart:' )) {
87
- isAfterDart = true ;
88
- }
89
- if (existingImport.uri.startsWith ('package:' )) {
90
- isFirstPackage = false ;
91
- }
92
- if (importUri.compareTo (existingImport.uri) < 0 ) {
93
- var importCode = 'import $quote $importUri $quote ;$eol ' ;
94
- doSourceChange_addElementEdit (change, targetLibrary,
95
- SourceEdit (existingImport.offset, 0 , importCode));
96
- inserted = true ;
97
- break ;
98
- }
99
- }
100
- if (! inserted) {
101
- var importCode = '${eol }import $quote $importUri $quote ;' ;
102
- if (isPackage && isFirstPackage && isAfterDart) {
103
- importCode = eol + importCode;
104
- }
105
- doSourceChange_addElementEdit (change, targetLibrary,
106
- SourceEdit (importDirectives.last.end, 0 , importCode));
107
- }
108
- if (isPackage) {
109
- isFirstPackage = false ;
110
- }
111
- }
112
- return ;
113
- }
114
-
115
- // Insert imports: after the library directive.
116
- if (libraryDirective != null ) {
117
- var prefix = eol + eol;
118
- for (var importUri in uriList) {
119
- var importCode = '${prefix }import $quote $importUri $quote ;' ;
120
- prefix = eol;
121
- doSourceChange_addElementEdit (change, targetLibrary,
122
- SourceEdit (libraryDirective.end, 0 , importCode));
123
- }
124
- return ;
125
- }
126
-
127
- // If still at the beginning of the file, skip shebang and line comments.
128
- {
129
- var desc = libUtils._getInsertionLocationTop ();
130
- var offset = desc.offset;
131
- for (var i = 0 ; i < uriList.length; i++ ) {
132
- var importUri = uriList[i];
133
- var importCode = 'import $quote $importUri $quote ;$eol ' ;
134
- if (i == 0 ) {
135
- importCode = desc.prefix + importCode;
136
- }
137
- if (i == uriList.length - 1 ) {
138
- importCode = importCode + desc.suffix;
139
- }
140
- doSourceChange_addElementEdit (
141
- change, targetLibrary, SourceEdit (offset, 0 , importCode));
142
- }
143
- }
144
- }
145
-
146
28
/// Climbs up [PrefixedIdentifier] and [PropertyAccess] nodes that include
147
29
/// [node] .
148
30
Expression climbPropertyAccess (Expression node) {
@@ -880,61 +762,7 @@ final class CorrectionUtils {
880
762
selection, range.node (node));
881
763
}
882
764
883
- /// Returns a description of the place in which to insert a new directive or a
884
- /// top-level declaration at the top of the file.
885
- _InsertionLocation _getInsertionLocationTop () {
886
- // skip leading line comments
887
- var offset = 0 ;
888
- var insertEmptyLineBefore = false ;
889
- var insertEmptyLineAfter = false ;
890
- var source = _buffer;
891
- // skip hash-bang
892
- if (offset < source.length - 2 ) {
893
- var linePrefix = getText (offset, 2 );
894
- if (linePrefix == '#!' ) {
895
- insertEmptyLineBefore = true ;
896
- offset = getLineNext (offset);
897
- // skip empty lines to first line comment
898
- var emptyOffset = offset;
899
- while (emptyOffset < source.length - 2 ) {
900
- var nextLineOffset = getLineNext (emptyOffset);
901
- var line = source.substring (emptyOffset, nextLineOffset);
902
- if (line.trim ().isEmpty) {
903
- emptyOffset = nextLineOffset;
904
- continue ;
905
- } else if (line.startsWith ('//' )) {
906
- offset = emptyOffset;
907
- break ;
908
- } else {
909
- break ;
910
- }
911
- }
912
- }
913
- }
914
- // skip line comments
915
- while (offset < source.length - 2 ) {
916
- var linePrefix = getText (offset, 2 );
917
- if (linePrefix == '//' ) {
918
- insertEmptyLineBefore = true ;
919
- offset = getLineNext (offset);
920
- } else {
921
- break ;
922
- }
923
- }
924
- // determine if empty line is required after
925
- var nextLineOffset = getLineNext (offset);
926
- var insertLine = source.substring (offset, nextLineOffset);
927
- if (insertLine.trim ().isNotEmpty) {
928
- insertEmptyLineAfter = true ;
929
- }
930
- return _InsertionLocation (
931
- prefix: insertEmptyLineBefore ? endOfLine : '' ,
932
- offset: offset,
933
- suffix: insertEmptyLineAfter ? endOfLine : '' ,
934
- );
935
- }
936
-
937
- /// @return the [InvertedCondition] for the given logical expression.
765
+ /// Returns the [_InvertedCondition] for the given logical expression.
938
766
_InvertedCondition _invertCondition0 (Expression expression) {
939
767
if (expression is BooleanLiteral ) {
940
768
if (expression.value) {
@@ -1128,27 +956,6 @@ class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
1128
956
}
1129
957
}
1130
958
1131
- class _ImportDirectiveInfo {
1132
- final String uri;
1133
- final int offset;
1134
- final int end;
1135
-
1136
- _ImportDirectiveInfo (this .uri, this .offset, this .end);
1137
- }
1138
-
1139
- /// Describes where to insert new text.
1140
- class _InsertionLocation {
1141
- final String prefix;
1142
- final int offset;
1143
- final String suffix;
1144
-
1145
- _InsertionLocation ({
1146
- required this .prefix,
1147
- required this .offset,
1148
- required this .suffix,
1149
- });
1150
- }
1151
-
1152
959
/// A container with a source and its precedence.
1153
960
class _InvertedCondition {
1154
961
final int _precedence;
0 commit comments