Skip to content

Commit 1a1f424

Browse files
committed
Improve handling of nameSpan
Even with the fix to enum spans, we could still get a case where a point to a name is missing (maybe because there is a long comment between the start of the definition and the name). The current fix makes the computation succeed more often, but it is not perfect.
1 parent c8265f1 commit 1a1f424

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,23 @@ object Trees {
351351
if (span.exists) {
352352
val point = span.point
353353
if (rawMods.is(Synthetic) || name.toTermName == nme.ERROR) Span(point)
354-
else Span(point, point + name.stripModuleClassSuffix.lastPart.length, point)
354+
else {
355+
val realName = name.stripModuleClassSuffix.lastPart.toString
356+
val nameStart =
357+
if (point != span.start) point
358+
else {
359+
// Point might be too far away from start to be recorded. In this case we fall back to scanning
360+
// forwards from the start offset for the name.
361+
// Note: This might be inaccurate since scanning might hit accidentally the same
362+
// name (e.g. in a comment) before finding the real definition.
363+
// To make this behavior more robust we'd have to change the trees for definitions to contain
364+
// a fully positioned Ident in place of a name.
365+
val idx = source.content().indexOfSlice(realName, point)
366+
if (idx >= 0) idx
367+
else point // use `point` anyway. This is important if no source exists so scanning fails
368+
}
369+
Span(point, point + realName.length, point)
370+
}
355371
}
356372
else span
357373
}

0 commit comments

Comments
 (0)