Skip to content

Commit

Permalink
Fix / improve line comments toggle (SpartanJ/ecode#384).
Browse files Browse the repository at this point in the history
Hide debugger panel when opening a single file.
  • Loading branch information
SpartanJ committed Jan 23, 2025
1 parent c945b0b commit ce47145
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
7 changes: 4 additions & 3 deletions include/eepp/ui/doc/textdocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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,
Expand Down
85 changes: 61 additions & 24 deletions src/eepp/ui/doc/textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Int64>( 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 );
Expand All @@ -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<Int64>( text.length() ) } } );
if ( startFrom < 0 || startFrom > static_cast<Int64>( line.length() ) )
continue;
if ( line.substr( startFrom, text.length() ) == text ) {
remove( 0, { { i, startFrom },
{ i, static_cast<Int64>( startFrom + text.length() ) } } );
if ( i == range.start().line() ) {
startRemoved = text.size();
} else if ( i == range.end().line() ) {
Expand Down Expand Up @@ -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;
}
}
if ( uncomment ) {
removeFromStartOfSelectedLines( commentText, true );
} else {
insertAtStartOfSelectedLines( commentText, true );
const std::string commentText = comment + " ";
Int64 commentLength = static_cast<Int64>( 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<Int64>( pos ) },
{ lineIdx, static_cast<Int64>( 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 );
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/tools/ecode/plugins/debugger/debuggerclientlistener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ DebuggerClientListener::~DebuggerClientListener() {
void DebuggerClientListener::initUI() {
auto sdc = getStatusDebuggerController();

if ( sdc == nullptr )
return;

sdc->createWidget();

mPlugin->getManager()->getPluginContext()->getStatusAppOutputController()->initNewOutput(
Expand Down
14 changes: 10 additions & 4 deletions src/tools/ecode/plugins/debugger/debuggerplugin.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "debuggerplugin.hpp"
#include "../../notificationcenter.hpp"
#include "../../projectbuild.hpp"
#include "../../terminalmanager.hpp"
Expand All @@ -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"
Expand Down Expand Up @@ -750,6 +750,9 @@ void DebuggerPlugin::buildSidePanelTab() {
updateSidePanelTab();

updateSelectedDebugConfig();

if ( mProjectPath.empty() )
hideSidePanel();
}

void DebuggerPlugin::updateSelectedDebugConfig() {
Expand Down Expand Up @@ -1018,7 +1021,8 @@ void DebuggerPlugin::updateDebuggerConfigurationList() {
std::vector<std::string> DebuggerPlugin::replaceKeyInString(
std::string val, int randomPort,
const std::unordered_map<std::string, std::string>& solvedInputs ) {
auto runConfig = getPluginContext()->getProjectBuildManager()->getCurrentRunConfig();
auto pbm = getPluginContext()->getProjectBuildManager();
auto runConfig = pbm ? pbm->getCurrentRunConfig() : std::optional<ProjectBuildStep>{};

const auto replaceVal = [this, &runConfig, &solvedInputs, randomPort]( std::string& val ) {
if ( runConfig ) {
Expand Down Expand Up @@ -1068,8 +1072,9 @@ std::vector<std::string> DebuggerPlugin::replaceKeyInString(
void DebuggerPlugin::replaceKeysInJson(
nlohmann::json& json, int randomPort,
const std::unordered_map<std::string, std::string>& solvedInputs ) {
auto runConfig = getPluginContext()->getProjectBuildManager()->getCurrentRunConfig();
auto buildConfig = getPluginContext()->getProjectBuildManager()->getCurrentBuild();
auto pbm = getPluginContext()->getProjectBuildManager();
auto runConfig = pbm ? pbm->getCurrentRunConfig() : std::optional<ProjectBuildStep>{};
auto buildConfig = pbm ? pbm->getCurrentBuild() : nullptr;

const auto replaceVal = [this, &solvedInputs, &runConfig,
randomPort]( nlohmann::json& j, std::string& val ) -> bool {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ce47145

Please sign in to comment.