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

Side by Side Diff: pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Issue 2781963002: Fix for Quick Fix to import SDK classes. (Closed)
Patch Set: Created 3 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analysis_server.src.services.correction.fix_internal; 5 library analysis_server.src.services.correction.fix_internal;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 import 'dart:core'; 9 import 'dart:core';
10 10
(...skipping 28 matching lines...) Expand all
39 import 'package:analyzer/src/dart/element/ast_provider.dart'; 39 import 'package:analyzer/src/dart/element/ast_provider.dart';
40 import 'package:analyzer/src/dart/element/element.dart'; 40 import 'package:analyzer/src/dart/element/element.dart';
41 import 'package:analyzer/src/dart/element/member.dart'; 41 import 'package:analyzer/src/dart/element/member.dart';
42 import 'package:analyzer/src/dart/element/type.dart'; 42 import 'package:analyzer/src/dart/element/type.dart';
43 import 'package:analyzer/src/dart/resolver/inheritance_manager.dart'; 43 import 'package:analyzer/src/dart/resolver/inheritance_manager.dart';
44 import 'package:analyzer/src/error/codes.dart'; 44 import 'package:analyzer/src/error/codes.dart';
45 import 'package:analyzer/src/generated/engine.dart'; 45 import 'package:analyzer/src/generated/engine.dart';
46 import 'package:analyzer/src/generated/error_verifier.dart'; 46 import 'package:analyzer/src/generated/error_verifier.dart';
47 import 'package:analyzer/src/generated/java_core.dart'; 47 import 'package:analyzer/src/generated/java_core.dart';
48 import 'package:analyzer/src/generated/parser.dart'; 48 import 'package:analyzer/src/generated/parser.dart';
49 import 'package:analyzer/src/generated/sdk.dart';
50 import 'package:analyzer/src/generated/source.dart'; 49 import 'package:analyzer/src/generated/source.dart';
51 import 'package:analyzer/src/generated/utilities_dart.dart'; 50 import 'package:analyzer/src/generated/utilities_dart.dart';
52 import 'package:analyzer/src/task/dart.dart';
53 import 'package:path/path.dart'; 51 import 'package:path/path.dart';
54 52
55 /** 53 /**
56 * A predicate is a one-argument function that returns a boolean value. 54 * A predicate is a one-argument function that returns a boolean value.
57 */ 55 */
58 typedef bool ElementPredicate(Element argument); 56 typedef bool ElementPredicate(Element argument);
59 57
60 /** 58 /**
61 * The implementation of [DartFixContext]. 59 * The implementation of [DartFixContext].
62 * 60 *
(...skipping 1547 matching lines...) Expand 10 before | Expand all | Expand 10 after
1610 } 1608 }
1611 // don't add this library again 1609 // don't add this library again
1612 alreadyImportedWithPrefix.add(libraryElement.source); 1610 alreadyImportedWithPrefix.add(libraryElement.source);
1613 // update library 1611 // update library
1614 String newShowCode = 'show ${showNames.join(', ')}'; 1612 String newShowCode = 'show ${showNames.join(', ')}';
1615 _addReplaceEdit( 1613 _addReplaceEdit(
1616 rf.rangeOffsetEnd(showCombinator), newShowCode, unitLibraryElement); 1614 rf.rangeOffsetEnd(showCombinator), newShowCode, unitLibraryElement);
1617 _addFix(DartFixKind.IMPORT_LIBRARY_SHOW, [libraryName]); 1615 _addFix(DartFixKind.IMPORT_LIBRARY_SHOW, [libraryName]);
1618 } 1616 }
1619 } 1617 }
1620 // check SDK libraries 1618 // Find new top-level declarations.
1621 {
1622 DartSdk sdk = context.sourceFactory.dartSdk;
1623 List<SdkLibrary> sdkLibraries = sdk.sdkLibraries;
1624 for (SdkLibrary sdkLibrary in sdkLibraries) {
1625 SourceFactory sdkSourceFactory = context.sourceFactory;
1626 String libraryUri = sdkLibrary.shortName;
1627 Source librarySource =
1628 sdkSourceFactory.resolveUri(unitSource, libraryUri);
1629 // maybe already imported
1630 if (alreadyImportedWithPrefix.contains(librarySource)) {
1631 continue;
1632 }
1633 // prepare LibraryElement
1634 LibraryElement libraryElement =
1635 context.getResult(librarySource, LIBRARY_ELEMENT1);
1636 if (libraryElement == null) {
1637 continue;
1638 }
1639 // prepare exported Element
1640 Element element = getExportedElement(libraryElement, name);
1641 if (element == null) {
1642 continue;
1643 }
1644 if (element is PropertyAccessorElement) {
1645 element = (element as PropertyAccessorElement).variable;
1646 }
1647 if (!elementKinds.contains(element.kind)) {
1648 continue;
1649 }
1650 // add import
1651 _addFix_importLibrary(
1652 DartFixKind.IMPORT_LIBRARY_SDK, libraryElement.source);
1653 }
1654 }
1655 // check project libraries
1656 { 1619 {
1657 List<TopLevelDeclarationInSource> declarations = 1620 List<TopLevelDeclarationInSource> declarations =
1658 await getTopLevelDeclarations(name); 1621 await getTopLevelDeclarations(name);
1659 for (TopLevelDeclarationInSource declaration in declarations) { 1622 for (TopLevelDeclarationInSource declaration in declarations) {
1660 // Check the kind. 1623 // Check the kind.
1661 if (declaration.declaration.kind != kind2) { 1624 if (declaration.declaration.kind != kind2) {
1662 continue; 1625 continue;
1663 } 1626 }
1664 // Check the source. 1627 // Check the source.
1665 Source librarySource = declaration.source; 1628 Source librarySource = declaration.source;
1666 if (librarySource.isInSystemLibrary) {
1667 continue;
1668 }
1669 if (alreadyImportedWithPrefix.contains(librarySource)) { 1629 if (alreadyImportedWithPrefix.contains(librarySource)) {
1670 continue; 1630 continue;
1671 } 1631 }
1672 // Compute the fix kind. 1632 // Compute the fix kind.
1673 FixKind fixKind; 1633 FixKind fixKind;
1674 if (_isLibSrcPath(librarySource.fullName)) { 1634 if (librarySource.isInSystemLibrary) {
1635 fixKind = DartFixKind.IMPORT_LIBRARY_SDK;
1636 } else if (_isLibSrcPath(librarySource.fullName)) {
1675 // Bad: non-API. 1637 // Bad: non-API.
1676 fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT3; 1638 fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT3;
1677 } else if (declaration.isExported) { 1639 } else if (declaration.isExported) {
1678 // Ugly: exports. 1640 // Ugly: exports.
1679 fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT2; 1641 fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT2;
1680 } else { 1642 } else {
1681 // Good: direct declaration. 1643 // Good: direct declaration.
1682 fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT1; 1644 fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT1;
1683 } 1645 }
1684 // Add the fix. 1646 // Add the fix.
(...skipping 1374 matching lines...) Expand 10 before | Expand all | Expand 10 after
3059 } 3021 }
3060 } 3022 }
3061 } 3023 }
3062 3024
3063 void _updateList(Iterable<Element> elements) { 3025 void _updateList(Iterable<Element> elements) {
3064 for (Element element in elements) { 3026 for (Element element in elements) {
3065 _update(element); 3027 _update(element);
3066 } 3028 }
3067 } 3029 }
3068 } 3030 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/correction/fix_test.dart » ('j') | pkg/front_end/front_end.iml » ('J')

Powered by Google App Engine
This is Rietveld 408576698