Skip to content

Commit

Permalink
bugfix: Document highlight on anon-fun parameters in Scala 2
Browse files Browse the repository at this point in the history
Previosuly anonymous function params with the same name were not distinguished
  • Loading branch information
jkciesluk authored and tgodzik committed Nov 24, 2023
1 parent f905aa4 commit ea38fec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
19 changes: 15 additions & 4 deletions mtags/src/main/scala-2/scala/meta/internal/pc/PcCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,22 @@ abstract class PcCollector[T](
* For comprehensions have two owners, one for the enumerators and one for
* yield. This is a heuristic to find that out.
*/
def isForComprehensionOwner(named: NameTree) =
soughtNames(named.name) &&
named.symbol.owner.isAnonymousFunction && owners.exists(owner =>
pos.isDefined && owner.pos.isDefined && owner.pos.point == named.symbol.owner.pos.point
def isForComprehensionOwner(named: NameTree) = {
if (named.symbol.pos.isDefined) {
def alternativeSymbol = sought.exists(symbol =>
symbol.name == named.name &&
symbol.pos.isDefined &&
symbol.pos.start == named.symbol.pos.start
)
def sameOwner = {
val owner = named.symbol.owner
owner.isAnonymousFunction && owners.exists(o =>
pos.isDefined && o.pos.isDefined && o.pos.point == owner.pos.point
)
}
alternativeSymbol && sameOwner
} else false
}

def soughtOrOverride(sym: Symbol) =
sought(sym) || sym.allOverriddenSymbols.exists(sought(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,21 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite {
|}""".stripMargin
)

check(
"for-comp-bind2",
"""
|object Main {
| val abc = for {
| <<f@@oo>> <- List(1)
| baz = <<foo>> + 1
| a <- List(<<foo>>, 123)
| } yield {
| val x = <<foo>> + baz
| x
| }
|}""".stripMargin
)

check(
"for-comp-map",
"""|object Main {
Expand Down Expand Up @@ -993,4 +1008,16 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite {
|}""".stripMargin
)

check(
"map-bind6",
"""
|object Main {
| List("test").map {
| case <<string@@Name>>: String if <<stringName>>.startsWith("a") => <<stringName>> + "a"
| case stringName: String if stringName.startsWith("b") => stringName + "b"
| case stringName: String => stringName + "c"
| }
|}""".stripMargin
)

}

0 comments on commit ea38fec

Please sign in to comment.