OLD | NEW |
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 services.src.correction.fix; | 5 library services.src.correction.fix; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analysis_server/src/protocol.dart' hide AnalysisError, Element, | 9 import 'package:analysis_server/src/protocol.dart' hide AnalysisError, Element, |
10 ElementKind; | 10 ElementKind; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 _addFix_createConstructorSuperExplicit(); | 123 _addFix_createConstructorSuperExplicit(); |
124 } | 124 } |
125 if (errorCode == | 125 if (errorCode == |
126 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT) { | 126 CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT) { |
127 _addFix_createConstructorSuperImplicit(); | 127 _addFix_createConstructorSuperImplicit(); |
128 } | 128 } |
129 if (errorCode == | 129 if (errorCode == |
130 CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT) { | 130 CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT) { |
131 _addFix_createConstructorSuperExplicit(); | 131 _addFix_createConstructorSuperExplicit(); |
132 } | 132 } |
| 133 if (errorCode == CompileTimeErrorCode.URI_DOES_NOT_EXIST) { |
| 134 _addFix_replaceImportUri(); |
| 135 } |
133 if (errorCode == HintCode.DIVISION_OPTIMIZATION) { | 136 if (errorCode == HintCode.DIVISION_OPTIMIZATION) { |
134 _addFix_useEffectiveIntegerDivision(); | 137 _addFix_useEffectiveIntegerDivision(); |
135 } | 138 } |
136 if (errorCode == HintCode.TYPE_CHECK_IS_NOT_NULL) { | 139 if (errorCode == HintCode.TYPE_CHECK_IS_NOT_NULL) { |
137 _addFix_isNotNull(); | 140 _addFix_isNotNull(); |
138 } | 141 } |
139 if (errorCode == HintCode.TYPE_CHECK_IS_NULL) { | 142 if (errorCode == HintCode.TYPE_CHECK_IS_NULL) { |
140 _addFix_isNull(); | 143 _addFix_isNull(); |
141 } | 144 } |
142 if (errorCode == HintCode.UNNECESSARY_CAST) { | 145 if (errorCode == HintCode.UNNECESSARY_CAST) { |
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
963 node.getAncestor((node) => node is ImportDirective); | 966 node.getAncestor((node) => node is ImportDirective); |
964 if (importDirective == null) { | 967 if (importDirective == null) { |
965 return; | 968 return; |
966 } | 969 } |
967 // remove the whole line with import | 970 // remove the whole line with import |
968 _addRemoveEdit(utils.getLinesRange(rf.rangeNode(importDirective))); | 971 _addRemoveEdit(utils.getLinesRange(rf.rangeNode(importDirective))); |
969 // done | 972 // done |
970 _addFix(FixKind.REMOVE_UNUSED_IMPORT, []); | 973 _addFix(FixKind.REMOVE_UNUSED_IMPORT, []); |
971 } | 974 } |
972 | 975 |
| 976 void _addFix_replaceImportUri() { |
| 977 if (node is SimpleStringLiteral) { |
| 978 SimpleStringLiteral stringLiteral = node; |
| 979 String uri = stringLiteral.value; |
| 980 String uriName = substringAfterLast(uri, '/'); |
| 981 AnalysisContext context = unitLibraryElement.context; |
| 982 for (Source libSource in context.librarySources) { |
| 983 String libFile = libSource.fullName; |
| 984 if (substringAfterLast(libFile, '/') == uriName) { |
| 985 String fixedUri; |
| 986 // may be "package:" URI |
| 987 String libPackageUri = _findPackageUri(context, libFile); |
| 988 if (libPackageUri != null) { |
| 989 fixedUri = libPackageUri; |
| 990 } else { |
| 991 String relativeFile = relative(libFile, from: unitLibraryFolder); |
| 992 fixedUri = split(relativeFile).join('/'); |
| 993 } |
| 994 // add fix |
| 995 SourceRange range = rf.rangeNode(node); |
| 996 _addReplaceEdit(range, "'$fixedUri'"); |
| 997 _addFix(FixKind.REPLACE_IMPORT_URI, [fixedUri]); |
| 998 } |
| 999 } |
| 1000 } |
| 1001 } |
| 1002 |
973 void _addFix_replaceWithConstInstanceCreation() { | 1003 void _addFix_replaceWithConstInstanceCreation() { |
974 if (coveredNode is InstanceCreationExpression) { | 1004 if (coveredNode is InstanceCreationExpression) { |
975 var instanceCreation = coveredNode as InstanceCreationExpression; | 1005 var instanceCreation = coveredNode as InstanceCreationExpression; |
976 _addReplaceEdit(rf.rangeToken(instanceCreation.keyword), "const"); | 1006 _addReplaceEdit(rf.rangeToken(instanceCreation.keyword), "const"); |
977 _addFix(FixKind.USE_CONST, []); | 1007 _addFix(FixKind.USE_CONST, []); |
978 } | 1008 } |
979 } | 1009 } |
980 | 1010 |
981 void _addFix_undefinedClass_useSimilar() { | 1011 void _addFix_undefinedClass_useSimilar() { |
982 if (_mayBeTypeIdentifier(node)) { | 1012 if (_mayBeTypeIdentifier(node)) { |
(...skipping 829 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1812 | 1842 |
1813 /** | 1843 /** |
1814 * Attempts to convert the given absolute path into a "package" URI. | 1844 * Attempts to convert the given absolute path into a "package" URI. |
1815 * | 1845 * |
1816 * [context] - the [AnalysisContext] to work in. | 1846 * [context] - the [AnalysisContext] to work in. |
1817 * [path] - the absolute path, not `null`. | 1847 * [path] - the absolute path, not `null`. |
1818 * | 1848 * |
1819 * Returns the "package" URI, may be `null`. | 1849 * Returns the "package" URI, may be `null`. |
1820 */ | 1850 */ |
1821 static String _findPackageUri(AnalysisContext context, String path) { | 1851 static String _findPackageUri(AnalysisContext context, String path) { |
1822 // Source fileSource = new FileBasedSource.con1(path); | |
1823 Source fileSource = new NonExistingSource(path, UriKind.FILE_URI); | 1852 Source fileSource = new NonExistingSource(path, UriKind.FILE_URI); |
1824 Uri uri = context.sourceFactory.restoreUri(fileSource); | 1853 Uri uri = context.sourceFactory.restoreUri(fileSource); |
1825 if (uri == null) { | 1854 if (uri == null) { |
1826 return null; | 1855 return null; |
1827 } | 1856 } |
1828 return uri.toString(); | 1857 return uri.toString(); |
1829 } | 1858 } |
1830 | 1859 |
1831 /** | 1860 /** |
1832 * @return the suggestions for given [Type] and [DartExpression], not empty. | 1861 * @return the suggestions for given [Type] and [DartExpression], not empty. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1896 /** | 1925 /** |
1897 * Describes the location for a newly created [ConstructorDeclaration]. | 1926 * Describes the location for a newly created [ConstructorDeclaration]. |
1898 */ | 1927 */ |
1899 class _ConstructorLocation { | 1928 class _ConstructorLocation { |
1900 final String _prefix; | 1929 final String _prefix; |
1901 final int _offset; | 1930 final int _offset; |
1902 final String _suffix; | 1931 final String _suffix; |
1903 | 1932 |
1904 _ConstructorLocation(this._prefix, this._offset, this._suffix); | 1933 _ConstructorLocation(this._prefix, this._offset, this._suffix); |
1905 } | 1934 } |
OLD | NEW |