Skip to content

Commit

Permalink
Fix anonymous and local class indices (#35)
Browse files Browse the repository at this point in the history
The previous code would consider type parameters an indexed class
  • Loading branch information
Matyrobbrt authored Jul 18, 2024
1 parent f166031 commit 64a0cd7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
6 changes: 4 additions & 2 deletions api/src/main/java/net/neoforged/jst/api/PsiHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiParameterListOwner;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.PsiTypes;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.SyntaxTraverser;
Expand Down Expand Up @@ -157,8 +158,9 @@ private static int getNonQualifiedClassIdx(@NotNull final PsiClass psiClass, @No
CachedValuesManager.getCachedValue(containingClass, () -> {
var map = new ObjectIntHashMap<PsiClass>();
int index = 0;
for (PsiClass aClass : SyntaxTraverser.psiTraverser().withRoot(containingClass).postOrderDfsTraversal().filter(PsiClass.class)) {
if (aClass.getQualifiedName() == null) {
for (var aClass : SyntaxTraverser.psiTraverser().withRoot(containingClass).postOrderDfsTraversal().filter(PsiClass.class)) {
// We're only interested in actual classes without qualified names (type parameters are an instance of PsiClass)
if (!(aClass instanceof PsiTypeParameter) && aClass.getQualifiedName() == null) {
map.put(aClass, ++index);
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/data/parchment/anonymous_classes/expected/FormattedText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.Optional;

public interface FormattedText {
FormattedText EMPTY = new FormattedText() {
@Override
public <T> Optional<T> visit(AtomicReference<T> named_inner1) {
return Optional.empty();
}
};

<T> Optional<T> visit(AtomicReference<T> p_ref);

static FormattedText of(final String p_txt) {
class Local {
void run(int named_local1) {}
}
return new FormattedText() {
@Override
public <T> Optional<T> visit(AtomicReference<T> named_inner2) {
return Optional.empty();
}
};
}
}
10 changes: 10 additions & 0 deletions tests/data/parchment/anonymous_classes/mappings.tsrg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tsrg2 left right
obfuscated_c$1 FormattedText$1
obfuscated_m (Ljava/util/concurrent/atomic/AtomicReference;)Ljava/util/Optional; visit
0 o named_inner1
obfuscated_c$2 FormattedText$2Local
obfuscated_m (I)V run
0 o named_local1
obfuscated_c$3 FormattedText$3
obfuscated_m (Ljava/util/concurrent/atomic/AtomicReference;)Ljava/util/Optional; visit
0 o named_inner2
25 changes: 25 additions & 0 deletions tests/data/parchment/anonymous_classes/source/FormattedText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.Optional;

public interface FormattedText {
FormattedText EMPTY = new FormattedText() {
@Override
public <T> Optional<T> visit(AtomicReference<T> p_130797_) {
return Optional.empty();
}
};

<T> Optional<T> visit(AtomicReference<T> p_ref);

static FormattedText of(final String p_txt) {
class Local {
void run(int p_127434_) {}
}
return new FormattedText() {
@Override
public <T> Optional<T> visit(AtomicReference<T> p_130787_) {
return Optional.empty();
}
};
}
}
5 changes: 5 additions & 0 deletions tests/src/test/java/net/neoforged/jst/tests/EmbeddedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ void testJavadoc() throws Exception {
void testTsrgMappings() throws Exception {
runParchmentTest("tsrg_file", "merged.tsrg");
}

@Test
void testAnonymousClasses() throws Exception {
runParchmentTest("anonymous_classes", "mappings.tsrg");
}
}

@Nested
Expand Down

0 comments on commit 64a0cd7

Please sign in to comment.