mirror of https://github.com/vitalif/openscad
Reimplemented Replace All to work properly. Fixes #1264
parent
21cce232bc
commit
5042d064b2
|
@ -20,7 +20,8 @@ public:
|
|||
virtual QTextDocument *document(){QTextDocument *t = new QTextDocument; return t;}
|
||||
virtual QString selectedText() = 0;
|
||||
virtual bool find(const QString &, bool findNext = false, bool findBackwards = false) = 0;
|
||||
virtual void replaceSelectedText(const QString &) = 0;
|
||||
virtual void replaceSelectedText(const QString &newText) = 0;
|
||||
virtual void replaceAll(const QString &findText, const QString &replaceText) = 0;
|
||||
virtual QStringList colorSchemes() = 0;
|
||||
|
||||
signals:
|
||||
|
@ -41,7 +42,7 @@ public slots:
|
|||
virtual void unhighlightLastError() = 0;
|
||||
virtual void setHighlightScheme(const QString&) = 0;
|
||||
virtual void insert(const QString&) = 0;
|
||||
virtual void replaceAll(const QString&) = 0;
|
||||
virtual void setText(const QString&) = 0;
|
||||
virtual void undo() = 0;
|
||||
virtual void redo() = 0;
|
||||
virtual void cut() = 0;
|
||||
|
|
|
@ -179,7 +179,7 @@ void LegacyEditor::insert(const QString &text)
|
|||
this->textedit->insertPlainText(text);
|
||||
}
|
||||
|
||||
void LegacyEditor::replaceAll(const QString &text)
|
||||
void LegacyEditor::setText(const QString &text)
|
||||
{
|
||||
this->textedit->selectAll();
|
||||
this->textedit->insertPlainText(text);
|
||||
|
@ -223,6 +223,18 @@ void LegacyEditor::replaceSelectedText(const QString &newText)
|
|||
}
|
||||
}
|
||||
|
||||
void LegacyEditor::replaceAll(const QString &findText, const QString &replaceText)
|
||||
{
|
||||
QTextCursor cursor(this->textedit->textCursor());
|
||||
cursor.setPosition(0);
|
||||
this->textedit->setTextCursor(cursor);
|
||||
this->textedit->find(findText);
|
||||
while (this->textedit->textCursor().hasSelection()){
|
||||
this->textedit->textCursor().insertText(replaceText);
|
||||
this->textedit->find(findText);
|
||||
}
|
||||
}
|
||||
|
||||
bool LegacyEditor::findString(const QString & exp, bool findBackwards) const
|
||||
{
|
||||
return this->textedit->find(exp, findBackwards ? QTextDocument::FindBackward : QTextDocument::FindFlags(0));
|
||||
|
|
|
@ -14,6 +14,7 @@ public:
|
|||
QString selectedText();
|
||||
bool find(const QString &, bool findNext = false, bool findBackwards = false);
|
||||
void replaceSelectedText(const QString &newText);
|
||||
void replaceAll(const QString &findText, const QString &replaceText);
|
||||
bool findString(const QString & exp, bool findBackwards) const;
|
||||
QStringList colorSchemes();
|
||||
|
||||
|
@ -31,7 +32,7 @@ public slots:
|
|||
void unhighlightLastError();
|
||||
void setHighlightScheme(const QString&);
|
||||
void insert(const QString&);
|
||||
void replaceAll(const QString&);
|
||||
void setText(const QString&);
|
||||
void undo();
|
||||
void redo();
|
||||
void cut();
|
||||
|
|
|
@ -1431,7 +1431,7 @@ void MainWindow::find()
|
|||
|
||||
void MainWindow::findString(QString textToFind)
|
||||
{
|
||||
editor->find(textToFind, false, false);
|
||||
editor->find(textToFind);
|
||||
}
|
||||
|
||||
void MainWindow::findAndReplace()
|
||||
|
@ -1462,9 +1462,7 @@ void MainWindow::replace()
|
|||
|
||||
void MainWindow::replaceAll()
|
||||
{
|
||||
while (this->editor->find(this->findInputField->text(), true)) {
|
||||
this->editor->replaceSelectedText(this->replaceInputField->text());
|
||||
}
|
||||
this->editor->replaceAll(this->findInputField->text(), this->replaceInputField->text());
|
||||
}
|
||||
|
||||
void MainWindow::convertTabsToSpaces()
|
||||
|
@ -1488,7 +1486,7 @@ void MainWindow::convertTabsToSpaces()
|
|||
}
|
||||
cnt--;
|
||||
}
|
||||
this->editor->replaceAll(converted);
|
||||
this->editor->setText(converted);
|
||||
}
|
||||
|
||||
void MainWindow::findNext()
|
||||
|
|
|
@ -426,7 +426,7 @@ void ScintillaEditor::insert(const QString &text)
|
|||
qsci->insert(text);
|
||||
}
|
||||
|
||||
void ScintillaEditor::replaceAll(const QString &text)
|
||||
void ScintillaEditor::setText(const QString &text)
|
||||
{
|
||||
qsci->selectAll(true);
|
||||
qsci->replaceSelectedText(text);
|
||||
|
@ -510,6 +510,28 @@ void ScintillaEditor::replaceSelectedText(const QString &newText)
|
|||
if (qsci->selectedText() != newText) qsci->replaceSelectedText(newText);
|
||||
}
|
||||
|
||||
void ScintillaEditor::replaceAll(const QString &findText, const QString &replaceText)
|
||||
{
|
||||
// We need to issue a Select All first due to a bug in QScintilla:
|
||||
// It doesn't update the find range when just doing findFirst() + findNext() causing the search
|
||||
// to end prematurely if the replaced string is larger than the selected string.
|
||||
qsci->selectAll();
|
||||
if (qsci->findFirstInSelection(findText,
|
||||
false /*re*/, false /*cs*/, false /*wo*/,
|
||||
false /*wrap*/, true /*forward*/)) {
|
||||
qsci->replace(replaceText);
|
||||
while (qsci->findNext()) {
|
||||
int lineFrom, indexFrom, lineTo, indexTo;
|
||||
qsci->getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);
|
||||
PRINTDB("replacing selection at line %d-%d, index %d-%d", lineFrom % lineTo % indexFrom % indexTo);
|
||||
qsci->replace(replaceText);
|
||||
}
|
||||
int lineFrom, indexFrom, lineTo, indexTo;
|
||||
qsci->getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo);
|
||||
PRINTDB("Last: %d-%d, index %d-%d", lineFrom % lineTo % indexFrom % indexTo);
|
||||
}
|
||||
}
|
||||
|
||||
void ScintillaEditor::getRange(int *lineFrom, int *lineTo)
|
||||
{
|
||||
int indexFrom, indexTo;
|
||||
|
|
|
@ -52,6 +52,7 @@ public:
|
|||
QString selectedText();
|
||||
bool find(const QString &, bool findNext = false, bool findBackwards = false);
|
||||
void replaceSelectedText(const QString&);
|
||||
void replaceAll(const QString &findText, const QString &replaceText);
|
||||
QStringList colorSchemes();
|
||||
|
||||
private:
|
||||
|
@ -77,7 +78,7 @@ public slots:
|
|||
void commentSelection();
|
||||
void uncommentSelection();
|
||||
void insert(const QString&);
|
||||
void replaceAll(const QString&);
|
||||
void setText(const QString&);
|
||||
void undo();
|
||||
void redo();
|
||||
void cut();
|
||||
|
|
Loading…
Reference in New Issue