MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / onFormat

Method onFormat

app/src/MQTT/PublisherScriptEditor.cpp:299–318  ·  view source on GitHub ↗

* @brief Reformats the entire script. */

Source from the content-addressed store, hash-verified

297 * @brief Reformats the entire script.
298 */
299void 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.

Callers

nothing calls this directly

Calls 2

formatDocumentFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected