Skip to content

Commit

Permalink
Fix navigation to test on click (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rattlehead931 authored Sep 30, 2024
1 parent a74f45f commit b2aaf31
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/io/kotest/plugin/intellij/KotestTestFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class KotestTestFinder : TestFinder {
}

/**
* This is used by the navivation menu to determine if it should show "navigate to tests" or
* "nagivate to test subjects". Depending on the response, [findClassesForTest] or
* This is used by the navigation menu to determine if it should show "navigate to tests" or
* "navigate to test subjects". Depending on the response, [findClassesForTest] or
* [findTestsForClass] will be called.
*/
override fun isTest(element: PsiElement): Boolean = element.isContainedInSpec()
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/io/kotest/plugin/intellij/psi/offsets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.execution.PsiLocation
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiUtilCore

/**
* Returns the offsets for the given line in this file, or -1 if the document cannot be loaded for this file.
Expand All @@ -21,14 +22,13 @@ fun PsiFile.offsetForLine(line: Int): IntRange? {

/**
* Finds the first [PsiElement] for the given offset range by iterating over
* the values in the range until an element is found.
* the values in the range until an element that is not a whitespace is found.
*/
fun PsiElement.findElementInRange(offsets: IntRange): PsiElement? {
return offsets.asSequence()
.mapNotNull { findElementAt(it) }
.mapNotNull { offset -> findElementAt(offset)?.takeIf { it.text?.isNotBlank() == true } }
.firstOrNull()
}

/**
* Returns the first element for the given line in this file,
* or null if the document cannot be loaded for this file.
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/io/kotest/plugin/intellij/psi/OffsetsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class OffsetsTest : LightJavaCodeInsightFixtureTestCase() {
val psiFile = myFixture.configureByFile("/funspec.kt")
val element: PsiElement? = psiFile.elementAtLine(24)
element.shouldNotBeNull()
element.node.shouldBeInstanceOf<PsiWhiteSpace>()
element.startOffset shouldBe 361
element.endOffset shouldBe 369
element.node.shouldBeInstanceOf<PsiElement>()
element.startOffset shouldBe 369
element.endOffset shouldBe 373
}
}
39 changes: 0 additions & 39 deletions src/test/kotlin/io/kotest/plugin/intellij/psi/SpecTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,6 @@ class SpecTests : LightJavaCodeInsightFixtureTestCase() {
return path.toString()
}

fun testIsContainedInSpecFunSpec() {

val psiFile = myFixture.configureByFiles(
"/funspec.kt",
"/io/kotest/core/spec/style/specs.kt"
)

psiFile[0].elementAtLine(3)!!.isContainedInSpec() shouldBe false
for (k in 10..40) {
psiFile[0].elementAtLine(k)!!.isContainedInSpec() shouldBe true
}
}

fun testIsContainedInSpecStringSpec() {

val psiFile = myFixture.configureByFiles(
"/stringspec.kt",
"/io/kotest/core/spec/style/specs.kt"
)

psiFile[0].elementAtLine(4)!!.isContainedInSpec() shouldBe false
for (k in 7..13) {
psiFile[0].elementAtLine(k)!!.isContainedInSpec() shouldBe true
}
}

fun testIsContainedInSpecFreeSpec() {

val psiFile = myFixture.configureByFiles(
"/freespec.kt",
"/io/kotest/core/spec/style/specs.kt"
)

psiFile[0].elementAtLine(4)!!.isContainedInSpec() shouldBe false
for (k in 7..21) {
psiFile[0].elementAtLine(k)!!.isContainedInSpec() shouldBe true
}
}

fun testasKtClassOrObjectOrNull() {

val psiFile = myFixture.configureByFiles(
Expand Down

0 comments on commit b2aaf31

Please sign in to comment.