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

Unified 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: Fixes for review comments 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analysis_services/lib/src/correction/levenshtein.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_services/lib/src/correction/fix.dart
diff --git a/pkg/analysis_services/lib/src/correction/fix.dart b/pkg/analysis_services/lib/src/correction/fix.dart
index 9d463ed0d12e0f9b858202ad1cf90b36c9f0f71b..22e331dff4a0b17f08e1d5c25534e7778e6a57ac 100644
--- a/pkg/analysis_services/lib/src/correction/fix.dart
+++ b/pkg/analysis_services/lib/src/correction/fix.dart
@@ -37,6 +37,8 @@ typedef bool Predicate<E>(E argument);
* The computer for Dart fixes.
*/
class FixProcessor {
+ static const int MAX_LEVENSHTEIN_DISTANCE = 3;
+
final SearchEngine searchEngine;
final Source source;
final String file;
@@ -1029,7 +1031,10 @@ class FixProcessor {
if (_mayBeTypeIdentifier(node)) {
String name = (node as SimpleIdentifier).name;
_ClosestElementFinder finder =
- new _ClosestElementFinder(name, (Element element) => element is ClassElement);
+ new _ClosestElementFinder(
+ name,
+ (Element element) => element is ClassElement,
+ MAX_LEVENSHTEIN_DISTANCE);
// find closest element
{
// elements of this library
@@ -1045,7 +1050,7 @@ class FixProcessor {
}
}
// if we have close enough element, suggest to use it
- if (finder != null && finder._distance < 5) {
+ if (finder._element != null) {
String closestName = finder._element.name;
_addReplaceEdit(rf.rangeNode(node), closestName);
// add proposal
@@ -1107,7 +1112,8 @@ class FixProcessor {
_ClosestElementFinder finder =
new _ClosestElementFinder(
name,
- (Element element) => element is FunctionElement);
+ (Element element) => element is FunctionElement,
+ MAX_LEVENSHTEIN_DISTANCE);
// this library
for (CompilationUnitElement unit in unitLibraryElement.units) {
finder._updateList(unit.functions);
@@ -1120,9 +1126,8 @@ class FixProcessor {
}
}
// if we have close enough element, suggest to use it
- String closestName = null;
- if (finder != null && finder._distance < 5) {
- closestName = finder._element.name;
+ if (finder._element != null) {
+ String closestName = finder._element.name;
_addReplaceEdit(rf.rangeNode(node), closestName);
_addFix(FixKind.CHANGE_TO, [closestName]);
}
@@ -1247,9 +1252,10 @@ class FixProcessor {
MethodInvocation invocation = node.parent as MethodInvocation;
String name = (node as SimpleIdentifier).name;
_ClosestElementFinder finder =
- new _ClosestElementFinder(name, (Element element) {
- return element is MethodElement && !element.isOperator;
- });
+ new _ClosestElementFinder(
+ name,
+ (Element element) => element is MethodElement && !element.isOperator,
+ MAX_LEVENSHTEIN_DISTANCE);
// unqualified invocation
Expression target = invocation.realTarget;
if (target == null) {
@@ -1267,9 +1273,8 @@ class FixProcessor {
}
}
// if we have close enough element, suggest to use it
- String closestName = null;
- if (finder != null && finder._distance < 5) {
- closestName = finder._element.name;
+ if (finder._element != null) {
+ String closestName = finder._element.name;
_addReplaceEdit(rf.rangeNode(node), closestName);
_addFix(FixKind.CHANGE_TO, [closestName]);
}
@@ -1923,13 +1928,13 @@ class _ClosestElementFinder {
final Predicate<Element> _predicate;
Element _element = null;
- int _distance = 1 << 10;
+ int _distance;
- _ClosestElementFinder(this._targetName, this._predicate);
+ _ClosestElementFinder(this._targetName, this._predicate, this._distance);
void _update(Element element) {
if (_predicate(element)) {
- int memberDistance = getLevenshteinDistance(element.name, _targetName);
+ int memberDistance = levenshtein(element.name, _targetName, _distance);
if (memberDistance < _distance) {
_element = element;
_distance = memberDistance;
« no previous file with comments | « no previous file | pkg/analysis_services/lib/src/correction/levenshtein.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698