Skip to content

Commit

Permalink
Fix string detection when cursor at end
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Jan 10, 2023
1 parent 0353154 commit 5b87323
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/gui/codeeditors/qgscodeeditorpython.cpp
Expand Up @@ -378,18 +378,36 @@ bool QgsCodeEditorPython::isCursorInsideString() const
int line, index;
getCursorPosition( &line, &index );
int position = positionFromLineIndex( line, index );
long style = SendScintilla( QsciScintillaBase::SCI_GETSTYLEAT, position );
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;

// Special case: cursor at the end of the document. Style will always be Default,
// so we have to check the style of the previous character.
// It it is an unclosed string (triple string, unclosed, or comment),
// consider cursor is inside a string.
if ( position >= length() && position > 0 )
{
long style = SendScintilla( QsciScintillaBase::SCI_GETSTYLEAT, position - 1 );
return style == QsciLexerPython::Comment
|| style == QsciLexerPython::TripleSingleQuotedString
|| style == QsciLexerPython::TripleDoubleQuotedString
|| style == QsciLexerPython::TripleSingleQuotedFString
|| style == QsciLexerPython::TripleDoubleQuotedFString
|| style == QsciLexerPython::UnclosedString;
}
else
{
long style = SendScintilla( QsciScintillaBase::SCI_GETSTYLEAT, position );
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
Expand Down

0 comments on commit 5b87323

Please sign in to comment.