Skip to content

Commit

Permalink
Fix non existing resource inspection: it should only look at annotate…
Browse files Browse the repository at this point in the history
…d elements
  • Loading branch information
BlueBoxWare committed Aug 4, 2018
1 parent 4e12f0d commit 4fb433d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ class AssetReference(element: PsiElement, val resourceName: String, val classNam
listOf<SkinFile>() to listOf()
}

if (assetFiles.first.isEmpty() && assetFiles.second.isEmpty()) {
return arrayOf()
}

return arrayOf(
AssetReference(
element,
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<idea-plugin>
<id>com.gmail.blueboxware.libgdxplugin</id>
<name>LibGDX Inspections</name>
<version>1.16</version>
<version>1.16.1</version>
<vendor url="https://github.com/BlueBoxWare/LibGDXPlugin">Blue Box Ware</vendor>

<description><![CDATA[
Expand All @@ -30,6 +30,11 @@
]]></description>

<change-notes><![CDATA[
<b>1.16.1</b>
<ul>
<li>Fix non existing resource inspection: it should only look at annotated elements</li>
</ul>
<b>1.16</b>
<ul>
<li>Inspection to highlight references in Java/Kotlin to resources which don't exist. @GDXAssets annotated fields and variables only.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
expectedType = "com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle"
)

fun testJavaResourceReferenceWithoutAnnotation() = doTest<SkinResource>(
JavaFileType.INSTANCE,
PsiLiteralExpression::class.java,
"""
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.gmail.blueboxware.libgdxplugin.annotations.GDXAssets;
class SkinTest {
static Skin staticSkin;
void f() {
staticSkin.get("sw<caret>itch", com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle.class);
}
}
""",
shouldBeFound = false
)

fun testJavaResourceReferenceWithTag1() = doTest<SkinResource>(
JavaFileType.INSTANCE,
PsiLiteralExpression::class.java,
Expand Down Expand Up @@ -97,6 +117,23 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
expectedType = "com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle"
)

fun testKotlinResourceReferenceWithoutAnnotation() = doTest<SkinResource>(
KotlinFileType.INSTANCE,
KtStringTemplateExpression::class.java,
"""
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.gmail.blueboxware.libgdxplugin.annotations.GDXAssets
val s: Skin = Skin()
fun test() {
s.get("sw<caret>itch", com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle::class.java)
}
""",
shouldBeFound = false
)

fun testKotlinResourceReference() = doTest<SkinResource>(
KotlinFileType.INSTANCE,
KtStringTemplateExpression::class.java,
Expand Down Expand Up @@ -143,7 +180,6 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
@GDXAssets(atlasFiles = [""], skinFiles = ["src/assets/libgdx.skin"])
val s: Skin = Skin()
fun test() {
s.get("tagged<caret>Style2", com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle::class.java)
}
Expand Down Expand Up @@ -299,7 +335,8 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
fileType: LanguageFileType,
referencingElementType: Class<out PsiElement>,
content: String,
expectedType: String? = null
expectedType: String? = null,
shouldBeFound: Boolean = true
) {

myFixture.configureByText(fileType, content)
Expand All @@ -308,8 +345,16 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
} ?: throw AssertionError("Referencing element not found")

val referentElement =
referencingElement.references.firstOrNull { it is AssetReference || it is FileReference }?.resolve()
?: throw AssertionError("Referent not found")
referencingElement.references.firstOrNull { it is AssetReference || it is FileReference }?.let {
if (!shouldBeFound) {
throw AssertionError("Unexpected reference found")
}
it.resolve()
}

if (!shouldBeFound && referentElement == null) {
return
}

assertTrue(referentElement is expectedReferentType)

Expand Down
7 changes: 7 additions & 0 deletions src/test/testdata/inspections/nonExistingAsset/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ public class Test {
@GDXAssets(atlasFiles = "src/dir/holo.atlas")
TextureAtlas atlas;

TextureAtlas atlas2;

Skin skin3;

void m() {
new Json().addClassTag("TagStyle", Button.ButtonStyle.class);

skin.get("jt1", Test.class);
skin.get(<error descr="Resource \"jt2\" with type \"Test\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"jt2"</error>, Test.class);

skin.getColor("yellow");
skin3.getColor("yellow");
skin.getColor(<error descr="Resource \"blue\" with type \"com.badlogic.gdx.graphics.Color\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"blue"</error>);
skin.getColor(<error descr="Resource \"button\" with type \"com.badlogic.gdx.graphics.Color\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"button"</error>);
skin.get(TextButton.TextButtonStyle.class);
Expand All @@ -35,6 +40,7 @@ void m() {
skin.get(<error descr="Resource \"taggedStyle2\" with type \"com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"taggedStyle2"</error>, TextButton.TextButtonStyle.class);
//noinspection LibGDXNonExistingAsset
skin.get("taggedStyle2", TextButton.TextButtonStyle.class);
skin3.get("taggedStyle2", TextButton.TextButtonStyle.class);
skin.get("user-red", Skin.TintedDrawable.class);
skin.getDrawable("button-left");
skin.getDrawable("user-red");
Expand All @@ -44,6 +50,7 @@ void m() {
skin.get(<error descr="Resource \"taggedStyle1\" with type \"com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"taggedStyle1"</error>, Button.ButtonStyle.class);

atlas.findRegion("button-left");
atlas2.findRegion("foo");
atlas.findRegion(<error descr="Resource \"button-foo\" with type \"com.badlogic.gdx.graphics.g2d.TextureRegion\" does not exist in file \"holo.atlas\"">"button-foo"</error>);

skin2.getRegion("button-back-disabled");
Expand Down
7 changes: 7 additions & 0 deletions src/test/testdata/inspections/nonExistingAsset/Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ val skin1 = Skin()
@GDXAssets(atlasFiles = ["src/dir/holo.atlas"])
val atlas = TextureAtlas()

val atlas2 = TextureAtlas()

val c1 = skin1.getColor("yellow")
val c2 = skin1.getColor(<error descr="Resource \"blue\" with type \"com.badlogic.gdx.graphics.Color\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"blue"</error>)
val c3 = skin1.getColor(<error descr="Resource \"button\" with type \"com.badlogic.gdx.graphics.Color\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"button"</error>)
Expand All @@ -23,10 +25,13 @@ fun f() {
@GDXAssets(skinFiles = arrayOf("src/libgdx.skin"))
val skin2 = Skin()

val skin3 = Skin()

skin1.get(<error descr="Resource \"kt1\" with type \"KotlinClass\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"kt1"</error>, KotlinClass::class.java)
skin1.get("kt2", KotlinClass::class.java)

skin1.get(TextButton.TextButtonStyle::class.java)
skin3.get(TextButton.TextButtonStyle::class.java)
skin1.get(<error descr="Resource \"\" with type \"com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">""</error>, TextButton.TextButtonStyle::class.java)
skin1.get("toggle", TextButton.TextButtonStyle::class.java)
skin1.get(<error descr="Resource \"user-red\" with type \"com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"user-red"</error>, TextButton.TextButtonStyle::class.java)
Expand All @@ -35,6 +40,7 @@ fun f() {
skin1.get("user-red", Skin.TintedDrawable::class.java)
skin1.getDrawable("button-left")
skin1.getDrawable("user-red")
skin3.getDrawable("user-red")
skin1.getRegion("button-left")
skin1.getRegion(<error descr="Resource \"green\" with type \"com.badlogic.gdx.graphics.g2d.TextureRegion\" does not exist in files \"libgdx.skin\", \"holo.atlas\" or \"libgdx.atlas\"">"green"</error>)
skin1.get("taggedStyle2", Button.ButtonStyle::class.java)
Expand All @@ -43,6 +49,7 @@ fun f() {
skin1.get("taggedStyle1", Button.ButtonStyle::class.java)

atlas.findRegion("button-left")
atlas2.findRegion("button-left")
atlas.findRegion(<error descr="Resource \"button-foo\" with type \"com.badlogic.gdx.graphics.g2d.TextureRegion\" does not exist in file \"holo.atlas\"">"button-foo"</error>)

skin2.getRegion("button-back-disabled")
Expand Down
2 changes: 1 addition & 1 deletion versions.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ext {

pluginVersion = '1.16'
pluginVersion = '1.16.1'

kotlinVersion = '1.2.40'

Expand Down

0 comments on commit 4fb433d

Please sign in to comment.