diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 15e88c1cd..e9f0a9a34 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -443,7 +443,16 @@ void App::onPluginEnabled( UICodeEditorPlugin* plugin ) { } void App::initPluginManager() { - mPluginManager = std::make_unique( mResPath, mPluginsPath, mThreadPool ); + mPluginManager = std::make_unique( + mResPath, mPluginsPath, mThreadPool, [this]( const std::string& path, const auto& cb ) { + UITab* tab = mSplitter->isDocumentOpen( path ); + if ( !tab ) { + loadFileFromPath( path, true, nullptr, cb ); + } else { + tab->getTabWidget()->setTabSelected( tab ); + cb( tab->getOwnedWidget()->asType(), path ); + } + } ); mPluginManager->onPluginEnabled = [&]( UICodeEditorPlugin* plugin ) { if ( nullptr == mUISceneNode || plugin->isReady() ) { onPluginEnabled( plugin ); @@ -1226,7 +1235,7 @@ void App::loadFileFromPathOrFocus( const std::string& path ) { } void App::createPluginManagerUI() { - UIPluginManager::New( mUISceneNode, mPluginManager.get(), [&]( const std::string& path ) { + UIPluginManager::New( mUISceneNode, mPluginManager.get(), [this]( const std::string& path ) { loadFileFromPathOrFocus( path ); } )->showWhenReady(); } diff --git a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp index 4a3093e7d..771205771 100644 --- a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp +++ b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp @@ -314,20 +314,8 @@ PluginRequestHandle LSPClientPlugin::processTextDocumentSymbol( const PluginMess return { uri.toString() }; } -bool LSPClientPlugin::processDocumentFormattingResponse( const URI& uri, - std::vector edits ) { - auto doc = mManager->getSplitter()->findDocFromURI( uri ); - if ( !doc ) { - auto te = mManager->getSplitter()->loadFileFromPathInNewTab( uri.getFSPath() ); - if ( te.first == nullptr || te.second == nullptr || !te.second->getDocumentRef() ) - return false; - doc = te.second->getDocumentRef(); - } - - for ( const auto& edit : edits ) - if ( !edit.range.isValid() ) - return false; - +void processFormattingResponse( const std::shared_ptr& doc, + std::vector edits ) { TextRanges ranges = doc->getSelections(); doc->resetCursor(); @@ -352,6 +340,25 @@ bool LSPClientPlugin::processDocumentFormattingResponse( const URI& uri, doc->setSelection( ranges ); doc->setRunningTransaction( false ); +} + +bool LSPClientPlugin::processDocumentFormattingResponse( const URI& uri, + std::vector edits ) { + for ( const auto& edit : edits ) + if ( !edit.range.isValid() ) + return false; + + auto doc = mManager->getSplitter()->findDocFromURI( uri ); + if ( !doc ) { + mManager->getLoadFileFn()( uri.getFSPath(), [this, edits]( UICodeEditor* editor, auto ) { + auto documentRef = editor->getDocumentRef(); + mManager->getSplitter()->getUISceneNode()->runOnMainThread( + [documentRef, edits]() { processFormattingResponse( documentRef, edits ); } ); + } ); + return false; + } + + processFormattingResponse( doc, edits ); return true; } diff --git a/src/tools/ecode/plugins/pluginmanager.cpp b/src/tools/ecode/plugins/pluginmanager.cpp index 3b4cda3fe..1fd000a3b 100644 --- a/src/tools/ecode/plugins/pluginmanager.cpp +++ b/src/tools/ecode/plugins/pluginmanager.cpp @@ -10,8 +10,11 @@ using json = nlohmann::json; namespace ecode { PluginManager::PluginManager( const std::string& resourcesPath, const std::string& pluginsPath, - std::shared_ptr pool ) : - mResourcesPath( resourcesPath ), mPluginsPath( pluginsPath ), mThreadPool( pool ) {} + std::shared_ptr pool, const OnLoadFileCb& loadFileCb ) : + mResourcesPath( resourcesPath ), + mPluginsPath( pluginsPath ), + mThreadPool( pool ), + mLoadFileFn( loadFileCb ) {} PluginManager::~PluginManager() { mClosing = true; @@ -220,6 +223,10 @@ void PluginManager::unsubscribeMessages( const std::string& uniqueComponentId ) } } +const PluginManager::OnLoadFileCb& PluginManager::getLoadFileFn() const { + return mLoadFileFn; +} + void PluginManager::subscribeMessages( UICodeEditorPlugin* plugin, std::function cb ) { subscribeMessages( plugin->getId(), cb ); diff --git a/src/tools/ecode/plugins/pluginmanager.hpp b/src/tools/ecode/plugins/pluginmanager.hpp index 0c6b6c0ac..a5e6633b5 100644 --- a/src/tools/ecode/plugins/pluginmanager.hpp +++ b/src/tools/ecode/plugins/pluginmanager.hpp @@ -229,8 +229,11 @@ class PluginManager { return String::format( "%d.%d.%.d", major, minor, patch ); } + using OnFileLoadedCb = std::function; + using OnLoadFileCb = std::function; + PluginManager( const std::string& resourcesPath, const std::string& pluginsPath, - std::shared_ptr pool ); + std::shared_ptr pool, const OnLoadFileCb& loadFileCb ); ~PluginManager(); @@ -298,6 +301,8 @@ class PluginManager { FileSystemListener* getFileSystemListener() const { return mFileSystemListener; }; + const OnLoadFileCb& getLoadFileFn() const; + protected: using SubscribedPlugins = std::map>; @@ -313,6 +318,7 @@ class PluginManager { FileSystemListener* mFileSystemListener{ nullptr }; Mutex mSubscribedPluginsMutex; SubscribedPlugins mSubscribedPlugins; + OnLoadFileCb mLoadFileFn; bool mClosing{ false }; bool hasDefinition( const std::string& id ); @@ -386,6 +392,7 @@ class Plugin : public UICodeEditorPlugin { Uint64 mFileSystemListenerCb{ 0 }; std::string mConfigPath; FileInfo mConfigFileInfo; + bool mReady{ false }; bool mShuttingDown{ false }; }; diff --git a/src/tools/ecode/universallocator.cpp b/src/tools/ecode/universallocator.cpp index 5dbd31deb..23916281c 100644 --- a/src/tools/ecode/universallocator.cpp +++ b/src/tools/ecode/universallocator.cpp @@ -1,5 +1,6 @@ #include "universallocator.hpp" #include "ecode.hpp" +#include namespace ecode { @@ -481,20 +482,25 @@ void UniversalLocator::showOpenDocuments() { } std::shared_ptr UniversalLocator::openDocumentsModel( const std::string& match ) { - std::map docs; + std::vector docs; mApp->getSplitter()->forEachDoc( [&docs]( TextDocument& doc ) { - if ( doc.hasFilepath() ) { - docs.insert( { FileSystem::fileNameFromPath( doc.getFilePath() ), doc.getFilePath() } ); + if ( doc.hasFilepath() && + std::find( docs.begin(), docs.end(), doc.getFilePath() ) == docs.end() ) { + docs.push_back( doc.getFilePath() ); } } ); + std::sort( docs.begin(), docs.end(), []( const auto& left, const auto& right ) { + return FileSystem::fileNameFromPath( left ) < FileSystem::fileNameFromPath( right ); + } ); + std::vector files; std::vector names; for ( const auto& doc : docs ) { - names.emplace_back( std::move( doc.first ) ); - files.emplace_back( std::move( doc.second ) ); + names.emplace_back( FileSystem::fileNameFromPath( doc ) ); + files.emplace_back( std::move( doc ) ); } if ( match.empty() ) @@ -503,8 +509,8 @@ std::shared_ptr UniversalLocator::openDocumentsModel( const std:: std::multimap> matchesMap; for ( size_t i = 0; i < names.size(); i++ ) { - int matchName = String::fuzzyMatch( names[i], match ); - int matchPath = String::fuzzyMatch( files[i], match ); + int matchName = String::fuzzyMatch( names[i], match, true, true ); + int matchPath = String::fuzzyMatch( files[i], match, true, true ); matchesMap.insert( { std::max( matchName, matchPath ), i } ); }