Don't remove keybindings of formatter and linter plugins if any document is still open.

Converting from UTF8 now checks for BOM UTF8 strings.
This commit is contained in:
Martín Lucas Golini
2023-09-15 23:47:47 -03:00
parent 044e289ba8
commit 2e80cdb77e
6 changed files with 60 additions and 23 deletions

View File

@@ -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 );
}

View File

@@ -2331,9 +2331,11 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) {
if ( !appInstance )
return;
UICodeEditor* editor = event->getNode()->asType<UICodeEditor>();
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 ) {

View File

@@ -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 {

View File

@@ -75,6 +75,7 @@ class FormatterPlugin : public Plugin {
protected:
std::vector<Formatter> mFormatters;
std::unordered_map<UICodeEditor*, std::vector<Uint32>> mEditors;
std::unordered_map<UICodeEditor*, TextDocument*> mEditorDocs;
std::mutex mWorkMutex;
std::condition_variable mWorkerCondition;
std::map<std::string, std::function<NativeFormatterResult( const std::string& file )>>

View File

@@ -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 );

View File

@@ -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 );