Skip to content

Commit 4fb433d

Browse files
committed
Fix non existing resource inspection: it should only look at annotated elements
1 parent 4e12f0d commit 4fb433d

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed

src/main/kotlin/com/gmail/blueboxware/libgdxplugin/references/AssetReference.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ class AssetReference(element: PsiElement, val resourceName: String, val classNam
183183
listOf<SkinFile>() to listOf()
184184
}
185185

186+
if (assetFiles.first.isEmpty() && assetFiles.second.isEmpty()) {
187+
return arrayOf()
188+
}
189+
186190
return arrayOf(
187191
AssetReference(
188192
element,

src/main/resources/META-INF/plugin.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<idea-plugin>
1818
<id>com.gmail.blueboxware.libgdxplugin</id>
1919
<name>LibGDX Inspections</name>
20-
<version>1.16</version>
20+
<version>1.16.1</version>
2121
<vendor url="https://github.com/BlueBoxWare/LibGDXPlugin">Blue Box Ware</vendor>
2222

2323
<description><![CDATA[
@@ -30,6 +30,11 @@
3030
]]></description>
3131

3232
<change-notes><![CDATA[
33+
<b>1.16.1</b>
34+
<ul>
35+
<li>Fix non existing resource inspection: it should only look at annotated elements</li>
36+
</ul>
37+
3338
<b>1.16</b>
3439
<ul>
3540
<li>Inspection to highlight references in Java/Kotlin to resources which don't exist. @GDXAssets annotated fields and variables only.</li>

src/test/kotlin/com/gmail/blueboxware/libgdxplugin/assetsInCode/TestReferences.kt

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
5555
expectedType = "com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle"
5656
)
5757

58+
fun testJavaResourceReferenceWithoutAnnotation() = doTest<SkinResource>(
59+
JavaFileType.INSTANCE,
60+
PsiLiteralExpression::class.java,
61+
"""
62+
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
63+
import com.gmail.blueboxware.libgdxplugin.annotations.GDXAssets;
64+
65+
class SkinTest {
66+
67+
static Skin staticSkin;
68+
69+
void f() {
70+
staticSkin.get("sw<caret>itch", com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle.class);
71+
}
72+
73+
}
74+
""",
75+
shouldBeFound = false
76+
)
77+
5878
fun testJavaResourceReferenceWithTag1() = doTest<SkinResource>(
5979
JavaFileType.INSTANCE,
6080
PsiLiteralExpression::class.java,
@@ -97,6 +117,23 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
97117
expectedType = "com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle"
98118
)
99119

120+
fun testKotlinResourceReferenceWithoutAnnotation() = doTest<SkinResource>(
121+
KotlinFileType.INSTANCE,
122+
KtStringTemplateExpression::class.java,
123+
"""
124+
import com.badlogic.gdx.scenes.scene2d.ui.Skin
125+
import com.gmail.blueboxware.libgdxplugin.annotations.GDXAssets
126+
127+
val s: Skin = Skin()
128+
129+
130+
fun test() {
131+
s.get("sw<caret>itch", com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle::class.java)
132+
}
133+
""",
134+
shouldBeFound = false
135+
)
136+
100137
fun testKotlinResourceReference() = doTest<SkinResource>(
101138
KotlinFileType.INSTANCE,
102139
KtStringTemplateExpression::class.java,
@@ -143,7 +180,6 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
143180
@GDXAssets(atlasFiles = [""], skinFiles = ["src/assets/libgdx.skin"])
144181
val s: Skin = Skin()
145182
146-
147183
fun test() {
148184
s.get("tagged<caret>Style2", com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle::class.java)
149185
}
@@ -299,7 +335,8 @@ class TestReferences : AssetsInCodeCodeInsightFixtureTestCase() {
299335
fileType: LanguageFileType,
300336
referencingElementType: Class<out PsiElement>,
301337
content: String,
302-
expectedType: String? = null
338+
expectedType: String? = null,
339+
shouldBeFound: Boolean = true
303340
) {
304341

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

310347
val referentElement =
311-
referencingElement.references.firstOrNull { it is AssetReference || it is FileReference }?.resolve()
312-
?: throw AssertionError("Referent not found")
348+
referencingElement.references.firstOrNull { it is AssetReference || it is FileReference }?.let {
349+
if (!shouldBeFound) {
350+
throw AssertionError("Unexpected reference found")
351+
}
352+
it.resolve()
353+
}
354+
355+
if (!shouldBeFound && referentElement == null) {
356+
return
357+
}
313358

314359
assertTrue(referentElement is expectedReferentType)
315360

src/test/testdata/inspections/nonExistingAsset/Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ public class Test {
1818
@GDXAssets(atlasFiles = "src/dir/holo.atlas")
1919
TextureAtlas atlas;
2020

21+
TextureAtlas atlas2;
22+
23+
Skin skin3;
24+
2125
void m() {
2226
new Json().addClassTag("TagStyle", Button.ButtonStyle.class);
2327

2428
skin.get("jt1", Test.class);
2529
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);
2630

2731
skin.getColor("yellow");
32+
skin3.getColor("yellow");
2833
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>);
2934
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>);
3035
skin.get(TextButton.TextButtonStyle.class);
@@ -35,6 +40,7 @@ void m() {
3540
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);
3641
//noinspection LibGDXNonExistingAsset
3742
skin.get("taggedStyle2", TextButton.TextButtonStyle.class);
43+
skin3.get("taggedStyle2", TextButton.TextButtonStyle.class);
3844
skin.get("user-red", Skin.TintedDrawable.class);
3945
skin.getDrawable("button-left");
4046
skin.getDrawable("user-red");
@@ -44,6 +50,7 @@ void m() {
4450
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);
4551

4652
atlas.findRegion("button-left");
53+
atlas2.findRegion("foo");
4754
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>);
4855

4956
skin2.getRegion("button-back-disabled");

src/test/testdata/inspections/nonExistingAsset/Test.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ val skin1 = Skin()
1414
@GDXAssets(atlasFiles = ["src/dir/holo.atlas"])
1515
val atlas = TextureAtlas()
1616

17+
val atlas2 = TextureAtlas()
18+
1719
val c1 = skin1.getColor("yellow")
1820
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>)
1921
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>)
@@ -23,10 +25,13 @@ fun f() {
2325
@GDXAssets(skinFiles = arrayOf("src/libgdx.skin"))
2426
val skin2 = Skin()
2527

28+
val skin3 = Skin()
29+
2630
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)
2731
skin1.get("kt2", KotlinClass::class.java)
2832

2933
skin1.get(TextButton.TextButtonStyle::class.java)
34+
skin3.get(TextButton.TextButtonStyle::class.java)
3035
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)
3136
skin1.get("toggle", TextButton.TextButtonStyle::class.java)
3237
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)
@@ -35,6 +40,7 @@ fun f() {
3540
skin1.get("user-red", Skin.TintedDrawable::class.java)
3641
skin1.getDrawable("button-left")
3742
skin1.getDrawable("user-red")
43+
skin3.getDrawable("user-red")
3844
skin1.getRegion("button-left")
3945
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>)
4046
skin1.get("taggedStyle2", Button.ButtonStyle::class.java)
@@ -43,6 +49,7 @@ fun f() {
4349
skin1.get("taggedStyle1", Button.ButtonStyle::class.java)
4450

4551
atlas.findRegion("button-left")
52+
atlas2.findRegion("button-left")
4653
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>)
4754

4855
skin2.getRegion("button-back-disabled")

versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ext {
22

3-
pluginVersion = '1.16'
3+
pluginVersion = '1.16.1'
44

55
kotlinVersion = '1.2.40'
66

0 commit comments

Comments
 (0)