Skip to content

Commit

Permalink
Merge pull request #274 from lrytz/t220
Browse files Browse the repository at this point in the history
use point positions in comment index
  • Loading branch information
lrytz authored Mar 16, 2021
2 parents bd654c7 + 4c9d2f4 commit 52b8fdb
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/main/scala-2.11/com/typesafe/genjavadoc/Comments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait Comments extends BaseComments { this: TransformCake =>
def foundStarComment(start: Int, end: Int) = try {
val str = docBuffer.toString
val pos = new RangePosition(unit.source, start, start, end)
comments += pos -> Comment(pos, str)
comments += pos.point -> Comment(pos, str)
true
} finally {
docBuffer = null
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala-2.12/com/typesafe/genjavadoc/Comments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait Comments extends BaseComments { this: TransformCake =>
override def newScanner = new parser.ScaladocUnitScanner(unit, Nil) {
override def registerDocComment(str: String, pos: Position) = {
super.registerDocComment(str, pos)
comments += pos -> Comment(pos, str)
comments += pos.point -> Comment(pos, str)
}
}
}.parse()
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala-2.13/com/typesafe/genjavadoc/Comments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait Comments extends BaseComments { this: TransformCake =>
override def newScanner = new parser.ScaladocUnitScanner(unit, Nil) {
override def registerDocComment(str: String, pos: Position) = {
super.registerDocComment(str, pos)
comments += pos -> Comment(pos, str)
comments += pos.point -> Comment(pos, str)
}
}
}.parse()
Expand Down
9 changes: 2 additions & 7 deletions src/main/scala/com/typesafe/genjavadoc/BaseComments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,8 @@ trait BaseComments { this: TransformCake =>
}
}

implicit val positionOrdering: Ordering[Position] = new Ordering[Position] {
def compare(a: Position, b: Position) =
if (a.end < b.start) -1
else if (a.start > b.end) 1
else 0
}
var comments = TreeMap[Position, Comment]()
implicit val positionOrdering: Ordering[Position] = Ordering.by(_.point)
var comments = TreeMap[Int, Comment]()

// This is overriden in the Scala Version Specific Comments.scala
protected def parseComments(): Unit
Expand Down
7 changes: 4 additions & 3 deletions src/main/scala/com/typesafe/genjavadoc/BasicTransform.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.typesafe.genjavadoc

import scala.reflect.internal.Flags
import scala.reflect.internal.util.Position

trait BasicTransform { this: TransformCake =>
import global._
Expand Down Expand Up @@ -36,7 +37,7 @@ trait BasicTransform { this: TransformCake =>
try code finally keep = old
}

private var pos: Position = rangePos(unit.source, 0, 0, 0)
private var pos: Position = Position.offset(unit.source, 0)

private var templateMaxPos: Position = pos
private var prevTemplateMaxPos: Position = pos
Expand All @@ -50,8 +51,8 @@ trait BasicTransform { this: TransformCake =>
val ret = if (tp.isDefined) {
val old = pos
pos = max(tp, prevTemplateMaxPos)
if (old.precedes(pos)) {
(positions.from(old) intersect positions.to(pos)).toSeq.map(comments).filter(Scaladoc).lastOption match {
if (old <= pos) {
(positions.from(old.point) intersect positions.to(pos.point)).toSeq.map(comments).filter(Scaladoc).lastOption match {
case Some(c) => c.text // :+ s"// found in '${between(old, pos)}'"
case None =>
// s"// empty '${between(old, pos)}' (${pos.lineContent}:${pos.column})" ::
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/expected_output/basic/Foo$.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* A simple class
*/
public class Foo$ {
/**
* Static reference to the singleton instance of this Scala object.
*/
public static final Foo$ MODULE$ = null;
public Foo$ () { throw new RuntimeException(); }
/**
* @return something something
*/
public java.lang.String bar () { throw new RuntimeException(); }
}
9 changes: 9 additions & 0 deletions src/test/resources/expected_output/basic/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* A simple class
*/
public class Foo {
/**
* @return something something
*/
static public java.lang.String bar () { throw new RuntimeException(); }
}
11 changes: 11 additions & 0 deletions src/test/resources/input/basic/root.scala
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
class AtTheRoot

/**
* A simple class
*/
object Foo {

/**
* @return something something
*/
def bar: String = ""
}
18 changes: 0 additions & 18 deletions src/test/scala/com/typesafe/genjavadoc/RangePosSpec.scala

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/scala/com/typesafe/genjavadoc/SignatureSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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,16 +20,13 @@ 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 @@ -9,12 +9,10 @@ import scala.tools.nsc.{Global, Settings}
/** An instance of the Scala compiler with the genjavadoc plugin enabled
* @param pluginOptions additional parameters to pass to the compiler
*/
class GenJavadocCompiler(pluginOptions: Seq[String], rangepos: Boolean) {
class GenJavadocCompiler(pluginOptions: Seq[String]) {

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 Down

0 comments on commit 52b8fdb

Please sign in to comment.