15#include <QRegularExpression>
16#include <QPlainTextEdit>
23static const QString EXPRESSION_PATTERN = QStringLiteral(
"\\[%\\s*(.*?)\\s*%\\]" );
28 start = startSelectionPos;
29 end = endSelectionPos;
31 expression = text.mid( startSelectionPos, endSelectionPos - startSelectionPos ).replace( QChar( 0x2029 ), QChar(
'\n' ) );
35 if ( startSelectionPos != endSelectionPos )
41 const QRegularExpression regex( pattern.isEmpty() ? EXPRESSION_PATTERN : pattern );
42 QRegularExpressionMatchIterator result = regex.globalMatch( text );
44 while ( result.hasNext() )
46 const QRegularExpressionMatch match = result.next();
49 if ( match.capturedStart() < startSelectionPos && match.capturedEnd() > endSelectionPos )
51 start = match.capturedStart();
52 end = match.capturedEnd();
54 expression = match.captured( 1 );
56 expression = expression.replace( QChar( 0x2029 ), QChar(
'\n' ) );
71 int newSelectionStart, newSelectionEnd;
72 findExpressionAtPos( editor->text(), startPosition, endPosition, newSelectionStart, newSelectionEnd, res, pattern );
83 int startPosition = editor->textCursor().selectionStart();
84 int endPosition = editor->textCursor().selectionEnd();
87 int newSelectionStart, newSelectionEnd;
88 findExpressionAtPos( editor->toPlainText(), startPosition, endPosition, newSelectionStart, newSelectionEnd, res, pattern );
90 QTextCursor cursor = editor->textCursor();
91 cursor.setPosition( newSelectionStart, QTextCursor::MoveAnchor );
92 cursor.setPosition( newSelectionEnd, QTextCursor::KeepAnchor );
93 editor->setTextCursor( cursor );
102 int startPosition = editor->textCursor().selectionStart();
103 int endPosition = editor->textCursor().selectionEnd();
106 int newSelectionStart, newSelectionEnd;
107 findExpressionAtPos( editor->toPlainText(), startPosition, endPosition, newSelectionStart, newSelectionEnd, res, pattern );
109 QTextCursor cursor = editor->textCursor();
110 cursor.setPosition( newSelectionStart, QTextCursor::MoveAnchor );
111 cursor.setPosition( newSelectionEnd, QTextCursor::KeepAnchor );
112 editor->setTextCursor( cursor );
A text editor based on QScintilla2.
void setLinearSelection(int start, int end)
Convenience function to set the selection using linear indexes.
int selectionEnd() const
Convenience function to return the end of the selection as a linear index Contrary to the getSelectio...
int selectionStart() const
Convenience function to return the start of the selection as a linear index Contrary to the getSelect...
static QString findAndSelectActiveExpression(QgsCodeEditor *editor, const QString &pattern=QString())
Find the expression under the cursor in the given editor and select it.
static void findExpressionAtPos(const QString &text, int startSelectionPos, int endSelectionPos, int &start, int &end, QString &expression, const QString &pattern=QString())
Find an expression at the given position in the given text.