From 7adb4784bf484f8cf57b5edb612d2c4f34388237 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sun, 4 Dec 2011 10:06:28 -0800 Subject: Tweaked ident suggestions. Rolled damaru-levenshtein algorithm back to my original "pure" version. Cut max distance to 1. Turned on by default because now it offers nothing unexpected, and removed short-lived -Ysuggest-idents option. --- .../scala/tools/nsc/settings/ScalaSettings.scala | 1 - .../scala/tools/nsc/typechecker/Typers.scala | 2 +- src/compiler/scala/tools/util/EditDistance.scala | 31 +++++++++++----------- 3 files changed, 16 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala index 1f8fa5bbe2..6be15e4e98 100644 --- a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala +++ b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala @@ -178,7 +178,6 @@ trait ScalaSettings extends AbsScalaSettings val exposeEmptyPackage = BooleanSetting("-Yexpose-empty-package", "Internal only: expose the empty package.").internalOnly() val YnoProductN = BooleanSetting ("-Yno-productN", "Do not add ProductN to case classes") - val suggestIdents = BooleanSetting("-Ysuggest-idents", "Suggest alternatives for `not found` identifiers") def stop = stopAfter diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 30d10325be..7671ccbed7 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -3874,7 +3874,7 @@ trait Typers extends Modes with Adaptations with PatMatVirtualiser { else { val similar = ( // name length check to limit unhelpful suggestions for e.g. "x" and "b1" - if (settings.suggestIdents.value && name.length > 2) { + if (name.length > 2) { val allowed = ( startingIdentContext.enclosingContextChain flatMap (ctx => ctx.scope.toList ++ ctx.imports.flatMap(_.allImportedSymbols)) diff --git a/src/compiler/scala/tools/util/EditDistance.scala b/src/compiler/scala/tools/util/EditDistance.scala index 704286d47e..a8d7408532 100644 --- a/src/compiler/scala/tools/util/EditDistance.scala +++ b/src/compiler/scala/tools/util/EditDistance.scala @@ -8,22 +8,22 @@ package util object EditDistance { def similarString(name: String, allowed: TraversableOnce[String]): String = { - val suggested = suggestions(name, allowed.toSeq, maxDistance = 2, maxSuggestions = 2) - if (suggested.isEmpty) "" + val suggested = suggestions(name, allowed.toSeq, maxDistance = 1, maxSuggestions = 2) + if (suggested.isEmpty) "" else suggested.mkString(" (similar: ", ", ", ")") } - def suggestions(a: String, bs: Seq[String], maxDistance: Int = 3, maxSuggestions: Int = 3): Seq[String] = - bs.map { b => (b, distance(a, b) ) } filter (_._2 <= maxDistance) sortBy(_._2) take(maxSuggestions) map(_._1) + def suggestions(a: String, bs: Seq[String], maxDistance: Int, maxSuggestions: Int): Seq[String] = ( + bs map (b => (b, distance(a, b))) + filter (_._2 <= maxDistance) + sortBy (_._2) + take (maxSuggestions) + map (_._1) + ) - def distance(a: String, b: String): Int = - levenshtein(a, b, insertCost = 1, deleteCost = 1, subCost = 2, transposeCost = 1, matchCost = -1, true) + def distance(a: String, b: String): Int = levenshtein(a, b, transpositions = true) - /** Translated from the java version at - * http://www.merriampark.com/ld.htm - * which is declared to be public domain. - */ - def levenshtein(s: String, t: String, insertCost: Int = 1, deleteCost: Int = 1, subCost: Int = 1, transposeCost: Int = 1, matchCost: Int = 0, transpositions: Boolean = false): Int = { + def levenshtein(s: String, t: String, transpositions: Boolean): Int = { val n = s.length val m = t.length if (n == 0) return m @@ -35,18 +35,17 @@ object EditDistance { for (i <- 1 to n ; val s_i = s(i - 1) ; j <- 1 to m) { val t_j = t(j - 1) - val cost = if (s_i == t_j) matchCost else subCost - val tcost = if (s_i == t_j) matchCost else transposeCost + val cost = if (s_i == t_j) 0 else 1 - val c1 = d(i - 1)(j) + deleteCost - val c2 = d(i)(j - 1) + insertCost + val c1 = d(i - 1)(j) + 1 + val c2 = d(i)(j - 1) + 1 val c3 = d(i - 1)(j - 1) + cost d(i)(j) = c1 min c2 min c3 if (transpositions) { if (i > 1 && j > 1 && s(i - 1) == t(j - 2) && s(i - 2) == t(j - 1)) - d(i)(j) = d(i)(j) min (d(i - 2)(j - 2) + cost) + d(i)(j) = d(i)(j) min (d(i - 2)(j - 2) + cost) } } -- cgit v1.2.3