Skip to content

Commit 5c79627

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
Move CorrectionUtils addLibraryImports to ExtractMethod
`_getInsertionLocationTop` and `addLibraryImports` are each moved to extract_method.dart, as the only location where they were used. We get to delete the `_InsertionLocation` class. The tests are also moved, unchanged, and CorrectionUtilsTest is very much simplified. Bug: #53402 Change-Id: Ia410f04a837d85a0e06ec523d156c6c6c8bf6a3b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363402 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 9a38563 commit 5c79627

File tree

6 files changed

+459
-496
lines changed

6 files changed

+459
-496
lines changed

pkg/analysis_server/lib/src/services/correction/util.dart

Lines changed: 1 addition & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import 'dart:io';
66
import 'dart:math';
77

88
import 'package:_fe_analyzer_shared/src/scanner/token.dart';
9-
import 'package:analysis_server/src/protocol_server.dart'
10-
show doSourceChange_addElementEdit;
119
import 'package:analysis_server/src/utilities/extensions/ast.dart';
1210
import 'package:analysis_server/src/utilities/strings.dart';
1311
import 'package:analyzer/dart/analysis/features.dart';
1412
import 'package:analyzer/dart/analysis/results.dart';
15-
import 'package:analyzer/dart/analysis/session.dart';
1613
import 'package:analyzer/dart/ast/precedence.dart';
1714
import 'package:analyzer/dart/ast/visitor.dart';
1815
import 'package:analyzer/dart/element/element.dart';
@@ -24,125 +21,10 @@ import 'package:analyzer/src/dart/ast/extensions.dart';
2421
import 'package:analyzer/src/dart/ast/utilities.dart';
2522
import 'package:analyzer/src/dart/scanner/reader.dart';
2623
import 'package:analyzer/src/dart/scanner/scanner.dart';
27-
import 'package:analyzer_plugin/protocol/protocol_common.dart'
28-
show SourceChange, SourceEdit;
2924
import 'package:analyzer_plugin/src/utilities/string_utilities.dart';
3025
import 'package:analyzer_plugin/utilities/range_factory.dart';
3126
import 'package:path/path.dart' as path;
3227

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-
14628
/// Climbs up [PrefixedIdentifier] and [PropertyAccess] nodes that include
14729
/// [node].
14830
Expression climbPropertyAccess(Expression node) {
@@ -880,61 +762,7 @@ final class CorrectionUtils {
880762
selection, range.node(node));
881763
}
882764

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.
938766
_InvertedCondition _invertCondition0(Expression expression) {
939767
if (expression is BooleanLiteral) {
940768
if (expression.value) {
@@ -1128,27 +956,6 @@ class _ElementReferenceCollector extends RecursiveAstVisitor<void> {
1128956
}
1129957
}
1130958

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-
1152959
/// A container with a source and its precedence.
1153960
class _InvertedCondition {
1154961
final int _precedence;

0 commit comments

Comments
 (0)