Skip to content

Commit 941b7b8

Browse files
committed
Make CommentSyntax option path based in Scaladoc
CommentSyntax option now allows to set the comment syntax based on a passed path prefix. This will allow to use a correct syntax for specific files in one project, for example a wiki syntax for scalalib and md syntax for dottylib.
1 parent 99c2b96 commit 941b7b8

File tree

7 files changed

+69
-28
lines changed

7 files changed

+69
-28
lines changed

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ object ScaladocConfigs {
18301830
.add(Revision("main"))
18311831
.add(ExternalMappings(List(javaExternalMapping)))
18321832
.add(DocRootContent(docRootFile.toString))
1833-
.add(CommentSyntax("wiki"))
1833+
.add(CommentSyntax(List("wiki")))
18341834
.add(VersionsDictionaryUrl("https://scala-lang.org/api/versions.json"))
18351835
.add(DocumentSyntheticTypes(true))
18361836
.add(SnippetCompiler(List(

project/ScaladocGeneration.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object ScaladocGeneration {
4545
def key: String = "-source-links"
4646
}
4747

48-
case class CommentSyntax(value: String) extends Arg[String] {
48+
case class CommentSyntax(value: List[String]) extends Arg[List[String]] {
4949
def key: String = "-comment-syntax"
5050
}
5151

scaladoc/src/dotty/tools/scaladoc/DocContext.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ case class NavigationNode(name: String, dri: DRI, nested: Seq[NavigationNode])
7474
case class DocContext(args: Scaladoc.Args, compilerContext: CompilerContext):
7575
lazy val sourceLinks = SourceLinks.load(args.sourceLinks, args.revision)(using compilerContext)
7676

77+
lazy val commentSyntaxArgs = tasty.comments.CommentSyntaxArgs.load(args.defaultSyntax)(using compilerContext)
78+
7779
lazy val snippetCompilerArgs = snippets.SnippetCompilerArgs.load(args.snippetCompiler)(using compilerContext)
7880

7981
lazy val snippetChecker = snippets.SnippetChecker(args)(using compilerContext)

scaladoc/src/dotty/tools/scaladoc/Scaladoc.scala

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ import dotty.tools.scaladoc.Inkuire
1818
import dotty.tools.scaladoc.Inkuire._
1919

2020
object Scaladoc:
21-
enum CommentSyntax:
22-
case Wiki
23-
case Markdown
24-
25-
object CommentSyntax:
26-
def parse(str: String) = str match
27-
case "wiki" => Some(CommentSyntax.Wiki)
28-
case "markdown" => Some(CommentSyntax.Markdown)
29-
case _ => None
30-
31-
val default = CommentSyntax.Markdown
32-
3321
case class Args(
3422
name: String,
3523
tastyDirs: Seq[File] = Nil,
@@ -41,7 +29,7 @@ object Scaladoc:
4129
projectVersion: Option[String] = None,
4230
projectLogo: Option[String] = None,
4331
projectFooter: Option[String] = None,
44-
defaultSyntax: CommentSyntax = CommentSyntax.Markdown,
32+
defaultSyntax: List[String] = Nil,
4533
sourceLinks: List[String] = Nil,
4634
revision: Option[String] = None,
4735
externalMappings: List[ExternalDocLink] = Nil,
@@ -164,12 +152,6 @@ object Scaladoc:
164152
report.warning("Destination is not provided, please provide '-d' parameter pointing to directory where docs should be created")
165153
File("output")
166154

167-
val parseSyntax: CommentSyntax = syntax.nonDefault.fold(CommentSyntax.default){ str =>
168-
CommentSyntax.parse(str).getOrElse{
169-
report.error(s"unrecognized value for -syntax option: $str")
170-
CommentSyntax.default
171-
}
172-
}
173155
val legacySourceLinkList = if legacySourceLink.get.nonEmpty then List(legacySourceLink.get) else Nil
174156

175157
val externalMappings =
@@ -219,7 +201,7 @@ object Scaladoc:
219201
projectVersion.nonDefault,
220202
projectLogo.nonDefault,
221203
projectFooter.nonDefault,
222-
parseSyntax,
204+
syntax.get,
223205
sourceLinks.get ++ legacySourceLinkList,
224206
revision.nonDefault,
225207
externalMappings ++ legacyExternalMappings,

scaladoc/src/dotty/tools/scaladoc/ScaladocSettings.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class ScaladocSettings extends SettingGroup with AllScalaSettings:
4040
val legacySourceLink: Setting[String] =
4141
StringSetting("-doc-source-url", "sources", "Legacy option from Scala 2. Use -source-links instead.", "")
4242

43-
val syntax: Setting[String] =
44-
StringSetting("-comment-syntax", "syntax", "Syntax of the comment used", "")
43+
val syntax: Setting[List[String]] =
44+
MultiStringSetting("-comment-syntax", "syntax", tasty.comments.CommentSyntaxArgs.usage)
4545

4646
val revision: Setting[String] =
4747
StringSetting("-revision", "revision", "Revision (branch or ref) used to build project project", "")

scaladoc/src/dotty/tools/scaladoc/tasty/ScalaDocSupport.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package tasty
33

44
import scala.jdk.CollectionConverters._
55

6-
import dotty.tools.scaladoc.Scaladoc.CommentSyntax
7-
import dotty.tools.scaladoc.tasty.comments.Comment
6+
import dotty.tools.scaladoc.tasty.comments.{Comment, CommentSyntax}
7+
import dotty.tools.scaladoc.tasty.SymOps.source
88

99
import scala.quoted._
1010

@@ -17,14 +17,16 @@ object ScaladocSupport:
1717
val commentSyntax =
1818
preparsed.syntax.headOption match {
1919
case Some(commentSetting) =>
20-
CommentSyntax.parse(commentSetting).getOrElse {
20+
CommentSyntax.CommentSyntaxParser.parse(commentSetting).getOrElse {
2121
val msg = s"not a valid comment syntax: $commentSetting, defaulting to Markdown syntax."
2222
// we should update pos with span from documentation
2323
pos.fold(report.warning(msg))(report.warning(msg, _))
2424

2525
CommentSyntax.default
2626
}
27-
case None => summon[DocContext].args.defaultSyntax
27+
case None =>
28+
val path = sym.source.map(_.path)
29+
summon[DocContext].commentSyntaxArgs.get(path)
2830
}
2931

3032
val parser = commentSyntax match {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package dotty.tools.scaladoc
2+
package tasty.comments
3+
4+
import java.nio.file.Path
5+
6+
enum CommentSyntax:
7+
case Wiki
8+
case Markdown
9+
10+
object CommentSyntax:
11+
object CommentSyntaxParser extends ArgParser[CommentSyntax]:
12+
def parse(s: String): Either[String, CommentSyntax] = s match
13+
case "wiki" => Right(CommentSyntax.Wiki)
14+
case "markdown" => Right(CommentSyntax.Markdown)
15+
case _ => Left(s"No such syntax found.")
16+
17+
val default = CommentSyntax.Markdown
18+
19+
case class CommentSyntaxArgs(csFormats: PathBased[CommentSyntax]):
20+
def get(path: Option[Path]): CommentSyntax =
21+
path
22+
.flatMap(p => csFormats.get(p).map(_.elem))
23+
.getOrElse(CommentSyntax.default)
24+
25+
object CommentSyntaxArgs:
26+
val usage =
27+
"""
28+
|Comment Syntax arguments provide a way to set comment syntax for specified paths.
29+
|
30+
|This setting accepts list of arguments in format:
31+
|args := arg{,arg}
32+
|arg := [path=]syntax
33+
|where `path` is a prefix of the path to source files that will have a specific comment syntax set and `syntax` specifies the one used.
34+
|
35+
|If the path is not present, the argument will be used as the default for all unmatched paths.
36+
|
37+
|Available syntaxes:
38+
|markdown
39+
|wiki
40+
|
41+
""".stripMargin
42+
43+
def load(args: List[String])(using CompilerContext): CommentSyntaxArgs = {
44+
PathBased.parse[CommentSyntax](args)(using CommentSyntax.CommentSyntaxParser) match {
45+
case PathBased.ParsingResult(errors, res) =>
46+
if errors.nonEmpty then report.warning(s"""
47+
|Got following errors during comment syntax args parsing:
48+
|$errors
49+
|
50+
|$usage
51+
|""".stripMargin
52+
)
53+
CommentSyntaxArgs(res)
54+
}
55+
}

0 commit comments

Comments
 (0)