Skip to content

Commit cf00a5b

Browse files
committed
Migrate end comment to end marker
1 parent 2d60032 commit cf00a5b

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package parsing
55
import scala.annotation.internal.sharable
66
import scala.collection.mutable.ListBuffer
77
import scala.collection.immutable.BitSet
8+
import scala.util.matching.Regex
89
import util.{ SourceFile, SourcePosition, NoSourcePosition }
910
import Tokens._
1011
import Scanners._
@@ -628,6 +629,14 @@ object Parsers {
628629
str.isEmpty ||
629630
testChar(from, str.head) && testChars(from + 1, str.tail)
630631

632+
class charSeqAt(from: Int, to: Int) extends CharSequence {
633+
override def charAt(i: Int) = source.content()(from + i)
634+
override def length() = to - from
635+
override def subSequence(from1: Int, to1: Int) = charSeqAt(from + from1, from + to1)
636+
override def toString() = 0.until(length()).map(charAt).mkString
637+
}
638+
def testRegex(from: Int, r: Regex): Option[Regex.Match] = r.findPrefixMatchOf(charSeqAt(from, source.length))
639+
631640
def skipBlanks(idx: Int, step: Int = 1): Int =
632641
if (testChar(idx, c => c == ' ' || c == '\t' || c == Chars.CR)) skipBlanks(idx + step, step)
633642
else idx
@@ -755,7 +764,21 @@ object Parsers {
755764
else ":"
756765
val (startClosing, endClosing) = closingElimRegion()
757766
patch(source, Span(startOpening, endOpening), openingPatchStr)
758-
patch(source, Span(startClosing, endClosing), "")
767+
var patched = false
768+
if testChars(endClosing, "//") then
769+
val maybeEnd = skipBlanks(endClosing + 2)
770+
if testChars(maybeEnd, "end ") then
771+
val r = raw"(.*)\R".r
772+
testRegex(maybeEnd + 4, r) match {
773+
case Some(m) =>
774+
val s = m.group(1).trim
775+
if !s.exists(_.isWhitespace) then
776+
patch(source, Span(startClosing, maybeEnd + 4 + s.length), s"end $s")
777+
patched = true
778+
case _ =>
779+
}
780+
end if
781+
if !patched then patch(source, Span(startClosing, endClosing), "")
759782
}
760783
t
761784
}

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CompilationTests {
7474
compileFile("tests/rewrites/i9632.scala", defaultOptions.and("-indent", "-rewrite")),
7575
compileFile("tests/rewrites/i11895.scala", defaultOptions.and("-indent", "-rewrite")),
7676
compileFile("tests/rewrites/i12340.scala", unindentOptions.and("-rewrite")),
77+
compileFile("tests/rewrites/i12340b.scala", defaultOptions.and("-indent", "-rewrite")),
7778
).checkRewrites()
7879
}
7980

tests/rewrites/i12340b.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
/** My class!
3+
*/
4+
class C:
5+
def f = 42
6+
end C
7+
8+
class D:
9+
def g = 17
10+
// end not actually D

tests/rewrites/i12340b.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
/** My class!
3+
*/
4+
class C {
5+
def f = 42
6+
} // end C
7+
8+
class D {
9+
def g = 17
10+
} // end not actually D

0 commit comments

Comments
 (0)