[PyQt] A first simple lexer for QSciScintilla ?

projetmbc projetmbc at club-internet.fr
Fri Jul 10 18:02:18 BST 2009


Hello,
I'm trying to do a very simple lexer wich only detects a python single 
line comment :
================================
ID_DEFAULT = 0
ID_COMMENT = 1

class myLexer(Qsci.QsciLexerCustom):
    def description(self, style):
        if style == 0:
            print 'ID_DEFAULT'
            return 'ID_DEFAULT'
        if style == 1:
            print 'ID_COMMENT'
            return 'ID_COMMENT'
        return ''

    def styleText(self, start, end):
        if QsciStyle.style() == ID_COMMENT :
            if self.atLineEnd() :
                self.startStyling(start)
                self.setStyling(end,ID_DEFAULT)

        if QsciStyle.style() == ID_DEFAULT :
            if self.ch == '#' :
                self.startStyling(start)
                self.setStyling(end,ID_COMMENT)
================================
To do it, I've used a lexer written in C but I have problem.

Can you help to find the mistakes I've made ?

Best regards.
Christophe.

-------------- next part --------------
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'C:\Documents and Settings\Christophe\Mes documents\2,pyBaNaMa\DebuterAvecPythonEtPyQT\CodesProjets\04-Proj4_MiniEditHTML\05-TestColorSyntax(WebKit)\window_LecteurCodePython_HTML.ui     '
#
# Created: Thu Aug 28 12:37:43 2008
#      by: PyQt4 UI code generator 4.4.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_window_LecteurCodePython_HTML(object):
    def setupUi(self, window_LecteurCodePython_HTML):
        window_LecteurCodePython_HTML.setObjectName("window_LecteurCodePython_HTML")
        window_LecteurCodePython_HTML.resize(516, 397)
        self.centralwidget = QtGui.QWidget(window_LecteurCodePython_HTML)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.textScintilla_Principal = Qsci.QsciScintilla(self.centralwidget)
        self.textScintilla_Principal.setObjectName("textScintilla_Principal")
        self.verticalLayout.addWidget(self.textScintilla_Principal)
        window_LecteurCodePython_HTML.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(window_LecteurCodePython_HTML)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 516, 21))
        self.menubar.setObjectName("menubar")
        self.menuFichier = QtGui.QMenu(self.menubar)
        self.menuFichier.setObjectName("menuFichier")
        window_LecteurCodePython_HTML.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(window_LecteurCodePython_HTML)
        self.statusbar.setObjectName("statusbar")
        window_LecteurCodePython_HTML.setStatusBar(self.statusbar)
        self.actionOuvrir = QtGui.QAction(window_LecteurCodePython_HTML)
        self.actionOuvrir.setObjectName("actionOuvrir")
        self.actionEnregistrer = QtGui.QAction(window_LecteurCodePython_HTML)
        self.actionEnregistrer.setObjectName("actionEnregistrer")
        self.actionExporterHTML = QtGui.QAction(window_LecteurCodePython_HTML)
        self.actionExporterHTML.setObjectName("actionExporterHTML")
        self.menuFichier.addAction(self.actionOuvrir)
        self.menuFichier.addAction(self.actionEnregistrer)
        self.menuFichier.addSeparator()
        self.menuFichier.addAction(self.actionExporterHTML)
        self.menubar.addAction(self.menuFichier.menuAction())

        self.retranslateUi(window_LecteurCodePython_HTML)
        QtCore.QMetaObject.connectSlotsByName(window_LecteurCodePython_HTML)

    def retranslateUi(self, window_LecteurCodePython_HTML):
        window_LecteurCodePython_HTML.setWindowTitle(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.menuFichier.setTitle(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Actions", None, QtGui.QApplication.UnicodeUTF8))
        self.actionOuvrir.setText(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ouvrir", None, QtGui.QApplication.UnicodeUTF8))
        self.actionOuvrir.setShortcut(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ctrl+O", None, QtGui.QApplication.UnicodeUTF8))
        self.actionEnregistrer.setText(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Rechercher", None, QtGui.QApplication.UnicodeUTF8))
        self.actionEnregistrer.setShortcut(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ctrl+S", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExporterHTML.setText(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Exporter", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExporterHTML.setShortcut(QtGui.QApplication.translate("window_LecteurCodePython_HTML", "Ctrl+E", None, QtGui.QApplication.UnicodeUTF8))

from PyQt4 import Qsci

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    window_LecteurCodePython_HTML = QtGui.QMainWindow()
    ui = Ui_window_LecteurCodePython_HTML()
    ui.setupUi(window_LecteurCodePython_HTML)
    window_LecteurCodePython_HTML.show()
    sys.exit(app.exec_())

-------------- next part --------------
# -*- coding: utf-8 -*-
#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui, Qsci
from window_LecteurCodePython_HTML import Ui_window_LecteurCodePython_HTML

ID_DEFAULT = 0
ID_COMMENT = 1

class myLexer(Qsci.QsciLexerCustom):
# Struggle for a lexer.
# Try that and be patient : http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciLexerCustom.html
    def description(self, style):
# See here : http://www.riverbankcomputing.co.uk/static/Docs/QScintilla2/classQsciLexer.html#dd9c20adb43bc38d1a0ca3083ac3e6fa

# WARNING ! This method must be re-implemented by a sub-class.
        if style == 0:
            print 'ID_DEFAULT'
            return 'ID_DEFAULT'
        if style == 1:
            print 'ID_COMMENT'
            return 'ID_COMMENT'
        return ''


    def styleText(self, start, end):
        if QsciStyle.style() == ID_COMMENT :
            if self.atLineEnd() :
                self.startStyling(start)
                self.setStyling(end,ID_DEFAULT)

        if QsciStyle.style() == ID_DEFAULT :
            if self.ch == '#' :
                self.startStyling(start)
                self.setStyling(end,ID_COMMENT)


class window_LecteurCodePython_HTML(QtGui.QMainWindow, Ui_window_LecteurCodePython_HTML):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_window_LecteurCodePython_HTML.__init__(self)
        self.setupUi(self)

        font = QtGui.QFont()
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setFixedPitch(True)
        font.setPointSize(10)
        fm = QtGui.QFontMetrics(font)

        self.textScintilla_Principal.setFont(font)
        self.textScintilla_Principal.setMarginsFont(font)
        self.textScintilla_Principal.setMarginLineNumbers(1,True)
        self.textScintilla_Principal.setMarginWidth(1, fm.width( "00000" ) + 5)

        self.textScintilla_Principal.setFolding(Qsci.QsciScintilla.BoxedTreeFoldStyle)
        self.textScintilla_Principal.setWrapMode(Qsci.QsciScintilla.WrapMode(Qsci.QsciScintillaBase.SC_WRAP_WORD))
        self.textScintilla_Principal.setWrapVisualFlags(Qsci.QsciScintilla.WrapVisualFlag(Qsci.QsciScintilla.WrapFlagByBorder), Qsci.QsciScintilla.WrapVisualFlag(Qsci.QsciScintilla.WrapFlagNone), 0)  

        self.textScintilla_Principal.setLexer(myLexer())
        textFichier= """# A red line
/* This must
be
a fold */
       
Nothing special for the moment  except that blue word."""
        self.textScintilla_Principal.setText(textFichier)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    LecteurCodePython = window_LecteurCodePython_HTML()
    LecteurCodePython.show()
    sys.exit(app.exec_())


More information about the PyQt mailing list