From 2e80cdb77edb1f9752cc5ed1a4c8659177eca080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Fri, 15 Sep 2023 23:47:47 -0300 Subject: [PATCH] Don't remove keybindings of formatter and linter plugins if any document is still open. Converting from UTF8 now checks for BOM UTF8 strings. --- src/eepp/core/string.cpp | 32 +++++++++++++++---- src/tools/ecode/ecode.cpp | 8 +++-- .../plugins/formatter/formatterplugin.cpp | 25 +++++++++++---- .../plugins/formatter/formatterplugin.hpp | 1 + .../ecode/plugins/linter/linterplugin.cpp | 14 ++++---- .../ecode/plugins/lsp/lspclientplugin.cpp | 3 +- 6 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src/eepp/core/string.cpp b/src/eepp/core/string.cpp index 348817623..70128cd5e 100644 --- a/src/eepp/core/string.cpp +++ b/src/eepp/core/string.cpp @@ -927,19 +927,32 @@ String::String( const char* utf8String ) { } String::String( const char* utf8String, const size_t& utf8StringSize ) { - if ( utf8String ) { - if ( utf8StringSize > 0 ) { - mString.reserve( utf8StringSize + 1 ); + if ( utf8String && utf8StringSize > 0 ) { + mString.reserve( utf8StringSize + 1 ); - Utf8::toUtf32( utf8String, utf8String + utf8StringSize, std::back_inserter( mString ) ); + int skip = 0; + // Skip BOM + if ( utf8StringSize >= 3 && (char)0xef == utf8String[0] && (char)0xbb == utf8String[1] && + (char)0xbf == utf8String[2] ) { + skip = 3; } + + Utf8::toUtf32( utf8String + skip, utf8String + utf8StringSize, + std::back_inserter( mString ) ); } } String::String( const std::string& utf8String ) { mString.reserve( utf8String.length() + 1 ); - Utf8::toUtf32( utf8String.begin(), utf8String.end(), std::back_inserter( mString ) ); + int skip = 0; + // Skip BOM + if ( utf8String.size() >= 3 && (char)0xef == utf8String[0] && (char)0xbb == utf8String[1] && + (char)0xbf == utf8String[2] ) { + skip = 3; + } + + Utf8::toUtf32( utf8String.begin() + skip, utf8String.end(), std::back_inserter( mString ) ); } String::String( const char* ansiString, const std::locale& locale ) { @@ -991,9 +1004,16 @@ String::String( const String& str ) : mString( str.mString ) {} String String::fromUtf8( const std::string& utf8String ) { String::StringType utf32; + // Skip BOM + int skip = 0; + if ( utf8String.size() >= 3 && (char)0xef == utf8String[0] && (char)0xbb == utf8String[1] && + (char)0xbf == utf8String[2] ) { + skip = 3; + } + utf32.reserve( utf8String.length() + 1 ); - Utf8::toUtf32( utf8String.begin(), utf8String.end(), std::back_inserter( utf32 ) ); + Utf8::toUtf32( utf8String.begin() + skip, utf8String.end(), std::back_inserter( utf32 ) ); return String( utf32 ); } diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index b43b82a68..6061d5e52 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -2331,9 +2331,11 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { if ( !appInstance ) return; UICodeEditor* editor = event->getNode()->asType(); - updateEditorTabTitle( editor ); - editor->getDocument().resetSyntax(); - editor->setSyntaxDefinition( editor->getDocument().getSyntaxDefinition() ); + editor->runOnMainThread( [this, editor] { + updateEditorTabTitle( editor ); + editor->getDocument().resetSyntax(); + editor->setSyntaxDefinition( editor->getDocument().getSyntaxDefinition() ); + } ); } ); auto docChanged = [this]( const Event* event ) { diff --git a/src/tools/ecode/plugins/formatter/formatterplugin.cpp b/src/tools/ecode/plugins/formatter/formatterplugin.cpp index ccdcfa755..e8c70c3d4 100644 --- a/src/tools/ecode/plugins/formatter/formatterplugin.cpp +++ b/src/tools/ecode/plugins/formatter/formatterplugin.cpp @@ -81,6 +81,12 @@ void FormatterPlugin::onRegister( UICodeEditor* editor ) { tryRequestCapabilities( editor->getDocumentRef() ); } ) ); + listeners.push_back( + editor->addEventListener( Event::OnDocumentChanged, [&, editor]( const Event* ) { + TextDocument* newDoc = editor->getDocumentRef().get(); + mEditorDocs[editor] = newDoc; + } ) ); + listeners.push_back( editor->addEventListener( Event::OnDocumentSave, [this]( const Event* event ) { if ( mAutoFormatOnSave && event->getNode()->isType( UI_TYPE_CODEEDITOR ) ) { @@ -92,15 +98,10 @@ void FormatterPlugin::onRegister( UICodeEditor* editor ) { } ) ); mEditors.insert( { editor, listeners } ); + mEditorDocs[editor] = editor->getDocumentRef().get(); } void FormatterPlugin::onUnregister( UICodeEditor* editor ) { - for ( auto& kb : mKeyBindings ) { - editor->getKeyBindings().removeCommandKeybind( kb.first ); - if ( editor->hasDocument() ) - editor->getDocument().removeCommand( kb.first ); - } - auto afIt = mIsAutoFormatting.find( &editor->getDocument() ); if ( afIt != mIsAutoFormatting.end() ) mIsAutoFormatting.erase( afIt ); @@ -112,6 +113,18 @@ void FormatterPlugin::onUnregister( UICodeEditor* editor ) { if ( mShuttingDown ) return; mEditors.erase( editor ); + mEditorDocs.erase( editor ); + + TextDocument* doc = &editor->getDocument(); + for ( auto editorIt : mEditorDocs ) + if ( editorIt.second == doc ) + return; + + for ( auto& kb : mKeyBindings ) { + editor->getKeyBindings().removeCommandKeybind( kb.first ); + if ( editor->hasDocument() ) + editor->getDocument().removeCommand( kb.first ); + } } bool FormatterPlugin::getAutoFormatOnSave() const { diff --git a/src/tools/ecode/plugins/formatter/formatterplugin.hpp b/src/tools/ecode/plugins/formatter/formatterplugin.hpp index 94cda90eb..04fe905df 100644 --- a/src/tools/ecode/plugins/formatter/formatterplugin.hpp +++ b/src/tools/ecode/plugins/formatter/formatterplugin.hpp @@ -75,6 +75,7 @@ class FormatterPlugin : public Plugin { protected: std::vector mFormatters; std::unordered_map> mEditors; + std::unordered_map mEditorDocs; std::mutex mWorkMutex; std::condition_variable mWorkerCondition; std::map> diff --git a/src/tools/ecode/plugins/linter/linterplugin.cpp b/src/tools/ecode/plugins/linter/linterplugin.cpp index 8ddea51cd..7bc78bbe7 100644 --- a/src/tools/ecode/plugins/linter/linterplugin.cpp +++ b/src/tools/ecode/plugins/linter/linterplugin.cpp @@ -642,14 +642,9 @@ void LinterPlugin::onRegister( UICodeEditor* editor ) { } void LinterPlugin::onUnregister( UICodeEditor* editor ) { - for ( auto& kb : mKeyBindings ) { - editor->getKeyBindings().removeCommandKeybind( kb.first ); - if ( editor->hasDocument() ) - editor->getDocument().removeCommand( kb.first ); - } - if ( mShuttingDown ) return; + Lock l( mDocMutex ); TextDocument* doc = mEditorDocs[editor]; auto cbs = mEditors[editor]; @@ -660,6 +655,13 @@ void LinterPlugin::onUnregister( UICodeEditor* editor ) { for ( auto editorIt : mEditorDocs ) if ( editorIt.second == doc ) return; + + for ( auto& kb : mKeyBindings ) { + editor->getKeyBindings().removeCommandKeybind( kb.first ); + if ( editor->hasDocument() ) + editor->getDocument().removeCommand( kb.first ); + } + mDocs.erase( doc ); mDirtyDoc.erase( doc ); Lock matchesLock( mMatchesMutex ); diff --git a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp index 58a5a6271..67dea6535 100644 --- a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp +++ b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp @@ -1282,10 +1282,9 @@ void LSPClientPlugin::onUnregister( UICodeEditor* editor ) { return; } - if ( editor->hasDocument() ) { + if ( editor->hasDocument() ) for ( auto& kb : mKeyBindings ) editor->getDocument().removeCommand( kb.first ); - } { Lock lds( mDocSymbolsMutex );