| 1320 | } |
| 1321 | |
| 1322 | void CodeTextEditor::toggle_inline_comment(const String &delimiter) { |
| 1323 | text_editor->begin_complex_operation(); |
| 1324 | text_editor->begin_multicaret_edit(); |
| 1325 | |
| 1326 | Vector<Point2i> line_ranges = text_editor->get_line_ranges_from_carets(); |
| 1327 | int folded_to = 0; |
| 1328 | for (Point2i line_range : line_ranges) { |
| 1329 | int from_line = line_range.x; |
| 1330 | int to_line = line_range.y; |
| 1331 | // If last line is folded, extends to the end of the folded section |
| 1332 | if (text_editor->is_line_folded(to_line)) { |
| 1333 | folded_to = text_editor->get_next_visible_line_offset_from(to_line + 1, 1) - 1; |
| 1334 | to_line += folded_to; |
| 1335 | } |
| 1336 | // Check first if there's any uncommented lines in selection. |
| 1337 | bool is_commented = true; |
| 1338 | bool is_all_empty = true; |
| 1339 | for (int line = from_line; line <= to_line; line++) { |
| 1340 | // `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it |
| 1341 | int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length()); |
| 1342 | // Empty lines should not be counted. |
| 1343 | bool is_empty = text_editor->get_line(line).strip_edges().is_empty(); |
| 1344 | is_all_empty = is_all_empty && is_empty; |
| 1345 | // get_delimiter_start_key will return `##` instead of `#` when there is multiple comment delimiter in a line. |
| 1346 | if (!is_empty && (delimiter_idx == -1 || !text_editor->get_delimiter_start_key(delimiter_idx).begins_with(delimiter))) { |
| 1347 | is_commented = false; |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | // Special case for commenting empty lines, treat it/them as uncommented lines. |
| 1353 | is_commented = is_commented && !is_all_empty; |
| 1354 | |
| 1355 | // Comment/uncomment. |
| 1356 | for (int line = from_line; line <= to_line; line++) { |
| 1357 | if (is_all_empty) { |
| 1358 | text_editor->insert_text(delimiter, line, 0); |
| 1359 | continue; |
| 1360 | } |
| 1361 | |
| 1362 | if (is_commented) { |
| 1363 | int delimiter_column = text_editor->get_line(line).find(delimiter); |
| 1364 | if (delimiter_column != -1) { |
| 1365 | text_editor->remove_text(line, delimiter_column, line, delimiter_column + delimiter.length()); |
| 1366 | } |
| 1367 | } else { |
| 1368 | text_editor->insert_text(delimiter, line, text_editor->get_first_non_whitespace_column(line)); |
| 1369 | } |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | text_editor->end_multicaret_edit(); |
| 1374 | text_editor->end_complex_operation(); |
| 1375 | } |
| 1376 | |
| 1377 | void CodeTextEditor::goto_line(int p_line, int p_column) { |
| 1378 | text_editor->remove_secondary_carets(); |
no test coverage detected