Skip to content

Commit 323a3e1

Browse files
author
Roberto De Ioris
committed
added autopep8 to the python editor
1 parent 3b9b3f3 commit 323a3e1

File tree

10 files changed

+85
-4
lines changed

10 files changed

+85
-4
lines changed

Source/PythonEditor/Private/PythonEditorStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void FPythonEditorStyle::Initialize()
6060
StyleSet->Set("PythonEditor.Execute.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
6161
StyleSet->Set("PythonEditor.ExecuteInSandbox", new IMAGE_BRUSH("UI/Excute_x40", Icon40x40));
6262
StyleSet->Set("PythonEditor.ExecuteInSandbox.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
63+
StyleSet->Set("PythonEditor.PEP8ize", new IMAGE_BRUSH("UI/Excute_x40", Icon40x40));
64+
StyleSet->Set("PythonEditor.PEP8ize.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
6365
}
6466

6567
const FSlateFontInfo Consolas10 = TTF_FONT("Font/DroidSansMono", 9);

Source/PythonEditor/Private/PythonProjectEditor.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ void FPythonProjectEditor::BindCommands()
273273
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanExecute)
274274
);
275275

276+
ToolkitCommands->MapAction(FPythonProjectEditorCommands::Get().PEP8ize,
277+
FExecuteAction::CreateSP(this, &FPythonProjectEditor::PEP8ize_Internal),
278+
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanExecute)
279+
);
280+
276281
}
277282

278283
void FPythonProjectEditor::CloseFileForEditing(UPythonProjectItem* Item)
@@ -493,6 +498,11 @@ void FPythonProjectEditor::Execute_Internal()
493498
Execute();
494499
}
495500

501+
void FPythonProjectEditor::PEP8ize_Internal()
502+
{
503+
PEP8ize();
504+
}
505+
496506
void FPythonProjectEditor::ExecuteInSandbox_Internal()
497507
{
498508
Execute();
@@ -521,6 +531,16 @@ bool FPythonProjectEditor::ExecuteInSandbox()
521531
return true;
522532
}
523533

534+
bool FPythonProjectEditor::PEP8ize()
535+
{
536+
if (DocumentManager.IsValid() && DocumentManager->GetActiveTab().IsValid())
537+
{
538+
TSharedRef<SPythonEditor> PythonEditorRef = StaticCastSharedRef<SPythonEditor>(DocumentManager->GetActiveTab()->GetContent());
539+
PythonEditorRef->PEP8ize();
540+
}
541+
542+
return true;
543+
}
524544

525545
bool FPythonProjectEditor::CanSaveAll() const
526546
{

Source/PythonEditor/Private/PythonProjectEditor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
6363

6464
bool ExecuteInSandbox();
6565

66+
bool PEP8ize();
67+
6668
FString GetSafeName(bool IsDirectory);
6769

6870
private:
@@ -82,6 +84,8 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
8284

8385
void ExecuteInSandbox_Internal();
8486

87+
void PEP8ize_Internal();
88+
8589
bool CanNew() const;
8690

8791
bool CanSave() const;

Source/PythonEditor/Private/PythonProjectEditorCommands.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void FPythonProjectEditorCommands::RegisterCommands()
2222
UI_COMMAND(SaveAll, "Save All", "Save all open documents.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::S));
2323
UI_COMMAND(Execute, "Execute", "Execute Current Python File.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::Enter));
2424
UI_COMMAND(ExecuteInSandbox, "Execute In Sandbox", "Execute Current Python File in a Sandbox.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::Enter));
25+
UI_COMMAND(PEP8ize, "PEP8-ize", "Enforce PEP8 to the current code.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::P));
2526

2627
}
2728

Source/PythonEditor/Private/PythonProjectEditorCommands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class FPythonProjectEditorCommands : public TCommands<FPythonProjectEditorComman
1616
TSharedPtr<FUICommandInfo> SaveAll;
1717
TSharedPtr<FUICommandInfo> Execute;
1818
TSharedPtr<FUICommandInfo> ExecuteInSandbox;
19+
TSharedPtr<FUICommandInfo> PEP8ize;
1920
/** Initialize commands */
2021
virtual void RegisterCommands() override;
2122
};

Source/PythonEditor/Private/PythonProjectEditorToolbar.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void FPythonProjectEditorToolbar::FillEditorToolbar(FToolBarBuilder& ToolbarBuil
3737
{
3838
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().Execute);
3939
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().ExecuteInSandbox);
40+
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().PEP8ize);
4041
}
4142
ToolbarBuilder.EndSection();
4243

Source/PythonEditor/Private/SPythonEditor.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ void SPythonEditor::ExecuteInSandbox() const
120120
PythonModule.RunStringSandboxed(TCHAR_TO_UTF8(*SelectionString));
121121
}
122122

