2 #include <QLineNumberArea> 3 #include <QSyntaxStyle> 5 #include <QStyleSyntaxHighlighter> 6 #include <QFramedTextAttribute> 7 #include <QCXXHighlighter> 12 #include <QPaintEvent> 13 #include <QFontDatabase> 15 #include <QAbstractTextDocumentLayout> 16 #include <QTextCharFormat> 19 #include <QAbstractItemView> 38 m_autoIndentation(true),
39 m_autoParentheses(true),
41 m_tabReplace(QString(4,
' '))
62 auto fnt = QFontDatabase::systemFont(QFontDatabase::FixedFont);
63 fnt.setFixedPitch(
true);
73 &QTextDocument::blockCountChanged,
80 &QScrollBar::valueChanged,
86 &QTextEdit::cursorPositionChanged,
93 &QTextEdit::selectionChanged,
139 auto currentPalette = palette();
142 currentPalette.setColor(
143 QPalette::ColorRole::Text,
148 currentPalette.setColor(
154 currentPalette.setColor(
159 setPalette(currentPalette);
167 auto selected = textCursor().selectedText();
169 auto cursor = textCursor();
178 cursor.select(QTextCursor::SelectionType::WordUnderCursor);
180 QSignalBlocker blocker(
this);
183 if (selected.size() > 1 &&
184 cursor.selectedText() == selected)
186 auto backup = textCursor();
191 setTextCursor(backup);
197 QTextEdit::resizeEvent(e);
204 QRect cr = contentsRect();
229 if (rect.contains(viewport()->rect()))
238 auto searchIterator = cursor;
239 searchIterator.movePosition(QTextCursor::Start);
240 searchIterator = document()->find(cursor.selectedText(), searchIterator);
241 while (searchIterator.hasSelection())
245 searchIterator = document()->find(cursor.selectedText(), searchIterator);
256 setExtraSelections(extra);
264 for (
auto& pair : parentheses)
270 auto position = textCursor().position();
272 if (pair.first == currentSymbol)
275 counterSymbol = pair.second[0];
276 activeSymbol = currentSymbol;
278 else if (pair.second == prevSymbol)
281 counterSymbol = pair.first[0];
282 activeSymbol = prevSymbol;
292 while (counter != 0 &&
294 position < (document()->characterCount() - 1))
297 position += direction;
299 auto character = document()->characterAt(position);
301 if (character == activeSymbol)
305 else if (character == counterSymbol)
316 ExtraSelection selection{};
324 selection.format =
format;
325 selection.cursor = textCursor();
326 selection.cursor.clearSelection();
327 selection.cursor.movePosition(
329 QTextCursor::MoveMode::MoveAnchor,
330 std::abs(textCursor().position() - position)
333 selection.cursor.movePosition(
335 QTextCursor::MoveMode::KeepAnchor,
339 extraSelection.append(selection);
341 selection.cursor = textCursor();
342 selection.cursor.clearSelection();
343 selection.cursor.movePosition(
345 QTextCursor::MoveMode::KeepAnchor,
349 extraSelection.append(selection);
360 QTextEdit::ExtraSelection selection{};
363 selection.format.setForeground(QBrush());
364 selection.format.setProperty(QTextFormat::FullWidthSelection,
true);
365 selection.cursor = textCursor();
366 selection.cursor.clearSelection();
368 extraSelection.append(selection);
375 QTextEdit::paintEvent(e);
386 QTextCursor curs = QTextCursor(document());
387 curs.movePosition(QTextCursor::Start);
388 for(
int i=0; i < document()->blockCount(); ++i)
390 QTextBlock
block = curs.block();
392 QRect r1 = viewport()->geometry();
393 QRect r2 = document()
395 ->blockBoundingRect(block)
397 viewport()->geometry().
x(),
398 viewport()->geometry().
y() - verticalScrollBar()->sliderPosition()
401 if (r1.intersects(r2))
406 curs.movePosition(QTextCursor::NextBlock);
423 case Qt::Key_Backtab:
432 auto isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space);
440 auto ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
443 (ctrlOrShift && e->text().isEmpty()) ||
444 e->key() == Qt::Key_Delete)
449 static QString eow(R
"(~!@#$%^&*()_+{}|:"<>?,./;'[]\-=)"); 451 auto isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space);
455 (e->text().isEmpty() ||
456 completionPrefix.length() < 2 ||
457 eow.contains(e->text().right(1))))
463 if (completionPrefix !=
m_completer->completionPrefix())
465 m_completer->setCompletionPrefix(completionPrefix);
469 auto cursRect = cursorRect();
472 m_completer->popup()->verticalScrollBar()->sizeHint().width()
479 #if QT_VERSION >= 0x050A00 480 const int defaultIndent = tabStopDistance() / fontMetrics().averageCharWidth();
482 const int defaultIndent = tabStopWidth() / fontMetrics().averageCharWidth();
487 if (!completerSkip) {
489 e->modifiers() == Qt::NoModifier) {
497 #if QT_VERSION >= 0x050A00 499 indentationLevel * fontMetrics().averageCharWidth() / tabStopDistance();
502 indentationLevel * fontMetrics().averageCharWidth() / tabStopWidth();
508 (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) &&
512 insertPlainText(
"\n");
515 insertPlainText(QString(indentationLevel + defaultIndent,
' '));
517 insertPlainText(QString(tabCounts + 1,
'\t'));
519 insertPlainText(
"\n");
524 insertPlainText(QString(indentationLevel,
' '));
525 charsBack += indentationLevel;
529 insertPlainText(QString(tabCounts,
'\t'));
530 charsBack += tabCounts;
540 indentationLevel = std::min(indentationLevel,
m_tabReplace.size());
542 auto cursor = textCursor();
544 cursor.movePosition(QTextCursor::MoveOperation::StartOfLine);
546 QTextCursor::MoveMode::KeepAnchor, indentationLevel);
548 cursor.removeSelectedText();
552 QTextEdit::keyPressEvent(e);
554 if (
m_autoIndentation && (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)) {
556 insertPlainText(QString(indentationLevel,
' '));
558 insertPlainText(QString(tabCounts,
'\t'));
563 for (
auto&& el : parentheses)
566 if (el.first == e->text())
568 insertPlainText(el.second);
574 if (el.second == e->text())
578 if (symbol == el.second)
580 textCursor().deletePreviousChar();
650 m_completer->setCompletionMode(QCompleter::CompletionMode::PopupCompletion);
654 QOverload<const QString&>::of(&QCompleter::activated),
667 QTextEdit::focusInEvent(e);
677 auto tc = textCursor();
678 tc.select(QTextCursor::SelectionType::WordUnderCursor);
690 auto block = textCursor().blockNumber();
691 auto index = textCursor().positionInBlock();
692 auto text = document()->findBlockByNumber(
block).text();
696 if (index < 0 || index >= text.size())
706 auto tc = textCursor();
707 tc.select(QTextCursor::WordUnderCursor);
708 return tc.selectedText();
713 insertPlainText(source->text());
718 auto blockText = textCursor().block().text();
720 int indentationLevel = 0;
723 i < blockText.size() && QString(
"\t ").contains(blockText[i]);
726 if (blockText[i] ==
' ')
732 #if QT_VERSION >= 0x050A00 733 indentationLevel += tabStopDistance() / fontMetrics().averageCharWidth();
735 indentationLevel += tabStopWidth() / fontMetrics().averageCharWidth();
740 return indentationLevel;
QSize sizeHint() const override
Overridden method for getting line number area size.
void focusInEvent(QFocusEvent *e) override
Method, that's called on focus into widget. It's required for setting this widget to set completer...
void frame(QTextCursor cursor)
Method for creating frame in cursor selection.
int tabReplaceSize() const
Method for getting number of spaces, that will replace tab if tabReplace is true. Default: 4...
QStyleSyntaxHighlighter * m_highlighter
void initDocumentLayoutHandlers()
Method for initializing document layout handlers.
bool autoIndentation() const
Method for getting is auto indentation enabled. Default: true.
QCompleter * completer() const
Method for getting completer.
Class, that describes line number area widget.
void onSelectionChanged()
Slot, that will be called on selection change.
QString wordUnderCursor() const
Method for getting word under cursor.
void setCompleter(QCompleter *completer)
Method for setting completer.
bool tabReplace() const
Method for getting is tab replacing enabled. Default value: true.
bool autoParentheses() const
Method for getting is auto parentheses enabled. Default value: true.
void paintEvent(QPaintEvent *e) override
Method, that's called on editor painting. This method if overloaded for line number area redraw...
void proceedCompleterEnd(QKeyEvent *e)
QCodeEditor(QWidget *widget=nullptr)
Constructor.
void highlightCurrentLine(QList< QTextEdit::ExtraSelection > &extraSelection)
Method, that adds highlighting of currently selected line to extra selection list.
void setTabReplace(bool enabled)
Method for setting tab replacing enabled.
void updateLineNumberAreaWidth(int)
Slot, that performs update of internal editor viewport based on line number area width.
void setTabReplaceSize(int val)
Method for setting amount of spaces, that will replace tab.
static int type()
Static method for getting framed text attribute type.
static void block(LexState *ls)
static QVector< QPair< QString, QString > > parentheses
Class, that describes Qt style parser for QCodeEditor.
void setHighlighter(QStyleSyntaxHighlighter *highlighter)
Method for setting highlighter.
QChar charUnderCursor(int offset=0) const
Method for getting character under cursor.
void keyPressEvent(QKeyEvent *e) override
Method, that's called on any key press, posted into code editor widget. This method is overloaded for...
Class, that descrubes highlighter with syntax style.
void setSyntaxStyle(QSyntaxStyle *style)
Method for setting syntax style object.
void insertFromMimeData(const QMimeData *source) override
Method, that's called on any text insertion of mimedata into editor. If it's text - it inserts text a...
QFramedTextAttribute * m_framedAttribute
int getFirstVisibleBlock()
Method for getting first visible block index.
void updateStyle()
Slot, that will update editor style.
void updateLineGeometry()
Method for updating geometry of line number area.
QTextCharFormat getFormat(QString name) const
Method for getting format for property name.
void initFont()
Method for initializing default monospace font.
void highlightParenthesis(QList< QTextEdit::ExtraSelection > &extraSelection)
Method, that adds highlighting of parenthesis if available.
void setSyntaxStyle(QSyntaxStyle *style)
Method for setting syntax style.
void clear(QTextCursor cursor)
Method for clearing all frames with desired cursor.
void updateExtraSelection()
Slot, that will proceed extra selection for current cursor position.
void insertCompletion(QString s)
Slot, that performs insertion of completion info into code.
void setAutoParentheses(bool enabled)
Method setting auto parentheses enabled.
QSyntaxStyle * m_syntaxStyle
int getIndentationSpaces()
Method for getting number of indentation spaces in current line. Tabs will be treated as tabWidth / s...
QLineNumberArea * m_lineNumberArea
static QSyntaxStyle * defaultStyle()
Static method for getting default style.
void updateLineNumberArea(const QRect &rect)
Slot, that performs update of some part of line number area.
void resizeEvent(QResizeEvent *e) override
Method, that's called on any widget resize. This method if overloaded for line number area resizing...
void setSyntaxStyle(QSyntaxStyle *style)
Method for setting syntax sty.e.
bool proceedCompleterBegin(QKeyEvent *e)
Method, that performs completer processing. Returns true if event has to be dropped.
void performConnections()
Method for performing connection of objects.
void handleSelectionQuery(QTextCursor cursor)
Method, that performs selection frame selection.
void setAutoIndentation(bool enabled)
Method for setting auto indentation enabled.
void setSyntaxStyle(QSyntaxStyle *style)
Method for setting syntax style for rendering.
Class, that describes attribute for making text frame.
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)