Skip to content

Commit

Permalink
refactor: extract isValidContext to a base class for top-level strate…
Browse files Browse the repository at this point in the history
…gies
  • Loading branch information
lppedd committed Nov 16, 2019
1 parent de964f3 commit 637dd88
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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<T : PsiElement> : ReturnHighlightStrategy<T> {
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
}
Original file line number Diff line number Diff line change
@@ -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<PsiKeyword> {
object JavaTopLevelHighlightStrategy : TopLevelReturnHighlightStrategy<PsiKeyword>() {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,29 +13,14 @@ import com.intellij.psi.util.PsiTreeUtil
/**
* @author Edoardo Luppi
*/
object JavaScriptTopLevelHighlightStrategy : ReturnHighlightStrategy<JSReturnStatement> {
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<JSReturnStatement>() {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,23 +14,8 @@ import com.jetbrains.php.lang.psi.elements.Function
/**
* @author Edoardo Luppi
*/
object PhpTopLevelHighlightStrategy : ReturnHighlightStrategy<PhpReturn> {
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<PhpReturn>() {
override fun check(psiElement: PsiElement): PsiResult =
when (psiElement) {
is PhpExpression -> checkExpression(psiElement)
is Function -> checkFunction(psiElement)
Expand Down

0 comments on commit 637dd88

Please sign in to comment.