Skip to content

Commit

Permalink
Fix migration maps not being readable by IntelliJ IDEA
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed Aug 30, 2024
1 parent bbeb8fb commit bf760fb
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.mappingio.format.intellij;

import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class MigrationMapConstants {
private MigrationMapConstants() {
}

public static final String ORDER_KEY = "migrationmap:order";
public static final String MISSING_NAME = "Unnamed migration map";
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,15 @@ private static void read0(BufferedReader reader, String sourceNs, String targetN

switch (name) {
case "name":
case "order":
case "description":
if (visitHeader) {
// TODO: visit as metadata once https://github.com/FabricMC/mapping-io/pull/29 is merged
String value = xmlReader.getAttributeValue(null, "value");

if (name.equals("order")) name = MigrationMapConstants.ORDER_KEY;
if (name.equals("name") && value.equals(MigrationMapConstants.MISSING_NAME)) break;

visitor.visitMetadata(name, value);
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public MigrationMapFileWriter(Writer writer) {
public void close() throws IOException {
try {
if (xmlWriter != null) {
if (!wroteName) {
xmlWriter.writeCharacters("\n\t");
xmlWriter.writeEmptyElement("name");
xmlWriter.writeAttribute("value", MigrationMapConstants.MISSING_NAME);
}

if (!wroteOrder) {
xmlWriter.writeCharacters("\n\t");
xmlWriter.writeEmptyElement("order");
xmlWriter.writeAttribute("value", "0");
}

xmlWriter.writeCharacters("\n");
xmlWriter.writeEndDocument();
xmlWriter.writeCharacters("\n");
Expand Down Expand Up @@ -77,7 +89,7 @@ public boolean visitHeader() throws IOException {
throw new IOException(e);
}

return false;
return true;
}

@Override
Expand All @@ -86,7 +98,23 @@ public void visitNamespaces(String srcNamespace, List<String> dstNamespaces) thr

@Override
public void visitMetadata(String key, @Nullable String value) throws IOException {
// TODO: Support once https://github.com/FabricMC/mapping-io/pull/29 is merged
try {
switch (key) {
case "name":
wroteName = true;
break;
case MigrationMapConstants.ORDER_KEY:
wroteOrder = true;
key = "order";
break;
}

xmlWriter.writeCharacters("\n\t");
xmlWriter.writeEmptyElement(key);
xmlWriter.writeAttribute("value", value);
} catch (XMLStreamException e) {
throw new IOException(e);
}
}

@Override
Expand Down Expand Up @@ -150,6 +178,8 @@ public void visitComment(MappedElementKind targetKind, String comment) throws IO

private final Writer writer;
private XMLStreamWriter xmlWriter;
private boolean wroteName;
private boolean wroteOrder;
private String srcName;
private String dstName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.fabricmc.mappingio.MappingFlag;
import net.fabricmc.mappingio.MappingWriter;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.intellij.MigrationMapConstants;

/**
* {@linkplain MappingFormat#TINY_2_FILE Tiny v2 file} writer.
Expand Down Expand Up @@ -66,9 +67,13 @@ public void visitNamespaces(String srcNamespace, List<String> dstNamespaces) thr

@Override
public void visitMetadata(String key, @Nullable String value) throws IOException {
if (key.equals(Tiny2Util.escapedNamesProperty)) {
switch (key) {
case Tiny2Util.escapedNamesProperty:
escapeNames = true;
wroteEscapedNamesProperty = true;
break;
case MigrationMapConstants.ORDER_KEY:
return;
}

writeTab();
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/net/fabricmc/mappingio/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import net.fabricmc.mappingio.adapter.ForwardingMappingVisitor;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.intellij.MigrationMapConstants;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.mappingio.tree.MemoryMappingTree;

Expand Down Expand Up @@ -152,6 +153,8 @@ public static <T extends MappingVisitor> T acceptTestMappings(T target) throws I

if (delegate.visitHeader()) {
delegate.visitNamespaces(MappingUtil.NS_SOURCE_FALLBACK, Arrays.asList(MappingUtil.NS_TARGET_FALLBACK, MappingUtil.NS_TARGET_FALLBACK + "2"));
delegate.visitMetadata("name", "valid");
delegate.visitMetadata(MigrationMapConstants.ORDER_KEY, "0");
}

if (delegate.visitContent()) {
Expand Down Expand Up @@ -188,6 +191,11 @@ public static <T extends MappingVisitor> T acceptTestMappingsWithRepeats(T targe
acceptTestMappings(new ForwardingMappingVisitor(new VisitOrderVerifyingVisitor(target, true)) {
private List<Runnable> replayQueue = new ArrayList<>();

@Override
public void visitMetadata(String key, @Nullable String value) throws IOException {
super.visitMetadata(key, key.equals("name") ? "repeated-elements" : value);
}

@Override
public boolean visitClass(String srcName) throws IOException {
replayQueue.clear();
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/net/fabricmc/mappingio/tree/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
Expand All @@ -33,6 +34,7 @@
import net.fabricmc.mappingio.MappingFlag;
import net.fabricmc.mappingio.NopMappingVisitor;
import net.fabricmc.mappingio.TestHelper;
import net.fabricmc.mappingio.adapter.ForwardingMappingVisitor;

public class MetadataTest {
private static final Random random = new Random();
Expand All @@ -42,7 +44,13 @@ public class MetadataTest {

@BeforeAll
public static void setup() throws Exception {
tree = TestHelper.acceptTestMappings(new MemoryMappingTree());
tree = new MemoryMappingTree();

TestHelper.acceptTestMappings(new ForwardingMappingVisitor(tree) {
@Override
public void visitMetadata(String key, @Nullable String value) throws IOException {
}
});

for (int i = 0; i < 40; i++) {
String key = "key" + random.nextInt(3);
Expand All @@ -59,8 +67,8 @@ public void testOrder() throws Exception {
tree.accept(new NopMappingVisitor(true) {
@Override
public void visitMetadata(String key, @Nullable String value) {
assertEquals(key, keys.get(visitCount));
assertEquals(value, values.get(visitCount));
assertEquals(keys.get(visitCount), key);
assertEquals(values.get(visitCount), value);
visitCount++;
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/read/repeated-elements/migration-map.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<migrationMap>
<name value="repeated-elements"/>
<order value="0"/>
<entry oldName="class_1" newName="class1Ns0Rename0" type="class"/>
<entry oldName="class_1" newName="class1Ns0Rename" type="class"/>
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename0" type="class"/>
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/read/repeated-elements/tinyV2.tiny
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tiny 2 0 source target target2
name repeated-elements
c class_1 class1Ns0Rename0 class1Ns1Rename0
c class_1 class1Ns0Rename class1Ns1Rename
f I field_1 field1Ns0Rename0 field1Ns1Rename0
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/read/valid-with-holes/migration-map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
<entry oldName="class_13$class_14" newName="class_13$class14Ns0Rename" type="class"/>
<entry oldName="class_17$class_18$class_19" newName="class_17$class_18$class19Ns0Rename" type="class"/>
<entry oldName="class_26$class_27$class_28" newName="class_26$class_27$class28Ns0Rename" type="class"/>
<name value="Unnamed migration map"/>
<order value="0"/>
</migrationMap>
2 changes: 2 additions & 0 deletions src/test/resources/read/valid/migration-map.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<migrationMap>
<name value="valid"/>
<order value="0"/>
<entry oldName="class_1" newName="class1Ns0Rename" type="class"/>
<entry oldName="class_1$class_2" newName="class1Ns0Rename$class2Ns0Rename" type="class"/>
<entry oldName="class_3" newName="class3Ns0Rename" type="class"/>
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/read/valid/tinyV2.tiny
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
tiny 2 0 source target target2
name valid
c class_1 class1Ns0Rename class1Ns1Rename
f I field_1 field1Ns0Rename field1Ns1Rename
m ()I method_1 method1Ns0Rename method1Ns1Rename
Expand Down

0 comments on commit bf760fb

Please sign in to comment.