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

Side by Side Diff: pkg/analysis_services/lib/src/correction/fix.dart

Issue 417263003: Use a Levenshtein calculating algorithm with a threshold. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library services.src.correction.fix; 8 library services.src.correction.fix;
9 9
10 import 'package:analysis_services/correction/change.dart'; 10 import 'package:analysis_services/correction/change.dart';
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after
1022 var instanceCreation = coveredNode as InstanceCreationExpression; 1022 var instanceCreation = coveredNode as InstanceCreationExpression;
1023 _addReplaceEdit(rf.rangeToken(instanceCreation.keyword), "const"); 1023 _addReplaceEdit(rf.rangeToken(instanceCreation.keyword), "const");
1024 _addFix(FixKind.USE_CONST, []); 1024 _addFix(FixKind.USE_CONST, []);
1025 } 1025 }
1026 } 1026 }
1027 1027
1028 void _addFix_undefinedClass_useSimilar() { 1028 void _addFix_undefinedClass_useSimilar() {
1029 if (_mayBeTypeIdentifier(node)) { 1029 if (_mayBeTypeIdentifier(node)) {
1030 String name = (node as SimpleIdentifier).name; 1030 String name = (node as SimpleIdentifier).name;
1031 _ClosestElementFinder finder = 1031 _ClosestElementFinder finder =
1032 new _ClosestElementFinder(name, (Element element) => element is ClassE lement); 1032 new _ClosestElementFinder(
1033 name,
1034 (Element element) => element is ClassElement,
1035 5);
1033 // find closest element 1036 // find closest element
1034 { 1037 {
1035 // elements of this library 1038 // elements of this library
1036 for (CompilationUnitElement unit in unitLibraryElement.units) { 1039 for (CompilationUnitElement unit in unitLibraryElement.units) {
1037 finder._updateList(unit.types); 1040 finder._updateList(unit.types);
1038 } 1041 }
1039 // elements from imports 1042 // elements from imports
1040 for (ImportElement importElement in unitLibraryElement.imports) { 1043 for (ImportElement importElement in unitLibraryElement.imports) {
1041 if (importElement.prefix == null) { 1044 if (importElement.prefix == null) {
1042 Map<String, Element> namespace = getImportNamespace(importElement); 1045 Map<String, Element> namespace = getImportNamespace(importElement);
1043 finder._updateList(namespace.values); 1046 finder._updateList(namespace.values);
1044 } 1047 }
1045 } 1048 }
1046 } 1049 }
1047 // if we have close enough element, suggest to use it 1050 // if we have close enough element, suggest to use it
1048 if (finder != null && finder._distance < 5) { 1051 if (finder._element != null) {
Paul Berry 2014/07/25 19:53:26 I think this introduces an unintentional off-by-on
scheglov 2014/07/25 21:18:24 Done.
1049 String closestName = finder._element.name; 1052 String closestName = finder._element.name;
1050 _addReplaceEdit(rf.rangeNode(node), closestName); 1053 _addReplaceEdit(rf.rangeNode(node), closestName);
1051 // add proposal 1054 // add proposal
1052 if (closestName != null) { 1055 if (closestName != null) {
1053 _addFix(FixKind.CHANGE_TO, [closestName]); 1056 _addFix(FixKind.CHANGE_TO, [closestName]);
1054 } 1057 }
1055 } 1058 }
1056 } 1059 }
1057 } 1060 }
1058 1061
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1100 // add proposal 1103 // add proposal
1101 _addFix(FixKind.CREATE_FUNCTION, [name]); 1104 _addFix(FixKind.CREATE_FUNCTION, [name]);
1102 } 1105 }
1103 1106
1104 void _addFix_undefinedFunction_useSimilar() { 1107 void _addFix_undefinedFunction_useSimilar() {
1105 if (node is SimpleIdentifier) { 1108 if (node is SimpleIdentifier) {
1106 String name = (node as SimpleIdentifier).name; 1109 String name = (node as SimpleIdentifier).name;
1107 _ClosestElementFinder finder = 1110 _ClosestElementFinder finder =
1108 new _ClosestElementFinder( 1111 new _ClosestElementFinder(
1109 name, 1112 name,
1110 (Element element) => element is FunctionElement); 1113 (Element element) => element is FunctionElement,
1114 5);
1111 // this library 1115 // this library
1112 for (CompilationUnitElement unit in unitLibraryElement.units) { 1116 for (CompilationUnitElement unit in unitLibraryElement.units) {
1113 finder._updateList(unit.functions); 1117 finder._updateList(unit.functions);
1114 } 1118 }
1115 // imports 1119 // imports
1116 for (ImportElement importElement in unitLibraryElement.imports) { 1120 for (ImportElement importElement in unitLibraryElement.imports) {
1117 if (importElement.prefix == null) { 1121 if (importElement.prefix == null) {
1118 Map<String, Element> namespace = getImportNamespace(importElement); 1122 Map<String, Element> namespace = getImportNamespace(importElement);
1119 finder._updateList(namespace.values); 1123 finder._updateList(namespace.values);
1120 } 1124 }
1121 } 1125 }
1122 // if we have close enough element, suggest to use it 1126 // if we have close enough element, suggest to use it
1123 String closestName = null; 1127 if (finder._element != null) {
1124 if (finder != null && finder._distance < 5) { 1128 String closestName = finder._element.name;
1125 closestName = finder._element.name;
1126 _addReplaceEdit(rf.rangeNode(node), closestName); 1129 _addReplaceEdit(rf.rangeNode(node), closestName);
1127 _addFix(FixKind.CHANGE_TO, [closestName]); 1130 _addFix(FixKind.CHANGE_TO, [closestName]);
1128 } 1131 }
1129 } 1132 }
1130 } 1133 }
1131 1134
1132 void _addFix_undefinedMethod_create() { 1135 void _addFix_undefinedMethod_create() {
1133 if (node is SimpleIdentifier && node.parent is MethodInvocation) { 1136 if (node is SimpleIdentifier && node.parent is MethodInvocation) {
1134 String name = (node as SimpleIdentifier).name; 1137 String name = (node as SimpleIdentifier).name;
1135 MethodInvocation invocation = node.parent as MethodInvocation; 1138 MethodInvocation invocation = node.parent as MethodInvocation;
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 sb.endPosition(); 1243 sb.endPosition();
1241 } 1244 }
1242 } 1245 }
1243 } 1246 }
1244 1247
1245 void _addFix_undefinedMethod_useSimilar() { 1248 void _addFix_undefinedMethod_useSimilar() {
1246 if (node is SimpleIdentifier && node.parent is MethodInvocation) { 1249 if (node is SimpleIdentifier && node.parent is MethodInvocation) {
1247 MethodInvocation invocation = node.parent as MethodInvocation; 1250 MethodInvocation invocation = node.parent as MethodInvocation;
1248 String name = (node as SimpleIdentifier).name; 1251 String name = (node as SimpleIdentifier).name;
1249 _ClosestElementFinder finder = 1252 _ClosestElementFinder finder =
1250 new _ClosestElementFinder(name, (Element element) { 1253 new _ClosestElementFinder(
1251 return element is MethodElement && !element.isOperator; 1254 name,
1252 }); 1255 (Element element) => element is MethodElement && !element.isOperat or,
1256 5);
1253 // unqualified invocation 1257 // unqualified invocation
1254 Expression target = invocation.realTarget; 1258 Expression target = invocation.realTarget;
1255 if (target == null) { 1259 if (target == null) {
1256 ClassDeclaration clazz = 1260 ClassDeclaration clazz =
1257 invocation.getAncestor((node) => node is ClassDeclaration); 1261 invocation.getAncestor((node) => node is ClassDeclaration);
1258 if (clazz != null) { 1262 if (clazz != null) {
1259 ClassElement classElement = clazz.element; 1263 ClassElement classElement = clazz.element;
1260 _updateFinderWithClassMembers(finder, classElement); 1264 _updateFinderWithClassMembers(finder, classElement);
1261 } 1265 }
1262 } else { 1266 } else {
1263 DartType type = target.bestType; 1267 DartType type = target.bestType;
1264 if (type is InterfaceType) { 1268 if (type is InterfaceType) {
1265 ClassElement classElement = type.element; 1269 ClassElement classElement = type.element;
1266 _updateFinderWithClassMembers(finder, classElement); 1270 _updateFinderWithClassMembers(finder, classElement);
1267 } 1271 }
1268 } 1272 }
1269 // if we have close enough element, suggest to use it 1273 // if we have close enough element, suggest to use it
1270 String closestName = null; 1274 if (finder._element != null) {
1271 if (finder != null && finder._distance < 5) { 1275 String closestName = finder._element.name;
1272 closestName = finder._element.name;
1273 _addReplaceEdit(rf.rangeNode(node), closestName); 1276 _addReplaceEdit(rf.rangeNode(node), closestName);
1274 _addFix(FixKind.CHANGE_TO, [closestName]); 1277 _addFix(FixKind.CHANGE_TO, [closestName]);
1275 } 1278 }
1276 } 1279 }
1277 } 1280 }
1278 1281
1279 void _addFix_useEffectiveIntegerDivision() { 1282 void _addFix_useEffectiveIntegerDivision() {
1280 for (AstNode n = node; n != null; n = n.parent) { 1283 for (AstNode n = node; n != null; n = n.parent) {
1281 if (n is MethodInvocation && 1284 if (n is MethodInvocation &&
1282 n.offset == errorOffset && 1285 n.offset == errorOffset &&
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after
1916 } 1919 }
1917 1920
1918 /** 1921 /**
1919 * Helper for finding [Element] with name closest to the given. 1922 * Helper for finding [Element] with name closest to the given.
1920 */ 1923 */
1921 class _ClosestElementFinder { 1924 class _ClosestElementFinder {
1922 final String _targetName; 1925 final String _targetName;
1923 final Predicate<Element> _predicate; 1926 final Predicate<Element> _predicate;
1924 1927
1925 Element _element = null; 1928 Element _element = null;
1926 int _distance = 1 << 10; 1929 int _distance;
1927 1930
1928 _ClosestElementFinder(this._targetName, this._predicate); 1931 _ClosestElementFinder(this._targetName, this._predicate, this._distance);
1929 1932
1930 void _update(Element element) { 1933 void _update(Element element) {
1931 if (_predicate(element)) { 1934 if (_predicate(element)) {
1932 int memberDistance = getLevenshteinDistance(element.name, _targetName); 1935 int memberDistance = levenshtein(element.name, _targetName, _distance);
1933 if (memberDistance < _distance) { 1936 if (memberDistance < _distance) {
1934 _element = element; 1937 _element = element;
1935 _distance = memberDistance; 1938 _distance = memberDistance;
1936 } 1939 }
1937 } 1940 }
1938 } 1941 }
1939 1942
1940 void _updateList(Iterable<Element> elements) { 1943 void _updateList(Iterable<Element> elements) {
1941 for (Element element in elements) { 1944 for (Element element in elements) {
1942 _update(element); 1945 _update(element);
1943 } 1946 }
1944 } 1947 }
1945 } 1948 }
1946 1949
1947 /** 1950 /**
1948 * Describes the location for a newly created [ConstructorDeclaration]. 1951 * Describes the location for a newly created [ConstructorDeclaration].
1949 */ 1952 */
1950 class _ConstructorLocation { 1953 class _ConstructorLocation {
1951 final String _prefix; 1954 final String _prefix;
1952 final int _offset; 1955 final int _offset;
1953 final String _suffix; 1956 final String _suffix;
1954 1957
1955 _ConstructorLocation(this._prefix, this._offset, this._suffix); 1958 _ConstructorLocation(this._prefix, this._offset, this._suffix);
1956 } 1959 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698