From 02975ed50d200f222bc561c335d2aa15b38f65ae Mon Sep 17 00:00:00 2001 From: Iulian Dragos Date: Sat, 5 Apr 2008 15:16:36 +0000 Subject: Enhanced @see tag in scaladoc: - if the text looks like a link, it is turned into a link - if it is a symbol name that can be resolved, it is turned into a link to that symbol - otherwise fall back to old behavior (plain text). Removed two printlns from Definitions (probably debug messages). --- src/compiler/scala/tools/nsc/doc/ModelFrames.scala | 1 - src/compiler/scala/tools/nsc/doc/ModelToXML.scala | 63 +++++++++++++++++++--- .../scala/tools/nsc/symtab/Definitions.scala | 2 - 3 files changed, 57 insertions(+), 9 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/doc/ModelFrames.scala b/src/compiler/scala/tools/nsc/doc/ModelFrames.scala index 3b3a093c2d..f8da165bb9 100644 --- a/src/compiler/scala/tools/nsc/doc/ModelFrames.scala +++ b/src/compiler/scala/tools/nsc/doc/ModelFrames.scala @@ -314,7 +314,6 @@ trait ModelFrames extends ModelExtractor { } } - def longComment(cmnt: Comment): NodeSeq val index = diff --git a/src/compiler/scala/tools/nsc/doc/ModelToXML.scala b/src/compiler/scala/tools/nsc/doc/ModelToXML.scala index c7895ab25f..bad17af2ee 100644 --- a/src/compiler/scala/tools/nsc/doc/ModelToXML.scala +++ b/src/compiler/scala/tools/nsc/doc/ModelToXML.scala @@ -33,7 +33,7 @@ trait ModelToXML extends ModelExtractor { aref(url, entity.nameString) } */ - def link(entity: Symbol)(implicit frame: Frame): NodeSeq = { + def link(entity: Symbol, label: String)(implicit frame: Frame): NodeSeq = { val url = urlFor(entity) if (url == null) { // external link (handled by script.js) val (href, attr) = @@ -45,9 +45,12 @@ trait ModelToXML extends ModelExtractor { {name}; } else - aref(url, entity.nameString) + aref(url, label) } + def link(entity: Symbol)(implicit frame: Frame): NodeSeq = + link(entity, entity.nameString) + def link(tpe: Type)(implicit frame: Frame): NodeSeq = { if (!tpe.typeArgs.isEmpty) { if (definitions.isFunctionType(tpe)) { @@ -129,7 +132,7 @@ trait ModelToXML extends ModelExtractor { } } - def longHeader(entity: Entity)(implicit from: Frame): NodeSeq = Group({ + def longHeader(entity: Entity)(implicit frame: Frame): NodeSeq = Group({ anchor(entity.sym) ++
{attrsFor(entity)} @@ -142,7 +145,7 @@ trait ModelToXML extends ModelExtractor { } ++ { val cmnt = entity.decodeComment if (cmnt.isEmpty) NodeSeq.Empty - else longComment(cmnt.get) + else longComment(entity, cmnt.get) } ++ (entity match { case entity: ClassOrObject => classBody(entity) case _ => NodeSeq.Empty @@ -163,7 +166,7 @@ trait ModelToXML extends ModelExtractor { } } ++
); - def longComment(cmnt: Comment): NodeSeq = { + def longComment(entity: Entity, cmnt: Comment)(implicit frame: Frame): NodeSeq = { val attrs =
{ var seq: NodeSeq = NodeSeq.Empty cmnt.decodeAttributes.foreach{ @@ -173,7 +176,10 @@ trait ModelToXML extends ModelExtractor { case (option,body) =>
{ if (option == null) NodeSeq.Empty; else decodeOption(tag, option); - }{parse(body)}
+ }{ tag match { + case "see" => resolveSee(entity.sym, body.trim) + case _ => parse(body) + }} }} }; seq @@ -184,6 +190,51 @@ trait ModelToXML extends ModelExtractor { } + /** + * Try to be smart about @see elements. If the body looks like a link, turn it into + * a link. If it can be resolved in the symbol table, turn it into a link to the referenced + * entity. + */ + private def resolveSee(owner: Symbol, body: String)(implicit frame: Frame): NodeSeq = { + /** find a class either in the root package, in the current class or in the current package. */ + def findClass(clsName: String): Symbol = { + try { definitions.getClass(clsName) } catch { + case f: FatalError => + try { definitions.getMember(owner, clsName.toTypeName) } catch { + case f: FatalError => + definitions.getMember(owner.enclosingPackage, clsName.toTypeName) + } + } + } + + if (body.startsWith("http://") + || body.startsWith("https://") + || body.startsWith("www")) { + // a link + body.split(" ") match { + case Seq(href, txt, rest @ _*) => + {txt}{rest} + case _ => + {body} + } + } else try { + // treat it like a class or member reference + body.split("#") match { + case Seq(clazz, member) => + val clazzSym = if (clazz.length == 0) owner.enclClass else findClass(clazz) + link(definitions.getMember(clazzSym, member), body) + case Seq(clazz, _*) => + link(findClass(clazz), body) + case _ => + parse(body) + } + } catch { + case f: FatalError => + log("Error resolving @see: " + f.toString) + parse(body) + } + } + def classBody(entity: ClassOrObject)(implicit from: Frame): NodeSeq = {categories.mkXML("","\n","")(c => shortList(entity, c)) : NodeSeq} diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala index aa5131a445..3c23a8f481 100644 --- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala +++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala @@ -328,8 +328,6 @@ trait Definitions { if (owner == NoSymbol) return NoSymbol val result = owner.info.nonPrivateMember(name) if (result == NoSymbol) { - Console.println(owner.infosString) - Console.println(owner.info.decls) throw new FatalError(owner.toString() + " does not have a member " + name) } result -- cgit v1.2.3