11package 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
313import 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 \n caused 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 ]
0 commit comments