Skip to content

Commit 322437c

Browse files
authored
Unpick fixes (#56)
1 parent 0830b1c commit 322437c

19 files changed

Lines changed: 190 additions & 27 deletions

File tree

api/src/main/java/net/neoforged/jst/api/PsiHelper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.neoforged.jst.api;
22

33
import com.intellij.lang.jvm.types.JvmPrimitiveTypeKind;
4+
import com.intellij.psi.JavaPsiFacade;
45
import com.intellij.psi.PsiClass;
56
import com.intellij.psi.PsiElement;
67
import com.intellij.psi.PsiExpression;
@@ -14,6 +15,7 @@
1415
import com.intellij.psi.PsiTypes;
1516
import com.intellij.psi.PsiWhiteSpace;
1617
import com.intellij.psi.SyntaxTraverser;
18+
import com.intellij.psi.search.GlobalSearchScope;
1719
import com.intellij.psi.util.CachedValueProvider;
1820
import com.intellij.psi.util.CachedValuesManager;
1921
import com.intellij.psi.util.ClassUtil;
@@ -255,4 +257,17 @@ public static PsiElement resolve(PsiReferenceExpression expression) {
255257
return null;
256258
}
257259
}
260+
261+
@Nullable
262+
public static PsiClass findClass(JavaPsiFacade facade, String name, GlobalSearchScope scope) {
263+
var inner = name.split("\\$");
264+
var cls = facade.findClass(inner[0], scope);
265+
if (cls == null) return null;
266+
267+
for (int i = 1; i < inner.length; i++) {
268+
cls = cls.findInnerClassByName(inner[i], true);
269+
if (cls == null) return null;
270+
}
271+
return cls;
272+
}
258273
}

tests/data/unpick/const/def.unpick

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unpick v3
22

33
group String
44
com.example.Constants.VERSION
5+
com.example.Constants$Inner1$Inner2.VALUE
56

67
group float
78
@strict

tests/data/unpick/const/expected/com/example/Constants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ public class Constants {
66
public static final float FLOAT_CT = 2.5;
77

88
public static final long LONG_VAL = 34L;
9+
10+
public static class Inner1 {
11+
public static class Inner2 {
12+
public static final String VALUE = "value";
13+
}
14+
}
915
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package com.stuff;
22

33
import com.example.Constants;
4+
import com.example.Constants.Inner1.Inner2;
45

56
public class Uses {
67
public String fld = Constants.VERSION;
78

89
void run() {
9-
String s = Constants.VERSION + "2";
10+
String s = Constants.VERSION + "2" + Inner2.VALUE;
1011

1112
float f = Math.PI;
1213

13-
f = Math.PI / 3;
14+
f = (Math.PI / 3);
1415

1516
double d = 3.141592653589793d; // PI unpick is strict float so this should not be replaced
1617

1718
d = Constants.FLOAT_CT; // but the other float unpick isn't so this double literal should be replaced
1819

19-
System.out.println(Long.toHexString((Constants.LONG_VAL + 1) * 2));
20+
System.out.println(Long.toHexString(((Constants.LONG_VAL + 1) * 2)));
2021
}
2122
}

tests/data/unpick/const/source/com/example/Constants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ public class Constants {
66
public static final float FLOAT_CT = 2.5;
77

88
public static final long LONG_VAL = 34L;
9+
10+
public static class Inner1 {
11+
public static class Inner2 {
12+
public static final String VALUE = "value";
13+
}
14+
}
915
}

tests/data/unpick/const/source/com/stuff/Uses.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class Uses {
44
public String fld = "1.21.4";
55

66
void run() {
7-
String s = "1.21.4" + "2";
7+
String s = "1.21.4" + "2" + "value";
88

99
float f = 3.141592653589793f;
1010

tests/data/unpick/flags/def.unpick

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@ unpick v3
22

33
group int Flags
44
@flags
5+
com.example.Example.FLAG_4 * 21
56
com.example.Example.FLAG_1
67
com.example.Example.FLAG_2
78
com.example.Example.FLAG_3
89
com.example.Example.FLAG_4
910

11+
group int FlagsHex
12+
@flags
13+
@format hex
14+
com.example.Example.FLAG_1
15+
com.example.Example.FLAG_3
16+
com.example.Example.FLAG_4
17+
1018
target_method com.example.Example applyFlags(I)V
1119
param 0 Flags
20+
21+
target_method com.example.Example applyHexFlags(I)V
22+
param 0 FlagsHex

tests/data/unpick/flags/expected/com/example/Example.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ public static void main(String[] args) {
1212
applyFlags(Example.FLAG_1 | Example.FLAG_2);
1313
applyFlags(Example.FLAG_3 | Example.FLAG_4 | Example.FLAG_1 | 129);
1414
applyFlags(-1);
15+
16+
applyFlags(~(Example.FLAG_1 | Example.FLAG_2));
17+
applyFlags(~Example.FLAG_4);
18+
applyFlags((Example.FLAG_4 * 21) | Example.FLAG_3);
19+
20+
applyHexFlags(Example.FLAG_3 | Example.FLAG_4 | Example.FLAG_1 | 0x81);
1521
}
1622

1723
public static void applyFlags(int flags) {}
24+
public static void applyHexFlags(int flags) {}
1825
}

tests/data/unpick/flags/source/com/example/Example.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ public static void main(String[] args) {
1212
applyFlags(6);
1313
applyFlags(155);
1414
applyFlags(-1);
15+
16+
applyFlags(-7);
17+
applyFlags(-17);
18+
applyFlags(344);
19+
20+
applyHexFlags(155);
1521
}
1622

1723
public static void applyFlags(int flags) {}
24+
public static void applyHexFlags(int flags) {}
1825
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
unpick v3
2+
3+
group String
4+
com.example.Example.I1 + com.example.Example.S
5+
(com.example.Example._S + com.example.Example.D1) + com.example.Example.I1
6+
com.example.Example.S + com.example.Example._S

0 commit comments

Comments
 (0)