Skip to content

Commit

Permalink
rename file when renaming class and both names are identical.
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Jul 13, 2024
1 parent 7162fff commit 49b56dc
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.intellij.plugins.haxe.HaxeLanguage;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.SearchScope;
import com.intellij.refactoring.listeners.RefactoringElementListener;
Expand Down Expand Up @@ -133,10 +134,29 @@ public void renameElement(@NotNull PsiElement element,
// the PomRenameProcessor stating that it could handle the rename after we've already substituted the
// proper elements. But since we are handling it anyway the PomRenameProcessor is cut out of the loop.
if (canBeRenamed(element)) {

PsiFile containingFile = element.getContainingFile();
if (shouldRenameFile(element, containingFile)) {
containingFile.setName(newName + ".hx");
}

super.renameElement(element, newName, usages, listener);
}
}

private boolean shouldRenameFile(PsiElement element, PsiFile containingFile) {
//TODO make it possible to only rename file ?
if (element instanceof HaxeClass haxeClass) {
String fileName = containingFile.getName();
String typeName = haxeClass.getName();

String expectedName = fileName.replace(".hx", "");
return expectedName.equals(typeName);
}
return false;
}


@Override
public void prepareRenaming(@NotNull PsiElement element, @NotNull String newName, @NotNull Map<PsiElement, String> allRenames) {
super.prepareRenaming(element, newName, allRenames);
Expand All @@ -147,6 +167,9 @@ public void prepareRenaming(@NotNull PsiElement element,
@NotNull String newName,
@NotNull Map<PsiElement, String> allRenames,
@NotNull SearchScope scope) {



super.prepareRenaming(element, newName, allRenames, scope);
}
}

0 comments on commit 49b56dc

Please sign in to comment.