From ea38fec3e27c11100c626c87de4ddb7587fe812c Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Wed, 22 Nov 2023 14:04:59 +0100 Subject: [PATCH] bugfix: Document highlight on anon-fun parameters in Scala 2 Previosuly anonymous function params with the same name were not distinguished --- .../scala/meta/internal/pc/PcCollector.scala | 19 ++++++++++--- .../highlight/DocumentHighlightSuite.scala | 27 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/PcCollector.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/PcCollector.scala index 730a56d046b..e7ed27fa62c 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/PcCollector.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/PcCollector.scala @@ -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(_)) diff --git a/tests/cross/src/test/scala/tests/highlight/DocumentHighlightSuite.scala b/tests/cross/src/test/scala/tests/highlight/DocumentHighlightSuite.scala index 4e9b11aefd5..975c746cb4e 100644 --- a/tests/cross/src/test/scala/tests/highlight/DocumentHighlightSuite.scala +++ b/tests/cross/src/test/scala/tests/highlight/DocumentHighlightSuite.scala @@ -836,6 +836,21 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite { |}""".stripMargin ) + check( + "for-comp-bind2", + """ + |object Main { + | val abc = for { + | <> <- List(1) + | baz = <> + 1 + | a <- List(<>, 123) + | } yield { + | val x = <> + baz + | x + | } + |}""".stripMargin + ) + check( "for-comp-map", """|object Main { @@ -993,4 +1008,16 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite { |}""".stripMargin ) + check( + "map-bind6", + """ + |object Main { + | List("test").map { + | case <>: String if <>.startsWith("a") => <> + "a" + | case stringName: String if stringName.startsWith("b") => stringName + "b" + | case stringName: String => stringName + "c" + | } + |}""".stripMargin + ) + }