diff options
author | Paul Phillips <paulp@improving.org> | 2013-03-06 07:39:19 -0800 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-03-09 11:59:07 -0800 |
commit | c6ca941ccc017a8869f4def717cfeb640f965077 (patch) | |
tree | e21038cc55d3a4231544d148913504a754eafdf8 /src/scaladoc/scala/tools/nsc/doc/DocFactory.scala | |
parent | 9094822181c398b945b7f30ac1e2b05da9796f53 (diff) | |
download | scala-c6ca941ccc017a8869f4def717cfeb640f965077.tar.gz scala-c6ca941ccc017a8869f4def717cfeb640f965077.tar.bz2 scala-c6ca941ccc017a8869f4def717cfeb640f965077.zip |
Moved scaladoc sources into separate directory.
This change is not externally visible. It moves the scaladoc
sources into src/scaladoc and adds an ant target for building
them. The compilation products are still packaged into
scala-compiler.jar as before, but with a small change to
build.xml a separate jar can be created instead.
Diffstat (limited to 'src/scaladoc/scala/tools/nsc/doc/DocFactory.scala')
-rw-r--r-- | src/scaladoc/scala/tools/nsc/doc/DocFactory.scala | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/scaladoc/scala/tools/nsc/doc/DocFactory.scala b/src/scaladoc/scala/tools/nsc/doc/DocFactory.scala new file mode 100644 index 0000000000..b4d2adaad4 --- /dev/null +++ b/src/scaladoc/scala/tools/nsc/doc/DocFactory.scala @@ -0,0 +1,132 @@ +/* NSC -- new Scala compiler + * Copyright 2007-2013 LAMP/EPFL + * @author David Bernard, Manohar Jonnalagedda + */ + +package scala.tools.nsc +package doc + +import scala.util.control.ControlThrowable +import reporters.Reporter +import scala.reflect.internal.util.BatchSourceFile + +/** A documentation processor controls the process of generating Scala + * documentation, which is as follows. + * + * * A simplified compiler instance (with only the front-end phases enabled) + * * is created, and additional `sourceless` comments are registered. + * * Documentable files are compiled, thereby filling the compiler's symbol table. + * * A documentation model is extracted from the post-compilation symbol table. + * * A generator is used to transform the model into the correct final format (HTML). + * + * A processor contains a single compiler instantiated from the processor's + * `settings`. Each call to `document` uses the same compiler instance with + * the same symbol table. In particular, this implies that the scaladoc site + * obtained from a call to `run` will contain documentation about files compiled + * during previous calls to the same processor's `run` method. + * + * @param reporter The reporter to which both documentation and compilation errors will be reported. + * @param settings The settings to be used by the documenter and compiler for generating documentation. + * + * @author Gilles Dubochet */ +class DocFactory(val reporter: Reporter, val settings: doc.Settings) { processor => + /** The unique compiler instance used by this processor and constructed from its `settings`. */ + object compiler extends ScaladocGlobal(settings, reporter) + + /** Creates a scaladoc site for all symbols defined in this call's `source`, + * as well as those defined in `sources` of previous calls to the same processor. + * @param source The list of paths (relative to the compiler's source path, + * or absolute) of files to document or the source code. */ + def makeUniverse(source: Either[List[String], String]): Option[Universe] = { + assert(settings.docformat.value == "html") + source match { + case Left(files) => + new compiler.Run() compile files + case Right(sourceCode) => + new compiler.Run() compileSources List(new BatchSourceFile("newSource", sourceCode)) + } + + if (reporter.hasErrors) + return None + + val extraTemplatesToDocument: Set[compiler.Symbol] = { + if (settings.docUncompilable.isDefault) Set() + else { + val uncompilable = new { + val global: compiler.type = compiler + val settings = processor.settings + } with Uncompilable { } + + compiler.docComments ++= uncompilable.comments + docdbg("" + uncompilable) + + uncompilable.templates + } + } + + val modelFactory = ( + new { override val global: compiler.type = compiler } + with model.ModelFactory(compiler, settings) + with model.ModelFactoryImplicitSupport + with model.ModelFactoryTypeSupport + with model.diagram.DiagramFactory + with model.CommentFactory + with model.TreeFactory + with model.MemberLookup { + override def templateShouldDocument(sym: compiler.Symbol, inTpl: DocTemplateImpl) = + extraTemplatesToDocument(sym) || super.templateShouldDocument(sym, inTpl) + } + ) + + modelFactory.makeModel match { + case Some(madeModel) => + if (!settings.scaladocQuietRun) + println("model contains " + modelFactory.templatesCount + " documentable templates") + Some(madeModel) + case None => + if (!settings.scaladocQuietRun) + println("no documentable class found in compilation units") + None + } + } + + object NoCompilerRunException extends ControlThrowable { } + + val documentError: PartialFunction[Throwable, Unit] = { + case NoCompilerRunException => + reporter.info(null, "No documentation generated with unsucessful compiler run", force = false) + case _: ClassNotFoundException => + () + } + + /** Generate document(s) for all `files` containing scaladoc documenataion. + * @param files The list of paths (relative to the compiler's source path, or absolute) of files to document. */ + def document(files: List[String]) { + def generate() = { + import doclet._ + val docletClass = Class.forName(settings.docgenerator.value) // default is html.Doclet + val docletInstance = docletClass.newInstance().asInstanceOf[Generator] + + docletInstance match { + case universer: Universer => + val universe = makeUniverse(Left(files)) getOrElse { throw NoCompilerRunException } + universer setUniverse universe + + docletInstance match { + case indexer: Indexer => indexer setIndex model.IndexModelFactory.makeIndex(universe) + case _ => () + } + case _ => () + } + docletInstance.generate() + } + + try generate() + catch documentError + } + + private[doc] def docdbg(msg: String) { + if (settings.Ydocdebug.value) + println(msg) + } +} |