-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement validation file rename refactoring on method or class rename
- Loading branch information
1 parent
fc49eb9
commit c17fa0a
Showing
10 changed files
with
423 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/de/cronn/validation_files_diff/helper/PsiElementValidationFileFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package de.cronn.validation_files_diff.helper; | ||
|
||
import com.intellij.openapi.module.Module; | ||
import com.intellij.openapi.module.ModuleManager; | ||
import com.intellij.openapi.module.ModuleUtilCore; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.vfs.LocalFileSystem; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.openapi.vfs.VirtualFileManager; | ||
import com.intellij.psi.PsiClass; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiMethod; | ||
import de.cronn.validation_files_diff.ValidationDiffProjectOptionsProvider; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
public class PsiElementValidationFileFinder { | ||
|
||
private final Project project; | ||
private final Module module; | ||
private final PsiElement element; | ||
|
||
private PsiElementValidationFileFinder(PsiElement element, Project project, Module module) { | ||
this.project = project; | ||
this.module = module; | ||
this.element = element; | ||
} | ||
|
||
public static PsiElementValidationFileFinder of(PsiElement element) { | ||
Project project = element.getProject(); | ||
Module currentModule = ModuleUtilCore.findModuleForFile(element.getContainingFile()); | ||
return new PsiElementValidationFileFinder(element, project, currentModule); | ||
} | ||
|
||
public boolean hasCorrespondingValidationFiles() { | ||
return !findCorrespondingValidationFiles().isEmpty(); | ||
} | ||
|
||
public List<VirtualFile> findCorrespondingValidationFiles() { | ||
VirtualFile moduleRoot = getModuleRoot(); | ||
if (moduleRoot == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
String validationFilePrefix = parseValidationFilePrefix(); | ||
|
||
return getValidationFileRelatedDirectories() | ||
.map(moduleRoot::findFileByRelativePath) | ||
.filter(Objects::nonNull) | ||
.map(VirtualFile::getChildren) | ||
.flatMap(Arrays::stream) | ||
.filter(validationFile -> fileNameStartsWith(validationFile, validationFilePrefix)) | ||
.toList(); | ||
} | ||
|
||
private Stream<String> getValidationFileRelatedDirectories() { | ||
ValidationDiffProjectOptionsProvider options = ValidationDiffProjectOptionsProvider.getInstance(project); | ||
return Stream.of(options.getRelativeOutputDirPath(), options.getRelativeValidationDirPath()); | ||
} | ||
|
||
private boolean fileNameStartsWith(VirtualFile file, String prefix) { | ||
return file.getName().startsWith(prefix); | ||
} | ||
|
||
private String parseValidationFilePrefix() { | ||
if (element instanceof PsiMethod psiMethod) { | ||
PsiClass psiClass = psiMethod.getContainingClass(); | ||
return PsiTestNameUtils.getTestName(psiClass, psiMethod); | ||
} | ||
|
||
if (element instanceof PsiClass psiClass) { | ||
return PsiTestNameUtils.getTestClassName(psiClass); | ||
} | ||
return null; | ||
} | ||
|
||
private LocalFileSystem getLocalFileSystem() { | ||
return (LocalFileSystem) VirtualFileManager | ||
.getInstance() | ||
.getFileSystem(LocalFileSystem.PROTOCOL); | ||
} | ||
|
||
private VirtualFile getModuleRoot() { | ||
Module[] modules = ModuleManager.getInstance(project).getModules(); | ||
Path moduleRootPath = new ModuleAnalyser(module, modules).getMatchingContentRootForNextNonLeafModule(); | ||
if (moduleRootPath == null) { | ||
return null; | ||
} | ||
|
||
return getLocalFileSystem().findFileByNioFile(moduleRootPath); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/de/cronn/validation_files_diff/helper/PsiTestNameUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.cronn.validation_files_diff.helper; | ||
|
||
import com.intellij.psi.PsiClass; | ||
import com.intellij.psi.PsiMethod; | ||
|
||
public class PsiTestNameUtils { | ||
|
||
private PsiTestNameUtils() { | ||
} | ||
|
||
public static String getTestName(PsiClass psiClass, PsiMethod psiMethod) { | ||
return join(getTestClassName(psiClass), psiMethod.getName()); | ||
} | ||
|
||
public static String getTestClassName(PsiClass psiClass) { | ||
String nestingHierarchy = psiClass.getName(); | ||
PsiClass enclosingClass = psiClass.getContainingClass(); | ||
while (enclosingClass != null) { | ||
nestingHierarchy = join(enclosingClass.getName(), nestingHierarchy); | ||
enclosingClass = enclosingClass.getContainingClass(); | ||
} | ||
return nestingHierarchy; | ||
} | ||
|
||
private static String join(String element, String other) { | ||
return other.startsWith("_") ? (element + other) : (element + "_" + other); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/de/cronn/validation_files_diff/impl/ValidationFileAutomaticRenameFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package de.cronn.validation_files_diff.impl; | ||
|
||
import com.intellij.psi.PsiClass; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiMethod; | ||
import com.intellij.refactoring.rename.naming.AutomaticRenamer; | ||
import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory; | ||
import com.intellij.usageView.UsageInfo; | ||
import de.cronn.validation_files_diff.ValidationDiffApplicationOptionsProvider; | ||
import de.cronn.validation_files_diff.helper.PsiElementValidationFileFinder; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collection; | ||
|
||
public class ValidationFileAutomaticRenameFactory implements AutomaticRenamerFactory { | ||
|
||
private static final String OPTION_NAME = "Rename Validation-Files"; | ||
|
||
@Override | ||
public String getOptionName() { | ||
return OPTION_NAME; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return ValidationDiffApplicationOptionsProvider.getInstance().isRenamingValidationFilesEnabled(); | ||
} | ||
|
||
@Override | ||
public void setEnabled(boolean enabled) { | ||
ValidationDiffApplicationOptionsProvider.getInstance().setRenameValidationFilesEnabled(enabled); | ||
} | ||
|
||
@Override | ||
public @NotNull AutomaticRenamer createRenamer(PsiElement element, String newName, Collection<UsageInfo> usages) { | ||
return new ValidationFileAutomaticRenamer(element, newName); | ||
} | ||
|
||
@Override | ||
public boolean isApplicable(@NotNull PsiElement element) { | ||
if (!(element instanceof PsiMethod || element instanceof PsiClass)) { | ||
return false; | ||
} | ||
|
||
return PsiElementValidationFileFinder.of(element).hasCorrespondingValidationFiles(); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/de/cronn/validation_files_diff/impl/ValidationFileAutomaticRenamer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package de.cronn.validation_files_diff.impl; | ||
|
||
import com.intellij.openapi.util.NlsContexts; | ||
import com.intellij.psi.*; | ||
import com.intellij.refactoring.rename.naming.AutomaticRenamer; | ||
import de.cronn.validation_files_diff.helper.PsiElementValidationFileFinder; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Objects; | ||
|
||
public class ValidationFileAutomaticRenamer extends AutomaticRenamer { | ||
|
||
private static final boolean ARE_SELECTED_BY_DEFAULT = true; | ||
private static final String TITLE = "Rename Validation-/Temp-/Output-File"; | ||
private static final String DESCRIPTION = "These Files Are Corresponding to the Test"; | ||
private static final String ENTITY_NAME = "Validation/Tmp/Output File"; | ||
|
||
protected ValidationFileAutomaticRenamer(PsiElement element, String newName) { | ||
if (!(element instanceof PsiMethod || element instanceof PsiClass)) { | ||
return; | ||
} | ||
|
||
PsiNamedElement psiNamedElement = (PsiNamedElement) element; | ||
|
||
PsiManager psiManager = PsiManager.getInstance(element.getProject()); | ||
|
||
String oldName = psiNamedElement.getName(); | ||
PsiElementValidationFileFinder.of(element) | ||
.findCorrespondingValidationFiles() | ||
.stream() | ||
.map(psiManager::findFile) | ||
.filter(Objects::nonNull) | ||
.forEach(psiFile -> suggestToRenameValidationFile(psiFile, getNewName(psiFile.getName(), oldName, newName))); | ||
} | ||
|
||
@NotNull | ||
private static String getNewName(String currentName, String oldName, String newName) { | ||
return currentName.replaceFirst(oldName, newName); | ||
} | ||
|
||
private void suggestToRenameValidationFile(PsiFile psiFile, String newFileName) { | ||
myElements.add(psiFile); | ||
suggestAllNames(psiFile.getName(), newFileName); | ||
} | ||
|
||
@Override | ||
public @NlsContexts.DialogTitle String getDialogTitle() { | ||
return TITLE; | ||
} | ||
|
||
@Override | ||
public @NlsContexts.Button String getDialogDescription() { | ||
return DESCRIPTION; | ||
} | ||
|
||
@Override | ||
public @NlsContexts.ColumnName String entityName() { | ||
return ENTITY_NAME; | ||
} | ||
|
||
@Override | ||
public boolean isSelectedByDefault() { | ||
return ARE_SELECTED_BY_DEFAULT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
<vendor email="[email protected]" url="https://www.cronn.de">Cronn GmbH</vendor> | ||
|
||
<depends>com.intellij.modules.platform</depends> | ||
<depends>com.intellij.java</depends> | ||
|
||
<description><![CDATA[ | ||
<a href="https://github.com/cronn-de/validation-files-comparison-intellij-plugin">Plugin on GitHub</a> | | ||
|
@@ -55,6 +56,8 @@ | |
serviceImplementation="de.cronn.validation_files_diff.impl.ValidationDiffProjectOptionsProviderImpl"/> | ||
<applicationService serviceInterface="de.cronn.validation_files_diff.ValidationDiffApplicationOptionsProvider" | ||
serviceImplementation="de.cronn.validation_files_diff.impl.ValidationDiffApplicationOptionsProviderImpl"/> | ||
<automaticRenamerFactory | ||
implementation="de.cronn.validation_files_diff.impl.ValidationFileAutomaticRenameFactory"/> | ||
</extensions> | ||
|
||
<actions> | ||
|
Oops, something went wrong.