-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from anboralabs/psi-impl
Annotators
- Loading branch information
Showing
20 changed files
with
295 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/co/anbora/labs/firebase/ide/annotator/FirebaseAnnotator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package co.anbora.labs.firebase.ide.annotator | ||
|
||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.lang.annotation.Annotator | ||
import com.intellij.openapi.Disposable | ||
import com.intellij.openapi.util.Disposer | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.util.containers.ContainerUtil | ||
import org.jetbrains.annotations.TestOnly | ||
|
||
abstract class FirebaseAnnotator : Annotator { | ||
final override fun annotate(element: PsiElement, holder: AnnotationHolder) { | ||
if (javaClass in enabledAnnotators) { | ||
annotateInternal(element, holder) | ||
} | ||
} | ||
|
||
protected abstract fun annotateInternal(element: PsiElement, holder: AnnotationHolder) | ||
|
||
companion object { | ||
private val enabledAnnotators: MutableSet<Class<out FirebaseAnnotator>> = ContainerUtil.newConcurrentSet() | ||
|
||
@TestOnly | ||
fun enableAnnotator(annotatorClass: Class<out FirebaseAnnotator>, parentDisposable: Disposable) { | ||
enabledAnnotators += annotatorClass | ||
Disposer.register( | ||
parentDisposable | ||
) { enabledAnnotators -= annotatorClass } | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/co/anbora/labs/firebase/ide/annotator/HighlightingAnnotator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package co.anbora.labs.firebase.ide.annotator | ||
|
||
class HighlightingAnnotator { | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/co/anbora/labs/firebase/ide/annotator/PathVariableHighlightAnnotator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package co.anbora.labs.firebase.ide.annotator | ||
|
||
import co.anbora.labs.firebase.ide.color.FirebaseColors | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesIdentifierExpr | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesMatchDef | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesPathStatement | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesVisitor | ||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.util.PsiTreeUtil | ||
|
||
class PathVariableHighlightAnnotator : FirebaseAnnotator() { | ||
override fun annotateInternal(element: PsiElement, holder: AnnotationHolder) { | ||
val visitor = object : FirebaseRulesVisitor() { | ||
override fun visitMatchDef(o: FirebaseRulesMatchDef) { | ||
checkPathVariablesInMatchDef(holder, element) | ||
} | ||
} | ||
element.accept(visitor) | ||
} | ||
|
||
private fun checkPathVariablesInMatchDef(holder: AnnotationHolder, element: PsiElement) { | ||
val color = FirebaseColors.NUMBERS | ||
val pathVariables = PsiTreeUtil.collectElementsOfType(element, FirebaseRulesPathStatement::class.java) | ||
.map { it.text } | ||
.map { it.replace("{", "") | ||
.replace("}", "") | ||
.replace("=", "") | ||
.replace("*", "") | ||
}.toSet() | ||
val variables = PsiTreeUtil.collectElementsOfType(element, FirebaseRulesIdentifierExpr::class.java) | ||
variables.forEach { | ||
if (pathVariables.contains(it.text)) { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/co/anbora/labs/firebase/ide/editor/FirebaseQuoteHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package co.anbora.labs.firebase.ide.editor | ||
|
||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesTypes.STRING | ||
import com.intellij.codeInsight.editorActions.SimpleTokenSetQuoteHandler | ||
|
||
class FirebaseQuoteHandler: SimpleTokenSetQuoteHandler(STRING) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/co/anbora/labs/firebase/ide/inspections/DuplicateFunctionsInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package co.anbora.labs.firebase.ide.inspections | ||
|
||
import co.anbora.labs.firebase.lang.core.psi.* | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.intellij.psi.util.PsiTreeUtil | ||
|
||
class DuplicateFunctionsDeclarationInspection : LocalInspectionTool() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object: PsiElementVisitor() { | ||
override fun visitElement(element: PsiElement) { | ||
super.visitElement(element) | ||
when (element) { | ||
is FirebaseFile -> checkFunctionSignature(element, holder) | ||
is FirebaseRulesFullPathStatement -> checkPathVariable(element, holder) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun checkFunctionSignature(element: FirebaseFile, holder: ProblemsHolder) { | ||
val functions = PsiTreeUtil.collectElementsOfType(element, FirebaseRulesFunctionDef::class.java) | ||
val mapFunctions = HashMap<String, Unit>() | ||
functions.forEach { | ||
val key = it.identifierExpr?.text + "_" + it.functionParameterList?.functionParameterList?.size | ||
mapFunctions.compute(key) { _, v -> | ||
if (v != null) markDuplicate(it.identifierExpr ?: it, holder) | ||
} | ||
} | ||
} | ||
|
||
private fun checkPathVariable(element: FirebaseRulesFullPathStatement, holder: ProblemsHolder) { | ||
val variables = PsiTreeUtil.collectElementsOfType(element, FirebaseRulesVariableInPath::class.java) | ||
val mapPathVariables = HashMap<String, Unit>() | ||
variables.forEach { | ||
val key = it.text | ||
mapPathVariables.compute(key) { _, v -> | ||
if (v != null) markDuplicate(it, holder) | ||
} | ||
} | ||
} | ||
|
||
private fun markDuplicate(element: FirebaseElement, holder: ProblemsHolder) { | ||
holder.registerProblem(element, "Duplicate definitions with name `${element.text}`", ProblemHighlightType.ERROR) | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/co/anbora/labs/firebase/ide/inspections/WeakRulesInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package co.anbora.labs.firebase.ide.inspections | ||
|
||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesConditionalBlock | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesLiteralExpr | ||
import com.intellij.codeInspection.LocalInspectionTool | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiElementVisitor | ||
|
||
private const val FIREBASE_TRUE = "true" | ||
|
||
class FirebaseWeakRulesInspection: LocalInspectionTool() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object: PsiElementVisitor() { | ||
override fun visitElement(element: PsiElement) { | ||
super.visitElement(element) | ||
when (element) { | ||
is FirebaseRulesConditionalBlock -> checkWeakRule(element, holder) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun checkWeakRule(element: FirebaseRulesConditionalBlock, holder: ProblemsHolder) { | ||
val decl = element.expression | ||
val literal = decl as? FirebaseRulesLiteralExpr | ||
val booleanStm = literal?.booleanStatement | ||
if (booleanStm?.text == FIREBASE_TRUE) { | ||
holder.registerProblem(booleanStm, "Weak rule, this is not recommended for production environments.", ProblemHighlightType.WEAK_WARNING) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/co/anbora/labs/firebase/lang/core/psi/FirebaseNameIdentifierOwner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package co.anbora.labs.firebase.lang.core.psi | ||
|
||
import com.intellij.psi.PsiNameIdentifierOwner | ||
|
||
interface FirebaseNameIdentifierOwner : FirebaseNamedElement, | ||
PsiNameIdentifierOwner |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/co/anbora/labs/firebase/lang/core/psi/FirebaseNamedElement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package co.anbora.labs.firebase.lang.core.psi | ||
|
||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesTypes.IDENTIFIER | ||
import co.anbora.labs.firebase.lang.core.psi.ext.findLastChildByType | ||
import com.intellij.psi.NavigatablePsiElement | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiNamedElement | ||
|
||
interface FirebaseNamedElement : FirebaseElement, | ||
PsiNamedElement, | ||
NavigatablePsiElement { | ||
|
||
val nameElement: PsiElement? | ||
get() = findLastChildByType(IDENTIFIER) | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/co/anbora/labs/firebase/lang/core/psi/ext/PsiElement.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package co.anbora.labs.firebase.lang.core.psi.ext | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.tree.IElementType | ||
import com.intellij.psi.util.PsiUtilCore | ||
|
||
val PsiElement.elementType: IElementType | ||
get() = PsiUtilCore.getElementType(this) | ||
|
||
val PsiElement.childrenWithLeaves: Sequence<PsiElement> | ||
get() = generateSequence(this.firstChild) { it.nextSibling } | ||
|
||
fun PsiElement.childrenByType(type: IElementType): Sequence<PsiElement> = | ||
childrenWithLeaves.filter { it.elementType == type } | ||
|
||
fun PsiElement.findLastChildByType(type: IElementType): PsiElement? = | ||
childrenByType(type).lastOrNull() |
11 changes: 11 additions & 0 deletions
11
...main/kotlin/co/anbora/labs/firebase/lang/core/psi/impl/FirebaseNameIdentifierOwnerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package co.anbora.labs.firebase.lang.core.psi.impl | ||
|
||
import co.anbora.labs.firebase.lang.core.psi.FirebaseNameIdentifierOwner | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.PsiElement | ||
|
||
abstract class FirebaseNameIdentifierOwnerImpl( | ||
node: ASTNode | ||
) : FirebaseNamedElementImpl(node), FirebaseNameIdentifierOwner { | ||
override fun getNameIdentifier(): PsiElement? = nameElement | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/co/anbora/labs/firebase/lang/core/psi/impl/FirebaseNamedElementImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package co.anbora.labs.firebase.lang.core.psi.impl | ||
|
||
import co.anbora.labs.firebase.lang.core.psi.FirebaseElementImpl | ||
import co.anbora.labs.firebase.lang.core.psi.FirebaseNamedElement | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.PsiElement | ||
|
||
abstract class FirebaseNamedElementImpl( | ||
node: ASTNode | ||
) : FirebaseElementImpl(node), FirebaseNamedElement { | ||
override fun getName(): String? = nameElement?.text | ||
|
||
override fun setName(name: String): PsiElement { | ||
//nameElement?.replace(MovePsiFactory(project).createIdentifier(name)) | ||
return this | ||
} | ||
|
||
override fun getNavigationElement(): PsiElement = nameElement ?: this | ||
|
||
override fun getTextOffset(): Int = nameElement?.textOffset ?: super.getTextOffset() | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/co/anbora/labs/firebase/lang/core/psi/mixings/IdentifierMixing.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package co.anbora.labs.firebase.lang.core.psi.mixings | ||
|
||
import co.anbora.labs.firebase.lang.core.psi.FirebaseRulesIdentifierExpr | ||
import co.anbora.labs.firebase.lang.core.psi.impl.FirebaseNameIdentifierOwnerImpl | ||
import com.intellij.lang.ASTNode | ||
|
||
abstract class IdentifierMixing(node: ASTNode): FirebaseNameIdentifierOwnerImpl(node), FirebaseRulesIdentifierExpr { | ||
} |
Oops, something went wrong.