From 7f3922f39ab597c4a4fb87acdfdc6c8eb30c3ba3 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Wed, 25 Nov 2009 16:41:47 +0000 Subject: Updating deprecated usages in new scaladoc code. as inspiration to chase down a few spots using get and getOrElse in suboptimal fashion. --- src/compiler/scala/tools/nsc/PhaseAssembly.scala | 9 +- .../scala/tools/nsc/backend/icode/GenICode.scala | 2 +- .../scala/tools/nsc/backend/icode/TypeKinds.scala | 4 +- .../scala/tools/nsc/doc/html/page/Template.scala | 2 +- .../scala/tools/nsc/doc/model/EntityFactory.scala | 44 +++-- .../nsc/doc/model/comment/CommentFactory.scala | 185 ++++++++++----------- src/library/scala/xml/Utility.scala | 2 +- 7 files changed, 120 insertions(+), 128 deletions(-) diff --git a/src/compiler/scala/tools/nsc/PhaseAssembly.scala b/src/compiler/scala/tools/nsc/PhaseAssembly.scala index 958bef5652..ab0b2cd26e 100644 --- a/src/compiler/scala/tools/nsc/PhaseAssembly.scala +++ b/src/compiler/scala/tools/nsc/PhaseAssembly.scala @@ -66,13 +66,8 @@ trait PhaseAssembly { self: Global => /* Given the name of a phase object, get the node for that name. If the * node object does not exits, then create it. */ - def getNodeByPhase(name: String): Node = { - nodes.get(name).getOrElse { - val node = new Node(name) - nodes += (name -> node) - node - } - } + def getNodeByPhase(name: String): Node = + nodes.getOrElseUpdate(name, new Node(name)) /* Connect the frm and to nodes with an edge and make it soft. * Also add the edge object to the set of edges, and to the dependency diff --git a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala index 22934a78a7..d4b00d1638 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/GenICode.scala @@ -266,7 +266,7 @@ abstract class GenICode extends SubComponent { val k = toTypeKind(arrayObj.tpe) val ARRAY(elem) = k var ctx1 = genLoad(arrayObj, ctx, k) - val elementType = (typeOfArrayOp get code) getOrElse abort("Unknown operation on arrays: " + tree + " code: " + code) + val elementType = typeOfArrayOp.getOrElse(code, abort("Unknown operation on arrays: " + tree + " code: " + code)) var generatedType = expectedType diff --git a/src/compiler/scala/tools/nsc/backend/icode/TypeKinds.scala b/src/compiler/scala/tools/nsc/backend/icode/TypeKinds.scala index 81461c1bf7..6aec1bb754 100644 --- a/src/compiler/scala/tools/nsc/backend/icode/TypeKinds.scala +++ b/src/compiler/scala/tools/nsc/backend/icode/TypeKinds.scala @@ -440,13 +440,13 @@ trait TypeKinds { self: ICodes => REFERENCE(sym) case SingleType(pre, sym) => - (primitiveTypeMap get sym) getOrElse REFERENCE(sym) + primitiveTypeMap.getOrElse(sym, REFERENCE(sym)) case ConstantType(value) => toTypeKind(t.underlying) case TypeRef(_, sym, args) => - (primitiveTypeMap get sym) getOrElse arrayOrClassType(sym, args) + primitiveTypeMap.getOrElse(sym, arrayOrClassType(sym, args)) case ClassInfoType(_, _, sym) => primitiveTypeMap get sym match { diff --git a/src/compiler/scala/tools/nsc/doc/html/page/Template.scala b/src/compiler/scala/tools/nsc/doc/html/page/Template.scala index 20293d1ff0..8dd46952da 100644 --- a/src/compiler/scala/tools/nsc/doc/html/page/Template.scala +++ b/src/compiler/scala/tools/nsc/doc/html/page/Template.scala @@ -152,7 +152,7 @@ class Template(tpl: DocTemplateEntity) extends HtmlPage { { tp.variance + tp.name + boundsToString(tp.hi, tp.lo) } - def tparams0(tpss: List[TypeParam]): NodeSeq = tpss match { + def tparams0(tpss: List[TypeParam]): NodeSeq = (tpss: @unchecked) match { case tp :: Nil => tparam0(tp) case tp :: tps => tparam0(tp) ++ Text(",") ++ tparams0(tps) } diff --git a/src/compiler/scala/tools/nsc/doc/model/EntityFactory.scala b/src/compiler/scala/tools/nsc/doc/model/EntityFactory.scala index 27403e49cb..033b51e3e7 100644 --- a/src/compiler/scala/tools/nsc/doc/model/EntityFactory.scala +++ b/src/compiler/scala/tools/nsc/doc/model/EntityFactory.scala @@ -13,13 +13,11 @@ import symtab.Flags class EntityFactory(val global: Global, val settings: doc.Settings) { extractor => import global._ + import definitions.{ ObjectClass, ScalaObjectClass, RootPackage, EmptyPackage } /** */ def makeModel: Package = - makePackage(definitions.RootPackage, null) match { - case Some(pack) => pack - case None => throw new Error("no documentable class found in compilation units") - } + makePackage(RootPackage, null) getOrElse { throw new Error("no documentable class found in compilation units") } /** */ protected val commentFactory = new CommentFactory(reporter) @@ -43,11 +41,11 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor val whichSym = if (comments isDefinedAt sym) Some(sym) else sym.allOverriddenSymbols find (comments isDefinedAt _) whichSym map { s => - (commentCache get s) getOrElse { + commentCache.getOrElse(s, { val c = commentFactory.parse(comments(s), s.pos) commentCache += s -> c c - } + }) } } } @@ -126,9 +124,9 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor val typeParams = if (sym.isClass) sym.typeParams map (makeTypeParam(_, this)) else Nil val parentType = if (sym.isPackage) None else - Some(makeType(RefinedType(sym.tpe.parents filter (_ != definitions.ScalaObjectClass.tpe), EmptyScope))) + Some(makeType(RefinedType(sym.tpe.parents filter (_ != ScalaObjectClass.tpe), EmptyScope))) val linearization = { - sym.ancestors filter (_ != definitions.ScalaObjectClass) map (makeTemplate(_)) + sym.ancestors filter (_ != ScalaObjectClass) map (makeTemplate(_)) // TODO: Register subclasses } private val subClassesCache = mutable.Buffer.empty[DocTemplateEntity] @@ -136,18 +134,18 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor def subClasses = subClassesCache.toList def memberSyms = sym.info.nonPrivateMembers val members: List[MemberEntity] = memberSyms flatMap (makeMember(_, this)) - val templates = members flatMap {case c: DocTemplateEntity => Some(c) case _ => None} - val methods = members flatMap {case d: Def => Some(d) case _ => None} - val values = members flatMap {case v: Val => Some(v) case _ => None} - val abstractTypes = members flatMap {case t: AbstractType => Some(t) case _ => None} - val aliasTypes = members flatMap {case t: AliasType => Some(t) case _ => None} + val templates = members partialMap { case c: DocTemplateEntity => c } + val methods = members partialMap { case d: Def => d } + val values = members partialMap { case v: Val => v } + val abstractTypes = members partialMap { case t: AbstractType => t } + val aliasTypes = members partialMap { case t: AliasType => t } override val isTemplate = true } abstract class PackageImpl(sym: Symbol, inTpl: => PackageImpl) extends DocTemplateImpl(sym, inTpl) with Package { override def inTemplate = inTpl override def toRoot: List[PackageImpl] = inTpl :: inTpl.toRoot - val packages = members flatMap {case p: Package => Some(p) case _ => None} + val packages = members partialMap { case p: Package => p } } abstract class NonTemplateMemberImpl(sym: Symbol, inTpl: => DocTemplateImpl) extends MemberImpl(sym, inTpl) with NonTemplateMemberEntity { @@ -164,9 +162,9 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor /** */ def normalizeTemplate(aSym: Symbol): Symbol = { - if (aSym == null || aSym == definitions.EmptyPackage || aSym == NoSymbol) - normalizeTemplate(definitions.RootPackage) - else if (aSym == definitions.ScalaObjectClass || aSym == definitions.ObjectClass) + if (aSym == null || aSym == EmptyPackage || aSym == NoSymbol) + normalizeTemplate(RootPackage) + else if (aSym == ScalaObjectClass || aSym == ObjectClass) normalizeTemplate(definitions.AnyRefClass) else if (aSym.isModuleClass || aSym.isPackageObject) normalizeTemplate(aSym.sourceModule) @@ -182,7 +180,7 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor Some(templatesCache(bSym, inTpl) match {case p: PackageImpl => p}) else { val pack = - if (bSym == definitions.RootPackage) + if (bSym == RootPackage) new PackageImpl(bSym, null) { override val name = "root" override def inTemplate = this @@ -191,8 +189,8 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor override lazy val inheritedFrom = Nil override val isRootPackage = true override def memberSyms = - (bSym.info.members ++ definitions.EmptyPackage.info.members) filter { s => - s != definitions.EmptyPackage && s != definitions.RootPackage + (bSym.info.members ++ EmptyPackage.info.members) filter { s => + s != EmptyPackage && s != RootPackage } } else @@ -205,7 +203,7 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor /** */ def makeTemplate(aSym: Symbol): TemplateImpl = { val bSym = normalizeTemplate(aSym) - if (bSym == definitions.RootPackage) + if (bSym == RootPackage) makePackage(bSym, null).get else makeTemplate(bSym, makeTemplate(bSym.owner)) @@ -250,7 +248,7 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor val valueParams = List(sym.constrParamAccessors map (makeValueParam(_, this))) val constructors = - members flatMap {case d: Constructor => Some(d) case _ => None} + members partialMap { case d: Constructor => d } val primaryConstructor = (constructors find (_.isPrimary)) val isCaseClass = sym.isClass && sym.hasFlag(Flags.CASE) } @@ -409,7 +407,7 @@ class EntityFactory(val global: Global, val settings: doc.Settings) { extractor } /* Refined types */ case RefinedType(parents, defs) => - appendTypes0((if (parents.length > 1) parents.toList - definitions.ObjectClass.tpe else parents.toList), " with ") + appendTypes0((if (parents.length > 1) parents filterNot (_ == ObjectClass.tpe) else parents), " with ") if (!defs.isEmpty) { nameBuffer append " {...}" // TODO: actually print the refinement } diff --git a/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala b/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala index a7c9f596d3..450c307092 100644 --- a/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala +++ b/src/compiler/scala/tools/nsc/doc/model/comment/CommentFactory.scala @@ -58,116 +58,115 @@ final class CommentFactory(val reporter: Reporter) { parser => * @param comment The raw comment string (including start and end markers) to be parsed. * @param pos The position of the comment in source. */ def parse(comment: String, pos: Position): Comment = { - /** The cleaned raw comment as a list of lines. Cleaning removes comment start and end markers, line start markers * and unnecessary whitespace. */ val cleaned: List[String] = { def cleanLine(line: String): Option[String] = { line.trim match { - case CleanCommentLine(ctl) => Some(ctl) - case "" => - None - case tl => - reporter.warning(pos, "Comment has no start-of-line marker ('*')") - Some(tl) - }} - comment.trim.stripPrefix("/*").stripSuffix("*/").lines.toList flatMap (cleanLine(_)) - } - - /** Parses a comment (in the form of a list of lines) to a Comment instance, recursively on lines. To do so, it - * splits the whole comment into main body and tag bodies, then runs the `WikiParser` on each body before creating - * the comment instance. - * - * @param body The body of the comment parsed until now. - * @param tags All tags parsed until now. - * @param lastTagKey The last parsed tag, or `None` if the tag section hasn't started. Lines that are not tagged - * are part of the previous tag or, if none exists, of the body. - * @param remaining The lines that must still recursively be parsed. */ - def parse0(docBody: String, tags: Map[TagKey, List[String]], lastTagKey: Option[TagKey], remaining: List[String]): Comment = - remaining match { - - case SymbolTag(name, sym, body) :: ls => - val key = SymbolTagKey(name, sym) - val value = body :: ((tags get key) getOrElse Nil) - parse0(docBody, tags + (key -> value), Some(key), ls) - - case SimpleTag(name, body) :: ls => - val key = SimpleTagKey(name) - val value = body :: ((tags get key) getOrElse Nil) - parse0(docBody, tags + (key -> value), Some(key), ls) - - case line :: ls if (lastTagKey.isDefined) => - val key = lastTagKey.get - val value = - ((tags get key): @unchecked) match { - case Some(b :: bs) => (b + endOfLine + line) :: bs - case None => oops("lastTagKey set when no tag exists for key") - } - parse0(docBody, tags + (key -> value), lastTagKey, ls) + case CleanCommentLine(ctl) => Some(ctl) + case "" => + None + case tl => + reporter.warning(pos, "Comment has no start-of-line marker ('*')") + Some(tl) + } + } + comment.trim.stripPrefix("/*").stripSuffix("*/").lines.toList flatMap (cleanLine(_)) + } - case line :: ls => - val newBody = - if (docBody == "") line else docBody + endOfLine + line - parse0(newBody, tags, lastTagKey, ls) + /** Parses a comment (in the form of a list of lines) to a Comment instance, recursively on lines. To do so, it + * splits the whole comment into main body and tag bodies, then runs the `WikiParser` on each body before creating + * the comment instance. + * + * @param body The body of the comment parsed until now. + * @param tags All tags parsed until now. + * @param lastTagKey The last parsed tag, or `None` if the tag section hasn't started. Lines that are not tagged + * are part of the previous tag or, if none exists, of the body. + * @param remaining The lines that must still recursively be parsed. */ + def parse0(docBody: String, tags: Map[TagKey, List[String]], lastTagKey: Option[TagKey], remaining: List[String]): Comment = + remaining match { + + case SymbolTag(name, sym, body) :: ls => + val key = SymbolTagKey(name, sym) + val value = body :: tags.getOrElse(key, Nil) + parse0(docBody, tags + (key -> value), Some(key), ls) + + case SimpleTag(name, body) :: ls => + val key = SimpleTagKey(name) + val value = body :: tags.getOrElse(key, Nil) + parse0(docBody, tags + (key -> value), Some(key), ls) + + case line :: ls if (lastTagKey.isDefined) => + val key = lastTagKey.get + val value = + ((tags get key): @unchecked) match { + case Some(b :: bs) => (b + endOfLine + line) :: bs + case None => oops("lastTagKey set when no tag exists for key") + } + parse0(docBody, tags + (key -> value), lastTagKey, ls) - case Nil => + case line :: ls => + val newBody = + if (docBody == "") line else docBody + endOfLine + line + parse0(newBody, tags, lastTagKey, ls) - val bodyTags: mutable.Map[TagKey, List[Body]] = - mutable.Map((tags map { case (key, values) => key -> (values map (parseWiki(_, pos))) }).toSeq:_*) + case Nil => - def oneTag(key: SimpleTagKey): Option[Body] = - ((bodyTags remove key): @unchecked) match { - case Some(r :: rs) => - if (!rs.isEmpty) reporter.warning(pos, "Only one '@" + key.name + "' tag is allowed") - Some(r) - case None => None - } + val bodyTags: mutable.Map[TagKey, List[Body]] = + mutable.Map((tags map { case (key, values) => key -> (values map (parseWiki(_, pos))) }).toSeq:_*) - def allTags(key: SimpleTagKey): List[Body] = - (bodyTags remove key) getOrElse Nil - - def allSymsOneTag(key: TagKey): Map[String, Body] = { - val keys: Sequence[SymbolTagKey] = - bodyTags.keys.toSeq flatMap { - case stk: SymbolTagKey if (stk.name == key.name) => Some(stk) - case stk: SimpleTagKey if (stk.name == key.name) => - reporter.warning(pos, "Tag '@" + stk.name + "' must be followed by a symbol name") - None - case _ => None - } - val pairs: Sequence[(String, Body)] = - for (key <- keys) yield { - val bs = (bodyTags remove key).get - if (bs.length > 1) - reporter.warning(pos, "Only one '@" + key.name + "' tag for symbol " + key.symbol + " is allowed") - (key.symbol, bs.head) - } - Map.empty[String, Body] ++ pairs + def oneTag(key: SimpleTagKey): Option[Body] = + ((bodyTags remove key): @unchecked) match { + case Some(r :: rs) => + if (!rs.isEmpty) reporter.warning(pos, "Only one '@" + key.name + "' tag is allowed") + Some(r) + case None => None } - val com = new Comment { - val body = parseWiki(docBody, pos) - val authors = allTags(SimpleTagKey("author")) - val see = allTags(SimpleTagKey("see")) - val result = oneTag(SimpleTagKey("return")) - val throws = allSymsOneTag(SimpleTagKey("throws")) - val valueParams = allSymsOneTag(SimpleTagKey("param")) - val typeParams = allSymsOneTag(SimpleTagKey("tparam")) - val version = oneTag(SimpleTagKey("version")) - val since = oneTag(SimpleTagKey("since")) - val todo = allTags(SimpleTagKey("todo")) - val deprecated = oneTag(SimpleTagKey("deprecated")) - } + def allTags(key: SimpleTagKey): List[Body] = + (bodyTags remove key) getOrElse Nil + + def allSymsOneTag(key: TagKey): Map[String, Body] = { + val keys: Seq[SymbolTagKey] = + bodyTags.keysIterator.toSeq flatMap { + case stk: SymbolTagKey if (stk.name == key.name) => Some(stk) + case stk: SimpleTagKey if (stk.name == key.name) => + reporter.warning(pos, "Tag '@" + stk.name + "' must be followed by a symbol name") + None + case _ => None + } + val pairs: Seq[(String, Body)] = + for (key <- keys) yield { + val bs = (bodyTags remove key).get + if (bs.length > 1) + reporter.warning(pos, "Only one '@" + key.name + "' tag for symbol " + key.symbol + " is allowed") + (key.symbol, bs.head) + } + Map.empty[String, Body] ++ pairs + } + + val com = new Comment { + val body = parseWiki(docBody, pos) + val authors = allTags(SimpleTagKey("author")) + val see = allTags(SimpleTagKey("see")) + val result = oneTag(SimpleTagKey("return")) + val throws = allSymsOneTag(SimpleTagKey("throws")) + val valueParams = allSymsOneTag(SimpleTagKey("param")) + val typeParams = allSymsOneTag(SimpleTagKey("tparam")) + val version = oneTag(SimpleTagKey("version")) + val since = oneTag(SimpleTagKey("since")) + val todo = allTags(SimpleTagKey("todo")) + val deprecated = oneTag(SimpleTagKey("deprecated")) + } - for (key <- bodyTags.keys) - reporter.warning(pos, "Tag '@" + key.name + "' is not recognised") + for ((key, _) <- bodyTags) + reporter.warning(pos, "Tag '@" + key.name + "' is not recognised") - com + com - } + } parse0("", Map.empty, None, cleaned) - } /** Parses a string containing wiki syntax into a `Comment` object. Note that the string is assumed to be clean: diff --git a/src/library/scala/xml/Utility.scala b/src/library/scala/xml/Utility.scala index 2613da6c61..012f368aa8 100644 --- a/src/library/scala/xml/Utility.scala +++ b/src/library/scala/xml/Utility.scala @@ -122,7 +122,7 @@ object Utility extends AnyRef with parsing.TokenTests * entity. */ final def unescape(ref: String, s: StringBuilder): StringBuilder = - (unescMap get ref) map (s append _) getOrElse null + (unescMap get ref) map (s append _) orNull /** * Returns a set of all namespaces used in a sequence of nodes -- cgit v1.2.3