[QScintilla] XML lexer and folding

Stefan Langer s.langer at artificial-technology.de
Wed May 7 10:19:28 BST 2008


Stefan Langer wrote:
> Stefan Langer wrote:
>>
>>>> so ... what is wrong:
>>>> - the QSci Lexer marks unknown Tags/Attributes red (but there is no
>>>> unknown tags in xml ... it is_* *_*_EXTENSIBLE_ Markup Language* )
>>>
>>> This problem can be solved by making the styles for *unknown*
>>> Tags/Attributes the same as those used for *known* Tags/Attributes.
>> ok, i haven't mentioned that I experimented a bit more ... when i 
>> changed the coloring of unknown tags/attributes to that of normal 
>> tags/attributes, the tags are colored correctly ... but the 
>> attributes aren't in "attribute color", if the tag is a unknown one 
>> (so in the unknown-tags there seems to be no attribute-detection or 
>> something like that)
>>> It would be nice if there was a qscintilla lexer to encapsulate a sane
>>> set of defaults for XML. If you feel like making a contribution, it
>>> would be pretty easy to write one (just take a look at 
>>> qscilexerhtml.cpp
>>> for inspiration).
>> i started playing around with a qscilexerxml.cpp ... if I find a 
>> solution for that unknown-attributes within unknown-tags, i will 
>> cleanup this experimental file and post it ...
> ah, I have to clear my keyword-list ^^
> LexHTML.cpp (scintilla):
> ...
>    if ((chAttr == SCE_H_TAGUNKNOWN) && !keywords) {
>        // No keywords -> all are known
>        chAttr = SCE_H_TAG;
>        isScript = 0 == strcmp(s, "script");
>    }
> ...
>
>
> after that the attributes seems ok ...

so now here is my xml-lexer ^^
it uses QsciLexerHTML as it's base class ... and tried to keep it as 
small as possible ...

I would be happy, if it finds it way to the next qsci release ;)


-------------- next part --------------
//TODO: add copyright header if you want XD

#include "Qsci/qscilexerxml.h"

#include <qcolor.h>
#include <qfont.h>
#include <qsettings.h>


// The ctor.
QsciLexerXML::QsciLexerXML(QObject *parent)
    : QsciLexerHTML(parent)
{
}

// The dtor.
QsciLexerXML::~QsciLexerXML()
{
}

// Returns the language name.
const char *QsciLexerXML::language() const
{
    return "XML";
}


// Returns the lexer name.
const char *QsciLexerXML::lexer() const
{
    return "xml";
}

// Returns the foreground colour of the text for a style.
QColor QsciLexerXML::defaultColor(int style) const
{
	if(style==UnknownTag) style=Tag;
	if(style==UnknownAttribute) style=Attribute;
	return QsciLexerHTML::defaultColor(style);
}

// Returns the set of keywords.
const char *QsciLexerXML::keywords(int set) const
{
    return 0; // in xml there are no keywords (coloring of unknown tags only work correctly if this list is really empty)
}


// Refresh all properties.
void QsciLexerXML::refreshProperties()
{
	QsciLexerHTML::refreshProperties();
    emit propertyChanged("fold.html","1");
//    emit propertyChanged("braces.xml.style","31"); //SciTE also disables brace matching in XML ... I would keep it active
}

-------------- next part --------------
//TODO: add copyright header if you want XD

#ifndef QSCILEXERXML_H
#define QSCILEXERXML_H


#include <qobject.h>

#include <Qsci/qsciglobal.h>
#include <Qsci/qscilexer.h>

#include <Qsci/qscilexerhtml.h>


//! \brief The QsciLexerXML class encapsulates the Scintilla HTML lexer.
class QSCINTILLA_EXPORT QsciLexerXML : public QsciLexerHTML
{
    Q_OBJECT

public:
    //! Construct a QsciLexerXML with parent \a parent.  \a parent is
    //! typically the QsciScintilla instance.
    QsciLexerXML(QObject *parent = 0);

    //! Destroys the QsciLexerXML instance.
    virtual ~QsciLexerXML();

    //! Returns the name of the language.
    const char *language() const;

    //! Returns the name of the lexer.  Some lexers support a number of
    //! languages.
    const char *lexer() const;

    //! Returns the foreground colour of the text for style number \a style.
    //!
    //! \sa defaultPaper()
    QColor defaultColor(int style) const;

    //! Returns the set of keywords for the keyword set \a set recognised
    //! by the lexer as a space separated string.
    const char *keywords(int set) const;

    //! Causes all properties to be refreshed by emitting the
    //! propertyChanged() signal as required.
    void refreshProperties();

private:
    QsciLexerXML(const QsciLexerXML &);
    QsciLexerXML &operator=(const QsciLexerXML &);
};

#endif


More information about the QScintilla mailing list