Gui: avoid code duplication in PythonEditor

This commit is contained in:
wmayer
2025-03-17 14:18:48 +01:00
committed by Ladislav Michl
parent 5d11b2938f
commit f5bfc10c81
3 changed files with 38 additions and 53 deletions

View File

@@ -243,27 +243,7 @@ void PythonEditor::onComment()
void PythonEditor::onUncomment()
{
QTextCursor cursor = textCursor();
int selStart = cursor.selectionStart();
int selEnd = cursor.selectionEnd();
QTextBlock block;
cursor.beginEditBlock();
for (block = document()->begin(); block.isValid(); block = block.next()) {
int pos = block.position();
int off = block.length()-1;
// at least one char of the block is part of the selection
if ( pos >= selStart || pos+off >= selStart) {
if ( pos+1 > selEnd )
break; // end of selection reached
if (block.text().startsWith(QLatin1String("#"))) {
cursor.setPosition(block.position());
cursor.deleteChar();
selEnd--;
}
}
}
cursor.endEditBlock();
remove(QStringLiteral("#"));
}
void PythonEditor::onExecuteInConsole()

View File

@@ -519,6 +519,34 @@ void PythonTextEditor::prepend(const QString& str)
cursor.endEditBlock();
}
void PythonTextEditor::remove(const QString& str)
{
QTextCursor cursor = textCursor();
int selStart = cursor.selectionStart();
int selEnd = cursor.selectionEnd();
QTextBlock block;
cursor.beginEditBlock();
for (block = document()->begin(); block.isValid(); block = block.next()) {
int pos = block.position();
int off = block.length()-1;
// at least one char of the block is part of the selection
if ( pos >= selStart || pos+off >= selStart) {
if ( pos+1 > selEnd )
break; // end of selection reached
QString text = block.text();
if (text.startsWith(str)) {
cursor.setPosition(block.position());
for (int i = 0; i < str.length(); i++) {
cursor.deleteChar();
selEnd--;
}
}
}
}
cursor.endEditBlock();
}
void PythonTextEditor::keyPressEvent (QKeyEvent * e)
{
if ( e->key() == Qt::Key_Tab ) {
@@ -547,40 +575,13 @@ void PythonTextEditor::keyPressEvent (QKeyEvent * e)
// If some text is selected we remove a leading tab or
// spaces from each selected block
ParameterGrp::handle hPrefGrp = getWindowParameter();
bool space = hPrefGrp->GetBool("Spaces", true);
int indent = hPrefGrp->GetInt( "IndentSize", 4 );
QString ch = space ? QString(indent, QLatin1Char(' '))
: QStringLiteral("\t");
int selStart = cursor.selectionStart();
int selEnd = cursor.selectionEnd();
QTextBlock block;
cursor.beginEditBlock();
for (block = document()->begin(); block.isValid(); block = block.next()) {
int pos = block.position();
int off = block.length()-1;
// at least one char of the block is part of the selection
if ( pos >= selStart || pos+off >= selStart) {
if ( pos+1 > selEnd )
break; // end of selection reached
// if possible remove one tab or several spaces
QString text = block.text();
if (text.startsWith(QLatin1String("\t"))) {
cursor.setPosition(block.position());
cursor.deleteChar();
selEnd--;
}
else {
cursor.setPosition(block.position());
for (int i=0; i<indent; i++) {
if (!text.startsWith(QLatin1String(" ")))
break;
text = text.mid(1);
cursor.deleteChar();
selEnd--;
}
}
}
}
cursor.endEditBlock();
// if possible remove one tab or several spaces
remove(ch);
return;
}

View File

@@ -144,6 +144,10 @@ public Q_SLOTS:
* nothing is selected
*/
void prepend(const QString& str);
/** Removes \a str from the beginning of each selected line or the current line if
* nothing is selected
*/
void remove(const QString& str);
protected:
void keyPressEvent(QKeyEvent *) override;