* @brief Reformats the selected lines, or the current line if nothing is selected. */
| 321 | * @brief Reformats the selected lines, or the current line if nothing is selected. |
| 322 | */ |
| 323 | void MQTT::PublisherScriptEditor::onFormatLine() |
| 324 | { |
| 325 | const auto lang = (m_language == SerialStudio::Lua) |
| 326 | ? DataModel::CodeFormatter::Language::Lua |
| 327 | : DataModel::CodeFormatter::Language::JavaScript; |
| 328 | const QString original = m_editor->toPlainText(); |
| 329 | |
| 330 | QTextCursor cursor = m_editor->textCursor(); |
| 331 | QTextCursor first(m_editor->document()); |
| 332 | first.setPosition(qMin(cursor.selectionStart(), cursor.selectionEnd())); |
| 333 | QTextCursor last(m_editor->document()); |
| 334 | last.setPosition(qMax(cursor.selectionStart(), cursor.selectionEnd())); |
| 335 | |
| 336 | const int firstLine = first.blockNumber(); |
| 337 | const int lastLine = last.blockNumber(); |
| 338 | const QString formatted = |
| 339 | DataModel::CodeFormatter::formatLineRange(original, lang, firstLine, lastLine); |
| 340 | if (formatted == original) |
| 341 | return; |
| 342 | |
| 343 | const int savedPos = cursor.position(); |
| 344 | cursor.beginEditBlock(); |
| 345 | cursor.select(QTextCursor::Document); |
| 346 | cursor.insertText(formatted); |
| 347 | cursor.endEditBlock(); |
| 348 | |
| 349 | cursor.setPosition(qMin(savedPos, formatted.size())); |
| 350 | m_editor->setTextCursor(cursor); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @brief Builds and shows the editor's right-click menu with format entries. |
nothing calls this directly
no test coverage detected