Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Public Member Functions | Private Attributes
SyntaxHighlighter Class Reference

Simple syntax highlighting for Scripter (QTextEdit). More...

#include <pconsole.h>

Collaboration diagram for SyntaxHighlighter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SyntaxHighlighter (QTextEdit *textEdit)
int highlightParagraph (const QString &text, int endStateOfLastPara)
 Reimplementation of the Qt highligtion for python.

Private Attributes

QStringList keywords
 Reserved python keywords.
SyntaxColors colors

Detailed Description

Simple syntax highlighting for Scripter (QTextEdit).

Based on the source of the Python Realizer (http://www.python-realizer.net) but very simplifier. Improved too (of course). TODO: colors of the higlited texts. User should set the colors in the preferences. Waiting for the new plugin API.

Author:
Petr Vanek, petr@.nosp@m.yarp.nosp@m.en.cz
Richard Magnor Stenbro stenb.nosp@m.ror@.nosp@m.hotma.nosp@m.il.c.nosp@m.om

Definition at line 128 of file pconsole.h.


Constructor & Destructor Documentation

SyntaxHighlighter::SyntaxHighlighter ( QTextEdit *  textEdit)

Definition at line 282 of file pconsole.cpp.

                                                        : QSyntaxHighlighter(textEdit)
{
       // Reserved keywords in Python 2.4
       keywords << "and" << "assert" << "break" << "class" << "continue" << "def"
                      << "del" << "elif" << "else" << "except" << "exec" << "finally"
                      << "for" << "from" << "global" << "if" << "import" << "in"
                      << "is" << "lambda" << "not" << "or" << "pass" << "print" << "raise"
                      << "return" << "try" << "while" << "yield";
}

Member Function Documentation

int SyntaxHighlighter::highlightParagraph ( const QString &  text,
int  endStateOfLastPara 
)

Reimplementation of the Qt highligtion for python.

Parameters:
textstring (one row) provided by text editor via QSyntaxHighlighter inheritance.
endStateOfLastParahow is the syntax left for next paragraph? 0 - normal text, 1 - multirows comment continues

Definition at line 292 of file pconsole.cpp.

{
       // position in the text
       unsigned int i = 0;

       /* ! ! signals error message, which we want in red */
       if (text.length() >= 3 && text[0] == '!' && text[1] == ' ' && text[2] == '!')
       {
              setFormat(0, 3, QColor(Qt::white));
              setFormat(3, text.length() - 2, colors.errorColor);
              return 0;
       }

       /* Turns of syntax highlighting for this line */
       if (text.length() >= 2 && text[0] == '!' && text[i + 1] == ' ')
       {
              setFormat(0, 2, QColor(Qt::white));
              setFormat(2, text.length() - 2, colors.textColor);
              return 0;
       }

       if (endStateOfLastPara == 1)
       {
              int docstrEnd = text.find("\"\"\"");
              if (docstrEnd == -1)
                     docstrEnd = text.length();
              setFormat(i, docstrEnd + 3, colors.commentColor);
              if (docstrEnd == (int)text.length())
                     return 1;
              i = docstrEnd + 3; // move back to """
       }

       while (i < text.length())
       {
              // HACK: remember the i is *the next* index!
              QChar ch = text[i++];

              if (ch.isLetter())
              {
                     // Read one word
                     unsigned long s = i - 1;
                     QString actualWord = ch;
                     while ((ch = text[i]).isLetterOrNumber() || ch == '_')
                     {
                            i++;
                            actualWord += ch;
                     }

                     // Check for reserved keywords
                     if (keywords.contains(actualWord) != 0)
                            setFormat(s, (i - s), colors.keywordColor);
                     else
                            setFormat(s, (i - s), colors.textColor);
              } // ch.isletter
              else if (ch == '+' && text[i] != '=')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '-' && text[i] != '=')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '*' && text[i] == '*' && text[i + 1] != '=')
                     setFormat(i - 1, 2, colors.signColor);
              else if (ch == '*' && (text[i] != '=' && text[i] != '*'))
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '/' && text[i] == '/' && text[i + 1] != '=')
                     setFormat(i - 1, 2, colors.signColor);
              else if (ch == '/' && (text[i] != '=' && text[i] != '/'))
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '%' && text[i] != '=')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '&' && text[i] != '=')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '|' && text[i] != '=')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '^' && text[i] != '=')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '=' && text[i] == '=')
              {
                     setFormat(i - 1, 2, colors.signColor);
                     i++;
              }
              else if (ch == '!' && text[i] == '=')
              {
                     setFormat(i - 1, 2, colors.signColor);
                     i++;
              }
              else if (ch == '~')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '<')
              {
                     if (text[i] == '>')
                     {
                            setFormat(i -1, 2, colors.signColor);
                            i++;
                     }
                     else if (text[i] == '<' && text[i + 1] == '=')
                     {
                            setFormat(i - 1, 3, colors.textColor);
                            i += 2;
                     }
                     else if (text[i] == '=' || text[i] == '<')
                     {
                            setFormat(i - 1, 2, colors.signColor);
                            i++;
                     }
                     else
                            setFormat(i - 1, 1, colors.signColor);
              } // ch == '<'
              else if (ch == '>')
              {
                     if (text[i] == '>' && text[i + 1] == '=')
                     {
                            setFormat(i - 1, 3, colors.textColor);
                            i += 2;
                     }
                     else if (text[i] == '=' || text[i] == '>')
                     {
                            setFormat(i - 1, 2, colors.signColor);
                            i++;
                     }
                     else
                            setFormat(i - 1, 1, colors.signColor);
              } // ch == '>'
              else if (ch >= '0' && ch <= '9')
              {
                     unsigned long s = i - 1; /* Start of number */
                     if (text[i] == 'x' || text[i] == 'X')
                     {
                            i++;
                            while ((text[i] >= '0' && text[i] <= '9') || (text[i] >= 'a' && text[i] <= 'f') || (text[i] >= 'A' && text[i] <= 'F'))
                                   i++;
                            if (text[i] == 'L')
                                   i++;
                            setFormat(s, (i - s), colors.numberColor);
                     }
                     while (text[i] >= '0' && text[i] <= '9')
                            i++;

                     bool floating = false;
                     if (text[i] == '.')
                     {
                            i++;
                            floating = true;
                            while (text[i] >= '0' && text[i] <= '9')
                                   i++;
                     }
                     if (text[i] == 'e' || text[i] == 'E')
                     {
                            floating = true;
                            i++;
                            if (text[i] == '+' || text[i] == '-')
                                   i++;
                            while (text[i] >= '0' && text[i] <= '9')
                                   i++;
                     }
                     if (!floating && text[i] == 'L')
                            i++;
                     setFormat(s, (i - s), colors.numberColor);
              } // if number
              else if ( ch == '@')
                     setFormat(i - 1, 1, colors.signColor);
              else if (ch == '#')
              {
                     setFormat((i - 1), (text.length() - i), colors.commentColor);
                     i = text.length();
              }
              // docstrings etc. (""" blah """)
              else if (ch == '"' && text.at(i) == '"' && text.at(i+1) == '"')
              {
                     bool cont = false; // continue?
                     int docstrEnd = text.find("\"\"\"", i + 2);
                     if (docstrEnd == -1)
                     {
                            docstrEnd = text.length() - i;
                            cont = true;
                     }
                     setFormat(i - 1, docstrEnd + 2, colors.commentColor);
                     if (cont)
                            return 1; // comment """ is opened
                     i += docstrEnd + 2;
              } // docstrings
              else if ( ch == '\'' || ch == '"')
              {
                     unsigned long s = i - 1;
                     if (text[i] != ch)
                     {
                            while (text[i] != ch && i != text.length())
                                   i++;
                            if (text[i] == ch)
                                   i++;
                            setFormat(s, (i - s), colors.stringColor);
                     }
                     else if (text[i] == ch && text[i + 1] == ch)
                     {
                            i += 2;
                            setFormat(s, (i - s), colors.textColor);

                     }
                     else if (text[i] == ch)
                     {
                            i++;
                            setFormat(s, (i - s), colors.stringColor);
                     }
              }
              /* Default */
              else setFormat(i - 1, 1, colors.textColor);
       }      // End of while statement

       return 0;
}

Member Data Documentation

Definition at line 142 of file pconsole.h.

QStringList SyntaxHighlighter::keywords [private]

Reserved python keywords.

Definition at line 141 of file pconsole.h.


The documentation for this class was generated from the following files: