Permalink
Browse files

Merge pull request #196 from dragos/issue/hyperlinking-classof-1001238

Fix hyperlinks to `classOf` and other instances that were not treated co...
  • Loading branch information...
2 parents d635441 + b24d311 commit 68d548447a3e0a81600c81710514b6e51b0a1d9e @dragos dragos committed Sep 11, 2012
View
42 org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/hyperlink/HyperlinkDetectorTests.scala
@@ -16,26 +16,34 @@ object HyperlinkDetectorTests extends TestProjectSetup("hyperlinks") with Hyperl
class HyperlinkDetectorTests {
import HyperlinkDetectorTests._
-
+
@Test
def simpleHyperlinks() {
- val unit = scalaCompilationUnit("hyperlinks/SimpleHyperlinking.scala")
+ val oracle = List(
+ Link("type scala.Predef.Set"),
+ Link("type hyperlinks.SimpleHyperlinking.Tpe"),
+ Link("method scala.Array.apply", "object scala.Array"),
+ Link("method scala.collection.TraversableOnce.sum"),
+ Link("type scala.Predef.String"),
+ Link("object scala.Some"),
+ Link("class scala.Option"),
+ Link("type scala.Predef.String"))
- reload(unit)
+ loadTestUnit("hyperlinks/SimpleHyperlinking.scala").andCheckAgainst(oracle)
+ }
+
+ @Test
+ def scalaPackageLinks() {
+ val oracle = List(
+ Link("method scala.collection.immutable.List.apply", "object scala.collection.immutable.List"),
+ Link("object scala.collection.immutable.List"),
+ Link("method scala.collection.generic.GenericCompanion.apply", "object scala.collection.Seq"),
+ Link("object scala.collection.Seq"),
+ Link("object scala.collection.immutable.Nil"),
+ Link("method scala.collection.LinearSeqOptimized.apply", "value scalalinks.Foo.xs")
+ )
- val contents = unit.getContents
- val positions = SDTTestUtils.positionsOf(contents, "/*^*/")
-
- println("checking %d positions".format(positions.size))
- val resolver = new ScalaDeclarationHyperlinkComputer
- for (pos <- positions) {
- val wordRegion = ScalaWordFinder.findWord(unit.getContents, pos - 1)
- val word = new String(unit.getContents.slice(wordRegion.getOffset, wordRegion.getOffset + wordRegion.getLength))
- val links = resolver.findHyperlinks(unit, wordRegion)
- println("Found links: " + links)
- assertTrue(links.isDefined)
- assertEquals("Failed hyperlinking at position %d (%s)".format(pos, word), 1, links.get.size)
- }
+ loadTestUnit("scalalinks/ScalaListLinks.scala").andCheckAgainst(oracle)
}
@Test
@@ -76,7 +84,7 @@ class HyperlinkDetectorTests {
// and make sure the classpath is up to date
project.resetPresentationCompiler()
- val oracle = List(Link("type util.Box.myInt"), Link("method util.Full.apply"))
+ val oracle = List(Link("type util.Box.myInt"), Link("method util.Full.apply", "object util.Full"))
loadTestUnit("bug1000656/Client.scala").andCheckAgainst(oracle)
}
}
View
14 org.scala-ide.sdt.core.tests/src/scala/tools/eclipse/hyperlink/HyperlinkTester.scala
@@ -15,7 +15,7 @@ trait HyperlinkTester extends TestProjectSetup {
private final val HyperlinkMarker = "/*^*/"
- case class Link(text: String)
+ case class Link(text: String*)
/** Given a source `path`, load the corresponding scala `unit`. */
def loadTestUnit(path: String): VerifyHyperlink = {
@@ -47,15 +47,9 @@ trait HyperlinkTester extends TestProjectSetup {
// Verify Expectations
assertTrue("no links found for `%s`".format(word), maybeLinks.isDefined)
val links = maybeLinks.get
- assertEquals("expected %d link, found %d".format(1, links.size), 1, links.size)
- val link = links.head
- assertEquals("text", oracle.text, link.getTypeLabel)
- //assertEquals("offset", oracle.region.getOffset(), link.getHyperlinkRegion().getOffset())
- unit.withSourceFile({ (sourceFile, compiler) =>
- val offset = link.getHyperlinkRegion().getOffset()
- val length = link.getHyperlinkRegion().getLength
- val linkedPos = compiler.rangePos(sourceFile, offset, offset, offset + length)
- })(None)
+ assertEquals("expected %d link, found %d".format(oracle.text.size, links.size), oracle.text.size, links.size)
+ val linkResults = links map (_.getTypeLabel)
+ assertEquals("text", oracle.text.toList.toString, linkResults.toList.toString)
}
}
}
View
2 org.scala-ide.sdt.core.tests/test-workspace/hyperlinks/src/bug1000656/Client.scala
@@ -10,6 +10,6 @@ class Client {
val t: b.myInt/*^*/ = 10
- val x = Full.apply/*^*/("a")
+ val x = Full/*^*/("a")
}
}
View
3 ...cala-ide.sdt.core.tests/test-workspace/hyperlinks/src/hyperlinks/SimpleHyperlinking.scala
@@ -5,12 +5,13 @@ package hyperlinks
*/
class SimpleHyperlinking {
- type Tpe[T] = List/*^*/[T]
+ type Tpe[T] = Set/*^*/[T]
def foo(xs: Tpe/*^*/[Int]) = {
val arr = Array/*^*/(1, 2, 3)
val sum = xs.sum/*^*/
val x: String/*^*/ = "Hello, world"
val Some/*^*/(x): Option/*^*/[Int] = Some(10)
+ classOf[String/*^*/]
}
}
View
12 org.scala-ide.sdt.core.tests/test-workspace/hyperlinks/src/scalalinks/ScalaListLinks.scala
@@ -0,0 +1,12 @@
+package scalalinks
+
+class Foo {
+ def bar {
+ val xs = List/*^*/(1, 2, 3)
+ List/*^*/
+ Seq/*^*/()
+ Seq/*^*/
+ Nil/*^*/
+ xs/*^*/(1)
+ }
+}
View
15 ...e/src/scala/tools/eclipse/hyperlink/text/detector/ScalaDeclarationHyperlinkComputer.scala
@@ -9,13 +9,18 @@ import scala.tools.eclipse.javaelements.ScalaCompilationUnit
import scala.tools.eclipse.logging.HasLogger
import scala.tools.eclipse.hyperlink.text._
import scala.tools.eclipse.InteractiveCompilationUnit
+import scala.tools.eclipse.ScalaPresentationCompiler
class ScalaDeclarationHyperlinkComputer extends HasLogger {
def findHyperlinks(icu: InteractiveCompilationUnit, wordRegion: IRegion): Option[List[IHyperlink]] = {
findHyperlinks(icu, wordRegion, wordRegion)
}
def findHyperlinks(icu: InteractiveCompilationUnit, wordRegion: IRegion, mappedRegion: IRegion): Option[List[IHyperlink]] = {
+ /** In 2.9 ClazzTag was called ClassTag. Source-compatibility hack */
+ implicit def compatClazzTag(com: ScalaPresentationCompiler) = new {
+ def ClazzTag = 12 // we really, really hope this constant won't change in 2.9.x
+ }
icu.withSourceFile({ (sourceFile, compiler) =>
object DeclarationHyperlinkFactory extends HyperlinkFactory {
protected val global: compiler.type = compiler
@@ -57,16 +62,18 @@ class ScalaDeclarationHyperlinkComputer extends HasLogger {
List(tpe.member(sel.name), tpe.member(sel.name.toTypeName))
} getOrElse Nil
}
- case Annotated(atp, _) => List(atp.symbol)
- case st: SymTree => List(st.symbol)
- case t => logger.info("unhandled tree " + t.getClass); List()
+ case Annotated(atp, _) => List(atp.symbol)
+ case Literal(const) if const.tag == compiler.ClazzTag => List(const.typeValue.typeSymbol)
+ case ap @ Select(qual, nme.apply) => List(qual.symbol, ap.symbol)
+ case st if st.symbol ne null => List(st.symbol)
+ case _ => List()
} flatMap { list =>
val filteredSyms = list filterNot { sym => sym.isPackage || sym == NoSymbol }
if (filteredSyms.isEmpty) None else Some(
filteredSyms.foldLeft(List[IHyperlink]()) { (links, sym) =>
if (sym.isJavaDefined) links
else
- DeclarationHyperlinkFactory.create(Hyperlink.withText("Open Declaration"), icu, sym, wordRegion).toList ::: links
+ DeclarationHyperlinkFactory.create(Hyperlink.withText("Open Declaration (%s)".format(sym.toString)), icu, sym, wordRegion).toList ::: links
})
}
}.flatten.headOption match {

0 comments on commit 68d5484

Please sign in to comment.