diff options
author | Paul Phillips <paulp@improving.org> | 2013-09-27 14:38:38 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2013-09-27 14:42:24 -0700 |
commit | 7d62df035cd4393c73e7530e1cad1130e79d90c6 (patch) | |
tree | 8a643ea65861b7bceb8d22397d991401b8716079 /src/reflect | |
parent | 5a8cd09819f58adcb866722f48b00066d23e7a82 (diff) | |
download | scala-7d62df035cd4393c73e7530e1cad1130e79d90c6.tar.gz scala-7d62df035cd4393c73e7530e1cad1130e79d90c6.tar.bz2 scala-7d62df035cd4393c73e7530e1cad1130e79d90c6.zip |
Updating Position call sites.
Calling position factories rather than instantiating these
particular classes. Not calling deprecated methods. Added a few
position combinator methods.
Diffstat (limited to 'src/reflect')
5 files changed, 33 insertions, 17 deletions
diff --git a/src/reflect/scala/reflect/api/Position.scala b/src/reflect/scala/reflect/api/Position.scala index 4d5f1f6e67..2019e2f1d9 100644 --- a/src/reflect/scala/reflect/api/Position.scala +++ b/src/reflect/scala/reflect/api/Position.scala @@ -50,11 +50,11 @@ trait Position extends Attachments { /** If opaque range, make this position transparent. */ def makeTransparent: Pos - /** The start of the position's range, error if not a range position. */ + /** The start of the position's range, or the point if not a range position. */ def start: Int /** The start of the position's range, or point if not a range position. */ - def startOrPoint: Int + @deprecated("Use `start` instead", "2.11.0") def startOrPoint: Int /** The point (where the ^ is) of the position, which is easiest to access using the [[line]] and [[column]] values. * The [[lineContent line content]] is also available. @@ -67,13 +67,13 @@ trait Position extends Attachments { */ def pointOrElse(default: Int): Int - /** The end of the position's range, error if not a range position. + /** The end of the position's range, or the point if not a range position. */ def end: Int /** The end of the position's range, or point if not a range position. */ - def endOrPoint: Int + @deprecated("Use `end` instead", "2.11.0") def endOrPoint: Int /** The same position with a different start value (if a range). */ diff --git a/src/reflect/scala/reflect/internal/Positions.scala b/src/reflect/scala/reflect/internal/Positions.scala index eef8159ad4..01fba1efc1 100644 --- a/src/reflect/scala/reflect/internal/Positions.scala +++ b/src/reflect/scala/reflect/internal/Positions.scala @@ -42,7 +42,7 @@ trait Positions extends api.Positions { self: SymbolTable => if (useOffsetPositions) default else { val ranged = trees filter (_.pos.isRange) if (ranged.isEmpty) if (focus) default.focus else default - else new RangePosition(default.source, (ranged map (_.pos.start)).min, default.point, (ranged map (_.pos.end)).max) + else Position.range(default.source, (ranged map (_.pos.start)).min, default.point, (ranged map (_.pos.end)).max) } } @@ -80,8 +80,8 @@ trait Positions extends api.Positions { self: SymbolTable => } def rangePos(source: SourceFile, start: Int, point: Int, end: Int): Position = - if (useOffsetPositions) new OffsetPosition(source, point) - else new RangePosition(source, start, point, end) + if (useOffsetPositions) Position.offset(source, point) + else Position.range(source, start, point, end) def validatePositions(tree: Tree) { if (useOffsetPositions) return @@ -156,7 +156,7 @@ trait Positions extends api.Positions { self: SymbolTable => /** A free range from `lo` to `hi` */ private def free(lo: Int, hi: Int): Range = - Range(new RangePosition(null, lo, lo, hi), EmptyTree) + Range(Position.range(null, lo, lo, hi), EmptyTree) /** The maximal free range */ private lazy val maxFree: Range = free(0, Int.MaxValue) diff --git a/src/reflect/scala/reflect/internal/Trees.scala b/src/reflect/scala/reflect/internal/Trees.scala index b1fc27f832..0976b993ca 100644 --- a/src/reflect/scala/reflect/internal/Trees.scala +++ b/src/reflect/scala/reflect/internal/Trees.scala @@ -22,10 +22,10 @@ trait Trees extends api.Trees { self: SymbolTable => protected def treeStatus(t: Tree, enclosingTree: Tree = null) = { val parent = if (enclosingTree eq null) " " else " P#%5s".format(enclosingTree.id) - "[L%4s%8s] #%-6s %-15s %-10s // %s".format(t.pos.safeLine, parent, t.id, t.pos.show, t.shortClass, treeLine(t)) + "[L%4s%8s] #%-6s %-15s %-10s // %s".format(t.pos.line, parent, t.id, t.pos.show, t.shortClass, treeLine(t)) } protected def treeSymStatus(t: Tree) = { - val line = if (t.pos.isDefined) "line %-4s".format(t.pos.safeLine) else " " + val line = if (t.pos.isDefined) "line %-4s".format(t.pos.line) else " " "#%-5s %s %-10s // %s".format(t.id, line, t.shortClass, if (t.symbol ne NoSymbol) "(" + t.symbol.fullLocationString + ")" else treeLine(t) diff --git a/src/reflect/scala/reflect/internal/util/Position.scala b/src/reflect/scala/reflect/internal/util/Position.scala index 8e7df28167..15cfda26b5 100644 --- a/src/reflect/scala/reflect/internal/util/Position.scala +++ b/src/reflect/scala/reflect/internal/util/Position.scala @@ -169,6 +169,20 @@ private[util] trait InternalPositionImpl { def focus: Position = if (this.isRange) asOffset(point) else this def focusEnd: Position = if (this.isRange) asOffset(end) else this + /** If you have it in for punctuation you might not like these methods. + * However I think they're aptly named. + * + * | means union + * ^ means "the point" (look, it's a caret) + * |^ means union, taking the point of the rhs + * ^| means union, taking the point of the lhs + */ + def |(that: Position, poses: Position*): Position = poses.foldLeft(this | that)(_ | _) + def |(that: Position): Position = this union that + def ^(point: Int): Position = this withPoint point + def |^(that: Position): Position = (this | that) ^ that.point + def ^|(that: Position): Position = (this | that) ^ this.point + def union(pos: Position): Position = ( if (!pos.isRange) this else if (this.isRange) copyRange(start = start min pos.start, end = end max pos.end) @@ -246,9 +260,9 @@ private[util] trait DeprecatedPosition { @deprecated("Use `withSource(source)` and `withShift`", "2.11.0") def withSource(source: SourceFile, shift: Int): Position = this withSource source withShift shift - @deprecated("Use `start`", "2.11.0") + @deprecated("Use `start` instead", "2.11.0") def startOrPoint: Int = if (isRange) start else point - @deprecated("Use `end`", "2.11.0") + @deprecated("Use `end` instead", "2.11.0") def endOrPoint: Int = if (isRange) end else point } diff --git a/src/reflect/scala/reflect/internal/util/SourceFile.scala b/src/reflect/scala/reflect/internal/util/SourceFile.scala index 6bb4cf3f0e..f36c50bff1 100644 --- a/src/reflect/scala/reflect/internal/util/SourceFile.scala +++ b/src/reflect/scala/reflect/internal/util/SourceFile.scala @@ -23,7 +23,7 @@ abstract class SourceFile { def length : Int def position(offset: Int) : Position = { assert(offset < length, file + ": " + offset + " >= " + length) - new OffsetPosition(this, offset) + Position.offset(this, offset) } def offsetToLine(offset: Int): Int @@ -34,7 +34,7 @@ abstract class SourceFile { */ def positionInUltimateSource(position: Position) = position override def toString() = file.name - def dbg(offset: Int) = (new OffsetPosition(this, offset)).dbgString + def dbg(offset: Int) = Position.offset(this, offset).toString def path = file.path def lineToString(index: Int): String = @@ -98,7 +98,7 @@ class ScriptSourceFile(underlying: BatchSourceFile, content: Array[Char], overri override def positionInUltimateSource(pos: Position) = if (!pos.isDefined) super.positionInUltimateSource(pos) - else pos.withSource(underlying, start) + else pos withSource underlying withShift start } /** a file whose contents do not change over time */ @@ -155,10 +155,12 @@ class BatchSourceFile(val file : AbstractFile, val content0: Array[Char]) extend */ def offsetToLine(offset: Int): Int = { val lines = lineIndices - def findLine(lo: Int, hi: Int, mid: Int): Int = - if (offset < lines(mid)) findLine(lo, mid - 1, (lo + mid - 1) / 2) + def findLine(lo: Int, hi: Int, mid: Int): Int = ( + if (mid < lo || hi < mid) mid // minimal sanity check - as written this easily went into infinite loopyland + else if (offset < lines(mid)) findLine(lo, mid - 1, (lo + mid - 1) / 2) else if (offset >= lines(mid + 1)) findLine(mid + 1, hi, (mid + 1 + hi) / 2) else mid + ) lastLine = findLine(0, lines.length, lastLine) lastLine } |