From a55728fc0fcc4862644fb1fd016fd58cfae1d19c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Fri, 7 Jan 2022 01:47:44 -0300 Subject: [PATCH] Improved formatter modelue. Updated efsw. --- bin/assets/formatter/formatter.json | 5 ++++ src/eepp/ui/uicodeeditor.cpp | 4 ++-- src/thirdparty/efsw | 2 +- src/tools/codeeditor/formattermodule.cpp | 30 +++++++++++++++++++----- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/bin/assets/formatter/formatter.json b/bin/assets/formatter/formatter.json index eb0837beb..3ed6733ac 100644 --- a/bin/assets/formatter/formatter.json +++ b/bin/assets/formatter/formatter.json @@ -6,5 +6,10 @@ { "file_patterns": ["%.cpp$", "%.h$", "%.hpp$"], "command": "clang-format --style=file $FILENAME" + }, + { + "file_patterns": ["%.py$", "%.pyw$"], + "command": "black $FILENAME", + "type": "inplace" } ] diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index 700619bbf..1d7652f83 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -1210,7 +1210,7 @@ void UICodeEditor::replaceKeyBinding( const KeyBindings::Shortcut& shortcut, void UICodeEditor::addKeyBindsString( const std::map& binds, const bool& allowLocked ) { mKeyBindings.addKeybindsString( binds ); - for ( auto bind : binds ) { + for ( const auto &bind : binds ) { if ( allowLocked ) { mUnlockedCmd.insert( bind.second ); } @@ -1220,7 +1220,7 @@ void UICodeEditor::addKeyBindsString( const std::map& void UICodeEditor::addKeyBinds( const std::map& binds, const bool& allowLocked ) { mKeyBindings.addKeybinds( binds ); - for ( auto bind : binds ) { + for ( const auto &bind : binds ) { if ( allowLocked ) { mUnlockedCmd.insert( bind.second ); } diff --git a/src/thirdparty/efsw b/src/thirdparty/efsw index 2bfda300a..50176c94f 160000 --- a/src/thirdparty/efsw +++ b/src/thirdparty/efsw @@ -1 +1 @@ -Subproject commit 2bfda300a9011a13a40988c8a99befe9e11e20cc +Subproject commit 50176c94f548b6ff0282d3c893025c16c234846b diff --git a/src/tools/codeeditor/formattermodule.cpp b/src/tools/codeeditor/formattermodule.cpp index 15ffe87ed..e009ec322 100644 --- a/src/tools/codeeditor/formattermodule.cpp +++ b/src/tools/codeeditor/formattermodule.cpp @@ -91,12 +91,15 @@ static std::string randString( size_t len ) { void FormatterModule::formatDoc( UICodeEditor* editor ) { if ( !mReady ) return; + + Clock clock; std::shared_ptr doc = editor->getDocumentRef(); auto formatter = supportsFormatter( doc ); - if ( formatter.command.empty() && doc->getFilePath().empty() ) + if ( formatter.command.empty() || doc->getFilePath().empty() ) return; IOStreamString fileString; - if ( doc->isDirty() || !doc->hasFilepath() ) { + std::string path; + if ( doc->isDirty() || !doc->hasFilepath() || formatter.type == FormatterType::Inplace ) { std::string tmpPath; if ( !doc->hasFilepath() ) { tmpPath = Sys::getTempPath() + ".ecode-" + doc->getFilename() + "." + randString( 8 ); @@ -110,15 +113,33 @@ void FormatterModule::formatDoc( UICodeEditor* editor ) { FileSystem::fileWrite( tmpPath, (Uint8*)fileString.getStreamPointer(), fileString.getSize() ); runFormatter( editor, formatter, tmpPath ); + + if ( formatter.type == FormatterType::Inplace ) { + std::string data; + FileSystem::fileGet( tmpPath, data ); + + editor->runOnMainThread( [&, data, editor]() { + std::shared_ptr doc = editor->getDocumentRef(); + TextPosition pos = doc->getSelection().start(); + doc->selectAll(); + doc->textInput( data ); + doc->setSelection( pos ); + } ); + } + FileSystem::fileRemove( tmpPath ); + path = tmpPath; } else { runFormatter( editor, formatter, doc->getFilePath() ); + path = doc->getFilePath(); } + + Log::info( "FormatterModule::formatDoc for %s took %.2fms", path.c_str(), + clock.getElapsedTime().asMilliseconds() ); } void FormatterModule::runFormatter( UICodeEditor* editor, const Formatter& formatter, const std::string& path ) { - Clock clock; std::string cmd( formatter.command ); String::replaceAll( cmd, "$FILENAME", path ); @@ -156,9 +177,6 @@ void FormatterModule::runFormatter( UICodeEditor* editor, const Formatter& forma doc->setSelection( pos ); } ); } - - Log::info( "FormatterModule::formatDoc for %s took %.2fms", path.c_str(), - clock.getElapsedTime().asMilliseconds() ); } }