|
1 | 1 | /* |
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 |
4 | 3 | * |
5 | 4 | * This file is part of GCodeWorkShop. |
6 | 5 | * |
|
18 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 18 | */ |
20 | 19 |
|
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 |
25 | 21 |
|
26 | 22 | #include "utils-comment.h" |
27 | 23 |
|
28 | 24 |
|
29 | | -void Utils::paraComment(QString& tx) |
| 25 | +bool Utils::autoComments(QString& text, int mode, const std::function<bool(int)>& interrupt) |
30 | 26 | { |
31 | | - QStringList list = tx.split(QChar::LineFeed); |
| 27 | + bool changed = false; |
32 | 28 | 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; |
33 | 47 |
|
34 | | - if (tx[0] == '(') { |
35 | | - remove = true; |
| 48 | + default: |
| 49 | + ; |
36 | 50 | } |
37 | 51 |
|
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; |
39 | 57 |
|
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 | + } |
44 | 61 |
|
45 | | - if (idx >= 0) { |
46 | | - txLine.remove(idx, 1); |
47 | | - } |
| 62 | + switch (mode) { |
| 63 | + case ParenthesisComments: |
| 64 | + localChange = parenthesisComments(line, remove); |
| 65 | + break; |
48 | 66 |
|
49 | | - idx = txLine.indexOf(")"); |
| 67 | + case SemicolonComments: |
| 68 | + localChange = semicolonComments(line, remove); |
| 69 | + break; |
50 | 70 |
|
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; |
58 | 77 | } |
59 | 78 |
|
60 | | - txLine.append("\n"); |
61 | | - tx.append(txLine); |
| 79 | + result += line; |
62 | 80 | } |
63 | 81 |
|
64 | | - tx.remove(tx.length() - 1, 1); |
| 82 | + text = result; |
| 83 | + return changed; |
65 | 84 | } |
66 | 85 |
|
67 | | -void Utils::semiComment(QString& tx) |
| 86 | +bool Utils::parenthesisComments(QString& line, bool remove) |
68 | 87 | { |
69 | | - QStringList list = tx.split(QChar::LineFeed); |
70 | | - bool remove = false; |
| 88 | + int oldSize = line.size(); |
71 | 89 |
|
72 | | - if (tx[0] == ';') { |
73 | | - remove = true; |
74 | | - } |
| 90 | + if (remove && line.startsWith('(')) { |
| 91 | + line.remove(0, 1); |
| 92 | + int pli = line.lastIndexOf(')'); |
75 | 93 |
|
76 | | - tx.clear(); |
| 94 | + if (pli >= 0) { |
| 95 | + line.remove(pli, 1); |
| 96 | + } |
| 97 | + } |
77 | 98 |
|
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'); |
82 | 102 |
|
83 | | - if (idx >= 0) { |
84 | | - txLine.remove(idx, 1); |
85 | | - } |
86 | | - } |
| 103 | + if (eol < 0) { |
| 104 | + line.append(')'); |
87 | 105 | } else { |
88 | | - txLine.prepend(";"); |
| 106 | + line.insert(eol, ')'); |
89 | 107 | } |
| 108 | + } |
| 109 | + |
| 110 | + return oldSize != line.size(); |
| 111 | +} |
| 112 | + |
| 113 | +bool Utils::semicolonComments(QString& line, bool remove) |
| 114 | +{ |
| 115 | + int oldSize = line.size(); |
90 | 116 |
|
91 | | - txLine.append("\n"); |
92 | | - tx.append(txLine); |
| 117 | + if (remove && line.startsWith(';')) { |
| 118 | + line.remove(0, 1); |
| 119 | + } else { |
| 120 | + line.prepend(";"); |
93 | 121 | } |
94 | 122 |
|
95 | | - tx.remove(tx.length() - 1, 1); |
| 123 | + return oldSize != line.size(); |
96 | 124 | } |
0 commit comments