summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-12-04 10:06:28 -0800
committerPaul Phillips <paulp@improving.org>2011-12-04 11:28:51 -0800
commit7adb4784bf484f8cf57b5edb612d2c4f34388237 (patch)
tree7698c4b214b020e3ccffad9518b21b2fb0973adb
parent3e9e4ecf360e6eda5c26f798abfcb9bb882cf772 (diff)
downloadscala-7adb4784bf484f8cf57b5edb612d2c4f34388237.tar.gz
scala-7adb4784bf484f8cf57b5edb612d2c4f34388237.tar.bz2
scala-7adb4784bf484f8cf57b5edb612d2c4f34388237.zip
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.
-rw-r--r--src/compiler/scala/tools/nsc/settings/ScalaSettings.scala1
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Typers.scala2
-rw-r--r--src/compiler/scala/tools/util/EditDistance.scala31
-rw-r--r--test/files/neg/nopredefs.check2
-rw-r--r--test/files/neg/suggest-similar.check9
-rw-r--r--test/files/neg/suggest-similar.flags1
-rw-r--r--test/files/neg/suggest-similar.scala2
-rw-r--r--test/files/neg/t2870.check2
8 files changed, 22 insertions, 28 deletions
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)
}
}
diff --git a/test/files/neg/nopredefs.check b/test/files/neg/nopredefs.check
index 0a0ab34482..e6c1af78a0 100644
--- a/test/files/neg/nopredefs.check
+++ b/test/files/neg/nopredefs.check
@@ -1,4 +1,4 @@
-nopredefs.scala:5: error: not found: value Set
+nopredefs.scala:5: error: not found: value Set (similar: Seq)
val y = Set(3)
^
one error found
diff --git a/test/files/neg/suggest-similar.check b/test/files/neg/suggest-similar.check
index 320c7d0092..0a858aaf2e 100644
--- a/test/files/neg/suggest-similar.check
+++ b/test/files/neg/suggest-similar.check
@@ -1,13 +1,10 @@
suggest-similar.scala:8: error: not found: value flippitx (similar: flippity)
flippitx = 123
^
-suggest-similar.scala:9: error: not found: value identipoo (similar: identity)
- Nil map identipoo
+suggest-similar.scala:9: error: not found: value identiyt (similar: identity)
+ Nil map identiyt
^
suggest-similar.scala:10: error: not found: type Bingus (similar: Dingus)
new Bingus
^
-suggest-similar.scala:11: error: value bap is not a member of object Nil
- Nil bap identity
- ^
-four errors found
+three errors found
diff --git a/test/files/neg/suggest-similar.flags b/test/files/neg/suggest-similar.flags
deleted file mode 100644
index 66bb23c396..0000000000
--- a/test/files/neg/suggest-similar.flags
+++ /dev/null
@@ -1 +0,0 @@
--Ysuggest-idents \ No newline at end of file
diff --git a/test/files/neg/suggest-similar.scala b/test/files/neg/suggest-similar.scala
index ae18c6ea62..ff327478fe 100644
--- a/test/files/neg/suggest-similar.scala
+++ b/test/files/neg/suggest-similar.scala
@@ -6,6 +6,6 @@ import Dingus._
class A {
flippitx = 123
- Nil map identipoo
+ Nil map identiyt
new Bingus
}
diff --git a/test/files/neg/t2870.check b/test/files/neg/t2870.check
index 6577577d3f..72bc0d98a1 100644
--- a/test/files/neg/t2870.check
+++ b/test/files/neg/t2870.check
@@ -1,4 +1,4 @@
-t2870.scala:1: error: not found: type Jar
+t2870.scala:1: error: not found: type Jar (similar: Jars)
class Jars(jar: Jar)
^
t2870.scala:6: error: illegal cyclic reference involving value <import>