Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5401 from dotty-staging/topic/ide-implementation
IDE: Support `textDocument/implementation`
- Loading branch information
Showing
5 changed files
with
217 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
language-server/test/dotty/tools/languageserver/ImplementationTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dotty.tools.languageserver | ||
|
||
import dotty.tools.languageserver.util.Code._ | ||
|
||
import org.junit.Test | ||
|
||
class ImplementationTest { | ||
|
||
@Test def implMethodFromTrait: Unit = { | ||
code"""trait A { | ||
def ${m1}foo${m2}(x: Int): String | ||
} | ||
class B extends A { | ||
override def ${m3}foo${m4}(x: Int): String = "" | ||
}""".withSource | ||
.implementation(m1 to m2, List(m3 to m4)) | ||
.implementation(m3 to m4, List(m3 to m4)) | ||
} | ||
|
||
@Test def implMethodFromTrait0: Unit = { | ||
code"""trait A { | ||
def ${m1}foo${m2}(x: Int): String | ||
} | ||
class B extends A { | ||
override def ${m3}foo${m4}(x: Int): String = "" | ||
} | ||
class C extends B { | ||
override def ${m5}foo${m6}(x: Int): String = "" | ||
}""".withSource | ||
.implementation(m1 to m2, List(m3 to m4, m5 to m6)) | ||
.implementation(m3 to m4, List(m3 to m4, m5 to m6)) | ||
.implementation(m5 to m6, List(m5 to m6)) | ||
} | ||
|
||
@Test def extendsTrait: Unit = { | ||
code"""trait ${m1}A${m2} | ||
class ${m3}B${m4} extends ${m5}A${m6}""".withSource | ||
.implementation(m1 to m2, List(m3 to m4)) | ||
.implementation(m3 to m4, List(m3 to m4)) | ||
.implementation(m5 to m6, List(m3 to m4)) | ||
} | ||
|
||
@Test def extendsClass: Unit = { | ||
code"""class ${m1}A${m2} | ||
class ${m3}B${m4} extends ${m5}A${m6}""".withSource | ||
.implementation(m1 to m2, List(m1 to m2, m3 to m4)) | ||
.implementation(m3 to m4, List(m3 to m4)) | ||
.implementation(m5 to m6, List(m1 to m2, m3 to m4)) | ||
} | ||
|
||
@Test def objExtendsTrait: Unit = { | ||
code"""trait ${m1}A${m2} | ||
object ${m3}B${m4} extends ${m5}A${m6}""".withSource | ||
.implementation(m1 to m2, List(m3 to m4)) | ||
.implementation(m3 to m4, List(m3 to m4)) | ||
.implementation(m5 to m6, List(m3 to m4)) | ||
} | ||
|
||
@Test def defineAbstractType: Unit = { | ||
code"""trait A { type ${m1}T${m2} } | ||
trait B extends A { type ${m3}T${m4} = Int }""".withSource | ||
.implementation(m1 to m2, List(m3 to m4)) | ||
.implementation(m3 to m4, List(m3 to m4)) | ||
} | ||
|
||
@Test def innerClass: Unit = { | ||
code"""trait A { trait ${m1}AA${m2} } | ||
class B extends A { | ||
class ${m3}AB${m4} extends ${m5}AA${m6} | ||
}""".withSource | ||
.implementation(m1 to m2, List(m3 to m4)) | ||
.implementation(m3 to m4, List(m3 to m4)) | ||
.implementation(m5 to m6, List(m3 to m4)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
language-server/test/dotty/tools/languageserver/util/actions/Implementation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dotty.tools.languageserver.util.actions | ||
|
||
import dotty.tools.languageserver.util.embedded.CodeMarker | ||
import dotty.tools.languageserver.util.{CodeRange, PositionContext} | ||
|
||
import org.eclipse.lsp4j.Location | ||
|
||
import org.junit.Assert.assertEquals | ||
|
||
import scala.collection.JavaConverters._ | ||
|
||
/** | ||
* An action requesting the implementations of the symbol inside `range`. | ||
* This action corresponds to the `textDocument/implementation` method of the Language Server | ||
* Protocol. | ||
* | ||
* @param range The range of position for which to request implementations. | ||
* @param expected The expected results. | ||
*/ | ||
class Implementation(override val range: CodeRange, expected: List[CodeRange]) extends ActionOnRange { | ||
|
||
private implicit val LocationOrdering: Ordering[Location] = Ordering.by(_.toString) | ||
|
||
override def onMarker(marker: CodeMarker): Exec[Unit] = { | ||
val expectedLocations = expected.map(_.toLocation) | ||
val results: Seq[org.eclipse.lsp4j.Location] = server.implementation(marker.toTextDocumentPositionParams).get().asScala | ||
|
||
assertEquals(expectedLocations.length, results.length) | ||
expectedLocations.sorted.zip(results.sorted).foreach { | ||
assertEquals(_, _) | ||
} | ||
} | ||
|
||
override def show: PositionContext.PosCtx[String] = | ||
s"Implementation(${range.show}, $expected)" | ||
} |