Skip to content

Commit 4fdd03a

Browse files
committed
Fix export go-to-definition
1 parent b2ba6dc commit 4fdd03a

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

compiler/src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ object Symbols extends SymUtils {
341341
}
342342
else if (denot.isPrimaryConstructor)
343343
denot.owner.sourceSymbol
344+
else if denot.is(Exported) then
345+
denot.info.finalResultType match
346+
case TypeAlias(target: NamedType) => target.symbol.sourceSymbol // exported type
347+
case target: NamedType => target.symbol.sourceSymbol // exported term
348+
case info => this
344349
else this
345350

346351
/** The position of this symbol, or NoSpan if the symbol was not loaded

compiler/src/dotty/tools/dotc/interactive/SourceTree.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ case class SourceTree(tree: tpd.Import | tpd.NameTree, source: SourceFile) {
4242
(treeSpan.end - nameLength, treeSpan.end)
4343
Span(start, end, start)
4444
}
45-
source.atSpan(position)
45+
// Don't widen the span, only narrow.
46+
// E.g. The star in a wildcard export is 1 character,
47+
// and that is the span of the type alias that results from it
48+
// but the name may very well be larger, which we don't want.
49+
val span1 = if treeSpan.contains(position) then position else treeSpan
50+
source.atSpan(span1)
4651
}
4752
case _ =>
4853
NoSourcePosition

presentation-compiler/src/main/dotty/tools/pc/MetalsInteractive.scala

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@ object MetalsInteractive:
109109
* Returns the list of tuple enclosing symbol and
110110
* the symbol's expression type if possible.
111111
*/
112-
@tailrec
113112
def enclosingSymbolsWithExpressionType(
114113
path: List[Tree],
115114
pos: SourcePosition,
116115
indexed: IndexedContext,
117116
skipCheckOnName: Boolean = false
118117
): List[(Symbol, Type)] =
119118
import indexed.ctx
120-
path match
119+
@tailrec def go(path: List[Tree]): List[(Symbol, Type)] = path match
121120
// For a named arg, find the target `DefDef` and jump to the param
122121
case NamedArg(name, _) :: Apply(fn, _) :: _ =>
123122
val funSym = fn.symbol
@@ -206,12 +205,7 @@ object MetalsInteractive:
206205

207206
case path @ head :: tail =>
208207
if head.symbol.is(Synthetic) then
209-
enclosingSymbolsWithExpressionType(
210-
tail,
211-
pos,
212-
indexed,
213-
skipCheckOnName
214-
)
208+
go(tail)
215209
else if head.symbol != NoSymbol then
216210
if skipCheckOnName ||
217211
MetalsInteractive.isOnName(
@@ -225,21 +219,17 @@ object MetalsInteractive:
225219
* https://github.com/lampepfl/dotty/issues/15937
226220
*/
227221
else if head.isInstanceOf[TypeTree] then
228-
enclosingSymbolsWithExpressionType(tail, pos, indexed)
222+
go(tail)
229223
else Nil
230224
else
231225
val recovered = recoverError(head, indexed)
232226
if recovered.isEmpty then
233-
enclosingSymbolsWithExpressionType(
234-
tail,
235-
pos,
236-
indexed,
237-
skipCheckOnName
238-
)
227+
go(tail)
239228
else recovered.map(sym => (sym, sym.info))
240229
end if
241230
case Nil => Nil
242-
end match
231+
end go
232+
go(path).map((sym, tp) => (sym.sourceSymbol, tp))
243233
end enclosingSymbolsWithExpressionType
244234

245235
import dotty.tools.pc.utils.MtagsEnrichments.*

presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
199199
|""".stripMargin
200200
)
201201

202+
@Test def `export` =
203+
check(
204+
"""object enumerations:
205+
| trait <<SymbolKind>>
206+
| trait CymbalKind
207+
|
208+
|object all:
209+
| export enumerations.*
210+
|
211+
|@main def hello =
212+
| import all.SymbolKind
213+
| import enumerations.CymbalKind
214+
|
215+
| val x = new Symbo@@lKind {}
216+
| val y = new CymbalKind {}
217+
|""".stripMargin
218+
)
219+
202220
@Test def `named-arg-local` =
203221
check(
204222
"""|

0 commit comments

Comments
 (0)