* @brief Reformats the selected lines, or the current line when nothing is selected. */
| 253 | * @brief Reformats the selected lines, or the current line when nothing is selected. |
| 254 | */ |
| 255 | void DataModel::OutputCodeEditor::formatSelection() |
| 256 | { |
| 257 | const QString original = m_widget.toPlainText(); |
| 258 | |
| 259 | QTextCursor cursor = m_widget.textCursor(); |
| 260 | QTextCursor first(m_widget.document()); |
| 261 | first.setPosition(qMin(cursor.selectionStart(), cursor.selectionEnd())); |
| 262 | QTextCursor last(m_widget.document()); |
| 263 | last.setPosition(qMax(cursor.selectionStart(), cursor.selectionEnd())); |
| 264 | |
| 265 | const int firstLine = first.blockNumber(); |
| 266 | const int lastLine = last.blockNumber(); |
| 267 | const QString formatted = CodeFormatter::formatLineRange( |
| 268 | original, CodeFormatter::Language::JavaScript, firstLine, lastLine); |
| 269 | if (formatted == original) |
| 270 | return; |
| 271 | |
| 272 | const int savedPos = cursor.position(); |
| 273 | cursor.beginEditBlock(); |
| 274 | cursor.select(QTextCursor::Document); |
| 275 | cursor.insertText(formatted); |
| 276 | cursor.endEditBlock(); |
| 277 | |
| 278 | cursor.setPosition(qMin(savedPos, formatted.size())); |
| 279 | m_widget.setTextCursor(cursor); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * @brief Opens a file dialog to import an external JS file. |
nothing calls this directly
no test coverage detected