Minor refactor.

This commit is contained in:
Martín Lucas Golini
2023-12-10 21:22:26 -03:00
parent 5ff638bf7a
commit efd8cc10d8
13 changed files with 283 additions and 249 deletions
+13 -8
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 12.0.0, 2023-11-28T21:49:34. -->
<!-- Written by QtCreator 12.0.0, 2023-12-10T21:22:03. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@@ -98,6 +98,10 @@
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
<valuemap type="QVariantMap" key="ClangdSettings">
<value type="bool" key="blockIndexing">false</value>
<value type="bool" key="useGlobalSettings">true</value>
</valuemap>
<valuemap type="QVariantMap" key="CppEditor.QuickFix">
<value type="bool" key="UseGlobalSettings">true</value>
</valuemap>
@@ -979,10 +983,11 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">eepp-physics-debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="int" key="RunConfiguration.UseCppDebugger">1</value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">true</value>
<value type="int" key="RunConfiguration.UseCppDebugger">0</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="int" key="RunConfiguration.UseQmlDebugger">1</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">false</value>
<value type="bool" key="RunConfiguration.UseTerminal">false</value>
<value type="QString" key="RunConfiguration.WorkingDirectory">%{buildDir}../../../bin/</value>
</valuemap>
@@ -1146,10 +1151,10 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">eepp-ui-perf-test-debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="int" key="RunConfiguration.UseCppDebugger">1</value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">true</value>
<value type="int" key="RunConfiguration.UseCppDebugger">0</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">false</value>
<value type="int" key="RunConfiguration.UseQmlDebugger">0</value>
<value type="int" key="RunConfiguration.UseQmlDebugger">1</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">false</value>
<value type="bool" key="RunConfiguration.UseTerminal">false</value>
<value type="QString" key="RunConfiguration.WorkingDirectory">%{buildDir}../../../bin/</value>
@@ -1167,7 +1172,7 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">ecode-debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="bool" key="ProjectExplorer.RunConfiguration.Customized">true</value>
<value type="QString" key="RunConfiguration.Arguments">--css=/root/.config/ecode/style.css</value>
<value type="int" key="RunConfiguration.UseCppDebugger">0</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">false</value>
+2
View File
@@ -1387,6 +1387,8 @@
../../src/tools/ecode/plugins/lsp/lspdocumentclient.cpp
../../src/tools/ecode/plugins/lsp/lspdocumentclient.hpp
../../src/tools/ecode/plugins/lsp/lspprotocol.hpp
../../src/tools/ecode/plugins/plugin.cpp
../../src/tools/ecode/plugins/plugin.hpp
../../src/tools/ecode/plugins/pluginmanager.cpp
../../src/tools/ecode/plugins/pluginmanager.hpp
../../src/tools/ecode/plugins/xmltools/xmltoolsplugin.cpp
+2 -2
View File
@@ -1705,9 +1705,9 @@ Float UINode::convertLength( const CSS::StyleSheetLength& length,
while ( NULL != node ) {
if ( node->isWidget() ) {
std::string fontSizeStr( node->asType<UIWidget>()->getPropertyString(
fontSizeStr = node->asType<UIWidget>()->getPropertyString(
CSS::StyleSheetSpecification::instance()->getProperty(
(Uint32)PropertyId::FontSize ) ) );
(Uint32)PropertyId::FontSize ) );
if ( !fontSizeStr.empty() ) {
Float num;
if ( String::fromString( num, fontSizeStr ) ) {
@@ -2,6 +2,7 @@
#define ECODE_AUTOCOMPLETEPLUGIN_HPP
#include "../lsp/lspprotocol.hpp"
#include "../plugin.hpp"
#include "../pluginmanager.hpp"
#include <eepp/config.hpp>
#include <eepp/system/clock.hpp>
@@ -1,6 +1,7 @@
#ifndef ECODE_FORMATTERPLUGIN_HPP
#define ECODE_FORMATTERPLUGIN_HPP
#include "../plugin.hpp"
#include "../pluginmanager.hpp"
#include <eepp/config.hpp>
#include <eepp/system/mutex.hpp>
@@ -1,6 +1,7 @@
#ifndef ECODE_LINTERPLUGIN_HPP
#define ECODE_LINTERPLUGIN_HPP
#include "../plugin.hpp"
#include "../pluginmanager.hpp"
#include <eepp/config.hpp>
#include <eepp/system/mutex.hpp>
@@ -1,6 +1,7 @@
#ifndef ECODE_LSPPLUGIN_HPP
#define ECODE_LSPPLUGIN_HPP
#include "../plugin.hpp"
#include "../pluginmanager.hpp"
#include "lspclientservermanager.hpp"
#include <eepp/config.hpp>
@@ -12,8 +13,6 @@
#include <eepp/ui/doc/syntaxdefinitionmanager.hpp>
#include <eepp/ui/uicodeeditor.hpp>
#include <eepp/ui/uilistview.hpp>
#include <set>
#include <unordered_set>
using namespace EE;
using namespace EE::System;
using namespace EE::UI;
+159
View File
@@ -0,0 +1,159 @@
#include "plugin.hpp"
#include "pluginmanager.hpp"
#include <eepp/system/filesystem.hpp>
namespace ecode {
Plugin::Plugin( PluginManager* manager ) :
mManager( manager ),
mThreadPool( manager->getThreadPool() ),
mReady( false ), // All plugins will start as not ready until proved the contrary
mLoading( true ) // All plugins will start as loading until the load is complete, this is to
// avoid concurrency issues
{}
bool Plugin::isReady() const {
return mReady;
}
bool Plugin::isLoading() const {
return mLoading;
}
bool Plugin::isShuttingDown() const {
return mShuttingDown;
}
bool Plugin::hasFileConfig() {
return !mConfigPath.empty();
}
void Plugin::subscribeFileSystemListener() {
mConfigFileInfo = FileInfo( mConfigPath );
mManager->subscribeFileSystemListener( this );
}
void Plugin::unsubscribeFileSystemListener() {
mManager->unsubscribeFileSystemListener( this );
}
std::string Plugin::getFileConfigPath() {
return mConfigPath;
}
PluginManager* Plugin::getManager() const {
return mManager;
}
void Plugin::onFileSystemEvent( const FileEvent& ev, const FileInfo& file ) {
if ( ev.type != FileSystemEventType::Modified || mShuttingDown || isLoading() )
return;
if ( file.getFilepath() != mConfigPath ||
file.getModificationTime() == mConfigFileInfo.getModificationTime() )
return;
std::string fileContents;
FileSystem::fileGet( file.getFilepath(), fileContents );
if ( getConfigFileHash() != String::hash( fileContents ) ) {
if ( mManager->isPluginReloadEnabled() && !isLoading() && isReady() ) {
mConfigFileInfo = file;
unsubscribeFileSystemListener();
mManager->reload( getId() );
} else {
Log::debug( "Plugin %s: Configuration file has been modified: %s. But "
"plugin reload is not enabled.",
getTitle().c_str(), mConfigPath.c_str() );
}
} else {
Log::debug( "Plugin %s: Configuration file has been modified: %s. But contents "
"are the same.",
getTitle().c_str(), mConfigPath.c_str() );
}
}
void Plugin::setReady() {
if ( mReady ) {
Log::info( "Plugin: %s loaded and ready from process %u", getTitle().c_str(),
Sys::getProcessID() );
}
}
PluginBase::~PluginBase() {
mShuttingDown = true;
unsubscribeFileSystemListener();
for ( auto editor : mEditors ) {
onBeforeUnregister( editor.first );
for ( auto listener : editor.second )
editor.first->removeEventListener( listener );
editor.first->unregisterPlugin( this );
}
}
void PluginBase::onRegister( UICodeEditor* editor ) {
Lock l( mMutex );
std::vector<Uint32> listeners;
listeners.push_back(
editor->addEventListener( Event::OnDocumentLoaded, [this, editor]( const Event* event ) {
Lock l( mMutex );
const DocEvent* docEvent = static_cast<const DocEvent*>( event );
mDocs.insert( docEvent->getDoc() );
mEditorDocs[editor] = docEvent->getDoc();
onDocumentLoaded( docEvent->getDoc() );
} ) );
listeners.push_back(
editor->addEventListener( Event::OnDocumentClosed, [this]( const Event* event ) {
{
Lock l( mMutex );
const DocEvent* docEvent = static_cast<const DocEvent*>( event );
TextDocument* doc = docEvent->getDoc();
onDocumentClosed( doc );
onUnregisterDocument( doc );
mDocs.erase( doc );
}
} ) );
listeners.push_back(
editor->addEventListener( Event::OnDocumentChanged, [&, editor]( const Event* ) {
TextDocument* oldDoc = mEditorDocs[editor];
TextDocument* newDoc = editor->getDocumentRef().get();
Lock l( mMutex );
mDocs.erase( oldDoc );
mDocs.insert( newDoc );
mEditorDocs[editor] = newDoc;
onDocumentChanged( editor, oldDoc );
} ) );
onRegisterListeners( editor, listeners );
mEditors.insert( { editor, listeners } );
if ( mDocs.count( editor->getDocumentRef().get() ) == 0 ) {
mDocs.insert( editor->getDocumentRef().get() );
onRegisterDocument( editor->getDocumentRef().get() );
}
mEditorDocs[editor] = editor->getDocumentRef().get();
}
void PluginBase::onUnregister( UICodeEditor* editor ) {
onBeforeUnregister( editor );
if ( mShuttingDown )
return;
Lock l( mMutex );
TextDocument* doc = mEditorDocs[editor];
auto cbs = mEditors[editor];
for ( auto listener : cbs )
editor->removeEventListener( listener );
onUnregisterEditor( editor );
mEditors.erase( editor );
mEditorDocs.erase( editor );
for ( auto editorIt : mEditorDocs )
if ( editorIt.second == doc )
return;
onUnregisterDocument( doc );
mDocs.erase( doc );
}
} // namespace ecode
+99
View File
@@ -0,0 +1,99 @@
#ifndef ECODE_PLUGIN_HPP
#define ECODE_PLUGIN_HPP
#include <eepp/ui/models/filesystemmodel.hpp>
#include <eepp/ui/uicodeeditor.hpp>
using namespace EE;
using namespace EE::UI;
using namespace EE::UI::Models;
namespace ecode {
class PluginManager;
class Plugin : public UICodeEditorPlugin {
public:
explicit Plugin( PluginManager* manager );
void subscribeFileSystemListener();
void unsubscribeFileSystemListener();
bool isReady() const;
bool isLoading() const;
bool isShuttingDown() const;
virtual bool hasFileConfig();
virtual std::string getFileConfigPath();
PluginManager* getManager() const;
virtual String::HashType getConfigFileHash() { return 0; }
virtual void onFileSystemEvent( const FileEvent& ev, const FileInfo& file );
protected:
PluginManager* mManager{ nullptr };
std::shared_ptr<ThreadPool> mThreadPool;
std::string mConfigPath;
FileInfo mConfigFileInfo;
std::atomic<bool> mReady{ false };
std::atomic<bool> mLoading{ false };
std::atomic<bool> mShuttingDown{ false };
void setReady();
};
class PluginBase : public Plugin {
public:
explicit PluginBase( PluginManager* manager ) : Plugin( manager ) {}
virtual ~PluginBase();
virtual void onRegister( UICodeEditor* ) override;
virtual void onUnregister( UICodeEditor* ) override;
virtual String::HashType getConfigFileHash() override { return mConfigHash; }
protected:
//! Keep track of the registered editors + all the listeners registered to each editor
UnorderedMap<UICodeEditor*, std::vector<Uint32>> mEditors;
//! Keep track of the documents opened
UnorderedSet<TextDocument*> mDocs;
//! Documents and Editors mutex
Mutex mMutex;
//! Keep track of the document pointer of each editor
UnorderedMap<UICodeEditor*, TextDocument*> mEditorDocs;
//! Keep track of the key bindings managed by the plugin
std::map<std::string, std::string> mKeyBindings; /* cmd, shortcut */
//! If the configuration is stored in a file, keep track of the config hash
String::HashType mConfigHash{ 0 };
virtual void onDocumentLoaded( TextDocument* ){};
virtual void onDocumentClosed( TextDocument* ){};
virtual void onDocumentChanged( UICodeEditor*, TextDocument* /*oldDoc*/ ){};
virtual void onRegisterListeners( UICodeEditor*, std::vector<Uint32>& /*listeners*/ ){};
//! Usually used to remove keybindings in an editor
virtual void onBeforeUnregister( UICodeEditor* ){};
virtual void onRegisterDocument( TextDocument* ){};
virtual void onUnregisterEditor( UICodeEditor* ){};
//! Usually used to unregister commands in a document
virtual void onUnregisterDocument( TextDocument* ){};
};
} // namespace ecode
#endif
+2 -153
View File
@@ -1,5 +1,6 @@
#include "pluginmanager.hpp"
#include "../filesystemlistener.hpp"
#include "plugin.hpp"
#include "pluginmanager.hpp"
#include <eepp/system/filesystem.hpp>
#include <eepp/ui/uicheckbox.hpp>
#include <eepp/ui/uitableview.hpp>
@@ -480,156 +481,4 @@ UIWindow* UIPluginManager::New( UISceneNode* sceneNode, PluginManager* manager,
return win;
}
Plugin::Plugin( PluginManager* manager ) :
mManager( manager ),
mThreadPool( manager->getThreadPool() ),
mReady( false ), // All plugins will start as not ready until proved the contrary
mLoading( true ) // All plugins will start as loading until the load is complete, this is to
// avoid concurrency issues
{}
bool Plugin::isReady() const {
return mReady;
}
bool Plugin::isLoading() const {
return mLoading;
}
bool Plugin::isShuttingDown() const {
return mShuttingDown;
}
bool Plugin::hasFileConfig() {
return !mConfigPath.empty();
}
void Plugin::subscribeFileSystemListener() {
mConfigFileInfo = FileInfo( mConfigPath );
mManager->subscribeFileSystemListener( this );
}
void Plugin::unsubscribeFileSystemListener() {
mManager->unsubscribeFileSystemListener( this );
}
std::string Plugin::getFileConfigPath() {
return mConfigPath;
}
PluginManager* Plugin::getManager() const {
return mManager;
}
void Plugin::onFileSystemEvent( const FileEvent& ev, const FileInfo& file ) {
if ( ev.type != FileSystemEventType::Modified || mShuttingDown || isLoading() )
return;
if ( file.getFilepath() != mConfigPath ||
file.getModificationTime() == mConfigFileInfo.getModificationTime() )
return;
std::string fileContents;
FileSystem::fileGet( file.getFilepath(), fileContents );
if ( getConfigFileHash() != String::hash( fileContents ) ) {
if ( mManager->isPluginReloadEnabled() && !isLoading() && isReady() ) {
mConfigFileInfo = file;
unsubscribeFileSystemListener();
mManager->reload( getId() );
} else {
Log::debug( "Plugin %s: Configuration file has been modified: %s. But "
"plugin reload is not enabled.",
getTitle().c_str(), mConfigPath.c_str() );
}
} else {
Log::debug( "Plugin %s: Configuration file has been modified: %s. But contents "
"are the same.",
getTitle().c_str(), mConfigPath.c_str() );
}
}
void Plugin::setReady() {
if ( mReady ) {
Log::info( "Plugin: %s loaded and ready from process %u", getTitle().c_str(),
Sys::getProcessID() );
}
}
PluginBase::~PluginBase() {
mShuttingDown = true;
unsubscribeFileSystemListener();
for ( auto editor : mEditors ) {
onBeforeUnregister( editor.first );
for ( auto listener : editor.second )
editor.first->removeEventListener( listener );
editor.first->unregisterPlugin( this );
}
}
void PluginBase::onRegister( UICodeEditor* editor ) {
Lock l( mMutex );
std::vector<Uint32> listeners;
listeners.push_back(
editor->addEventListener( Event::OnDocumentLoaded, [this, editor]( const Event* event ) {
Lock l( mMutex );
const DocEvent* docEvent = static_cast<const DocEvent*>( event );
mDocs.insert( docEvent->getDoc() );
mEditorDocs[editor] = docEvent->getDoc();
onDocumentLoaded( docEvent->getDoc() );
} ) );
listeners.push_back(
editor->addEventListener( Event::OnDocumentClosed, [this]( const Event* event ) {
{
Lock l( mMutex );
const DocEvent* docEvent = static_cast<const DocEvent*>( event );
TextDocument* doc = docEvent->getDoc();
onDocumentClosed( doc );
onUnregisterDocument( doc );
mDocs.erase( doc );
}
} ) );
listeners.push_back(
editor->addEventListener( Event::OnDocumentChanged, [&, editor]( const Event* ) {
TextDocument* oldDoc = mEditorDocs[editor];
TextDocument* newDoc = editor->getDocumentRef().get();
Lock l( mMutex );
mDocs.erase( oldDoc );
mDocs.insert( newDoc );
mEditorDocs[editor] = newDoc;
onDocumentChanged( editor, oldDoc );
} ) );
onRegisterListeners( editor, listeners );
mEditors.insert( { editor, listeners } );
if ( mDocs.count( editor->getDocumentRef().get() ) == 0 ) {
mDocs.insert( editor->getDocumentRef().get() );
onRegisterDocument( editor->getDocumentRef().get() );
}
mEditorDocs[editor] = editor->getDocumentRef().get();
}
void PluginBase::onUnregister( UICodeEditor* editor ) {
onBeforeUnregister( editor );
if ( mShuttingDown )
return;
Lock l( mMutex );
TextDocument* doc = mEditorDocs[editor];
auto cbs = mEditors[editor];
for ( auto listener : cbs )
editor->removeEventListener( listener );
onUnregisterEditor( editor );
mEditors.erase( editor );
mEditorDocs.erase( editor );
for ( auto editorIt : mEditorDocs )
if ( editorIt.second == doc )
return;
onUnregisterDocument( doc );
mDocs.erase( doc );
}
} // namespace ecode
-82
View File
@@ -407,88 +407,6 @@ class UIPluginManager {
std::function<void( const std::string& )> loadFileCb );
};
class Plugin : public UICodeEditorPlugin {
public:
explicit Plugin( PluginManager* manager );
void subscribeFileSystemListener();
void unsubscribeFileSystemListener();
bool isReady() const;
bool isLoading() const;
bool isShuttingDown() const;
virtual bool hasFileConfig();
virtual std::string getFileConfigPath();
PluginManager* getManager() const;
virtual String::HashType getConfigFileHash() { return 0; }
virtual void onFileSystemEvent( const FileEvent& ev, const FileInfo& file );
protected:
PluginManager* mManager{ nullptr };
std::shared_ptr<ThreadPool> mThreadPool;
std::string mConfigPath;
FileInfo mConfigFileInfo;
std::atomic<bool> mReady{ false };
std::atomic<bool> mLoading{ false };
std::atomic<bool> mShuttingDown{ false };
void setReady();
};
class PluginBase : public Plugin {
public:
explicit PluginBase( PluginManager* manager ) : Plugin( manager ) {}
virtual ~PluginBase();
virtual void onRegister( UICodeEditor* ) override;
virtual void onUnregister( UICodeEditor* ) override;
virtual String::HashType getConfigFileHash() override { return mConfigHash; }
protected:
//! Keep track of the registered editors + all the listeners registered to each editor
std::unordered_map<UICodeEditor*, std::vector<Uint32>> mEditors;
//! Keep track of the documents opened
std::set<TextDocument*> mDocs;
//! Documents and Editors mutex
Mutex mMutex;
//! Keep track of the document pointer of each editor
std::unordered_map<UICodeEditor*, TextDocument*> mEditorDocs;
//! Keep track of the key bindings managed by the plugin
std::map<std::string, std::string> mKeyBindings; /* cmd, shortcut */
//! If the configuration is stored in a file, keep track of the config hash
String::HashType mConfigHash{ 0 };
virtual void onDocumentLoaded( TextDocument* ){};
virtual void onDocumentClosed( TextDocument* ){};
virtual void onDocumentChanged( UICodeEditor*, TextDocument* /*oldDoc*/ ){};
virtual void onRegisterListeners( UICodeEditor*, std::vector<Uint32>& /*listeners*/ ){};
//! Usually used to remove keybindings in an editor
virtual void onBeforeUnregister( UICodeEditor* ){};
virtual void onRegisterDocument( TextDocument* ){};
virtual void onUnregisterEditor( UICodeEditor* ){};
//! Usually used to unregister commands in a document
virtual void onUnregisterDocument( TextDocument* ){};
};
} // namespace ecode
#endif // ECODE_PLUGINMANAGER_HPP
@@ -1,12 +1,12 @@
#ifndef ECODE_XMLTOOLSPLUGIN_HPP
#define ECODE_XMLTOOLSPLUGIN_HPP
#include "../plugin.hpp"
#include "../pluginmanager.hpp"
#include <eepp/config.hpp>
#include <eepp/system/mutex.hpp>
#include <eepp/system/threadpool.hpp>
#include <eepp/ui/uicodeeditor.hpp>
#include <set>
using namespace EE;
using namespace EE::System;
using namespace EE::UI;