Skip to content

Commit

Permalink
add helper functions to get cursor context info
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Jan 10, 2023
1 parent a888d9c commit da260a7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/gui/auto_generated/codeeditors/qgscodeeditorpython.sip.in
Expand Up @@ -55,6 +55,23 @@ Load APIs from one or more files
Loads a ``script`` file.
%End

bool isCursorInsideString() const;
%Docstring
Check whether the current cursor position is inside a string or comment
%End

QString characterBeforeCursor() const;
%Docstring
Returns the character before the cursor, or an empty string if cursor is set at start
%End


QString characterAfterCursor() const;
%Docstring
Returns the character after the cursor, or an empty string if cursor is set at end
%End


public slots:

void searchSelectedTextInPyQGISDocs();
Expand Down
43 changes: 43 additions & 0 deletions src/gui/codeeditors/qgscodeeditorpython.cpp
Expand Up @@ -245,6 +245,49 @@ bool QgsCodeEditorPython::loadScript( const QString &script )
return true;
}

bool QgsCodeEditorPython::isCursorInsideString() const
{
int line, index;
getCursorPosition(&line, &index);
auto postion = positionFromLineIndex(line, index);
auto style = SendScintilla(QsciScintillaBase::SCI_GETSTYLEAT, postion);
return style == QsciLexerPython::Comment
|| style == QsciLexerPython::DoubleQuotedString
|| style == QsciLexerPython::SingleQuotedString
|| style == QsciLexerPython::TripleSingleQuotedString
|| style == QsciLexerPython::TripleDoubleQuotedString
|| style == QsciLexerPython::CommentBlock
|| style == QsciLexerPython::UnclosedString
|| style == QsciLexerPython::DoubleQuotedFString
|| style == QsciLexerPython::SingleQuotedFString
|| style == QsciLexerPython::TripleSingleQuotedFString
|| style == QsciLexerPython::TripleDoubleQuotedFString;
}

QString QgsCodeEditorPython::characterBeforeCursor() const
{
int line, index;
getCursorPosition(&line, &index);
auto position = positionFromLineIndex(line, index);
if ( position <= 0 )
{
return "";
}
return text(position - 1, position);
}

QString QgsCodeEditorPython::characterAfterCursor() const
{
int line, index;
getCursorPosition(&line, &index);
auto position = positionFromLineIndex(line, index);
if ( position >= length() )
{
return "";
}
return text(position, position + 1);
}

void QgsCodeEditorPython::searchSelectedTextInPyQGISDocs()
{
if ( !hasSelectedText() )
Expand Down
15 changes: 15 additions & 0 deletions src/gui/codeeditors/qgscodeeditorpython.h
Expand Up @@ -75,6 +75,21 @@ class GUI_EXPORT QgsCodeEditorPython : public QgsCodeEditor
*/
bool loadScript( const QString &script );

/**
* Check whether the current cursor position is inside a string or comment
*/
bool isCursorInsideString() const;

/**
* Returns the character before the cursor, or an empty string if cursor is set at start
*/
QString characterBeforeCursor() const;

/**
* Returns the character after the cursor, or an empty string if the cursot is set at end
*/
QString characterAfterCursor() const;

public slots:

/**
Expand Down

0 comments on commit da260a7

Please sign in to comment.