* @brief Reformats the entire script. */
| 297 | * @brief Reformats the entire script. |
| 298 | */ |
| 299 | void MQTT::PublisherScriptEditor::onFormat() |
| 300 | { |
| 301 | const auto lang = (m_language == SerialStudio::Lua) |
| 302 | ? DataModel::CodeFormatter::Language::Lua |
| 303 | : DataModel::CodeFormatter::Language::JavaScript; |
| 304 | const QString original = m_editor->toPlainText(); |
| 305 | const QString formatted = DataModel::CodeFormatter::formatDocument(original, lang); |
| 306 | if (formatted == original) |
| 307 | return; |
| 308 | |
| 309 | QTextCursor cursor = m_editor->textCursor(); |
| 310 | const int savedPos = cursor.position(); |
| 311 | cursor.beginEditBlock(); |
| 312 | cursor.select(QTextCursor::Document); |
| 313 | cursor.insertText(formatted); |
| 314 | cursor.endEditBlock(); |
| 315 | |
| 316 | cursor.setPosition(qMin(savedPos, formatted.size())); |
| 317 | m_editor->setTextCursor(cursor); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @brief Reformats the selected lines, or the current line if nothing is selected. |
nothing calls this directly
no test coverage detected