Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Toggle comment capability to the Html Editor
  • Loading branch information
YoannQDQ authored and nyalldawson committed Mar 30, 2023
1 parent 738b16e commit 443df70
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/gui/auto_generated/codeeditors/qgscodeeditorhtml.sip.in
Expand Up @@ -37,6 +37,17 @@ Constructor for QgsCodeEditorHTML
virtual Qgis::ScriptLanguageCapabilities languageCapabilities() const;


public slots:

virtual void toggleComment();

%Docstring
Toggle comment for the selected text.

.. versionadded:: 3.32
%End


protected:
virtual void initializeLexer();

Expand Down
102 changes: 102 additions & 0 deletions src/gui/codeeditors/qgscodeeditorhtml.cpp
Expand Up @@ -36,6 +36,7 @@ QgsCodeEditorHTML::QgsCodeEditorHTML( QWidget *parent )
}
QgsCodeEditorHTML::initializeLexer();

mCapabilities = Qgis::ScriptLanguageCapability::ToggleComment;
if ( QgsPythonRunner::isValid() )
{
// we could potentially check for beautifulsoup4 import here and reflect the capability accordingly.
Expand Down Expand Up @@ -144,3 +145,104 @@ QString QgsCodeEditorHTML::reformatCodeString( const QString &string )

return newText;
}

void QgsCodeEditorHTML::toggleComment()
{
if ( isReadOnly() )
{
return;
}

const QString commentStart( "<!--" );
const QString commentEnd( "-->" );

int startLine, startPos, endLine, endPos;
if ( hasSelectedText() )
{
getSelection( &startLine, &startPos, &endLine, &endPos );
}
else
{
getCursorPosition( &startLine, &startPos );
endLine = startLine;
endPos = startPos;
}


// Compute first and last non-blank lines
while ( text( startLine ).trimmed().isEmpty() )
{
startLine++;
if ( startLine > endLine )
{
// Only blank lines selected
return;
}
}
while ( text( endLine ).trimmed().isEmpty() )
{
endLine--;
}

// Remove leading spaces from the start line
QString startLineTrimmed = text( startLine );
startLineTrimmed.remove( QRegularExpression( "^\\s+" ) );
// Remove trailing spaces from the end line
QString endLineTrimmed = text( endLine );
endLineTrimmed.remove( QRegularExpression( "\\s+$" ) );

const bool commented = startLineTrimmed.startsWith( commentStart ) && endLineTrimmed.endsWith( commentEnd );

// Special case, selected text is <!--> or <!--->
if ( commented && startLine == endLine && text( endLine ).trimmed() < commentStart.size() + commentEnd.size() )
{
return;
}

beginUndoAction();

// Selection is commented: uncomment it
if ( commented )
{
int c1, c2;

// Remove trailing comment tag ( --> )
c2 = endLineTrimmed.size();
if ( endLineTrimmed.endsWith( QStringLiteral( " " ) + commentEnd ) )
{
c1 = c2 - commentEnd.size() - 1;
}
else
{
c1 = c2 - commentEnd.size();
}

setSelection( endLine, c1, endLine, c2 );
removeSelectedText();

// Remove leading comment tag ( <!-- )
c1 = indentation( startLine );
if ( startLineTrimmed.startsWith( commentStart + QStringLiteral( " " ) ) )
{
c2 = c1 + commentStart.size() + 1;
}
else
{
c2 = c1 + commentStart.size();
}

setSelection( startLine, c1, startLine, c2 );
removeSelectedText();
}
// Selection is not commented: comment it
else
{
insertAt( QStringLiteral( " " ) + commentEnd, endLine, endLineTrimmed.size() );
insertAt( commentStart + QStringLiteral( " " ), startLine, indentation( startLine ) );
}

endUndoAction();

// Restore selection
setSelection( startLine, startPos, endLine, endPos );
}
10 changes: 10 additions & 0 deletions src/gui/codeeditors/qgscodeeditorhtml.h
Expand Up @@ -41,6 +41,16 @@ class GUI_EXPORT QgsCodeEditorHTML : public QgsCodeEditor
Qgis::ScriptLanguage language() const override;
Qgis::ScriptLanguageCapabilities languageCapabilities() const override;

public slots:

/**
* Toggle comment for the selected text.
*
* \since QGIS 3.32
*/
void toggleComment() override;


protected:
void initializeLexer() override;
QString reformatCodeString( const QString &string ) override;
Expand Down

0 comments on commit 443df70

Please sign in to comment.