Skip to content

Commit

Permalink
Optimize file creation logic and path handling
Browse files Browse the repository at this point in the history
1. Improved the determination logic of the target directory and handled a variety of project structures
2. Move the file creation operation to DumbService.runWhenSmart to improve IDE responsiveness
  • Loading branch information
zhaopengjun committed Sep 2, 2024
1 parent 8e27862 commit 1010459
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/main/java/com/zhongan/devpilot/util/NewFileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ private static String handleGeneratedJavaFileName(String generatedText, String f

private static PsiDirectory handleGeneratedJavaPackageName(Project project, String generatedText, String fileUrl) {
String result = StringUtils.EMPTY;
String target = "src" + File.separator + "main" + File.separator + "java";
String target = File.separator + "src" + File.separator + "main" + File.separator + "java";
if (StringUtils.isNotEmpty(fileUrl) && fileUrl.contains(target)) {
// Find the project root path for the currently selected code
result = fileUrl.substring(0, fileUrl.indexOf(target) + target.length());
result = fileUrl.substring(0, fileUrl.indexOf(target));
if (StringUtils.isNotEmpty(result)) {
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByPath(result);
if (vf == null) {
Expand All @@ -118,14 +118,16 @@ private static PsiDirectory handleGeneratedJavaPackageName(Project project, Stri
}
// When the root path is obtained, append the sourceDirectory
if (StringUtils.isNotEmpty(result)) {
result += File.separator + target + File.separator;
result += target + File.separator;
}
if (StringUtils.isEmpty(result)) {
// Try to determine whether the target exists in the root path. If the target exists, use it directly
result = project.getBasePath() + File.separator;
result = project.getBasePath();
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByPath(result + target);
if (null != vf) {
result += target + File.separator;
} else {
result += File.separator;
}
}
// Append PackageDirectory
Expand Down Expand Up @@ -166,10 +168,13 @@ private static void openAndWriteFile(Project project, String generatedText, Stri
FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(fileExtension.substring(1));
PsiDirectory finalSelectedFileDir = targetFilePsiDir;
String finalFileName = fileName;
WriteCommandAction.runWriteCommandAction(project, () -> {
PsiFile fileFromText = PsiFileFactory.getInstance(project).createFileFromText(finalFileName, fileType, generatedText);
PsiFile createdFile = (PsiFile) finalSelectedFileDir.add(fileFromText);
FileEditorManager.getInstance(project).openFile(createdFile.getVirtualFile(), true);

DumbService.getInstance(project).runWhenSmart(() -> {
WriteCommandAction.runWriteCommandAction(project, () -> {
PsiFile fileFromText = PsiFileFactory.getInstance(project).createFileFromText(finalFileName, fileType, generatedText);
PsiFile createdFile = (PsiFile) finalSelectedFileDir.add(fileFromText);
FileEditorManager.getInstance(project).openFile(createdFile.getVirtualFile(), true);
});
});
}

Expand Down

0 comments on commit 1010459

Please sign in to comment.