diff --git a/src/main/kotlin/com/github/lppedd/highlighter/TopLevelReturnHighlightStrategy.kt b/src/main/kotlin/com/github/lppedd/highlighter/TopLevelReturnHighlightStrategy.kt new file mode 100644 index 0000000..e468c8a --- /dev/null +++ b/src/main/kotlin/com/github/lppedd/highlighter/TopLevelReturnHighlightStrategy.kt @@ -0,0 +1,28 @@ +package com.github.lppedd.highlighter + +import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult +import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult.* +import com.intellij.openapi.progress.ProgressManager +import com.intellij.psi.PsiElement + +/** + * @author Edoardo Luppi + */ +abstract class TopLevelReturnHighlightStrategy : ReturnHighlightStrategy { + override fun isValidContext(psiElement: T): Boolean { + var psi: PsiElement? = psiElement + + while (psi != null) { + ProgressManager.checkCanceled() + psi = when (check(psi)) { + VALID -> return true + INVALID -> return false + CONTINUE -> psi.parent + } + } + + return false + } + + protected abstract fun check(psiElement: PsiElement): PsiResult +} diff --git a/src/main/kotlin/com/github/lppedd/highlighter/java/JavaTopLevelHighlightStrategy.kt b/src/main/kotlin/com/github/lppedd/highlighter/java/JavaTopLevelHighlightStrategy.kt index b465264..90ce878 100644 --- a/src/main/kotlin/com/github/lppedd/highlighter/java/JavaTopLevelHighlightStrategy.kt +++ b/src/main/kotlin/com/github/lppedd/highlighter/java/JavaTopLevelHighlightStrategy.kt @@ -1,43 +1,31 @@ package com.github.lppedd.highlighter.java -import com.github.lppedd.highlighter.ReturnHighlightStrategy import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult.* -import com.intellij.openapi.progress.ProgressManager +import com.github.lppedd.highlighter.TopLevelReturnHighlightStrategy import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil /** * @author Edoardo Luppi */ -object JavaTopLevelHighlightStrategy : ReturnHighlightStrategy { +object JavaTopLevelHighlightStrategy : TopLevelReturnHighlightStrategy() { override fun isValidContext(psiElement: PsiKeyword): Boolean { // Note: it seems the Java Psi structure isn't quite right when errors are // present. Thus, we need to intercept a lower-level PsiElement - if (psiElement.text != PsiKeyword.RETURN) { - return false + return if (psiElement.text != PsiKeyword.RETURN) { + false + } else { + super.isValidContext(psiElement) } - - var psi: PsiElement? = psiElement - - while (psi != null) { - ProgressManager.checkCanceled() - psi = when (check(psi)) { - VALID -> return true - INVALID -> return false - CONTINUE -> psi.parent - } - } - - return false } - private fun check(psiElement: PsiElement): PsiResult = - when (psiElement) { - is PsiLambdaExpression -> checkLambdaExpression(psiElement) - is PsiMethod -> VALID - else -> CONTINUE - } + override fun check(psiElement: PsiElement): PsiResult = + when (psiElement) { + is PsiLambdaExpression -> checkLambdaExpression(psiElement) + is PsiMethod -> VALID + else -> CONTINUE + } private fun checkLambdaExpression(psiElement: PsiLambdaExpression): PsiResult { // Lambda expressions are valid only if immediately assigned to a Field diff --git a/src/main/kotlin/com/github/lppedd/highlighter/javascript/JavaScriptTopLevelHighlightStrategy.kt b/src/main/kotlin/com/github/lppedd/highlighter/javascript/JavaScriptTopLevelHighlightStrategy.kt index 14c7137..528f450 100644 --- a/src/main/kotlin/com/github/lppedd/highlighter/javascript/JavaScriptTopLevelHighlightStrategy.kt +++ b/src/main/kotlin/com/github/lppedd/highlighter/javascript/JavaScriptTopLevelHighlightStrategy.kt @@ -1,8 +1,8 @@ package com.github.lppedd.highlighter.javascript -import com.github.lppedd.highlighter.ReturnHighlightStrategy import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult.* +import com.github.lppedd.highlighter.TopLevelReturnHighlightStrategy import com.intellij.lang.javascript.psi.* import com.intellij.lang.javascript.psi.ecmal4.JSClass import com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement @@ -13,29 +13,14 @@ import com.intellij.psi.util.PsiTreeUtil /** * @author Edoardo Luppi */ -object JavaScriptTopLevelHighlightStrategy : ReturnHighlightStrategy { - override fun isValidContext(psiElement: JSReturnStatement): Boolean { - var psi: PsiElement? = psiElement - - while (psi != null) { - ProgressManager.checkCanceled() - psi = when (check(psi)) { - VALID -> return true - INVALID -> return false - CONTINUE -> psi.parent - } +object JavaScriptTopLevelHighlightStrategy : TopLevelReturnHighlightStrategy() { + override fun check(psiElement: PsiElement): PsiResult = + when (psiElement) { + is JSFunctionExpression -> checkJSFunctionExpression(psiElement) + is JSFunction -> checkJSFunction(psiElement) + else -> CONTINUE } - return false - } - - private fun check(psiElement: PsiElement): PsiResult = - when (psiElement) { - is JSFunctionExpression -> checkJSFunctionExpression(psiElement) - is JSFunction -> checkJSFunction(psiElement) - else -> CONTINUE - } - private fun checkJSFunctionExpression(psiElement: JSFunctionExpression): PsiResult { // Function Expressions are valid only if immediately assigned // to a Class Field, when in Class scope diff --git a/src/main/kotlin/com/github/lppedd/highlighter/php/PhpTopLevelHighlightStrategy.kt b/src/main/kotlin/com/github/lppedd/highlighter/php/PhpTopLevelHighlightStrategy.kt index 1f5e516..7f2d9b8 100644 --- a/src/main/kotlin/com/github/lppedd/highlighter/php/PhpTopLevelHighlightStrategy.kt +++ b/src/main/kotlin/com/github/lppedd/highlighter/php/PhpTopLevelHighlightStrategy.kt @@ -1,8 +1,8 @@ package com.github.lppedd.highlighter.php -import com.github.lppedd.highlighter.ReturnHighlightStrategy import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult import com.github.lppedd.highlighter.ReturnHighlightStrategy.PsiResult.* +import com.github.lppedd.highlighter.TopLevelReturnHighlightStrategy import com.intellij.openapi.progress.ProgressManager import com.intellij.psi.PsiElement import com.intellij.psi.util.PsiTreeUtil @@ -14,23 +14,8 @@ import com.jetbrains.php.lang.psi.elements.Function /** * @author Edoardo Luppi */ -object PhpTopLevelHighlightStrategy : ReturnHighlightStrategy { - override fun isValidContext(psiElement: PhpReturn): Boolean { - var psi: PsiElement? = psiElement - - while (psi != null) { - ProgressManager.checkCanceled() - psi = when (check(psi)) { - VALID -> return true - INVALID -> return false - CONTINUE -> psi.parent - } - } - - return false - } - - private fun check(psiElement: PsiElement): PsiResult = +object PhpTopLevelHighlightStrategy : TopLevelReturnHighlightStrategy() { + override fun check(psiElement: PsiElement): PsiResult = when (psiElement) { is PhpExpression -> checkExpression(psiElement) is Function -> checkFunction(psiElement)