From 8a34c48d561d55524a41220d8b117a9e5ab1b677 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Mon, 23 Oct 2017 16:39:24 +0300 Subject: [PATCH] AbstractUastTest: `findUElementByTextFromPsi` refactored for readability --- .../jetbrains/uast/test/env/AbstractUastTest.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt index 0f0343fa443..4292f11a9f1 100644 --- a/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt +++ b/plugins/uast-kotlin/tests/org/jetbrains/uast/test/env/AbstractUastTest.kt @@ -67,8 +67,15 @@ inline fun UElement.findElementByText(refText: String): T = fi inline fun UElement.findElementByTextFromPsi(refText: String): T = (this.psi ?: fail("no psi for $this")).findUElementByTextFromPsi(refText) -inline fun PsiElement.findUElementByTextFromPsi(refText: String): T = - (this.findElementAt(this.text.indexOf(refText)) ?: throw AssertionError("requested text '$refText' was not found in $this")) - .parentsWithSelf.dropWhile { !it.text.contains(refText) }.mapNotNull { it.toUElementOfType() }.first().also { - if (it.psi != null && it.psi?.text != refText) throw AssertionError("requested text '$refText' found as '${it.psi?.text}' in $it") - } +inline fun PsiElement.findUElementByTextFromPsi(refText: String): T { + val elementAtStart = this.findElementAt(this.text.indexOf(refText)) + ?: throw AssertionError("requested text '$refText' was not found in $this") + val uElementContainingText = elementAtStart.parentsWithSelf. + dropWhile { !it.text.contains(refText) }. + mapNotNull { it.toUElementOfType() }. + first() + if (uElementContainingText.psi != null && uElementContainingText.psi?.text != refText) { + throw AssertionError("requested text '$refText' found as '${uElementContainingText.psi?.text}' in $uElementContainingText") + } + return uElementContainingText; +}