@@ -26,23 +26,30 @@ a copy of this software and associated documentation files (the "Software"),
2626import com .intellij .diff .DiffManager ;
2727import com .intellij .diff .contents .DocumentContentImpl ;
2828import com .intellij .diff .requests .SimpleDiffRequest ;
29+ import com .intellij .ide .util .PropertiesComponent ;
2930import com .intellij .lang .Language ;
3031import com .intellij .openapi .actionSystem .AnActionEvent ;
3132import com .intellij .openapi .actionSystem .PlatformDataKeys ;
3233import com .intellij .openapi .application .ApplicationManager ;
34+ import com .intellij .openapi .command .WriteCommandAction ;
3335import com .intellij .openapi .compiler .CompileContext ;
3436import com .intellij .openapi .diagnostic .Logger ;
3537import com .intellij .openapi .editor .Editor ;
38+ import com .intellij .openapi .fileChooser .FileChooser ;
39+ import com .intellij .openapi .fileChooser .FileChooserDescriptor ;
3640import com .intellij .openapi .fileEditor .FileDocumentManager ;
3741import com .intellij .openapi .fileEditor .FileEditorManager ;
3842import com .intellij .openapi .fileTypes .FileType ;
3943import com .intellij .openapi .fileTypes .FileTypeManager ;
4044import com .intellij .openapi .module .Module ;
4145import com .intellij .openapi .project .Project ;
4246import com .intellij .openapi .roots .OrderEnumerator ;
47+ import com .intellij .openapi .roots .ProjectFileIndex ;
4348import com .intellij .openapi .roots .ProjectRootManager ;
4449import com .intellij .openapi .ui .Messages ;
50+ import com .intellij .openapi .vfs .LocalFileSystem ;
4551import com .intellij .openapi .vfs .VirtualFile ;
52+ import com .intellij .openapi .vfs .VirtualFileManager ;
4653import com .intellij .psi .*;
4754import com .intellij .psi .impl .file .PsiDirectoryFactory ;
4855import com .intellij .psi .search .FileTypeIndex ;
@@ -82,23 +89,58 @@ public static void generateNewTypescriptFile(String text, String fileName, Strin
8289 moveFileToGeneratedDir (file , project , module );
8390 if (alreadyExisting != null && alreadyExisting .size () > 0 ) {
8491 for (PsiFile origFile : alreadyExisting ) {
85- SimpleDiffRequest request = new SimpleDiffRequest (
86- "Reference already exists" ,
87- new DocumentContentImpl (PsiDocumentManager .getInstance (project ).getDocument (origFile )),
88- new DocumentContentImpl (PsiDocumentManager .getInstance (project ).getDocument (file )),
89- "Original File: " + origFile .getVirtualFile ().getPath ().substring (project .getBasePath ().length ()),
90- "Newly Generated File"
91- );
92-
93- DiffManager .getInstance ().showDiff (project , request );
92+ showDiff (project , file , origFile );
9493 }
9594 } else {
96- FileEditorManager .getInstance (project ).openFile (file .getVirtualFile (), true );
95+ ApplicationManager .getApplication ().invokeLater (() -> {
96+
97+ String oldPath = PropertiesComponent .getInstance (project ).getValue ("__lastSelTarget__" );
98+ VirtualFile vfile1 = null ;
99+ FileChooserDescriptor descriptor = new FileChooserDescriptor (false , true , false , false , false , false );
100+ descriptor .setTitle ("Select Generation Target Directory" );
101+ descriptor .setDescription ("Please choose a target directory" );
102+
103+
104+ if (!Strings .isNullOrEmpty (oldPath )) {
105+ VirtualFile storedPath = LocalFileSystem .getInstance ().findFileByPath (oldPath );
106+ vfile1 = FileChooser .chooseFile (descriptor , project , storedPath );
107+ } else {
108+ vfile1 = FileChooser .chooseFile (descriptor , project , module .getModuleFile ());
109+ }
110+ final VirtualFile vfile = vfile1 ;
111+
112+ PropertiesComponent .getInstance (project ).setValue ("__lastSelTarget__" , vfile .getPath ());
113+ if (vfile != null ) {
114+ WriteCommandAction .runWriteCommandAction (project , () -> {
115+
116+
117+ PsiDirectory dir = PsiDirectoryFactory .getInstance (project ).createDirectory (vfile );
118+ dir .add (file );
119+ FileEditorManager .getInstance (project ).openFile (file .getVirtualFile (), true );
120+
121+ });
122+ } else {
123+ FileEditorManager .getInstance (project ).openFile (file .getVirtualFile (), true );
124+ }
125+ });
126+
97127 }
98128
99129 });
100130 }
101131
132+ public static void showDiff (Project project , PsiFile file , PsiFile origFile ) {
133+ SimpleDiffRequest request = new SimpleDiffRequest (
134+ "Reference already exists" ,
135+ new DocumentContentImpl (PsiDocumentManager .getInstance (project ).getDocument (origFile )),
136+ new DocumentContentImpl (PsiDocumentManager .getInstance (project ).getDocument (file )),
137+ "Original File: " + origFile .getVirtualFile ().getPath ().substring (project .getBasePath ().length ()),
138+ "Newly Generated File"
139+ );
140+
141+ DiffManager .getInstance ().showDiff (project , request );
142+ }
143+
102144 public static URLClassLoader getClassLoader (CompileContext compileContext , Module module ) throws MalformedURLException {
103145 VirtualFile virtualFile = compileContext .getModuleOutputDirectoryForTests (module );
104146
0 commit comments