| 1260 | } |
| 1261 | |
| 1262 | bool CodeEditor::removeInEachLineOfSelection(const QRegularExpression ®ex, bool force) |
| 1263 | { |
| 1264 | auto cursor = textCursor(); |
| 1265 | auto lines = toPlainText().remove('\r').split('\n'); |
| 1266 | int selectionStart = cursor.selectionStart(); |
| 1267 | int selectionEnd = cursor.selectionEnd(); |
| 1268 | bool cursorAtEnd = cursor.position() == selectionEnd; |
| 1269 | cursor.setPosition(selectionStart); |
| 1270 | int lineStart = cursor.blockNumber(); |
| 1271 | cursor.setPosition(selectionEnd); |
| 1272 | int lineEnd = cursor.blockNumber(); |
| 1273 | QString newText; |
| 1274 | QTextStream stream(&newText); |
| 1275 | int deleteTotal = 0; |
| 1276 | int deleteFirst = 0; |
| 1277 | for (int i = lineStart; i <= lineEnd; ++i) |
| 1278 | { |
| 1279 | auto line = lines[i]; |
| 1280 | auto match = regex.match(line).captured(1); |
| 1281 | int len = match.length(); |
| 1282 | if (len == 0 && !force) |
| 1283 | return false; |
| 1284 | if (i == lineStart) |
| 1285 | deleteFirst = len; |
| 1286 | deleteTotal += len; |
| 1287 | stream << line.remove(line.indexOf(match), len); |
| 1288 | if (i != lineEnd) |
| 1289 | stream << Qt::endl; |
| 1290 | } |
| 1291 | cursor.movePosition(QTextCursor::Start); |
| 1292 | cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, lineStart); |
| 1293 | cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, lineEnd - lineStart); |
| 1294 | cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); |
| 1295 | cursor.insertText(newText); |
| 1296 | cursor.setPosition(qMax(0, selectionStart - deleteFirst)); |
| 1297 | if (cursor.blockNumber() < lineStart) |
| 1298 | { |
| 1299 | cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, lineStart - cursor.blockNumber()); |
| 1300 | cursor.movePosition(QTextCursor::StartOfBlock); |
| 1301 | } |
| 1302 | int pos = cursor.position(); |
| 1303 | cursor.setPosition(selectionEnd - deleteTotal); |
| 1304 | if (cursor.blockNumber() < lineEnd) |
| 1305 | { |
| 1306 | cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, lineEnd - cursor.blockNumber()); |
| 1307 | cursor.movePosition(QTextCursor::StartOfBlock); |
| 1308 | } |
| 1309 | int pos2 = cursor.position(); |
| 1310 | if (cursorAtEnd) |
| 1311 | { |
| 1312 | cursor.setPosition(pos); |
| 1313 | cursor.setPosition(pos2, QTextCursor::KeepAnchor); |
| 1314 | } |
| 1315 | else |
| 1316 | { |
| 1317 | cursor.setPosition(pos2); |
| 1318 | cursor.setPosition(pos, QTextCursor::KeepAnchor); |
| 1319 | } |
nothing calls this directly
no test coverage detected