Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Unified Diff: pkg/analysis_server/lib/src/services/correction/util.dart

Issue 961583002: Issue 20827. Extract library importing helper and use it fox Quick Fixes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/correction/util.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/util.dart b/pkg/analysis_server/lib/src/services/correction/util.dart
index c89a42dc0d4bdd1045210c5bc454416e5371daf4..95fb59097c69bd8863a6f806b70d80ca10ddc764 100644
--- a/pkg/analysis_server/lib/src/services/correction/util.dart
+++ b/pkg/analysis_server/lib/src/services/correction/util.dart
@@ -8,6 +8,8 @@ import 'dart:math';
import 'package:analysis_server/src/protocol.dart' show SourceChange,
SourceEdit;
+import 'package:analysis_server/src/protocol_server.dart' show
+ doSourceChange_addElementEdit;
import 'package:analysis_server/src/services/correction/source_range.dart';
import 'package:analysis_server/src/services/correction/strings.dart';
import 'package:analyzer/src/generated/ast.dart';
@@ -20,6 +22,51 @@ import 'package:path/path.dart';
/**
+ * Adds edits to the given [change] that ensure that all the [libraries] are
+ * imported into the given [targetLibrary].
+ */
+void addLibraryImports(SourceChange change, LibraryElement targetLibrary,
+ Set<LibraryElement> libraries) {
+ CompilationUnit libUnit = targetLibrary.definingCompilationUnit.node;
+ // prepare new import location
+ int offset = 0;
+ String prefix;
+ String suffix;
+ {
+ // if no directives
+ prefix = '';
+ CorrectionUtils libraryUtils = new CorrectionUtils(libUnit);
+ String eol = libraryUtils.endOfLine;
+ suffix = eol;
+ // after last directive in library
+ for (Directive directive in libUnit.directives) {
+ if (directive is LibraryDirective || directive is ImportDirective) {
+ offset = directive.end;
+ prefix = eol;
+ suffix = '';
+ }
+ }
+ // if still at the beginning of the file, skip shebang and line comments
+ if (offset == 0) {
+ CorrectionUtils_InsertDesc desc = libraryUtils.getInsertDescTop();
+ offset = desc.offset;
+ prefix = desc.prefix;
+ suffix = desc.suffix + eol;
+ }
+ }
+ // insert imports
+ for (LibraryElement library in libraries) {
+ String importPath = getLibrarySourceUri(targetLibrary, library.source);
+ String importCode = "${prefix}import '$importPath';$suffix";
+ doSourceChange_addElementEdit(
+ change,
+ targetLibrary,
+ new SourceEdit(offset, 0, importCode));
+ }
+}
+
+
+/**
* @return <code>true</code> if given [List]s are identical at given position.
*/
bool allListsIdentical(List<List> lists, int position) {
@@ -91,6 +138,7 @@ List<SourceRange> getCommentRanges(CompilationUnit unit) {
}
+
String getDefaultValueCode(DartType type) {
if (type != null) {
String typeName = type.displayName;
@@ -111,8 +159,6 @@ String getDefaultValueCode(DartType type) {
return "null";
}
-
-
/**
* Return the name of the [Element] kind.
*/
@@ -120,6 +166,7 @@ String getElementKindName(Element element) {
return element.kind.displayName;
}
+
/**
* Returns the name to display in the UI for the given [Element].
*/
@@ -186,6 +233,7 @@ ExecutableElement getEnclosingExecutableElement(AstNode node) {
}
+
/**
* @return the enclosing executable [AstNode].
*/
@@ -957,7 +1005,8 @@ class CorrectionUtils {
return source;
}
// check if imported
- if (element.library != _library) {
+ LibraryElement library = element.library;
+ if (library != null && library != _library) {
ImportElement importElement = _getImportElement(element);
if (importElement != null) {
if (importElement.prefix != null) {
@@ -965,7 +1014,7 @@ class CorrectionUtils {
sb.write(".");
}
} else {
- librariesToImport.add(element.library);
+ librariesToImport.add(library);
}
}
// append simple name
@@ -1000,18 +1049,6 @@ class CorrectionUtils {
}
/**
- * Checks if [type] is visible at [targetOffset].
- */
- bool _isTypeVisible(DartType type) {
- if (type is TypeParameterType) {
- TypeParameterElement parameterElement = type.element;
- Element parameterClassElement = parameterElement.enclosingElement;
- return identical(parameterClassElement, targetClassElement);
- }
- return true;
- }
-
- /**
* Indents given source left or right.
*/
String indentSourceLeftRight(String source, bool right) {
@@ -1258,6 +1295,18 @@ class CorrectionUtils {
return _InvertedCondition._simple(getNodeText(expression));
}
+ /**
+ * Checks if [type] is visible at [targetOffset].
+ */
+ bool _isTypeVisible(DartType type) {
+ if (type is TypeParameterType) {
+ TypeParameterElement parameterElement = type.element;
+ Element parameterClassElement = parameterElement.enclosingElement;
+ return identical(parameterClassElement, targetClassElement);
+ }
+ return true;
+ }
+
bool _selectionIncludesNonWhitespaceOutsideOperands(SourceRange selection,
List<Expression> operands) {
return _selectionIncludesNonWhitespaceOutsideRange(

Powered by Google App Engine
This is Rietveld 408576698