From af284a4c1b7d3f6031d52db661804c14169f2b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 25 May 2023 22:51:23 -0300 Subject: [PATCH] Crash fix when formatting a document while searching for a string in the document. --- include/eepp/ui/doc/syntaxtokenizer.hpp | 3 +++ src/eepp/ui/uicodeeditor.cpp | 15 +++++++++++++-- src/tools/ecode/ecode.hpp | 3 +++ .../ecode/plugins/formatter/formatterplugin.cpp | 6 ++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/eepp/ui/doc/syntaxtokenizer.hpp b/include/eepp/ui/doc/syntaxtokenizer.hpp index 56a4709a2..4e01fc6b2 100644 --- a/include/eepp/ui/doc/syntaxtokenizer.hpp +++ b/include/eepp/ui/doc/syntaxtokenizer.hpp @@ -19,6 +19,9 @@ struct EE_API SyntaxToken { }; struct EE_API SyntaxTokenPosition { + // TODO: type should be the hash of the name of the type. Using std::string takes at least + // 40 bytes per token vs 4 bytes. It's much easier to debug a string than a hash and that's + // the reason why we keep it for the moment. std::string type; Int64 pos{ 0 }; size_t len{ 0 }; diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index a41f20e08..e4a8bbfa6 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -165,6 +165,11 @@ UICodeEditor::UICodeEditor( const bool& autoRegisterBaseCommands, UICodeEditor( "codeeditor", autoRegisterBaseCommands, autoRegisterBaseKeybindings ) {} UICodeEditor::~UICodeEditor() { + if ( getUISceneNode()->hasThreadPool() ) { + Uint64 tag = reinterpret_cast( this ); + getUISceneNode()->getThreadPool()->removeWithTag( tag ); + } + if ( mCurrentMenu ) { mCurrentMenu->clearEventListener(); mCurrentMenu = nullptr; @@ -1587,8 +1592,7 @@ void UICodeEditor::onDocumentLineCountChange( const size_t&, const size_t& ) { void UICodeEditor::onDocumentLineChanged( const Int64& lineNumber ) { mDoc->getHighlighter()->invalidate( lineNumber ); - if ( !mHighlightWord.isEmpty() ) - updateHighlightWordCache(); + updateHighlightWordCache(); } void UICodeEditor::onDocumentUndoRedo( const TextDocument::UndoRedo& ) { @@ -2513,11 +2517,16 @@ const TextSearchParams& UICodeEditor::getHighlightWord() const { } void UICodeEditor::updateHighlightWordCache() { + if ( mHighlightWord.isEmpty() ) + return; + if ( getUISceneNode()->hasThreadPool() ) { Uint64 tag = reinterpret_cast( this ); getUISceneNode()->getThreadPool()->removeWithTag( tag ); getUISceneNode()->getThreadPool()->run( [this]() { + if ( mDoc->isRunningTransaction() ) + return; mHighlightWordProcessing = true; mHighlightWordCache = mDoc->findAll( mHighlightWord.escapeSequences ? String::unescape( mHighlightWord.text ) @@ -2527,6 +2536,8 @@ void UICodeEditor::updateHighlightWordCache() { }, [this]( const auto& ) { mHighlightWordProcessing = false; }, tag ); } else { + if ( mDoc->isRunningTransaction() ) + return; mHighlightWordCache = mDoc->findAll( mHighlightWord.escapeSequences ? String::unescape( mHighlightWord.text ) : mHighlightWord.text, diff --git a/src/tools/ecode/ecode.hpp b/src/tools/ecode/ecode.hpp index 40f16a3ab..22e5b252c 100644 --- a/src/tools/ecode/ecode.hpp +++ b/src/tools/ecode/ecode.hpp @@ -236,6 +236,9 @@ class App : public UICodeEditorSplitter::Client { t.setCommand( "open-command-palette", [&] { mUniversalLocator->showCommandPalette(); } ); t.setCommand( "project-build-start", [&] { if ( mProjectBuildManager && mStatusBuildOutputController ) { + if ( mProjectBuildManager->isBuilding() ) { + mProjectBuildManager->cancelBuild(); + } mProjectBuildManager->buildCurrentConfig( mStatusBuildOutputController.get() ); } } ); diff --git a/src/tools/ecode/plugins/formatter/formatterplugin.cpp b/src/tools/ecode/plugins/formatter/formatterplugin.cpp index fafea5771..00dd7788e 100644 --- a/src/tools/ecode/plugins/formatter/formatterplugin.cpp +++ b/src/tools/ecode/plugins/formatter/formatterplugin.cpp @@ -369,6 +369,7 @@ void FormatterPlugin::formatDoc( UICodeEditor* editor ) { auto pos = doc->getSelection(); auto scroll = editor->getScroll(); doc->selectAll(); + doc->setRunningTransaction( true ); doc->textInput( data ); doc->setSelection( pos ); editor->setScroll( scroll ); @@ -377,6 +378,7 @@ void FormatterPlugin::formatDoc( UICodeEditor* editor ) { doc->save(); mIsAutoFormatting[doc.get()] = false; } + doc->setRunningTransaction( false ); } ); } @@ -402,9 +404,11 @@ void FormatterPlugin::runFormatter( UICodeEditor* editor, const Formatter& forma TextPosition pos = doc->getSelection().start(); auto scroll = editor->getScroll(); doc->selectAll(); + doc->setRunningTransaction( true ); doc->textInput( res.result ); doc->setSelection( pos ); editor->setScroll( scroll ); + doc->setRunningTransaction( false ); } ); return; } @@ -441,9 +445,11 @@ void FormatterPlugin::runFormatter( UICodeEditor* editor, const Formatter& forma TextPosition pos = doc->getSelection().start(); auto scroll = editor->getScroll(); doc->selectAll(); + doc->setRunningTransaction( true ); doc->textInput( data ); doc->setSelection( pos ); editor->setScroll( scroll ); + doc->setRunningTransaction( false ); } ); } }