123+
void SPythonEditor::PEP8ize() const
124+
{
125+
Save();
126+
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
127+
128+
FString CleanedCode = PythonModule.Pep8ize(PythonEditableText->GetText().ToString());
129+
130+
PythonEditableText->SetText(FText::FromString(CleanedCode));
131+
}
132+
123133

124134
void SPythonEditor::GotoLineAndColumn(int32 LineNumber, int32 ColumnNumber)
125135
{

Source/PythonEditor/Private/SPythonEditor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class SPythonEditor : public SCompoundWidget
2020

2121
void GotoLineAndColumn(int32 LineNumber, int32 ColumnNumber);
2222

23+
void PEP8ize() const;
24+
2325
private:
2426
void OnTextChanged(const FText& NewText);
2527
FText GetLineAndColumn() const;

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void FUnrealEnginePythonModule::StartupModule()
161161
char *home = TCHAR_TO_UTF8(*IniValue);
162162
#endif
163163
Py_SetPythonHome(home);
164-
}
164+
}
165165

166166
if (GConfig->GetString(UTF8_TO_TCHAR("Python"), UTF8_TO_TCHAR("RelativeHome"), IniValue, GEngineIni)) {
167167
IniValue = FPaths::Combine(*FPaths::GameContentDir(), *IniValue);
@@ -286,6 +286,43 @@ void FUnrealEnginePythonModule::RunString(char *str) {
286286
Py_DECREF(eval_ret);
287287
}
288288

289+
FString FUnrealEnginePythonModule::Pep8ize(FString Code) {
290+
PyObject *pep8izer_module = PyImport_ImportModule("autopep8");
291+
if (!pep8izer_module) {
292+
unreal_engine_py_log_error();
293+
UE_LOG(LogPython, Error, TEXT("unable to load autopep8 module, please install it"));
294+
// return the original string to avoid losing data
295+
return Code;
296+
}
297+
298+
PyObject *pep8izer_func = PyObject_GetAttrString(pep8izer_module, (char*)"fix_code");
299+
if (!pep8izer_func) {
300+
unreal_engine_py_log_error();
301+
UE_LOG(LogPython, Error, TEXT("unable to get autopep8.fix_code function"));
302+
// return the original string to avoid losing data
303+
return Code;
304+
}
305+
306+
PyObject *ret = PyObject_CallFunction(pep8izer_func, "s", TCHAR_TO_UTF8(*Code));
307+
if (!ret) {
308+
unreal_engine_py_log_error();
309+
// return the original string to avoid losing data
310+
return Code;
311+
}
312+
313+
if (!PyUnicode_Check(ret)) {
314+
UE_LOG(LogPython, Error, TEXT("returned value is not a string"));
315+
// return the original string to avoid losing data
316+
return Code;
317+
}
318+
319+
char *pep8ized = PyUnicode_AsUTF8(ret);
320+
FString NewCode = FString(pep8ized);
321+
Py_DECREF(ret);
322+
323+
return NewCode;
324+
}
325+
289326
// run a python string in a new sub interpreter
290327
void FUnrealEnginePythonModule::RunStringSandboxed(char *str) {
291328
FScopePythonGIL gil;
@@ -345,7 +382,7 @@ void FUnrealEnginePythonModule::RunFile(char *filename) {
345382
if (!fd) {
346383
UE_LOG(LogPython, Error, TEXT("Unable to open file %s"), UTF8_TO_TCHAR(full_path));
347384
return;
348-
}
385+
}
349386
#endif
350387

351388
PyObject *eval_ret = PyRun_File(fd, full_path, Py_file_input, (PyObject *)main_dict, (PyObject *)local_dict);
@@ -396,7 +433,7 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void(*callback)
396433
Py_EndInterpreter(py_new_state);
397434
PyThreadState_Swap(_main);
398435
return;
399-
}
436+
}
400437
PyObject *global_dict = PyModule_GetDict(m);
401438

402439
#if PY_MAJOR_VERSION >= 3
@@ -441,7 +478,7 @@ void FUnrealEnginePythonModule::RunFileSandboxed(char *filename, void(*callback)
441478

442479
Py_EndInterpreter(py_new_state);
443480
PyThreadState_Swap(_main);
444-
}
481+
}
445482

446483
#undef LOCTEXT_NAMESPACE
447484

Source/UnrealEnginePython/Public/UnrealEnginePython.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
3838

3939
bool BrutalFinalize;
4040

41+
// pep8ize a string using various strategy (currently only autopep8 is supported)
42+
FString Pep8ize(FString Code);
43+
4144
private:
4245
void *ue_python_gil;
4346
// used by console

0 commit comments

Comments
 (0)