Skip to content

Commit 2be40a5

Browse files
committed
Add LongJobHelper to the Addons::Comments
1 parent 42e80f2 commit 2be40a5

3 files changed

Lines changed: 115 additions & 57 deletions

File tree

addons/src/addons-actions.cpp

Lines changed: 25 additions & 5 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
*
@@ -233,8 +233,18 @@ void Addons::Actions::doParaComment()
233233
return;
234234
}
235235

236-
Utils::paraComment(ctx.text());
237-
ctx.push();
236+
LongJobHelper helper{GCodeWorkShop::instance()};
237+
helper.begin(ctx.text().length(), tr("Comments/uncomments"));
238+
239+
bool changed = Utils::autoComments(ctx.text(), Utils::ParenthesisComments, [&helper](int pos) -> bool{
240+
return helper.check(pos) == LongJobHelper::CANCEL;
241+
});
242+
243+
helper.end();
244+
245+
if (changed) {
246+
ctx.push();
247+
}
238248
}
239249

240250
void Addons::Actions::doSemiComment()
@@ -245,8 +255,18 @@ void Addons::Actions::doSemiComment()
245255
return;
246256
}
247257

248-
Utils::semiComment(ctx.text());
249-
ctx.push();
258+
LongJobHelper helper{GCodeWorkShop::instance()};
259+
helper.begin(ctx.text().length(), tr("Comments/uncomments"));
260+
261+
bool changed = Utils::autoComments(ctx.text(), Utils::SemicolonComments, [&helper](int pos) -> bool{
262+
return helper.check(pos) == LongJobHelper::CANCEL;
263+
});
264+
265+
helper.end();
266+
267+
if (changed) {
268+
ctx.push();
269+
}
250270
}
251271

252272
void Addons::Actions::doCompileMacro()
Lines changed: 76 additions & 48 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
*
@@ -18,79 +17,108 @@
1817
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1918
*/
2019

21-
#include <QChar> // for operator==, QChar, QChar::LineFeed
22-
#include <QString> // for QString, QCharRef
23-
#include <QStringList> // for QStringList
24-
#include <QtGlobal> // for QForeachContainer, qMakeForeachContainer, foreach
20+
#include <QString> // for QString
2521

2622
#include "utils-comment.h"
2723

2824

29-
void Utils::paraComment(QString& tx)
25+
bool Utils::autoComments(QString& text, int mode, const std::function<bool(int)>& interrupt)
3026
{
31-
QStringList list = tx.split(QChar::LineFeed);
27+
bool changed = false;
3228
bool remove = false;
29+
QString result;
30+
int substr_start = 0;
31+
int substr_end = 0;
32+
33+
switch (mode) {
34+
case ParenthesisComments:
35+
if (text.startsWith('(')) {
36+
remove = true;
37+
}
38+
39+
break;
40+
41+
case SemicolonComments:
42+
if (text.startsWith(';')) {
43+
remove = true;
44+
}
45+
46+
break;
3347

34-
if (tx[0] == '(') {
35-
remove = true;
48+
default:
49+
;
3650
}
3751

38-
tx.clear();
52+
while (substr_end >= 0) {
53+
substr_end = text.indexOf('\n', substr_start);
54+
QString line = text.mid(substr_start, substr_end - substr_start + 1);
55+
substr_start = substr_end + 1;
56+
bool localChange = false;
3957

40-
foreach (QString txLine, list) {
41-
if (remove) {
42-
if (txLine.length() > 0) {
43-
int idx = txLine.indexOf("(");
58+
if (interrupt(substr_start)) {
59+
return false;
60+
}
4461

45-
if (idx >= 0) {
46-
txLine.remove(idx, 1);
47-
}
62+
switch (mode) {
63+
case ParenthesisComments:
64+
localChange = parenthesisComments(line, remove);
65+
break;
4866

49-
idx = txLine.indexOf(")");
67+
case SemicolonComments:
68+
localChange = semicolonComments(line, remove);
69+
break;
5070

51-
if (idx >= 0) {
52-
txLine.remove(idx, 1);
53-
}
54-
}
55-
} else {
56-
txLine.prepend("(");
57-
txLine.append(")");
71+
default:
72+
;
73+
}
74+
75+
if (localChange) {
76+
changed = true;
5877
}
5978

60-
txLine.append("\n");
61-
tx.append(txLine);
79+
result += line;
6280
}
6381

64-
tx.remove(tx.length() - 1, 1);
82+
text = result;
83+
return changed;
6584
}
6685

67-
void Utils::semiComment(QString& tx)
86+
bool Utils::parenthesisComments(QString& line, bool remove)
6887
{
69-
QStringList list = tx.split(QChar::LineFeed);
70-
bool remove = false;
88+
int oldSize = line.size();
7189

72-
if (tx[0] == ';') {
73-
remove = true;
74-
}
90+
if (remove && line.startsWith('(')) {
91+
line.remove(0, 1);
92+
int pli = line.lastIndexOf(')');
7593

76-
tx.clear();
94+
if (pli >= 0) {
95+
line.remove(pli, 1);
96+
}
97+
}
7798

78-
foreach (QString txLine, list) {
79-
if (remove) {
80-
if (txLine.length() > 0) {
81-
int idx = txLine.indexOf(";");
99+
if (!remove) {
100+
line.prepend("(");
101+
int eol = line.indexOf('\n');
82102

83-
if (idx >= 0) {
84-
txLine.remove(idx, 1);
85-
}
86-
}
103+
if (eol < 0) {
104+
line.append(')');
87105
} else {
88-
txLine.prepend(";");
106+
line.insert(eol, ')');
89107
}
108+
}
109+
110+
return oldSize != line.size();
111+
}
112+
113+
bool Utils::semicolonComments(QString& line, bool remove)
114+
{
115+
int oldSize = line.size();
90116

91-
txLine.append("\n");
92-
tx.append(txLine);
117+
if (remove && line.startsWith(';')) {
118+
line.remove(0, 1);
119+
} else {
120+
line.prepend(";");
93121
}
94122

95-
tx.remove(tx.length() - 1, 1);
123+
return oldSize != line.size();
96124
}

addons/src/comment/utils-comment.h

Lines changed: 14 additions & 4 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,22 @@
2020
#ifndef UTILS_COMMENT_H
2121
#define UTILS_COMMENT_H
2222

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

2527

2628
namespace Utils {
27-
void paraComment(QString& tx);
28-
void semiComment(QString& tx);
29-
}
29+
30+
enum CommentMode {
31+
ParenthesisComments,
32+
SemicolonComments
33+
};
34+
35+
bool autoComments(QString& text, int mode, const std::function<bool(int)>& interrupt);
36+
bool parenthesisComments(QString& line, bool remove);
37+
bool semicolonComments(QString& line, bool remove);
38+
39+
} // namespace Utils
3040

3141
#endif // UTILS_COMMENT_H

0 commit comments

Comments
 (0)