* @brief Reformats the entire editor contents. */
| 463 | * @brief Reformats the entire editor contents. |
| 464 | */ |
| 465 | void DataModel::JsCodeEditor::formatDocument() |
| 466 | { |
| 467 | const auto lang = |
| 468 | (m_language == 1) ? CodeFormatter::Language::Lua : CodeFormatter::Language::JavaScript; |
| 469 | const QString original = m_widget.toPlainText(); |
| 470 | const QString formatted = CodeFormatter::formatDocument(original, lang); |
| 471 | if (formatted == original) |
| 472 | return; |
| 473 | |
| 474 | QTextCursor cursor = m_widget.textCursor(); |
| 475 | const int savedPos = cursor.position(); |
| 476 | cursor.beginEditBlock(); |
| 477 | cursor.select(QTextCursor::Document); |
| 478 | cursor.insertText(formatted); |
| 479 | cursor.endEditBlock(); |
| 480 | |
| 481 | cursor.setPosition(qMin(savedPos, formatted.size())); |
| 482 | m_widget.setTextCursor(cursor); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * @brief Reformats the selected lines, or the current line when nothing is selected. |
nothing calls this directly
no test coverage detected