generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
106 additions
and
49 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
75 changes: 69 additions & 6 deletions
75
src/main/kotlin/me/mattco/serenityos/gml/annotators/GMLErrorAnnotator.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 |
---|---|---|
@@ -1,30 +1,93 @@ | ||
package me.mattco.serenityos.gml.annotators | ||
|
||
import ai.grazie.utils.dropPrefix | ||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.openapi.components.service | ||
import com.intellij.psi.PsiElement | ||
import me.mattco.serenityos.common.DSLAnnotator | ||
import me.mattco.serenityos.common.ancestorOfType | ||
import me.mattco.serenityos.gml.GMLService | ||
import me.mattco.serenityos.gml.psi.api.GMLComponent | ||
import me.mattco.serenityos.gml.psi.api.GMLComponentName | ||
import me.mattco.serenityos.gml.psi.api.GMLPropertyIdentifier | ||
import me.mattco.serenityos.gml.Type | ||
import me.mattco.serenityos.gml.psi.api.* | ||
|
||
class GMLErrorAnnotator : DSLAnnotator() { | ||
override fun annotate(element: PsiElement, holder: Holder) = with(holder) { | ||
override fun annotate(element: PsiElement) { | ||
when (element) { | ||
is GMLComponentName -> { | ||
val name = element.text.dropPrefix("@") | ||
if (element.project.service<GMLService>().lookupComponent(name) == null) | ||
element.highlightError("Unknown component") | ||
} | ||
is GMLPropertyIdentifier -> { | ||
val parentWidget = element.ancestorOfType<GMLComponent>() ?: return@with | ||
val parentWidget = element.ancestorOfType<GMLComponent>() ?: return | ||
val gmlService = element.project.service<GMLService>() | ||
val component = gmlService.lookupComponent(parentWidget.identWithoutAt) ?: return@with | ||
val component = gmlService.lookupComponent(parentWidget.identWithoutAt) ?: return | ||
if (component.getProperty(element.identifier.text, gmlService) == null) | ||
element.highlightError("Unknown property") | ||
} | ||
is GMLProperty -> { | ||
val type = element.gmlProperty?.type ?: return | ||
val value = element.value ?: return | ||
lintType(type, value)?.let { | ||
it.targetElement.highlightError(it.error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private data class Lint(val targetElement: PsiElement, val error: String) | ||
|
||
private fun lintType(type: Type, value: GMLValue): Lint? { | ||
when (type) { | ||
is Type.Array -> { | ||
if (value.array == null) | ||
return Lint(value, "Expected Array") | ||
|
||
if (value.array!!.valueList.size !in type.min..type.max) { | ||
if (type.min == type.max) | ||
return Lint(value, "Expected Array length to have ${type.min} elements") | ||
return Lint(value, "Expected Array length to have ${type.min}-${type.max} elements") | ||
} | ||
|
||
for (element in value.array!!.valueList) { | ||
lintType(type.inner, element)?.let { return it } | ||
} | ||
} | ||
Type.Bitmap -> if (value.string == null) return Lint(value, "Expected String") | ||
Type.Bool -> if (value.boolean == null) return Lint(value, "Expected bool") | ||
Type.Color -> if (value.string == null) return Lint(value, "Expected String") | ||
Type.Double -> if (value.number == null) return Lint(value, "Expected double") | ||
is Type.EnumType -> if (value.string == null) return Lint(value, "Expected String") | ||
is Type.ErrorType -> return Lint(value, type.message) | ||
is Type.Int -> { | ||
if (value.number == null) | ||
return Lint(value, "Expected ${type.presentation()}") | ||
if (!type.signed && value.number!!.text.startsWith('-')) | ||
return Lint(value, "Unsigned int cannot hold a negative number") | ||
} | ||
Type.Margins -> return lintType(Type.Array(1, 4, Type.Int(true)), value) | ||
Type.String -> if (value.string == null) return Lint(value, "Expected String") | ||
Type.UIDimension -> { | ||
if (value.string != null) { | ||
if (value.string!!.text !in uiDimEnumValues) | ||
return Lint(value, "Invalid enum value, valid value are: $uiDimEnumValuesForDisplay") | ||
} else if (value.number == null) { | ||
return Lint(value, "Expected an i64 or one of: $uiDimEnumValuesForDisplay") | ||
} else { | ||
return lintType(Type.Int(false), value) | ||
} | ||
} | ||
is Type.Variant -> { | ||
if (type.types.all { lintType(it, value) != null }) | ||
return Lint(value, "Expected one of: ${type.presentation()}") | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
companion object { | ||
private val uiDimEnumValues = setOf("grow", "opportunistic_grow", "fit", "shrink") | ||
private val uiDimEnumValuesForDisplay = uiDimEnumValues.joinToString { "\"$it\"" } | ||
} | ||
} |
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
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