Skip to content

Commit

Permalink
Fix anonymous and local class indices
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 committed Jul 18, 2024
1 parent f166031 commit da192c4
Show file tree
Hide file tree
Showing 6 changed files with 169 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();
}
};
}
}
100 changes: 100 additions & 0 deletions tests/data/parchment/tsrg_file/source/FormattedText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Optional;
import net.minecraft.util.Unit;

public interface FormattedText {
Optional<Unit> STOP_ITERATION = Optional.of(Unit.INSTANCE);
FormattedText EMPTY = new FormattedText() {
@Override
public <T> Optional<T> visit(FormattedText.ContentConsumer<T> p_130797_) {
return Optional.empty();
}

@Override
public <T> Optional<T> visit(FormattedText.StyledContentConsumer<T> p_130799_, Style p_130800_) {
return Optional.empty();
}
};

<T> Optional<T> visit(FormattedText.ContentConsumer<T> p_130770_);

<T> Optional<T> visit(FormattedText.StyledContentConsumer<T> p_130771_, Style p_130772_);

static FormattedText of(final String p_130776_) {
return new FormattedText() {
@Override
public <T> Optional<T> visit(FormattedText.ContentConsumer<T> p_130787_) {
return p_130787_.accept(p_130776_);
}

@Override
public <T> Optional<T> visit(FormattedText.StyledContentConsumer<T> p_130789_, Style p_130790_) {
return p_130789_.accept(p_130790_, p_130776_);
}
};
}

static FormattedText of(final String p_130763_, final Style p_130764_) {
return new FormattedText() {
@Override
public <T> Optional<T> visit(FormattedText.ContentConsumer<T> p_130797_) {
return p_130797_.accept(p_130763_);
}

@Override
public <T> Optional<T> visit(FormattedText.StyledContentConsumer<T> p_130799_, Style p_130800_) {
return p_130799_.accept(p_130764_.applyTo(p_130800_), p_130763_);
}
};
}

static FormattedText composite(FormattedText... p_130774_) {
return composite(ImmutableList.copyOf(p_130774_));
}

static FormattedText composite(final List<? extends FormattedText> p_130769_) {
return new FormattedText() {
@Override
public <T> Optional<T> visit(FormattedText.ContentConsumer<T> p_130805_) {
for (FormattedText formattedtext : p_130769_) {
Optional<T> optional = formattedtext.visit(p_130805_);
if (optional.isPresent()) {
return optional;
}
}

return Optional.empty();
}

@Override
public <T> Optional<T> visit(FormattedText.StyledContentConsumer<T> p_130807_, Style p_130808_) {
for (FormattedText formattedtext : p_130769_) {
Optional<T> optional = formattedtext.visit(p_130807_, p_130808_);
if (optional.isPresent()) {
return optional;
}
}

return Optional.empty();
}
};
}

default String getString() {
StringBuilder stringbuilder = new StringBuilder();
this.visit(p_130767_ -> {
stringbuilder.append(p_130767_);
return Optional.empty();
});
return stringbuilder.toString();
}

public interface ContentConsumer<T> {
Optional<T> accept(String p_130810_);
}

public interface StyledContentConsumer<T> {
Optional<T> accept(Style p_130811_, String p_130812_);
}
}
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 da192c4

Please sign in to comment.