86 : QPlainTextEdit(parent),
92 setLineWrapMode(QPlainTextEdit::NoWrap);
93 setProperty(
"class",
"mono");
97 completer->setCaseSensitivity(Qt::CaseSensitive);
98 completer->setCompletionMode(QCompleter::PopupCompletion);
99 completer->setModelSorting(QCompleter::CaseSensitivelySortedModel);
103 callbacks.connect(
this, &QPlainTextEdit::blockCountChanged,
105 callbacks.connect(
this, &QPlainTextEdit::updateRequest,
106 [
this](
const QRect& rect,
const int dy) {
111 if (rect.contains(viewport()->rect()))
114 callbacks.connect(
this, &QPlainTextEdit::cursorPositionChanged, [
this] {
214 QTextCursor cursor = textCursor();
215 const auto currentBlock = cursor.block();
216 const auto currentText = currentBlock.text();
217 const auto currentUtf8 = currentText.toUtf8();
219 std::array<QByteArray, 2> previousUtf8;
220 std::array<std::string_view, 2> previousLines;
221 std::size_t previousCount = 0;
222 for (
auto block = currentBlock.previous(); block.isValid() && previousCount < 2;
223 block = block.previous()) {
224 if (block.text().trimmed().isEmpty())
226 previousUtf8[previousCount] = block.text().toUtf8();
227 previousLines[previousCount] =
228 std::string_view(previousUtf8[previousCount].constData(),
229 static_cast<std::size_t
>(previousUtf8[previousCount].size()));
234 .currentLine = std::string_view(currentUtf8.constData(),
235 static_cast<std::size_t
>(currentUtf8.size())),
236 .previousNonBlankLines =
237 std::span<const std::string_view>(previousLines).first(previousCount),
240 const auto desiredIndentUtf8 =
indentation->indentationFor(context);
241 const auto desiredIndent = QString::fromUtf8(
242 desiredIndentUtf8.data(),
static_cast<qsizetype
>(desiredIndentUtf8.size()));
244 qsizetype existingIndentLength = 0;
245 while (existingIndentLength < currentText.size()
246 && (currentText[existingIndentLength] == QLatin1Char(
' ')
247 || currentText[existingIndentLength] == QLatin1Char(
'\t'))) {
248 ++existingIndentLength;
250 if (currentText.first(existingIndentLength) == desiredIndent)
253 const int contentOffset =
254 std::max(0, cursor.positionInBlock() -
static_cast<int>(existingIndentLength));
255 cursor.beginEditBlock();
256 QTextCursor indentationCursor(document());
257 indentationCursor.setPosition(currentBlock.position());
258 indentationCursor.setPosition(currentBlock.position()
259 +
static_cast<int>(existingIndentLength),
260 QTextCursor::KeepAnchor);
261 indentationCursor.insertText(desiredIndent);
262 cursor.setPosition(currentBlock.position() + desiredIndent.size() + contentOffset);
263 setTextCursor(cursor);
264 cursor.endEditBlock();
294 if (!explicitRequest && prefix.size() < 2) {
303 constexpr int COMPLETION_POPUP_PADDING = 8;
304 QRect rect = cursorRect();
305 rect.translate(0, COMPLETION_POPUP_PADDING);
306 rect.setWidth(
completer->popup()->sizeHintForColumn(0)
307 +
completer->popup()->verticalScrollBar()->sizeHint().width());
327 QTextCursor cursor = textCursor();
328 const auto line = cursor.block().text();
329 const int offset = cursor.positionInBlock();
330 const QChar next = offset < line.size() ? line.at(offset) : QChar{};
331 const QChar close = closingDelimiter(typed);
333 if (!close.isNull()) {
334 if (cursor.hasSelection()) {
335 const int start = cursor.selectionStart();
336 const auto selection =
337 cursor.selectedText().replace(QChar::ParagraphSeparator, QLatin1Char(
'\n'));
338 cursor.insertText(QString(typed) + selection + close);
339 cursor.setPosition(start + 1);
340 cursor.setPosition(start + 1 + selection.size(), QTextCursor::KeepAnchor);
341 }
else if (next == close) {
342 cursor.insertText(QString(typed));
343 }
else if (next.isNull() || next.isSpace() || isClosingDelimiter(next)
344 || next == QLatin1Char(
';') || next == QLatin1Char(
',')) {
345 const int position = cursor.position();
346 cursor.insertText(QString(typed) + close);
347 cursor.setPosition(position + 1);
351 setTextCursor(cursor);
355 if (!cursor.hasSelection() && isClosingDelimiter(typed) && next == typed) {
356 cursor.movePosition(QTextCursor::Right);
357 setTextCursor(cursor);
366 QTextCursor cursor = textCursor();
367 if (cursor.hasSelection())
370 const auto line = cursor.block().text();
371 const int offset = cursor.positionInBlock();
372 if (offset <= 0 || offset >= line.size()
373 || closingDelimiter(line.at(offset - 1)) != line.at(offset))
376 cursor.setPosition(cursor.position() - 1);
377 cursor.setPosition(cursor.position() + 2, QTextCursor::KeepAnchor);
378 cursor.removeSelectedText();
379 setTextCursor(cursor);
391 QTextCursor cursor = textCursor();
392 if (cursor.hasSelection())
395 const auto line = cursor.block().text();
396 const int offset = cursor.positionInBlock();
397 if (offset <= 0 || offset >= line.size()
398 || closingDelimiter(line.at(offset - 1)) != line.at(offset))
401 qsizetype prefixLength = 0;
402 while (prefixLength < line.size()
403 && (line.at(prefixLength) == QLatin1Char(
' ')
404 || line.at(prefixLength) == QLatin1Char(
'\t')))
407 const auto baseIndent = line.first(prefixLength);
408 const auto innerIndent =
409 baseIndent + QString(
indentation->indentWidth, QLatin1Char(
' '));
410 const int position = cursor.position();
411 cursor.insertText(QLatin1Char(
'\n') + innerIndent + QLatin1Char(
'\n') + baseIndent);
412 cursor.setPosition(position + 1 + innerIndent.size());
413 setTextCursor(cursor);
420 switch (event->key()) {
424 case Qt::Key_Backtab:
425 if (
const auto index =
completer->popup()->currentIndex(); index.isValid())
438 const bool plainKey =
440 && !(
event->modifiers()
441 & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
442 if (plainKey && event->key() == Qt::Key_Backspace &&
removeEmptyPair()) {
448 if (event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab) {
449 if (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) {
450 QPlainTextEdit::keyPressEvent(event);
454 if (event->key() == Qt::Key_Tab) {
455 QTextCursor cursor = textCursor();
456 cursor.insertText(QString(indent, QLatin1Char(
' ')));
457 setTextCursor(cursor);
465 if (event->matches(QKeySequence::InsertParagraphSeparator)) {
471 QTextCursor cursor = textCursor();
472 cursor.beginEditBlock();
473 QPlainTextEdit::keyPressEvent(event);
475 cursor.endEditBlock();
479 const bool explicitRequest =
480 event->modifiers() == Qt::ControlModifier &&
event->key() == Qt::Key_Space;
481 const bool typedText =
482 !
event->text().isEmpty()
483 && !(
event->modifiers()
484 & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
485 if (plainKey && event->text().size() == 1 &&
insertAutoPair(event->text().front())) {
490 if (!explicitRequest)
491 QPlainTextEdit::keyPressEvent(event);
494 if (explicitRequest || typedText)
503 #ifdef __EMSCRIPTEN__
504 auto* menu =
new QMenu(
this);
505 menu->setAttribute(Qt::WA_DeleteOnClose);
507 QMenu stackMenu(
this);
508 auto* menu = &stackMenu;
511 auto* cutAction = menu->addAction(tr(
"Cut"),
this, &QPlainTextEdit::cut);
512 cutAction->setEnabled(!isReadOnly() && textCursor().hasSelection());
513 auto* copyAction = menu->addAction(tr(
"Copy"),
this, &QPlainTextEdit::copy);
514 copyAction->setEnabled(textCursor().hasSelection());
515 auto* pasteAction = menu->addAction(tr(
"Paste"),
this, &QPlainTextEdit::paste);
516 pasteAction->setEnabled(!isReadOnly() && canPaste());
517 auto* deleteAction = menu->addAction(tr(
"Delete"),
callbacks.context(), [
this] {
518 auto cursor = textCursor();
519 cursor.removeSelectedText();
521 deleteAction->setEnabled(!isReadOnly() && textCursor().hasSelection());
522 menu->addSeparator();
523 auto* selectAllAction =
524 menu->addAction(tr(
"Select All"),
this, &QPlainTextEdit::selectAll);
525 selectAllAction->setEnabled(!document()->isEmpty());
527 #ifdef __EMSCRIPTEN__
528 menu->popup(event->globalPos());
530 menu->exec(event->globalPos());
590 const QFontMetrics metrics = painter.fontMetrics();
591 painter.fillRect(event->rect(), palette().color(QPalette::AlternateBase));
592 QTextBlock block = firstVisibleBlock();
593 int blockNumber = block.blockNumber();
594 int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
595 int bottom = top + qRound(blockBoundingRect(block).height());
596 const int currentBlock = textCursor().blockNumber();
597 while (block.isValid() && top <= event->rect().bottom()) {
598 if (block.isVisible() && bottom >= event->rect().top()) {
599 painter.setPen(palette().color(
600 blockNumber == currentBlock ? QPalette::Text : QPalette::PlaceholderText));
601 painter.drawText(0, top,
lineNumberArea->width() - 6, metrics.height(),
602 Qt::AlignRight, QString::number(blockNumber + 1));
604 block = block.next();
606 bottom = top + qRound(blockBoundingRect(block).height());
661 QTextCursor cursor = textCursor();
663 cursor.beginEditBlock();
665 const QTextBlock firstBlock = cursor.block();
666 const QTextBlock lastBlock =
667 cursor.hasSelection() ? document()->findBlock(cursor.selectionEnd()) : firstBlock;
669 std::vector<std::pair<int, int>> removals;
670 for (QTextBlock block = firstBlock;
671 block.isValid() && block.position() <= lastBlock.position();
672 block = block.next()) {
673 const QString text = block.text();
675 while (removeCount < text.size() && removeCount < indent) {
676 const QChar ch = text.at(removeCount);
677 if (ch == QLatin1Char(
' ')) {
679 }
else if (ch == QLatin1Char(
'\t')) {
687 removals.emplace_back(block.position(), removeCount);
690 for (
auto it = removals.rbegin(); it != removals.rend(); ++it) {
691 QTextCursor blockCursor(document());
692 blockCursor.setPosition(it->first);
693 blockCursor.setPosition(it->first + it->second, QTextCursor::KeepAnchor);
694 blockCursor.removeSelectedText();
697 cursor.endEditBlock();
698 setTextCursor(cursor);