[Eric] How to build a custom lexer....

projetmbc projetmbc at club-internet.fr
Sun Jul 5 11:10:06 BST 2009


Hello,
I'm trying to understand the use of the custom Lexer of QSciScintilla 
but it is too hard to understand what it must be done. My lexer would 
for example ....

    1. ... put in red all line which begins with a #

    2. ... put in blue only the word blue

    3. ... make a folding with something starting with /* and ending
    with */

Can you help me ? Where can I find concret informations showing how to 
implement the four methods (a Python and not a C code will be great) :

    1) description : I know that I must return a string. I guess that
    this to make a correspondance betwenn a number style and a string
    description.
    2) styleText : I know that this must find the word to be coulour but
    I do not know how. For example, how we have acces to a special
    character of the text ?
    3) setStyling and startStyling : two big mysterious things...

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

class myLexer(Qsci.QsciLexerCustom):
    def description(self, style):
        return 'Style No ' + str(style)

#    def styleText(self, start, end):
#        ?????
#
#    def setStyling(self, lenght, style_bits = 0):
#        ?????
#
#    def startStyling(self, pos, style_bits = 0):
#        ?????

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 Eric mailing list