* @brief Reformats the entire transform script. */
| 348 | * @brief Reformats the entire transform script. |
| 349 | */ |
| 350 | void DataModel::DatasetTransformEditor::onFormat() |
| 351 | { |
| 352 | const auto lang = (m_language == SerialStudio::Lua) ? CodeFormatter::Language::Lua |
| 353 | : CodeFormatter::Language::JavaScript; |
| 354 | const QString original = m_editor->toPlainText(); |
| 355 | const QString formatted = CodeFormatter::formatDocument(original, lang); |
| 356 | if (formatted == original) |
| 357 | return; |
| 358 | |
| 359 | QTextCursor cursor = m_editor->textCursor(); |
| 360 | const int savedPos = cursor.position(); |
| 361 | cursor.beginEditBlock(); |
| 362 | cursor.select(QTextCursor::Document); |
| 363 | cursor.insertText(formatted); |
| 364 | cursor.endEditBlock(); |
| 365 | |
| 366 | cursor.setPosition(qMin(savedPos, formatted.size())); |
| 367 | m_editor->setTextCursor(cursor); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * @brief Reformats the selected lines, or the current line when nothing is selected. |
nothing calls this directly
no test coverage detected