Skip to content

Commit bb2aadb

Browse files
authored
improvement: use heuristic to figure out nameSpan if pointDelta too big (#22484)
This is a hacky workaround when `pointDelta >= SyntheticPointDelta`, see: https://github.com/scala/scala3/blob/6d9b0f14f18b89d1719fd5346284eae9cc6e39bc/compiler/src/dotty/tools/dotc/util/Spans.scala#L161 Fix for: scalameta/metals#3053
2 parents d60a914 + a625254 commit bb2aadb

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,11 @@ object Trees {
461461
else if qualifier.span.exists && qualifier.span.start > span.point then // right associative
462462
val realName = name.stripModuleClassSuffix.lastPart
463463
Span(span.start, span.start + realName.length, point)
464-
else
465-
Span(point, span.end, point)
464+
else if span.pointMayBeIncorrect then
465+
val realName = name.stripModuleClassSuffix.lastPart
466+
val probablyPoint = span.end - realName.length
467+
Span(probablyPoint, span.end, probablyPoint)
468+
else Span(point, span.end, point)
466469
else span
467470
}
468471

compiler/src/dotty/tools/dotc/util/Spans.scala

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ object Spans {
5959
if (poff == SyntheticPointDelta) start else start + poff
6060
}
6161

62+
def pointMayBeIncorrect =
63+
pointDelta == 0 && end - start >= SyntheticPointDelta
64+
6265
/** The difference between point and start in this span */
6366
def pointDelta: Int =
6467
(coords >>> (StartEndBits * 2)).toInt

presentation-compiler/test/dotty/tools/pc/tests/highlight/DocumentHighlightSuite.scala

+24
Original file line numberDiff line numberDiff line change
@@ -1462,5 +1462,29 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
14621462
|""".stripMargin
14631463
)
14641464

1465+
@Test def i3053 =
1466+
check(
1467+
s"""import Aaaa.*
1468+
|
1469+
|def classDef2(cdef: List[Int]): Int = {
1470+
| def aaa(ddef: Thicket2): List[Int] = ddef match {
1471+
| case Thicket2(_) => ???
1472+
| }
1473+
|${("//" + "x" * 64 + "\n") * 64}
1474+
| 1
1475+
|}.<<showing2>>("aaa")
1476+
|
1477+
|case class Thicket2(trees: List[Int]) {}
1478+
|
1479+
|object Aaaa {
1480+
| extension [T](x: T)
1481+
| def <<show@@ing2>>[U](aaa: String): T = {
1482+
| x
1483+
| }
1484+
|}
1485+
|
1486+
|""".stripMargin
1487+
)
1488+
14651489

14661490
end DocumentHighlightSuite

0 commit comments

Comments
 (0)