Skip to content

Commit

Permalink
Ensure True/False are treated as Python keywords in Python code editor
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2020
1 parent ea6ad85 commit 5f66104
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/app/options/qgscodeeditoroptions.cpp
Expand Up @@ -172,7 +172,7 @@ QgsCodeEditorOptionsWidget::QgsCodeEditorOptionsWidget( QWidget *parent )
"""
Function docstring
"""
return [1, 1.2, "val", 'a string', {'a': 1, 'b': 2}]
return [1, 1.2, "val", 'a string', {'a': True, 'b': False}]
@my_decorator
def somefunc(param1: str='', param2=0):
Expand Down
23 changes: 22 additions & 1 deletion src/gui/codeeditors/qgscodeeditorpython.cpp
Expand Up @@ -55,7 +55,7 @@ void QgsCodeEditorPython::initializeLexer()
QFont font = lexerFont();
QColor defaultColor = lexerColor( QgsCodeEditorColorScheme::ColorRole::Default );

QsciLexerPython *pyLexer = new QsciLexerPython( this );
QsciLexerPython *pyLexer = new QgsQsciLexerPython( this );

pyLexer->setIndentationWarning( QsciLexerPython::Inconsistent );
pyLexer->setFoldComments( true );
Expand Down Expand Up @@ -236,3 +236,24 @@ void QgsCodeEditorPython::searchSelectedTextInPyQGISDocs()
const QString version = QString( Qgis::version() ).split( '.' ).mid( 0, 2 ).join( '.' );
QDesktopServices::openUrl( QUrl( QStringLiteral( "https://qgis.org/pyqgis/%1/search.html?q=%2" ).arg( version, text ) ) );
}

//
// QgsQsciLexerPython
//
QgsQsciLexerPython::QgsQsciLexerPython( QObject *parent )
: QsciLexerPython( parent )
{

}

const char *QgsQsciLexerPython::keywords( int set ) const
{
if ( set == 1 )
{
return "True False and as assert break class continue def del elif else except exec "
"finally for from global if import in is lambda None not or pass "
"print raise return try while with yield";
}

return QsciLexerPython::keywords( set );
}
15 changes: 15 additions & 0 deletions src/gui/codeeditors/qgscodeeditorpython.h
Expand Up @@ -19,9 +19,24 @@
#include "qgscodeeditor.h"
#include "qgis_sip.h"
#include "qgis_gui.h"
#include <Qsci/qscilexerpython.h>

SIP_IF_MODULE( HAVE_QSCI_SIP )

#ifndef SIP_RUN
///@cond PRIVATE
class QgsQsciLexerPython : public QsciLexerPython
{
Q_OBJECT
public:

QgsQsciLexerPython( QObject *parent = nullptr );

const char *keywords( int set ) const override;

};
///@endcond
#endif

/**
* \ingroup gui
Expand Down

0 comments on commit 5f66104

Please sign in to comment.