Skip to content

Commit f1872e5

Browse files
authored
Merge pull request #10568 from romanowski/scala3doc/doc-context
Scala3doc/doc context
2 parents e1d9f9c + b135490 commit f1872e5

37 files changed

+528
-388
lines changed

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import core.Decorators._
99
import printing.Highlighting.{Blue, Red, Yellow}
1010
import printing.SyntaxHighlighting
1111
import Diagnostic._
12-
import util.SourcePosition
12+
import util.{ SourcePosition, NoSourcePosition }
1313
import util.Chars.{ LF, CR, FF, SU }
1414
import scala.annotation.switch
1515

@@ -112,9 +112,9 @@ trait MessageRendering {
112112
* @return separator containing error location and kind
113113
*/
114114
def posStr(pos: SourcePosition, diagnosticLevel: String, message: Message)(using Context): String =
115-
if (pos.exists) hl(diagnosticLevel)({
115+
if (pos.source != NoSourcePosition.source) hl(diagnosticLevel)({
116116
val pos1 = pos.nonInlined
117-
val file =
117+
val file = if !pos.exists then pos1.source.file.toString else
118118
s"${pos1.source.file.toString}:${pos1.line + 1}:${pos1.column}"
119119
val errId =
120120
if (message.errorId ne ErrorMessageID.NoExplanationID) {
Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,127 @@
11
package dotty.dokka
22

3+
import org.jetbrains.dokka._
4+
import org.jetbrains.dokka.DokkaSourceSetImpl
5+
import org.jetbrains.dokka.plugability.DokkaContext
6+
import java.io.File
7+
import java.nio.file.Files
8+
import java.nio.file.Path
9+
import java.nio.file.Paths
10+
11+
import collection.JavaConverters._
12+
import dotty.dokka.site.StaticSiteContext
313
import dotty.tools.dotc.core.Contexts._
14+
import dotty.tools.io.VirtualFile
15+
import dotty.tools.dotc.util.SourceFile
16+
import dotty.tools.dotc.util.SourcePosition
17+
import dotty.tools.dotc.util.Spans
18+
import java.io.ByteArrayOutputStream
19+
import java.io.PrintStream
20+
import scala.io.Codec
21+
22+
type CompilerContext = dotty.tools.dotc.core.Contexts.Context
23+
24+
given compilerContext(using docContext: DocContext) as CompilerContext =
25+
docContext.compilerContext
26+
27+
given docContextFromDokka(using dokkaContext: DokkaContext) as DocContext =
28+
dokkaContext.getConfiguration.asInstanceOf[DocContext]
29+
30+
val report = dotty.tools.dotc.report
31+
32+
def relativePath(p: Path)(using Context): Path =
33+
val root = Paths.get("").toAbsolutePath()
34+
val absPath = p.toAbsolutePath
35+
println(Seq(p, absPath, absPath.startsWith(root), root.relativize(absPath)))
36+
if absPath.startsWith(root) then root.relativize(p.toAbsolutePath()) else p
37+
38+
39+
def throwableToString(t: Throwable)(using CompilerContext): String =
40+
val os = new ByteArrayOutputStream
41+
t.printStackTrace(new PrintStream(os))
42+
val stLinkes = os.toString().linesIterator
43+
if ctx.settings.verbose.value then stLinkes.mkString("\n")
44+
else stLinkes.take(5).mkString("\n")
45+
46+
private def sourcePostionFor(f: File)(using CompilerContext) =
47+
val relPath = relativePath(f.toPath)
48+
val virtualFile = new VirtualFile(relPath.toString, relPath.toString)
49+
val sourceFile = new SourceFile(virtualFile, Codec.UTF8)
50+
SourcePosition(sourceFile, Spans.NoSpan)
51+
52+
// TODO (https://github.com/lampepfl/scala3doc/issues/238): provide proper error handling
53+
private def createMessage(
54+
msg: String, file: File, e: Throwable | Null)(using CompilerContext): String =
55+
val localizedMessage = s"$file: $msg"
56+
e match
57+
case null => localizedMessage
58+
case throwable: Throwable =>
59+
s"$localizedMessage \ncaused by: ${throwableToString(throwable)}"
60+
61+
extension (r: report.type):
62+
def error(m: String, f: File, e: Throwable | Null = null)(using CompilerContext): Unit =
63+
r.error(createMessage(m, f, e), sourcePostionFor(f))
64+
65+
def warn(m: String, f: File, e: Throwable)(using CompilerContext): Unit =
66+
r.warning(createMessage(m, f, e), sourcePostionFor(f))
67+
68+
def warn(m: String, f: File)(using CompilerContext): Unit =
69+
r.warning(createMessage(m, f, null), sourcePostionFor(f))
70+
71+
72+
case class DocContext(args: Scala3doc.Args, compilerContext: CompilerContext)
73+
extends DokkaConfiguration:
74+
override def getOutputDir: File = args.output
75+
override def getCacheRoot: File = null
76+
override def getOfflineMode: Boolean = false
77+
override def getFailOnWarning: Boolean = false
78+
override def getSourceSets: JList[DokkaSourceSet] = JList(mkSourceSet)
79+
override def getModules: JList[DokkaConfiguration.DokkaModuleDescription] = JList()
80+
override def getPluginsClasspath: JList[File] = JList()
81+
override def getModuleName(): String = "ModuleName"
82+
override def getModuleVersion(): String = ""
83+
84+
lazy val sourceLinks: SourceLinks = SourceLinks.load(using this)
85+
86+
lazy val displaySourceSets = getSourceSets.toDisplaySourceSet
87+
88+
val logger = new Scala3DocDokkaLogger(using compilerContext)
89+
90+
lazy val staticSiteContext = args.docsRoot.map(path => StaticSiteContext(
91+
File(path).getAbsoluteFile(),
92+
Set(mkSourceSet.asInstanceOf[SourceSetWrapper]),
93+
args,
94+
sourceLinks
95+
)(using compilerContext))
96+
97+
override def getPluginsConfiguration: JList[DokkaConfiguration.PluginConfiguration] =
98+
JList()
99+
100+
val mkSourceSet: DokkaSourceSet =
101+
new DokkaSourceSetImpl(
102+
/*displayName=*/ args.name,
103+
/*sourceSetID=*/ new DokkaSourceSetID(args.name, "main"),
104+
/*classpath=*/ JList(),
105+
/*sourceRoots=*/ JSet(),
106+
/*dependentSourceSets=*/ JSet(),
107+
/*samples=*/ JSet(),
108+
/*includes=*/ JSet(),
109+
/*includeNonPublic=*/ true,
110+
/* changed because of exception in reportUndocumentedTransformer - there's 'when' which doesnt match because it contains only KotlinVisbility cases */
111+
/*reportUndocumented=*/ false,
112+
// Now all our packages are empty from dokka perspective
113+
/*skipEmptyPackages=*/ false,
114+
/*skipDeprecated=*/ true,
115+
/*jdkVersion=*/ 8,
116+
/*sourceLinks=*/ JSet(),
117+
/*perPackageOptions=*/ JList(),
118+
/*externalDocumentationLinks=*/ JSet(),
119+
/*languageVersion=*/ null,
120+
/*apiVersion=*/ null,
121+
/*noStdlibLink=*/ true,
122+
/*noJdkLink=*/ true,
123+
/*suppressedFiles=*/ JSet(),
124+
/*suppressedFiles=*/ Platform.jvm
125+
).asInstanceOf[DokkaSourceSet] // Why I do need to cast here? Kotlin magic?
4126

5-
type DocContext = Context
127+
val sourceSet = mkSourceSet.asInstanceOf[SourceSetWrapper]

scala3doc/src/dotty/dokka/DottyDokkaConfig.scala

Lines changed: 0 additions & 55 deletions
This file was deleted.

scala3doc/src/dotty/dokka/DottyDokkaPlugin.scala

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import dotty.dokka.model.api._
2323
import org.jetbrains.dokka.CoreExtensions
2424
import org.jetbrains.dokka.base.DokkaBase
2525

26+
import dotty.dokka.site.NavigationCreator
2627
import dotty.dokka.site.SitePagesCreator
2728
import dotty.dokka.site.StaticSiteContext
28-
import dotty.dokka.site.RootIndexPageCreator
2929
import dotty.dokka.site.SiteResourceManager
3030
import dotty.dokka.site.StaticSiteLocationProviderFactory
3131

@@ -49,7 +49,7 @@ class DottyDokkaPlugin extends DokkaJavaPlugin:
4949
// Just turn off another translator since multiple overrides does not work
5050
val disableDescriptorTranslator = extend(
5151
_.extensionPoint(CoreExtensions.INSTANCE.getSourceToDocumentableTranslator)
52-
.fromRecipe(ctx => new ScalaModuleProvider(using ctx.docContext))
52+
.fromRecipe { case ctx @ given DokkaContext => new ScalaModuleProvider }
5353
.overrideExtension(dokkaBase.getDescriptorToDocumentableTranslator)
5454
.name("disableDescriptorTranslator")
5555
)
@@ -63,14 +63,14 @@ class DottyDokkaPlugin extends DokkaJavaPlugin:
6363

6464
val ourSignatureProvider = extend(
6565
_.extensionPoint(dokkaBase.getSignatureProvider)
66-
.fromRecipe(ctx =>
67-
new ScalaSignatureProvider(ctx.single(dokkaBase.getCommentsToContentConverter), ctx.getLogger)
68-
).overrideExtension(dokkaBase.getKotlinSignatureProvider)
66+
.fromRecipe{ case ctx @ given DokkaContext =>
67+
new ScalaSignatureProvider(ctx.single(dokkaBase.getCommentsToContentConverter))
68+
}.overrideExtension(dokkaBase.getKotlinSignatureProvider)
6969
)
7070

7171
val scalaResourceInstaller = extend(
7272
_.extensionPoint(dokkaBase.getHtmlPreprocessors)
73-
.fromRecipe(ctx => new ScalaResourceInstaller(ctx.args))
73+
.fromRecipe{ case ctx @ given DokkaContext => new ScalaResourceInstaller }
7474
.after(dokkaBase.getCustomResourceInstaller)
7575
)
7676

@@ -83,17 +83,14 @@ class DottyDokkaPlugin extends DokkaJavaPlugin:
8383

8484
val scalaDocumentableToPageTranslator = extend(
8585
_.extensionPoint(CoreExtensions.INSTANCE.getDocumentableToPageTranslator)
86-
.fromRecipe(ctx =>
86+
.fromRecipe { case ctx @ given DokkaContext =>
8787
new DocumentableToPageTranslator {
8888
override def invoke(module: DModule): ModulePageNode = ScalaPageCreator(
8989
ctx.single(dokkaBase.getCommentsToContentConverter),
90-
ctx.single(dokkaBase.getSignatureProvider),
91-
ctx.getConfiguration.asInstanceOf[DottyDokkaConfig].sourceLinks,
92-
ctx.getLogger
90+
ctx.single(dokkaBase.getSignatureProvider)
9391
).pageForModule(module)
9492
}
95-
)
96-
.overrideExtension(dokkaBase.getDocumentableToPageTranslator)
93+
}.overrideExtension(dokkaBase.getDocumentableToPageTranslator)
9794
)
9895

9996
val packageHierarchyTransformer = extend(
@@ -111,7 +108,7 @@ class DottyDokkaPlugin extends DokkaJavaPlugin:
111108

112109
val ourRenderer = extend(
113110
_.extensionPoint(CoreExtensions.INSTANCE.getRenderer)
114-
.fromRecipe(ctx => ScalaHtmlRenderer(ctx, ctx.args))
111+
.fromRecipe { case ctx @ given DokkaContext => new ScalaHtmlRenderer }
115112
.overrideExtension(dokkaBase.getHtmlRenderer)
116113
)
117114

@@ -129,7 +126,7 @@ class DottyDokkaPlugin extends DokkaJavaPlugin:
129126

130127
val customDocumentationProvider = extend(
131128
_.extensionPoint(dokkaBase.getHtmlPreprocessors)
132-
.fromRecipe(c => SitePagesCreator(c.siteContext))
129+
.fromRecipe{ case c @ given DokkaContext => new SitePagesCreator }
133130
.name("customDocumentationProvider")
134131
.ordered(
135132
before = Seq(
@@ -142,39 +139,36 @@ class DottyDokkaPlugin extends DokkaJavaPlugin:
142139
)
143140
)
144141

145-
val customIndexRootProvider = extend(
142+
val customNavigation = extend(
146143
_.extensionPoint(dokkaBase.getHtmlPreprocessors)
147-
.fromRecipe(c => RootIndexPageCreator(c.siteContext))
148-
.name("customIndexRootProvider")
144+
.fromRecipe{ case c @ given DokkaContext => new NavigationCreator }
145+
.name("customNavigation")
149146
.ordered(
150147
before = Seq(
151148
dokkaBase.getScriptsInstaller,
152149
dokkaBase.getStylesInstaller,
153150
),
154-
after = Seq(dokkaBase.getNavigationPageInstaller)
151+
after = Seq(customDocumentationProvider.getValue)
155152
)
153+
.overrideExtension(dokkaBase.getNavigationPageInstaller)
156154
)
157155

158156
val customDocumentationResources = extend(
159157
_.extensionPoint(dokkaBase.getHtmlPreprocessors)
160-
.fromRecipe(c => SiteResourceManager(c.siteContext))
158+
.fromRecipe{ case c @ given DokkaContext => new SiteResourceManager }
161159
.name("customDocumentationResources")
162160
.after(
163-
scalaEmbeddedResourceAppender.getValue
161+
scalaEmbeddedResourceAppender.getValue,
162+
customDocumentationProvider.getValue
164163
)
165164
)
166165

167166
val locationProvider = extend(
168167
_.extensionPoint(dokkaBase.getLocationProviderFactory)
169-
.fromRecipe(StaticSiteLocationProviderFactory(_))
168+
.fromRecipe { case c @ given DokkaContext => new StaticSiteLocationProviderFactory }
170169
.overrideExtension(dokkaBase.getLocationProvider)
171170
)
172171

173-
extension (ctx: DokkaContext):
174-
def siteContext: Option[StaticSiteContext] = ctx.getConfiguration.asInstanceOf[DottyDokkaConfig].staticSiteContext
175-
def args: Scala3doc.Args = ctx.getConfiguration.asInstanceOf[DottyDokkaConfig].args
176-
def docContext = ctx.getConfiguration.asInstanceOf[DottyDokkaConfig].docContext
177-
178172
// TODO (https://github.com/lampepfl/scala3doc/issues/232): remove once problem is fixed in Dokka
179173
extension [T] (builder: ExtensionBuilder[T]):
180174
def ordered(before: Seq[Extension[_, _, _]], after: Seq[Extension[_, _, _]]): ExtensionBuilder[T] =

scala3doc/src/dotty/dokka/Main.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import java.nio.file.Files
1313

1414
import dotty.tools.dotc.config.Settings._
1515
import dotty.tools.dotc.config.CommonScalaSettings
16-
import dotty.tools.dotc.report
1716
import dotty.tools.dotc.core.Contexts._
1817

1918
/** Main class for the doctool.
@@ -25,13 +24,13 @@ import dotty.tools.dotc.core.Contexts._
2524
*
2625
* - [](package.DottyDokkaPlugin) is our class that Dokka calls back and which
2726
* actually generates the documentation.
28-
* - [](package.DottyDokkaConfig) is our config for Dokka.
27+
* - [](package.DocContext) is our config for Dokka
2928
*/
3029
object Main:
3130
def main(args: Array[String]): Unit =
3231
try
3332
// We should create our own context here...
34-
val reporter = Scala3doc.run(args)(using (new ContextBase).initialCtx)
33+
val reporter = Scala3doc.run(args, (new ContextBase).initialCtx)
3534
// Sometimes jvm is hanging, so we want to be sure that we force shout down the jvm
3635
sys.exit(if reporter.hasErrors then 1 else 0)
3736
catch

0 commit comments

Comments
 (0)