Skip to content

Commit

Permalink
Fixes the backticks in use case signature crash
Browse files Browse the repository at this point in the history
Suggested by Simon in
https://groups.google.com/forum/?hl=en&fromgroups#!topic/scala-internals/z7s1CCRCz74

Now it eliminates backticks and gracefully bails out with an error
message when it can't remove the wiki syntax.
  • Loading branch information
VladUreche authored and paulp committed May 3, 2012
1 parent 00b22ed commit b6e989f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/compiler/scala/tools/nsc/ast/DocComments.scala
Expand Up @@ -383,7 +383,7 @@ trait DocComments { self: Global =>
} }


// !!! todo: inherit from Comment? // !!! todo: inherit from Comment?
case class DocComment(raw: String, pos: Position = NoPosition) { case class DocComment(raw: String, pos: Position = NoPosition, codePos: Position = NoPosition) {


/** Returns: /** Returns:
* template: the doc comment minus all @define and @usecase sections * template: the doc comment minus all @define and @usecase sections
Expand Down Expand Up @@ -412,7 +412,7 @@ trait DocComments { self: Global =>
val comment = "/** " + raw.substring(commentStart, end) + "*/" val comment = "/** " + raw.substring(commentStart, end) + "*/"
val commentPos = subPos(commentStart, end) val commentPos = subPos(commentStart, end)


UseCase(DocComment(comment, commentPos), code, codePos) UseCase(DocComment(comment, commentPos, codePos), code, codePos)
} }


private def subPos(start: Int, end: Int) = private def subPos(start: Int, end: Int) =
Expand Down Expand Up @@ -461,7 +461,18 @@ trait DocComments { self: Global =>
findIn(classes ::: List(pkgs.head, definitions.RootClass)) findIn(classes ::: List(pkgs.head, definitions.RootClass))
} }


def getType(str: String): Type = { def getType(_str: String, variable: String): Type = {
/*
* work around the backticks issue suggested by Simon in
* https://groups.google.com/forum/?hl=en&fromgroups#!topic/scala-internals/z7s1CCRCz74
* ideally, we'd have a removeWikiSyntax method in the CommentFactory to completely eliminate the wiki markup
*/
val str =
if (_str.length >= 2 && _str.startsWith("`") && _str.endsWith("`"))
_str.substring(1, _str.length - 2)
else
_str

def getParts(start: Int): List[String] = { def getParts(start: Int): List[String] = {
val end = skipIdent(str, start) val end = skipIdent(str, start)
if (end == start) List() if (end == start) List()
Expand All @@ -471,7 +482,11 @@ trait DocComments { self: Global =>
} }
} }
val parts = getParts(0) val parts = getParts(0)
assert(parts.nonEmpty, "parts is empty '" + str + "' in site " + site) if (parts.isEmpty) {
reporter.error(comment.codePos, "Incorrect variable expansion for " + variable + " in use case. Does the " +
"variable expand to wiki syntax when documenting " + site + "?")
return ErrorType
}
val partnames = (parts.init map newTermName) :+ newTypeName(parts.last) val partnames = (parts.init map newTermName) :+ newTypeName(parts.last)
val (start, rest) = parts match { val (start, rest) = parts match {
case "this" :: _ => (site.thisType, partnames.tail) case "this" :: _ => (site.thisType, partnames.tail)
Expand All @@ -490,7 +505,7 @@ trait DocComments { self: Global =>
for (alias <- aliases) yield for (alias <- aliases) yield
lookupVariable(alias.name.toString.substring(1), site) match { lookupVariable(alias.name.toString.substring(1), site) match {
case Some(repl) => case Some(repl) =>
val tpe = getType(repl.trim) val tpe = getType(repl.trim, alias.name.toString)
if (tpe != NoType) tpe if (tpe != NoType) tpe
else { else {
val alias1 = alias.cloneSymbol(definitions.RootClass, alias.rawflags, newTypeName(repl)) val alias1 = alias.cloneSymbol(definitions.RootClass, alias.rawflags, newTypeName(repl))
Expand Down
4 changes: 4 additions & 0 deletions test/scaladoc/run/usecase-var-expansion.check
@@ -0,0 +1,4 @@
newSource:8: error: Incorrect variable expansion for $Coll in use case. Does the variable expand to wiki syntax when documenting class Test2?
* @usecase def foo: $Coll[T]
^
Done.
26 changes: 26 additions & 0 deletions test/scaladoc/run/usecase-var-expansion.scala
@@ -0,0 +1,26 @@
import scala.tools.nsc.doc.model._
import scala.tools.partest.ScaladocModelTest
import language._

object Test extends ScaladocModelTest {

override def code = """
/**
* @define Coll `Test`
*/
class Test[T] {
/**
* member $Coll
* @usecase def foo: $Coll[T]
* usecase $Coll
*/
def foo(implicit err: String): Test[T] = sys.error(err)
}
/** @define Coll {{{some `really` < !! >> invalid $$$ thing}}} */
class Test2[T] extends Test[Int]
"""

def scaladocSettings = ""
def testModel(root: Package) = ()
}

0 comments on commit b6e989f

Please sign in to comment.