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

Method formatDocument

app/src/DataModel/Editors/JsCodeEditor.cpp:465–483  ·  view source on GitHub ↗

* @brief Reformats the entire editor contents. */

Source from the content-addressed store, hash-verified

463 * @brief Reformats the entire editor contents.
464 */
465void 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.

Callers

nothing calls this directly

Calls 2

formatDocumentFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected