Minor: Extract findElementByComment() function
This commit is contained in:
@@ -18,6 +18,11 @@ package org.jetbrains.kotlin.test.util
|
||||
|
||||
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.psi.PsiComment
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import java.io.File
|
||||
|
||||
public fun String.trimTrailingWhitespacesAndAddNewlineAtEOF(): String =
|
||||
@@ -62,3 +67,29 @@ public fun String.trimIndent(): String {
|
||||
}
|
||||
}.joinToString(separator = "\n")
|
||||
}
|
||||
|
||||
public fun JetFile.findElementByComment(commentText: String): JetElement? {
|
||||
var result: JetElement? = null
|
||||
accept(
|
||||
object : JetTreeVisitorVoid() {
|
||||
override fun visitComment(comment: PsiComment) {
|
||||
if (comment.getText() == commentText) {
|
||||
val parent = comment.getParent()
|
||||
if (parent is JetDeclaration) {
|
||||
result = parent
|
||||
}
|
||||
else {
|
||||
result = PsiTreeUtil.skipSiblingsForward(
|
||||
comment,
|
||||
javaClass<PsiWhiteSpace>(),
|
||||
javaClass<PsiComment>(),
|
||||
javaClass<JetPackageDirective>()
|
||||
) as? JetElement
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
+2
-22
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.test.ConfigLibraryUtil
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.JetTestUtils
|
||||
import org.jetbrains.kotlin.test.util.findElementByComment
|
||||
import org.jetbrains.kotlin.utils.emptyOrSingletonList
|
||||
import java.io.File
|
||||
import java.util.Collections
|
||||
@@ -87,28 +88,7 @@ public abstract class AbstractJetExtractionTest() : JetLightCodeInsightFixtureTe
|
||||
|
||||
protected fun doExtractFunctionTest(path: String) {
|
||||
doTest(path) { file ->
|
||||
var explicitPreviousSibling: PsiElement? = null
|
||||
file.accept(
|
||||
object: JetTreeVisitorVoid() {
|
||||
override fun visitComment(comment: PsiComment) {
|
||||
if (comment.getText() == "// SIBLING:") {
|
||||
val parent = comment.getParent()
|
||||
if (parent is JetDeclaration) {
|
||||
explicitPreviousSibling = parent
|
||||
}
|
||||
else {
|
||||
explicitPreviousSibling = PsiTreeUtil.skipSiblingsForward(
|
||||
comment,
|
||||
javaClass<PsiWhiteSpace>(),
|
||||
javaClass<PsiComment>(),
|
||||
javaClass<JetPackageDirective>()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
val explicitPreviousSibling = file.findElementByComment("// SIBLING:")
|
||||
val fileText = file.getText() ?: ""
|
||||
val expectedNames = InTextDirectivesUtils.findListWithPrefixes(fileText, "// SUGGESTED_NAMES: ")
|
||||
val expectedDescriptors =
|
||||
|
||||
Reference in New Issue
Block a user