Skip to content

Commit

Permalink
Fix overlapping MultiTextEdits causing conflicts
Browse files Browse the repository at this point in the history
This takes all of the children in the `MutliTextEdit` we want to merge
and makes them children of the `MutliTextEdit` being merged into.
Conflicts were occurring when one MTE's edit range was overlapping
with the others edit range.

I ran this over multiple codebases and this change didn't affect
anything other than the static imports that were failing.
  • Loading branch information
phase authored and jamierocks committed Mar 7, 2021
1 parent 7fa0ba1 commit 8672323
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/org/cadixdev/mercury/RewriteContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ private static TextEdit combineEdit(TextEdit before, TextEdit edit) {
return edit;
}
if (edit != null) {
before.addChild(edit);
if (edit instanceof MultiTextEdit) {
MultiTextEdit multiTextEdit = (MultiTextEdit) edit;
before.addChildren(multiTextEdit.removeChildren());
} else {
before.addChild(edit);
}
}
return before;
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/cadixdev/mercury/test/RemappingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RemappingTests {
// 5. Anonymous class remapping
// This test verifies we can handle remapping cases for different anonymous class remapping
// combinations (GH-31).
// 6. Import remapping tests (GH-28)

@Test
void remap() throws Exception {
Expand All @@ -81,6 +82,11 @@ void remap() throws Exception {
//this.copy(in, "eclipse/Test.java");
// - Test 5
this.copy(in, "anon/Test.java");
// - Test 6
this.copy(in, "com/example/ImportTest.java");
this.copy(in, "com/example/other/AnotherClass.java");
this.copy(in, "com/example/other/OtherClass.java");
this.copy(in, "com/example/pkg/Constants.java");

// Load our test mappings
final MappingSet mappings = MappingSet.create();
Expand Down Expand Up @@ -110,6 +116,11 @@ void remap() throws Exception {
//this.verify(out, "eclipse/Test.java");
// - Test 5
this.verify(out, "anon/Anon.java");
// - Test 6
this.verify(out, "net/example/ImportTestNew.java");
this.verify(out, "net/example/newother/AnotherClass.java");
this.verify(out, "net/example/newother/OtherClass.java");
this.verify(out, "net/example/pkg/Util.java");

// Delete the directory
Files.walk(tempDir)
Expand Down
26 changes: 26 additions & 0 deletions src/test/resources/a/com/example/ImportTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package com.example;

import com.example.other.AnotherClass;
import static com.example.pkg.Constants.*;
import com.example.other.OtherClass;
import java.lang.String;
import java.lang.Exception;

public class ImportTest {

public void test() {
OtherClass otherClass = new OtherClass();
AnotherClass anotherClass = new AnotherClass();
}

}
15 changes: 15 additions & 0 deletions src/test/resources/a/com/example/other/AnotherClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package com.example.other;

public class AnotherClass {

}
15 changes: 15 additions & 0 deletions src/test/resources/a/com/example/other/OtherClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package com.example.other;

public class OtherClass {

}
19 changes: 19 additions & 0 deletions src/test/resources/a/com/example/pkg/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package com.example.pkg;

public class Constants {

public static final String NAME = "George";
public static final String BIRTHDAY = "1st June";
public static final String VERSION = "1";

}
27 changes: 27 additions & 0 deletions src/test/resources/b/net/example/ImportTestNew.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package net.example;

import static net.example.pkg.Util.*;

import java.lang.String;
import net.example.newother.AnotherClass;
import net.example.newother.OtherClass;
import java.lang.Exception;

public class ImportTestNew {

public void test() {
OtherClass otherClass = new OtherClass();
AnotherClass anotherClass = new AnotherClass();
}

}
15 changes: 15 additions & 0 deletions src/test/resources/b/net/example/newother/AnotherClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package net.example.newother;

public class AnotherClass {

}
15 changes: 15 additions & 0 deletions src/test/resources/b/net/example/newother/OtherClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package net.example.newother;

public class OtherClass {

}
19 changes: 19 additions & 0 deletions src/test/resources/b/net/example/pkg/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018 Cadix Development (https://www.cadixdev.org)
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package net.example.pkg;

public class Util {

public static final String NAME = "George";
public static final String BIRTHDAY = "1st June";
public static final String VERSION = "1";

}
6 changes: 6 additions & 0 deletions src/test/resources/test.jam
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ FD anon/Test$2 field I abc
MD anon/Test$1 name (II)V remapped
MD anon/Test$2 name (DD)V remapped
MD anon/Test$3 name (ZZ)V remapped

# Test 6. Import tests
CL com/example/pkg/Constants net/example/pkg/Util
CL com/example/ImportTest net/example/ImportTestNew
CL com/example/other/OtherClass net/example/newother/OtherClass
CL com/example/other/AnotherClass net/example/newother/AnotherClass

0 comments on commit 8672323

Please sign in to comment.