Skip to content

Commit

Permalink
fixed package rename with multiple matches in path, fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Sep 22, 2024
1 parent c15f3b0 commit ea4c80f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rename.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
build:
strategy:
matrix:
haxe-version: ['4.3.4', 'nightly']
platform: [ubuntu-latest, macos-latest, windows-latest]
haxe-version: ['4.3.6', 'nightly']
platform: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Set Git Auto CRLF to false on Windows
Expand Down
6 changes: 5 additions & 1 deletion src/refactor/rename/RenamePackage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ class RenamePackage {

var dotPath:String = path.dir.replace("/", ".").replace("\\", ".");

var index:Int = dotPath.indexOf(packageName);
var index:Int = dotPath.lastIndexOf("." + packageName) + 1;
if (index < 0) {
index = dotPath.indexOf(packageName);
}

var pathParts:Array<String> = context.what.toName.split(".");

var rootPath:String = Path.join(dotPath.substr(0, index).split("."));
Expand Down
21 changes: 15 additions & 6 deletions test/refactor/PackageTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class PackageTest extends TestBase {

public function testRenameTypesModul(async:Async) {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Types.Type3", 53, 73),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Types", 82, 96),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Type1", 289, 303),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Types.Type3", 75, 95),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Types", 104, 118),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.Type1", 311, 325),
makeMoveTestEdit("testcases/packages/Types.hx", "testcases/packages/sub/Types.hx"),
makeReplaceTestEdit("testcases/packages/Types.hx", "packages.sub", 8, 16),

Expand All @@ -19,7 +19,7 @@ class PackageTest extends TestBase {

public function testRenameMoreTypesModul(async:Async) {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.MoreTypes", 26, 44),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.MoreTypes", 48, 66),
makeMoveTestEdit("testcases/packages/MoreTypes.hx", "testcases/packages/sub/MoreTypes.hx"),
makeReplaceTestEdit("testcases/packages/MoreTypes.hx", "packages.sub", 8, 16),
];
Expand All @@ -29,8 +29,8 @@ class PackageTest extends TestBase {
public function testRenameOtherTypesModul(async:Async) {
var edits:Array<TestEdit> = [
makeInsertTestEdit("testcases/packages/Main.hx", "import packages.sub.OtherTypes;\n", 19),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.OtherTypes", 202, 221),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.OtherTypes.OtherTypeA", 240, 270),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.OtherTypes", 224, 243),
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.OtherTypes.OtherTypeA", 262, 292),
makeMoveTestEdit("testcases/packages/OtherTypes.hx", "testcases/packages/sub/OtherTypes.hx"),
makeReplaceTestEdit("testcases/packages/OtherTypes.hx", "packages.sub", 8, 16),
];
Expand All @@ -45,4 +45,13 @@ class PackageTest extends TestBase {
];
refactorAndCheck({fileName: "testcases/packages/HelperTypes.hx", toName: "packages.sub", pos: 12}, edits, async);
}

public function testRenameHelperECTypesModul(async:Async) {
var edits:Array<TestEdit> = [
makeReplaceTestEdit("testcases/packages/Main.hx", "packages.sub.ECTypeA", 26, 39),
makeMoveTestEdit("testcases/packages/kages/ECTypes.hx", "testcases/packages/packages/sub/ECTypes.hx"),
makeReplaceTestEdit("testcases/packages/kages/ECTypes.hx", "packages.sub", 8, 13),
];
refactorAndCheck({fileName: "testcases/packages/kages/ECTypes.hx", toName: "packages.sub", pos: 10}, edits, async);
}
}
2 changes: 2 additions & 0 deletions testcases/packages/Main.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package packages;

import kages.ECTypeA;
import packages.MoreTypes;
import packages.Types.Type3;
import packages.Types;
Expand All @@ -12,5 +13,6 @@ class Main {
var test4:packages.OtherTypes.OtherTypeA = {};
var test5:packages.Type1 = {};
var test6:HelperTypes;
var test7:ECTypeA;
}
}
4 changes: 4 additions & 0 deletions testcases/packages/kages/ECTypes.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kages;

typedef ECTypeA = {}
typedef ECTypeB = {}

0 comments on commit ea4c80f

Please sign in to comment.