Skip to content

Commit 2e8e640

Browse files
committed
Add LongJobHelper to the Addons::EmptyLines
1 parent ac97e45 commit 2e8e640

3 files changed

Lines changed: 74 additions & 29 deletions

File tree

addons/src/addons-actions.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,18 @@ void Addons::Actions::doInsertEmptyLines()
313313
return;
314314
}
315315

316-
QApplication::setOverrideCursor(Qt::BusyCursor);
317-
Utils::insertEmptyLines(ctx.text());
318-
ctx.push();
319-
QApplication::restoreOverrideCursor();
316+
LongJobHelper helper{GCodeWorkShop::instance()};
317+
helper.begin(ctx.text().length(), tr("Inserting empty lines"));
318+
319+
bool changed = Utils::insertEmptyLines(ctx.text(), [&helper](int pos) -> bool{
320+
return helper.check(pos) == LongJobHelper::CANCEL;
321+
});
322+
323+
helper.end();
324+
325+
if (changed) {
326+
ctx.push();
327+
}
320328
}
321329

322330
void Addons::Actions::doRemoveEmptyLines()
@@ -327,10 +335,18 @@ void Addons::Actions::doRemoveEmptyLines()
327335
return;
328336
}
329337

330-
QApplication::setOverrideCursor(Qt::BusyCursor);
331-
Utils::removeEmptyLines(ctx.text());
332-
ctx.push();
333-
QApplication::restoreOverrideCursor();
338+
LongJobHelper helper{GCodeWorkShop::instance()};
339+
helper.begin(ctx.text().length(), tr("Removing empty lines"));
340+
341+
bool changed = Utils::removeEmptyLines(ctx.text(), [&helper](int pos) -> bool{
342+
return helper.check(pos) == LongJobHelper::CANCEL;
343+
});
344+
345+
helper.end();
346+
347+
if (changed) {
348+
ctx.push();
349+
}
334350
}
335351

336352
void Addons::Actions::doFeeds()
Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
* Copyright (C) 2006-2018 by Artur Kozioł, artkoz78@gmail.com
3-
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
2+
* Copyright (C) 2023-2025 Nick Egorrov, nicegorov@yandex.ru
43
*
54
* This file is part of GCodeWorkShop.
65
*
@@ -25,28 +24,56 @@
2524
#include "utils-emptylines.h"
2625

2726

28-
void Utils::insertEmptyLines(QString& tx)
27+
bool Utils::insertEmptyLines(QString& tx, const std::function<bool (int)>& interrupt)
2928
{
30-
if (tx.contains(QLatin1String("\r\n"))) {
31-
tx.replace(QLatin1String("\r\n"), QLatin1String("\r\n\r\n"));
32-
} else {
33-
tx.replace(QLatin1String("\n"), QLatin1String("\n\n"));
29+
bool changed = false;
30+
int pos = 0;
31+
QRegularExpression regexpr{
32+
"\\n|\\r\\n"
33+
};
34+
QRegularExpressionMatchIterator iterator = regexpr.globalMatch(tx);
35+
QString result;
36+
37+
while (iterator.hasNext()) {
38+
if (interrupt(pos)) {
39+
return false;
40+
}
41+
42+
QRegularExpressionMatch match = iterator.next();
43+
result.append(tx.mid(pos, match.capturedStart() - pos));
44+
result.append("\n\n");
45+
pos = match.capturedEnd();
46+
changed = true;
3447
}
48+
49+
result.append(tx.mid(pos));
50+
tx = result;
51+
return changed;
3552
}
3653

37-
void Utils::removeEmptyLines(QString& tx)
54+
bool Utils::removeEmptyLines(QString& tx, const std::function<bool (int)>& interrupt)
3855
{
39-
int i;
40-
QRegularExpression regex;
56+
bool changed = false;
57+
int pos = 0;
58+
QRegularExpression regexpr{
59+
"[\\n\\r]+"
60+
};
61+
QRegularExpressionMatchIterator iterator = regexpr.globalMatch(tx);
62+
QString result;
4163

42-
regex.setPattern("[\\n]{2,}");
43-
i = 0;
44-
45-
while (i >= 0) {
46-
i = tx.indexOf(regex, 0);
47-
48-
if (i >= 0) {
49-
tx.replace(regex, "\r\n");
64+
while (iterator.hasNext()) {
65+
if (interrupt(pos)) {
66+
return false;
5067
}
68+
69+
QRegularExpressionMatch match = iterator.next();
70+
result.append(tx.mid(pos, match.capturedStart() - pos));
71+
result.append("\n");
72+
pos = match.capturedEnd();
73+
changed = true;
5174
}
75+
76+
result.append(tx.mid(pos));
77+
tx = result;
78+
return changed;
5279
}

addons/src/emptylines/utils-emptylines.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023 Nick Egorrov, nicegorov@yandex.ru
2+
* Copyright (C) 2023-2025 Nick Egorrov, nicegorov@yandex.ru
33
*
44
* This file is part of GCodeWorkShop.
55
*
@@ -20,12 +20,14 @@
2020
#ifndef UTILS_EMPTYLINES_H
2121
#define UTILS_EMPTYLINES_H
2222

23+
#include <functional> // for function
24+
2325
class QString;
2426

2527

2628
namespace Utils {
27-
void insertEmptyLines(QString& tx);
28-
void removeEmptyLines(QString& tx);
29+
bool insertEmptyLines(QString& tx, const std::function<bool(int)>& interrupt);
30+
bool removeEmptyLines(QString& tx, const std::function<bool(int)>& interrupt);
2931
}
3032

3133
#endif // UTILS_EMPTYLINES_H

0 commit comments

Comments
 (0)