From ce47145ba92fb289d04538c2d5c6e06360419caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 23 Jan 2025 00:44:37 -0300 Subject: [PATCH] Fix / improve line comments toggle (SpartanJ/ecode#384). Hide debugger panel when opening a single file. --- include/eepp/ui/doc/textdocument.hpp | 7 +- src/eepp/ui/doc/textdocument.cpp | 83 ++++++++++++++----- .../debugger/debuggerclientlistener.cpp | 3 + .../ecode/plugins/debugger/debuggerplugin.cpp | 14 +++- 4 files changed, 77 insertions(+), 30 deletions(-) diff --git a/include/eepp/ui/doc/textdocument.hpp b/include/eepp/ui/doc/textdocument.hpp index dafc0947a..abd3e0842 100644 --- a/include/eepp/ui/doc/textdocument.hpp +++ b/include/eepp/ui/doc/textdocument.hpp @@ -175,7 +175,7 @@ class EE_API TextDocument { const TextRange& getSelection() const; - const TextRange& getSelectionIndex( const size_t& index ) const; + TextRange getSelectionIndex( const size_t& index, bool sort = false ) const; TextDocumentLine& line( const size_t& index ); @@ -745,10 +745,11 @@ class EE_API TextDocument { void notifyFoldRegionsUpdated( size_t oldCount, size_t newCount ); - void insertAtStartOfSelectedLines( const String& text, bool skipEmpty ); + void insertAtStartOfSelectedLines( const String& text, bool skipEmpty, Int64 startFrom = 0, + std::size_t cursorIndex = 0 ); void removeFromStartOfSelectedLines( const String& text, bool skipEmpty, - bool removeExtraSpaces = false ); + bool removeExtraSpaces = false, Int64 startFrom = 0 ); /** @return The number of lines removed (complete lines, not modified lines) */ size_t remove( const size_t& cursorIdx, TextRange range, UndoStackContainer& undoStack, diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index a036dc908..6d4fc845d 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -989,9 +989,9 @@ TextRanges TextDocument::getSelectionsSorted() const { return selections; } -const TextRange& TextDocument::getSelectionIndex( const size_t& index ) const { +TextRange TextDocument::getSelectionIndex( const size_t& index, bool sort ) const { eeASSERT( index < mSelection.size() ); - return mSelection[index]; + return sort ? mSelection[index].normalized() : mSelection[index]; } const TextRange& TextDocument::getSelection() const { @@ -2170,23 +2170,26 @@ void TextDocument::newLineAbove() { } } -void TextDocument::insertAtStartOfSelectedLines( const String& text, bool skipEmpty ) { +void TextDocument::insertAtStartOfSelectedLines( const String& text, bool skipEmpty, + Int64 startFrom, std::size_t cursorIndex ) { BoolScopedOpOptional op( !mDoingTextInput, mDoingTextInput, true ); TextPosition prevStart = getSelection().start(); - TextRange range = getSelection( true ); + TextRange range = getSelectionIndex( cursorIndex, true ); bool swap = prevStart != range.start(); for ( auto i = range.start().line(); i <= range.end().line(); i++ ) { const String& line = this->line( i ).getText(); - if ( !skipEmpty || line.length() != 1 ) { - insert( 0, { i, 0 }, text ); + if ( ( !skipEmpty || line.length() != 1 ) && startFrom >= 0 && + startFrom <= static_cast( line.length() ) ) { + insert( 0, { i, startFrom }, text ); } } - setSelection( TextPosition( range.start().line(), range.start().column() + text.size() ), + setSelection( cursorIndex, + TextPosition( range.start().line(), range.start().column() + text.size() ), TextPosition( range.end().line(), range.end().column() + text.size() ), swap ); } void TextDocument::removeFromStartOfSelectedLines( const String& text, bool skipEmpty, - bool removeExtraSpaces ) { + bool removeExtraSpaces, Int64 startFrom ) { BoolScopedOpOptional op( !mDoingTextInput, mDoingTextInput, true ); TextPosition prevStart = getSelection().start(); TextRange range = getSelection( true ); @@ -2197,8 +2200,11 @@ void TextDocument::removeFromStartOfSelectedLines( const String& text, bool skip for ( auto i = range.start().line(); i <= range.end().line(); i++ ) { const String& line = this->line( i ).getText(); if ( !skipEmpty || line.length() > 1 ) { - if ( line.substr( 0, text.length() ) == text ) { - remove( 0, { { i, 0 }, { i, static_cast( text.length() ) } } ); + if ( startFrom < 0 || startFrom > static_cast( line.length() ) ) + continue; + if ( line.substr( startFrom, text.length() ) == text ) { + remove( 0, { { i, startFrom }, + { i, static_cast( startFrom + text.length() ) } } ); if ( i == range.start().line() ) { startRemoved = text.size(); } else if ( i == range.end().line() ) { @@ -3403,20 +3409,51 @@ void TextDocument::toggleLineComments() { std::string comment = mSyntaxDefinition.getComment(); if ( comment.empty() ) return; - std::string commentText = comment + " "; - TextRange selection = getSelection( true ); - bool uncomment = true; - for ( Int64 i = selection.start().line(); i <= selection.end().line(); i++ ) { - const String& text = mLines[i].getText(); - if ( text.find_first_not_of( " \t\n" ) != std::string::npos && - text.find( commentText ) == std::string::npos ) { - uncomment = false; + const std::string commentText = comment + " "; + Int64 commentLength = static_cast( commentText.size() ); + + BoolScopedOpOptional op( !mDoingTextInput, mDoingTextInput, true ); + + for ( size_t cursorIdx = 0; cursorIdx < mSelection.size(); cursorIdx++ ) { + TextRange selection = getSelectionIndex( cursorIdx, true ); + bool uncomment = false; + + std::size_t startSpaces = std::string::npos; + for ( Int64 i = selection.start().line(); i <= selection.end().line(); i++ ) { + const String& text = mLines[i].getText(); + auto firstNonSpace = text.find_first_not_of( " \t\n" ); + if ( firstNonSpace != std::string::npos && text[firstNonSpace] != '\n' ) { + startSpaces = eemin( startSpaces, firstNonSpace ); + if ( text.find( commentText, firstNonSpace ) != std::string::npos ) { + uncomment = true; + break; + } + } + } + + if ( startSpaces == std::string::npos ) + startSpaces = 0; + + if ( uncomment ) { + TextPosition prevStart = getSelectionIndex( cursorIdx ).start(); + TextRange range = getSelectionIndex( cursorIdx, true ); + bool swap = prevStart != range.start(); + for ( Int64 lineIdx = selection.start().line(); lineIdx <= selection.end().line(); + lineIdx++ ) { + const String& text = mLines[lineIdx].getText(); + auto pos = text.find( commentText, startSpaces ); + if ( pos != std::string::npos ) { + remove( cursorIdx, { { lineIdx, static_cast( pos ) }, + { lineIdx, static_cast( pos + commentLength ) } } ); + } + } + setSelection( + cursorIdx, + TextPosition( range.start().line(), range.start().column() - commentLength ), + TextPosition( range.end().line(), range.end().column() - commentLength ), swap ); + } else { + insertAtStartOfSelectedLines( commentText, true, startSpaces, cursorIdx ); } - } - if ( uncomment ) { - removeFromStartOfSelectedLines( commentText, true ); - } else { - insertAtStartOfSelectedLines( commentText, true ); } } diff --git a/src/tools/ecode/plugins/debugger/debuggerclientlistener.cpp b/src/tools/ecode/plugins/debugger/debuggerclientlistener.cpp index c1ee6d01c..4b178ddbe 100644 --- a/src/tools/ecode/plugins/debugger/debuggerclientlistener.cpp +++ b/src/tools/ecode/plugins/debugger/debuggerclientlistener.cpp @@ -44,6 +44,9 @@ DebuggerClientListener::~DebuggerClientListener() { void DebuggerClientListener::initUI() { auto sdc = getStatusDebuggerController(); + if ( sdc == nullptr ) + return; + sdc->createWidget(); mPlugin->getManager()->getPluginContext()->getStatusAppOutputController()->initNewOutput( diff --git a/src/tools/ecode/plugins/debugger/debuggerplugin.cpp b/src/tools/ecode/plugins/debugger/debuggerplugin.cpp index 6847e1bd8..8011ab7de 100644 --- a/src/tools/ecode/plugins/debugger/debuggerplugin.cpp +++ b/src/tools/ecode/plugins/debugger/debuggerplugin.cpp @@ -1,4 +1,3 @@ -#include "debuggerplugin.hpp" #include "../../notificationcenter.hpp" #include "../../projectbuild.hpp" #include "../../terminalmanager.hpp" @@ -8,6 +7,7 @@ #include "bussocket.hpp" #include "bussocketprocess.hpp" #include "dap/debuggerclientdap.hpp" +#include "debuggerplugin.hpp" #include "models/breakpointsmodel.hpp" #include "models/processesmodel.hpp" #include "models/variablesmodel.hpp" @@ -750,6 +750,9 @@ void DebuggerPlugin::buildSidePanelTab() { updateSidePanelTab(); updateSelectedDebugConfig(); + + if ( mProjectPath.empty() ) + hideSidePanel(); } void DebuggerPlugin::updateSelectedDebugConfig() { @@ -1018,7 +1021,8 @@ void DebuggerPlugin::updateDebuggerConfigurationList() { std::vector DebuggerPlugin::replaceKeyInString( std::string val, int randomPort, const std::unordered_map& solvedInputs ) { - auto runConfig = getPluginContext()->getProjectBuildManager()->getCurrentRunConfig(); + auto pbm = getPluginContext()->getProjectBuildManager(); + auto runConfig = pbm ? pbm->getCurrentRunConfig() : std::optional{}; const auto replaceVal = [this, &runConfig, &solvedInputs, randomPort]( std::string& val ) { if ( runConfig ) { @@ -1068,8 +1072,9 @@ std::vector DebuggerPlugin::replaceKeyInString( void DebuggerPlugin::replaceKeysInJson( nlohmann::json& json, int randomPort, const std::unordered_map& solvedInputs ) { - auto runConfig = getPluginContext()->getProjectBuildManager()->getCurrentRunConfig(); - auto buildConfig = getPluginContext()->getProjectBuildManager()->getCurrentBuild(); + auto pbm = getPluginContext()->getProjectBuildManager(); + auto runConfig = pbm ? pbm->getCurrentRunConfig() : std::optional{}; + auto buildConfig = pbm ? pbm->getCurrentBuild() : nullptr; const auto replaceVal = [this, &solvedInputs, &runConfig, randomPort]( nlohmann::json& j, std::string& val ) -> bool { @@ -1554,6 +1559,7 @@ void DebuggerPlugin::runConfig( const std::string& debugger, const std::string& auto runConfig = debuggerIt->run; if ( !usingExternalConfig && configIt->request == REQUEST_TYPE_LAUNCH && + getPluginContext()->getProjectBuildManager() && !getPluginContext()->getProjectBuildManager()->getCurrentRunConfig() ) { auto msg = i18n( "no_run_config",