Skip to content

Commit

Permalink
fix: workaround the Java Psi structure when errors are present
Browse files Browse the repository at this point in the history
  • Loading branch information
lppedd committed Nov 10, 2019
1 parent 0d86866 commit dbaaa27
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.github.lppedd.highlighter.java

import com.github.lppedd.highlighter.ReturnHighlightStrategy
import com.intellij.psi.PsiReturnStatement
import com.intellij.psi.PsiKeyword

/**
* @author Edoardo Luppi
*/
object JavaAlwaysHighlightStrategy : ReturnHighlightStrategy<PsiReturnStatement> {
override fun isValidContext(psiElement: PsiReturnStatement) = true
object JavaAlwaysHighlightStrategy : ReturnHighlightStrategy<PsiKeyword> {
// Note: it seems the Java Psi structure isn't quite right when errors are
// present. Thus, we need to intercept a lower-level PsiElement
override fun isValidContext(psiElement: PsiKeyword) = "$psiElement" == "PsiKeyword:return"
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.lppedd.highlighter.java

import com.intellij.psi.PsiReturnStatement
import com.intellij.psi.PsiKeyword
import com.github.lppedd.highlighter.AbstractReturnAnnotator as ARA

/**
* @author Edoardo Luppi
*/
class JavaReturnAnnotator : ARA<PsiReturnStatement>(PsiReturnStatement::class.java) {
class JavaReturnAnnotator : ARA<PsiKeyword>(PsiKeyword::class.java) {
private val config = JavaReturnHighlighterConfig.INSTANCE

override fun isValidContext(psiElement: PsiReturnStatement) =
override fun isValidContext(psiElement: PsiKeyword) =
config.getHighlightStrategy().isValidContext(psiElement)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.project.ProjectManager
import com.intellij.psi.PsiReturnStatement
import com.intellij.psi.PsiKeyword

/**
* @author Edoardo Luppi
Expand All @@ -32,7 +32,7 @@ class JavaReturnHighlighterConfig : PersistentStateComponent<JavaConfig> {
refreshFiles()
}

fun getHighlightStrategy(): ReturnHighlightStrategy<PsiReturnStatement> =
fun getHighlightStrategy(): ReturnHighlightStrategy<PsiKeyword> =
if (state.isOnlyTopLevelReturns) JavaTopLevelHighlightStrategy
else JavaAlwaysHighlightStrategy

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.github.lppedd.highlighter.java

import com.intellij.psi.PsiReturnStatement
import com.intellij.psi.PsiKeyword
import com.github.lppedd.highlighter.AbstractReturnLineMarkerProvider as ARLMP

/**
* @author Edoardo Luppi
*/
class JavaReturnLineMarkerProvider : ARLMP<PsiReturnStatement>(PsiReturnStatement::class.java) {
class JavaReturnLineMarkerProvider : ARLMP<PsiKeyword>(PsiKeyword::class.java) {
private val config = JavaReturnHighlighterConfig.INSTANCE

override fun getName() = "Java"
override fun isValidContext(psiElement: PsiReturnStatement) =
override fun isValidContext(psiElement: PsiKeyword) =
config.getHighlightStrategy().isValidContext(psiElement)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import com.intellij.psi.*
/**
* @author Edoardo Luppi
*/
object JavaTopLevelHighlightStrategy : ReturnHighlightStrategy<PsiReturnStatement> {
override fun isValidContext(psiElement: PsiReturnStatement): Boolean {
object JavaTopLevelHighlightStrategy : ReturnHighlightStrategy<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" != "PsiKeyword:return") {
return false;
}

var psi: PsiElement? = psiElement

while (psi != null) {
Expand Down

0 comments on commit dbaaa27

Please sign in to comment.