Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prefer non-export definition locations #20252

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dotty.tools.dotc.ast.NavigateAST
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.ast.untpd
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Flags.ModuleClass
import dotty.tools.dotc.core.Flags.{Exported, ModuleClass}
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.InteractiveDriver
Expand Down Expand Up @@ -123,9 +123,12 @@ class PcDefinitionProvider(
case symbols @ (sym :: other) =>
val isLocal = sym.source == pos.source
if isLocal then
val defs =
Interactive.findDefinitions(List(sym), driver, false, false).filter(_.source == sym.source)
defs.headOption match
val (exportedDefs, otherDefs) =
Interactive.findDefinitions(List(sym), driver, false, false)
.filter(_.source == sym.source)
.partition(_.tree.symbol.is(Exported))

otherDefs.headOption.orElse(exportedDefs.headOption) match
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if instead of returning the head option we returned all the locations, in that List(loc) below, so that then downstream can show users a choice of location (i.e. the source of the export and the original)?

Other than that lgtm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, that makes sense. However, for this particular case it will cause an inconsistency in Metals. If the definition and user's cursor are not in the same file, pc will use Metals's symbol search, and that search will not be able to find the export <smth>.*. So I am not sure that it would be better.

case Some(srcTree) =>
val pos = srcTree.namePos
pos.toLocation match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
|""".stripMargin
)

@Test def exportTermExtension =
check(
"""|package a
|class Test extends A {
| assert("Hello".fo@@o == "HelloFoo")
|}
|
|trait A {
| export B.*
|}
|
|object B {
| extension (value: String) def <<foo>>: String = s"${value}Foo"
|}
|""".stripMargin
)

@Test def `named-arg-local` =
check(
"""|
Expand Down
Loading