diff --git a/bin/assets/plugins/formatters.json b/bin/assets/plugins/formatters.json index 7cdcee000..847d58aa3 100644 --- a/bin/assets/plugins/formatters.json +++ b/bin/assets/plugins/formatters.json @@ -2,6 +2,9 @@ "config": { "auto_format_on_save": false }, + "keybindings": { + "format-doc": "alt+f" + }, "formatters": [ { "file_patterns": ["%.js$", "%.ts$"], diff --git a/include/eepp/ui/uicodeeditor.hpp b/include/eepp/ui/uicodeeditor.hpp index 37e17fd75..ed8f69683 100644 --- a/include/eepp/ui/uicodeeditor.hpp +++ b/include/eepp/ui/uicodeeditor.hpp @@ -31,7 +31,7 @@ class UIMenuItem; class UICodeEditorPlugin { public: - typedef std::function OnReadyCb; + typedef std::function OnReadyCb; virtual std::string getId() = 0; virtual std::string getTitle() = 0; virtual std::string getDescription() = 0; @@ -40,7 +40,6 @@ class UICodeEditorPlugin { virtual bool hasFileConfig() { return false; } virtual UIWindow* getGUIConfig() { return nullptr; } virtual std::string getFileConfigPath() { return ""; } - virtual void setOnReadyCallback( const OnReadyCb& cb ) { mOnReadyCallback = cb; }; virtual ~UICodeEditorPlugin() {} @@ -80,8 +79,23 @@ class UICodeEditorPlugin { virtual void minimapDrawAfterLineText( UICodeEditor*, const Int64&, const Vector2f&, const Vector2f&, const Float&, const Float& ){}; + Uint32 addOnReadyCallback( const OnReadyCb& cb ) { + mOnReadyCallbacks[mReadyCbNum++] = cb; + return mReadyCbNum; + }; + + void removeReadyCallback( const Uint32& id ) { mOnReadyCallbacks.erase( id ); } + protected: - OnReadyCb mOnReadyCallback; + Uint32 mReadyCbNum{ 0 }; + std::map mOnReadyCallbacks; + + void fireReadyCbs() { + auto cpyCbs = mOnReadyCallbacks; + for ( auto& cb : cpyCbs ) + if ( cb.second ) + cb.second( this, cb.first ); + } }; class EE_API DocEvent : public Event { diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 2443be02c..366851327 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -355,8 +355,9 @@ void App::initPluginManager() { onPluginEnabled( plugin ); } else { // If plugin loads asynchronously and is not ready, delay the plugin enabled callback - plugin->setOnReadyCallback( [&, plugin]( auto* ) { + plugin->addOnReadyCallback( [&]( UICodeEditorPlugin* plugin, const Uint32& cbId ) { mUISceneNode->runOnMainThread( [&, plugin]() { onPluginEnabled( plugin ); } ); + plugin->removeReadyCallback( cbId ); } ); } }; @@ -3903,6 +3904,8 @@ void App::init( const LogLevel& logLevel, std::string file, const Float& pidelDe mUISceneNode->addEventListener( Event::KeyDown, [&]( const Event* event ) { trySendUnlockedCmd( *static_cast( event ) ); } ); + if ( logLevel == LogLevel::Debug ) + mUISceneNode->setVerbose( true ); mDocInfo->setVisible( mConfig.editor.showDocInfo ); mProjectSplitter->setSplitPartition( diff --git a/src/tools/ecode/plugins/formatter/formatterplugin.cpp b/src/tools/ecode/plugins/formatter/formatterplugin.cpp index 8e9d75b3c..58537924e 100644 --- a/src/tools/ecode/plugins/formatter/formatterplugin.cpp +++ b/src/tools/ecode/plugins/formatter/formatterplugin.cpp @@ -55,12 +55,9 @@ FormatterPlugin::~FormatterPlugin() { } void FormatterPlugin::onRegister( UICodeEditor* editor ) { - Log::info( "FormatterPlugin::onRegister" ); mEditors.insert( editor ); for ( auto& kb : mKeyBindings ) { - Log::info( "FormatterPlugin::onRegister addKeybindString %s %s", kb.first.c_str(), - kb.second.c_str() ); editor->getKeyBindings().addKeybindString( kb.second, kb.first ); } @@ -74,7 +71,6 @@ void FormatterPlugin::onRegister( UICodeEditor* editor ) { } void FormatterPlugin::onUnregister( UICodeEditor* editor ) { - Log::info( "FormatterPlugin::onUnregister" ); for ( auto& kb : mKeyBindings ) { editor->getKeyBindings().removeCommandKeybind( kb.first ); if ( editor->hasDocument() ) @@ -130,12 +126,13 @@ void FormatterPlugin::loadFormatterConfig( const std::string& path ) { auto& config = j["config"]; if ( config.contains( "auto_format_on_save" ) ) setAutoFormatOnSave( config["auto_format_on_save"].get() ); - - mKeyBindings["format-doc"] = "alt+f"; - if ( config.contains( "keybindings" ) && config["keybindings"].contains( "format-doc" ) ) - mKeyBindings["format-doc"] = config["keybindings"]["format-doc"]; } + if ( mKeyBindings.empty() ) + mKeyBindings["format-doc"] = "alt+f"; + if ( j.contains( "keybindings" ) && j["keybindings"].contains( "format-doc" ) ) + mKeyBindings["format-doc"] = j["keybindings"]["format-doc"]; + if ( !j.contains( "formatters" ) ) return; @@ -200,8 +197,8 @@ void FormatterPlugin::load( const PluginManager* pluginManager ) { } } mReady = !mFormatters.empty(); - if ( mReady && mOnReadyCallback ) - mOnReadyCallback( this ); + if ( mReady ) + fireReadyCbs(); } bool FormatterPlugin::hasFileConfig() { @@ -269,8 +266,8 @@ void FormatterPlugin::formatDoc( UICodeEditor* editor ) { path = doc->getFilePath(); } - Log::debug( "FormatterPlugin::formatDoc for %s took %.2fms", path.c_str(), - clock.getElapsedTime().asMilliseconds() ); + Log::info( "FormatterPlugin::formatDoc for %s took %.2fms", path.c_str(), + clock.getElapsedTime().asMilliseconds() ); } void FormatterPlugin::runFormatter( UICodeEditor* editor, const Formatter& formatter, diff --git a/src/tools/ecode/plugins/linter/linterplugin.cpp b/src/tools/ecode/plugins/linter/linterplugin.cpp index 1f0a68fe0..2dc1b946d 100644 --- a/src/tools/ecode/plugins/linter/linterplugin.cpp +++ b/src/tools/ecode/plugins/linter/linterplugin.cpp @@ -181,8 +181,8 @@ void LinterPlugin::load( const PluginManager* pluginManager ) { } } mReady = !mLinters.empty(); - if ( mReady && mOnReadyCallback ) - mOnReadyCallback( this ); + if ( mReady ) + fireReadyCbs(); } void LinterPlugin::onRegister( UICodeEditor* editor ) { @@ -442,10 +442,10 @@ void LinterPlugin::runLinter( std::shared_ptr doc, const Linter& l invalidateEditors( doc.get() ); - Log::debug( "LinterPlugin::runLinter for %s took %.2fms. Found: %d matches. Errors: %d, " - "Warnings: %d, Notices: %d.", - path.c_str(), clock.getElapsedTime().asMilliseconds(), totalMatches, - totalErrors, totalWarns, totalNotice ); + Log::info( "LinterPlugin::runLinter for %s took %.2fms. Found: %d matches. Errors: %d, " + "Warnings: %d, Notices: %d.", + path.c_str(), clock.getElapsedTime().asMilliseconds(), totalMatches, totalErrors, + totalWarns, totalNotice ); } } diff --git a/src/tools/ecode/plugins/pluginmanager.cpp b/src/tools/ecode/plugins/pluginmanager.cpp index 84a3abc10..60b549fe4 100644 --- a/src/tools/ecode/plugins/pluginmanager.cpp +++ b/src/tools/ecode/plugins/pluginmanager.cpp @@ -135,6 +135,8 @@ PluginManager* PluginsModel::getManager() const { class UIPluginManagerTable : public UITableView { public: + std::map readyCbs; + UIPluginManagerTable() : UITableView() {} std::function onModelEnabledChange; @@ -241,9 +243,29 @@ UIWindow* UIPluginManager::New( UISceneNode* sceneNode, PluginManager* manager, prefs->setEnabled( manager->isEnabled( def->id ) && manager->get( def->id )->hasFileConfig() ); } ); - tv->onModelEnabledChange = [&, prefs, manager]( const std::string& id, bool enabled ) { - prefs->setEnabled( enabled && manager->get( id )->hasFileConfig() ); + tv->onModelEnabledChange = [&, prefs, manager, tv]( const std::string& id, bool enabled ) { + auto* plugin = manager->get( id ); + if ( enabled && !plugin->isReady() ) { + tv->readyCbs[id] = plugin->addOnReadyCallback( + [&, manager, prefs, tv]( UICodeEditorPlugin* plugin, const Uint32& cbId ) { + prefs->runOnMainThread( [prefs, manager, plugin]() { + prefs->setEnabled( manager->isEnabled( plugin->getId() ) && + plugin->hasFileConfig() ); + } ); + tv->readyCbs.erase( plugin->getId() ); + plugin->removeReadyCallback( cbId ); + } ); + } else { + prefs->setEnabled( enabled && plugin->hasFileConfig() ); + } }; + tv->addEventListener( Event::OnClose, [&, manager, tv]( const Event* ) { + for ( auto& cb : tv->readyCbs ) { + auto* plugin = manager->get( cb.first ); + if ( plugin ) + plugin->removeReadyCallback( cb.second ); + } + } ); win->center(); return win; }