Skip to content

Commit

Permalink
Make 'appendToComment' work on C-style comments (#165)
Browse files Browse the repository at this point in the history
Previously, `appendToComment` assumed there was either no existing comment or
an existing Javadoc comment. I ran into this situation when compiling Akka with
-Yrangepos enabled (hence the test).

This change makes `appendToComment` also work when there is an existing
C-style comment.
  • Loading branch information
raboof authored Mar 19, 2019
1 parent 4202973 commit 09c41a5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 8 deletions.
8 changes: 5 additions & 3 deletions plugin/src/main/scala/com/typesafe/genjavadoc/AST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ trait AST { this: TransformCake ⇒
def maybeSinceDot = if (since.endsWith(".")) " " else ". "
def render = s" * @deprecated ${msg}${maybeDot}Since $since${maybeSinceDot}"

def appendToComment(comment: Seq[String]): Seq[String] = comment match {
case Nil => List("/**", render, "*/")
case c => c.dropRight(1) ++ List(" *", render, "*/")
def appendToComment(comment: Seq[String]): Seq[String] = comment.toVector match {
case init :+ " */" =>
init ++ List(" *", render, " */")
case c =>
c ++ List("/**", render, "*/")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package akka.cluster;
public class ClusterRouterGroupSettings {
// not preceding
public ClusterRouterGroupSettings () { throw new RuntimeException(); }
/**
* @deprecated useRole has been replaced with useRoles. Since 2.5.4.
*/
public scala.Option<java.lang.String> useRole () { throw new RuntimeException(); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package akka.cluster;

class ClusterRouterGroupSettings {
@deprecated("useRole has been replaced with useRoles", since = "2.5.4")
def useRole: Option[String] = ???
}
18 changes: 18 additions & 0 deletions plugin/src/test/scala/com/typesafe/genjavadoc/RangePosSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.typesafe.genjavadoc

import java.io.File

import com.typesafe.genjavadoc.util.CompilerSpec

class RangePosSpec extends CompilerSpec {
/** Sources to compile. */
override def sources: Seq[String] = Seq(
new File("src/test/resources/input/rangepos/akka/cluster/ClusterRouterGroupSettings.scala").getAbsolutePath
)

override def rangepos = true

/** Root directory that contains expected Java output. */
override def expectedPath: String =
new File("src/test/resources/expected_output/rangepos").getAbsolutePath
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SignatureSpec {
val scalac = new GenJavadocCompiler(Seq(
s"genjavadoc:out=$docPath",
"genjavadoc:suppressSynthetic=false"
))
), rangepos = false)

val javaSources = expectedClasses.map{cls =>
docPath + "/" + cls.replace(".", "/") + ".java"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ trait CompilerSpec {
/** Extra plugin arguments. */
def extraSettings: Seq[String] = Seq.empty

/** whether to enable -Yrangepos */
def rangepos: Boolean = false

@Test def compileSourcesAndGenerateExpectedOutput(): Unit = {
val doc = IO.tempDir("java")
val docPath = doc.getAbsolutePath
val defaultSettings = Seq(s"out=$docPath", "suppressSynthetic=false")
val scalac = new GenJavadocCompiler((defaultSettings ++ extraSettings).map{ kv =>
s"genjavadoc:$kv"
})
}, rangepos)

scalac.compile(sources)
assertFalse("Scala compiler reported errors", scalac.reporter.hasErrors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.nsc.{Global, Settings}

/** An instance of the Scala compiler with the genjavadoc plugin enabled
* @param params additional parameters to pass to the compiler
* @param pluginOptions additional parameters to pass to the compiler
*/
class GenJavadocCompiler(params: Seq[String]) {
class GenJavadocCompiler(pluginOptions: Seq[String], rangepos: Boolean) {

private val settings = new Settings

settings.Yrangepos.value = rangepos

val reporter = new ConsoleReporter(settings)
private val global = new Global(settings, reporter) {
override protected def loadRoughPluginsList() =
Expand All @@ -28,7 +31,7 @@ class GenJavadocCompiler(params: Seq[String]) {
}

settings.outputDirs.setSingleOutput(AbstractFile.getDirectory(target))
settings.pluginOptions.value = params.toList
settings.pluginOptions.value = pluginOptions.toList

def compile(fileNames: Seq[String]): Unit = {
val run = new global.Run
Expand Down

0 comments on commit 09c41a5

Please sign in to comment.