Skip to content

Commit bf263eb

Browse files
committed
fix: make pc actions work for re-exported symbols
1 parent 7b12a1c commit bf263eb

File tree

11 files changed

+110
-12
lines changed

11 files changed

+110
-12
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
15361536
* @param selectorPredicate A test to find the selector to use.
15371537
* @return The symbols imported.
15381538
*/
1539-
def importedSymbols(imp: Import,
1539+
def importedSymbols(imp: ImportOrExport,
15401540
selectorPredicate: untpd.ImportSelector => Boolean = util.common.alwaysTrue)
15411541
(using Context): List[Symbol] =
15421542
imp.selectors.find(selectorPredicate) match

compiler/src/dotty/tools/dotc/typer/Namer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ class Namer { typer: Typer =>
13431343
val ddef = tpd.DefDef(forwarder.asTerm, prefss => {
13441344
val forwarderCtx = ctx.withOwner(forwarder)
13451345
val (pathRefss, methRefss) = prefss.splitAt(extensionParamsCount(path.tpe.widen))
1346-
val ref = path.appliedToArgss(pathRefss).select(sym.asTerm)
1346+
val ref = path.appliedToArgss(pathRefss).select(sym.asTerm).withSpan(span.focus)
13471347
val rhs = ref.appliedToArgss(adaptForwarderParams(Nil, sym.info, methRefss))
13481348
.etaExpandCFT(using forwarderCtx)
13491349
if forwarder.isInlineMethod then

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ object MetalsInteractive:
135135
(sym, sym.info, None)
136136
)
137137

138-
case (imp: Import) :: _ =>
138+
case (imp: ImportOrExport) :: _ =>
139139
importedSymbols(imp, _.span.contains(pos.span)).map(sym =>
140140
(sym, sym.info, None)
141141
)

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ trait PcCollector[T]:
6363
o.span.exists && o.span.point == named.symbol.owner.span.point
6464
)
6565

66-
def soughtOrOverride(sym: Symbol) =
66+
def soughtOrOverride(sym0: Symbol) =
67+
val sym = if sym0.is(Flags.Exported) then sym0.sourceSymbol else sym0
6768
sought(sym) || sym.allOverriddenSymbols.exists(sought(_))
6869

6970
def soughtTreeFilter(tree: Tree): Boolean =
@@ -76,7 +77,7 @@ trait PcCollector[T]:
7677
case df: NamedDefTree
7778
if soughtOrOverride(df.symbol) && !df.symbol.isSetter =>
7879
true
79-
case imp: Import if owners(imp.expr.symbol) => true
80+
case imp: ImportOrExport if owners(imp.expr.symbol) => true
8081
case _ => false
8182

8283
def soughtFilter(f: Symbol => Boolean): Boolean =
@@ -115,11 +116,13 @@ trait PcCollector[T]:
115116
*/
116117
case ident: Ident if ident.isCorrectSpan && filter(ident) =>
117118
// symbols will differ for params in different ext methods, but source pos will be the same
118-
if soughtFilter(_.sourcePos == ident.symbol.sourcePos)
119+
val symbol = if ident.symbol.is(Flags.Exported) then ident.symbol.sourceSymbol else ident.symbol
120+
if soughtFilter(_.sourcePos == symbol.sourcePos)
119121
then
120122
occurrences + collect(
121123
ident,
122-
ident.sourcePos
124+
ident.sourcePos,
125+
Some(symbol)
123126
)
124127
else occurrences
125128
/**
@@ -228,7 +231,7 @@ trait PcCollector[T]:
228231
* For traversing import selectors:
229232
* import scala.util.<<Try>>
230233
*/
231-
case imp: Import if filter(imp) =>
234+
case imp: ImportOrExport if filter(imp) =>
232235
imp.selectors
233236
.collect {
234237
case sel: ImportSelector

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ trait PcSymbolSearch:
4949
lazy val soughtSymbols: Option[(Set[Symbol], SourcePosition)] =
5050
soughtSymbols(path)
5151

52-
def soughtSymbols(path: List[Tree]): Option[(Set[Symbol], SourcePosition)] =
52+
private def soughtSymbols(path: List[Tree]): Option[(Set[Symbol], SourcePosition)] =
5353
val sought = path match
5454
/* reference of an extension paramter
5555
* extension [EF](<<xs>>: List[EF])
@@ -148,7 +148,7 @@ trait PcSymbolSearch:
148148
/* Import selectors:
149149
* import scala.util.Tr@@y
150150
*/
151-
case (imp: Import) :: _ if imp.span.contains(pos.span) =>
151+
case (imp: ImportOrExport) :: _ if imp.span.contains(pos.span) =>
152152
imp
153153
.selector(pos.span)
154154
.map(sym => (symbolAlternatives(sym), sym.sourcePos))

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ class WithCompilationUnit(
7676
}
7777
else Set.empty
7878
val all =
79-
if sym.is(Flags.ModuleClass) then
79+
if sym.is(Flags.Exported) then
80+
Set(sym, sym.sourceSymbol)
81+
else if sym.is(Flags.ModuleClass) then
8082
Set(sym, sym.companionModule, sym.companionModule.companion)
8183
else if sym.isClass then
8284
Set(sym, sym.companionModule, sym.companion.moduleClass)

presentation-compiler/src/main/dotty/tools/pc/utils/InteractiveEnrichments.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ object InteractiveEnrichments extends CommonMtagsEnrichments:
336336
end enclosedChildren
337337
end extension
338338

339-
extension (imp: Import)
339+
extension (imp: ImportOrExport)
340340
def selector(span: Span)(using Context): Option[Symbol] =
341341
for sel <- imp.selectors.find(_.span.contains(span))
342342
yield imp.expr.symbol.info.member(sel.name).symbol

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

+28
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,31 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
513513
|val foo_name = foo.na@@me
514514
|""".stripMargin
515515
)
516+
517+
@Test def i7256 =
518+
check(
519+
"""|object Test:
520+
| def <<methodA>>: Unit = ???
521+
|export Test.me@@thodA
522+
|""".stripMargin
523+
)
524+
525+
@Test def `i7256-2` =
526+
check(
527+
"""|object Test:
528+
| def <<methodA>>: Unit = ???
529+
| def methodB: Unit = ???
530+
|export Test.{me@@thodA, methodB}
531+
|""".stripMargin
532+
)
533+
534+
@Test def `i7256-3` =
535+
check(
536+
"""|object Test:
537+
| def <<methodA>>: Unit = ???
538+
| def methodB: Unit = ???
539+
|export Test.{methodA, methodB}
540+
|
541+
|val i = met@@hodA
542+
|""".stripMargin
543+
)

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

+40
Original file line numberDiff line numberDiff line change
@@ -1486,5 +1486,45 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
14861486
|""".stripMargin
14871487
)
14881488

1489+
@Test def i7256 =
1490+
check(
1491+
"""|package a
1492+
|object Test:
1493+
| def <<methodA>>: Unit = ???
1494+
|export Test.<<me@@thodA>>
1495+
|val i = <<methodA>>
1496+
|""".stripMargin
1497+
)
1498+
1499+
@Test def `i7256-2` =
1500+
check(
1501+
"""|object Test:
1502+
| def <<methodA>>: Unit = ???
1503+
| def methodB: Unit = ???
1504+
|export Test.{<<me@@thodA>>, methodB}
1505+
|val i = <<methodA>>
1506+
|""".stripMargin
1507+
)
1508+
1509+
1510+
@Test def `i7256-3` =
1511+
check(
1512+
"""|object Test:
1513+
| def <<methodA>>: Unit = ???
1514+
| def methodB: Unit = ???
1515+
|export Test.<<methodA>>
1516+
|val i = <<met@@hodA>>
1517+
|val j = <<methodA>>
1518+
|""".stripMargin
1519+
)
1520+
1521+
@Test def `i7256-4` =
1522+
check(
1523+
"""|object Test:
1524+
| class <<Foo>>
1525+
| object <<Foo>>
1526+
|export Test.<<F@@oo>>
1527+
|""".stripMargin
1528+
)
14891529

14901530
end DocumentHighlightSuite

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverDefnSuite.scala

+17
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,20 @@ class HoverDefnSuite extends BaseHoverSuite:
262262
"""|type Numeric: Numeric
263263
|""".stripMargin.hover
264264
)
265+
266+
@Test def i7256 =
267+
check(
268+
"""|object Test:
269+
| def methodA: Unit = ???
270+
|export Test.me@@thodA
271+
|""".stripMargin,
272+
"""|**Expression type**:
273+
|```scala
274+
|=> Unit
275+
|```
276+
|**Symbol signature**:
277+
|```scala
278+
|def methodA: Unit
279+
|```
280+
|""".stripMargin
281+
)

presentation-compiler/test/dotty/tools/pc/tests/tokens/SemanticTokensSuite.scala

+8
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,12 @@ class SemanticTokensSuite extends BaseSemanticTokensSuite:
431431
| <<usage>>/*method*/[<<Option>>/*class,abstract*/[<<Int>>/*class,abstract*/]](<<_>>/*parameter,readonly*/.<<typeArg>>/*method*/[<<Some>>/*class*/[<<Int>>/*class,abstract*/]].<<value>>/*variable,readonly*/.<<inferredTypeArg>>/*method*/[<<String>>/*type*/]("str"))
432432
|}
433433
|""".stripMargin
434+
)
435+
436+
@Test def i7256 =
437+
check(
438+
"""|object <<Test>>/*class*/:
439+
| def <<methodA>>/*method,definition*/: <<Unit>>/*class,abstract*/ = <<???>>/*method*/
440+
|export <<Test>>/*class*/.<<methodA>>/*method*/
441+
|""".stripMargin
434442
)

0 commit comments

Comments
 (0)