@@ -221,6 +221,10 @@ void FPythonProjectEditor::BindCommands()
221221 FExecuteAction::CreateSP (this , &FPythonProjectEditor::New_Internal),
222222 FCanExecuteAction::CreateSP (this , &FPythonProjectEditor::CanNew)
223223 );
224+ ToolkitCommands->MapAction (FPythonProjectEditorCommands::Get ().NewDirectory ,
225+ FExecuteAction::CreateSP (this , &FPythonProjectEditor::NewDirectory_Internal),
226+ FCanExecuteAction::CreateSP (this , &FPythonProjectEditor::CanNew)
227+ );
224228 ToolkitCommands->MapAction (FPythonProjectEditorCommands::Get ().Delete ,
225229 FExecuteAction::CreateSP (this , &FPythonProjectEditor::Delete_Internal),
226230 FCanExecuteAction::CreateSP (this , &FPythonProjectEditor::CanDelete)
@@ -299,24 +303,41 @@ TSharedRef<SWidget> FPythonProjectEditor::CreatePythonEditorWidget(TSharedRef<FT
299303 return SNew (SPythonEditor, Item);
300304}
301305
302- FString FPythonProjectEditor::GetNoneRepeatName ( )
306+ FString FPythonProjectEditor::GetSafeName ( bool IsDirectory )
303307{
304308 UPythonProject* PythonProject = PythonProjectBeingEdited.Get ();
305309
310+ TArray<UPythonProjectItem*> selItems = MyPythonProjectEditor->GetSelectedItems ();
311+ FString basePath = PythonProject->Path ;
312+ if (selItems.Num () > 0 ) {
313+ if (selItems[0 ]->Type == EPythonProjectItemType::Folder) {
314+ basePath = selItems[0 ]->Path ;
315+ }
316+ }
317+
306318 int suf = 2 ;
307319 FString scriptName;
308320 while (true )
309321 {
310322
311323 FString SufStr = FString::Printf (TEXT (" %d" ), suf);
312- scriptName = PythonProject->Path / TEXT (" PythonScript" ) + SufStr + TEXT (" .py" );
313- if (!FPaths::FileExists (scriptName)) {
314- return scriptName;
324+ if (!IsDirectory) {
325+ scriptName = basePath / TEXT (" PythonScript" ) + SufStr + TEXT (" .py" );
326+ if (!FPaths::FileExists (scriptName)) {
327+ return scriptName;
328+ }
329+ }
330+ else {
331+ scriptName = basePath / TEXT (" NewDirectory" ) + SufStr;
332+ if (!FPaths::DirectoryExists (scriptName)) {
333+ return scriptName;
334+ }
315335 }
316336
317337 suf++;
318338 }
319339}
340+
320341void FPythonProjectEditor::New_Internal ()
321342{
322343 New ();
@@ -326,7 +347,7 @@ bool FPythonProjectEditor::New()
326347{
327348 IPlatformFile& PlatformFile = FPlatformFileManager::Get ().GetPlatformFile ();
328349
329- FString scriptName = GetNoneRepeatName ( );
350+ FString scriptName = GetSafeName ( false );
330351 IFileHandle* fileHandle = PlatformFile.OpenWrite (*scriptName);
331352 if (fileHandle == NULL ) {
332353 return false ;
@@ -335,7 +356,26 @@ bool FPythonProjectEditor::New()
335356 UPythonProject* PythonProject = PythonProjectBeingEdited.Get ();
336357 PythonProject->RescanChildren ();
337358 UPythonProjectItem* item = MyPythonProjectEditor->SelectByPath (scriptName);
338- OpenFileForEditing (item);
359+ if (item)
360+ OpenFileForEditing (item);
361+ return true ;
362+ }
363+
364+ void FPythonProjectEditor::NewDirectory_Internal ()
365+ {
366+ NewDirectory ();
367+ }
368+
369+ bool FPythonProjectEditor::NewDirectory ()
370+ {
371+ IPlatformFile& PlatformFile = FPlatformFileManager::Get ().GetPlatformFile ();
372+
373+ FString dirName = GetSafeName (true );
374+ if (!PlatformFile.CreateDirectory (*dirName)) {
375+ return false ;
376+ }
377+ UPythonProject* PythonProject = PythonProjectBeingEdited.Get ();
378+ PythonProject->RescanChildren ();
339379 return true ;
340380}
341381
0 commit comments