Skip to content

Commit 517754c

Browse files
authored
Merge pull request #5240 from dotty-staging/fix/constructor-fixes
Fix go to definition on constructor calls
2 parents 3351f8b + f43654d commit 517754c

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ object Interactive {
7676
}.getOrElse(fn.symbol)
7777
}
7878

79+
// For constructor calls, return the `<init>` that was selected
80+
case _ :: (_: New) :: (select: Select) :: _ =>
81+
select.symbol
82+
7983
case _ =>
8084
enclosingTree(path).symbol
8185
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ case class SourceTree(tree: tpd.NameTree, source: SourceFile) {
2121
if (treePos.isZeroExtent || tree.name.toTermName == nme.ERROR)
2222
NoSourcePosition
2323
else {
24-
val nameLength = tree.name.stripModuleClassSuffix.show.toString.length
24+
// Constructors are named `<init>` in the trees, but `this` in the source.
25+
val nameLength = tree.name match {
26+
case nme.CONSTRUCTOR => nme.this_.toString.length
27+
case other => other.stripModuleClassSuffix.show.toString.length
28+
}
2529
val position = {
2630
// FIXME: This is incorrect in some cases, like with backquoted identifiers,
2731
// see https://github.com/lampepfl/dotty/pull/1634#issuecomment-257079436

language-server/test/dotty/tools/languageserver/DefinitionTest.scala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ class DefinitionTest {
1919
.definition(m5 to m6, List(m1 to m2))
2020
}
2121

22+
@Test def caseClassDefinition0: Unit = {
23+
withSources(
24+
code"""case class ${m1}Foo${m2}(x: String) {
25+
def ${m3}this${m4}(x: Int) = ${m5}this${m6}(x.toString)
26+
}""",
27+
code"""class Bar {
28+
val foo: ${m7}Foo${m8} = ${m9}Foo${m10}("hello")
29+
val bar: ${m11}Foo${m12} = new ${m13}Foo${m14}(2)
30+
}"""
31+
) .definition(m1 to m2, List(m1 to m2))
32+
.definition(m3 to m4, List(m3 to m4))
33+
.definition(m5 to m6, List(m1 to m2))
34+
.definition(m7 to m8, List(m1 to m2))
35+
.definition(m9 to m10, List(m1 to m2))
36+
.definition(m11 to m12, List(m1 to m2))
37+
.definition(m13 to m14, List(m3 to m4))
38+
}
39+
2240
@Test def valDefinition0: Unit = {
2341
withSources(
2442
code"class Foo { val ${m1}x$m2 = 0; ${m3}x$m4 }",
@@ -134,6 +152,18 @@ class DefinitionTest {
134152
.definition(m11 to m12, List(m1 to m2))
135153
}
136154

155+
@Test def goToParamApply: Unit = {
156+
withSources(
157+
code"""case class Foo(${m1}x${m2}: Int, ${m3}y${m4}: String)""",
158+
code"""object Bar {
159+
Foo(${m5}x${m6} = 1, ${m7}y${m8} = "hello")
160+
}"""
161+
) .definition(m1 to m2, List(m1 to m2))
162+
.definition(m3 to m4, List(m3 to m4))
163+
.definition(m5 to m6, List(m1 to m2))
164+
.definition(m7 to m8, List(m3 to m4))
165+
}
166+
137167
@Test def goToParamCopyMethod: Unit = {
138168

139169
withSources(
@@ -148,4 +178,15 @@ class DefinitionTest {
148178
.definition(m9 to m10, List(m3 to m4))
149179
}
150180

181+
@Test def constructorDefinition: Unit = {
182+
withSources(
183+
code"class ${m1}A${m2}(x: Boolean) { def ${m3}this${m4}(x: Int) = ${m5}this${m6}(x > 0) }",
184+
code"object B { val a0 = new ${m7}A${m8}(true); val a1 = new ${m9}A${m10}(0) }"
185+
) .definition(m1 to m2, List(m1 to m2))
186+
.definition(m3 to m4, List(m3 to m4))
187+
.definition(m5 to m6, List(m1 to m2))
188+
.definition(m7 to m8, List(m1 to m2))
189+
.definition(m9 to m10, List(m3 to m4))
190+
}
191+
151192
}

0 commit comments

Comments
 (0)