]> git.saurik.com Git - wxWidgets.git/blobdiff - src/stc/scintilla/src/LexPython.cxx
Update Scintilla to version 1.75
[wxWidgets.git] / src / stc / scintilla / src / LexPython.cxx
index 6675c1f0376d00bc9ceba566312a91cb07037bad..01d406d0d076701d562f0b5b1c86b72f9d0161e8 100644 (file)
@@ -1,7 +1,7 @@
 // Scintilla source code edit control
 /** @file LexPython.cxx
  ** Lexer for Python.
- **/ 
+ **/
 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
 // The License.txt file describes the conditions under which this software may be distributed.
 
 #include "Scintilla.h"
 #include "SciLexer.h"
 
+#ifdef SCI_NAMESPACE
+using namespace Scintilla;
+#endif
+
 enum kwType { kwOther, kwClass, kwDef, kwImport };
+static const int indicatorWhitespace = 1;
 
 static bool IsPyComment(Accessor &styler, int pos, int len) {
        return len > 0 && styler[pos] == '#';
@@ -42,7 +47,7 @@ static bool IsPyStringStart(int ch, int chNext, int chNext2) {
 }
 
 /* Return the state to use for the string starting at i; *nextIndex will be set to the first index following the quote(s) */
-static int GetPyStringState(Accessor &styler, int i, int *nextIndex) {
+static int GetPyStringState(Accessor &styler, int i, unsigned int *nextIndex) {
        char ch = styler.SafeGetCharAt(i);
        char chNext = styler.SafeGetCharAt(i + 1);
 
@@ -99,7 +104,8 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
        int lineCurrent = styler.GetLine(startPos);
        if (startPos > 0) {
                if (lineCurrent > 0) {
-                       startPos = styler.LineStart(lineCurrent - 1);
+                       lineCurrent--;
+                       startPos = styler.LineStart(lineCurrent);
                        if (startPos == 0)
                                initStyle = SCE_P_DEFAULT;
                        else
@@ -108,6 +114,7 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
        }
 
        WordList &keywords = *keywordlists[0];
+       WordList &keywords2 = *keywordlists[1];
 
        const int whingeLevel = styler.GetPropertyInt("tab.timmy.whinge.level");
 
@@ -121,25 +128,29 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
        styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
        bool hexadecimal = false;
 
-       // Python uses a different mask because bad indentation is marked by oring with 32
-       StyleContext sc(startPos, endPos - startPos, initStyle, styler, 0x7f);
+       StyleContext sc(startPos, endPos - startPos, initStyle, styler);
+
+       bool indentGood = true;
+       int startIndicator = sc.currentPos;
 
        for (; sc.More(); sc.Forward()) {
 
                if (sc.atLineStart) {
-                       const char chBad = static_cast<char>(64);
-                       const char chGood = static_cast<char>(0);
-                       char chFlags = chGood;
+                       styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
+                       indentGood = true;
                        if (whingeLevel == 1) {
-                               chFlags = (spaceFlags & wsInconsistent) ? chBad : chGood;
+                               indentGood = (spaceFlags & wsInconsistent) == 0;
                        } else if (whingeLevel == 2) {
-                               chFlags = (spaceFlags & wsSpaceTab) ? chBad : chGood;
+                               indentGood = (spaceFlags & wsSpaceTab) == 0;
                        } else if (whingeLevel == 3) {
-                               chFlags = (spaceFlags & wsSpace) ? chBad : chGood;
+                               indentGood = (spaceFlags & wsSpace) == 0;
                        } else if (whingeLevel == 4) {
-                               chFlags = (spaceFlags & wsTab) ? chBad : chGood;
+                               indentGood = (spaceFlags & wsTab) == 0;
+                       }
+                       if (!indentGood) {
+                               styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 0);
+                               startIndicator = sc.currentPos;
                        }
-                       styler.SetFlags(chFlags, static_cast<char>(sc.state));
                }
 
                if (sc.atLineEnd) {
@@ -148,10 +159,9 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                (sc.state == SCE_P_TRIPLEDOUBLE)) {
                                // Perform colourisation of white space and triple quoted strings at end of each line to allow
                                // tab marking to work inside white space and triple quoted strings
-                               sc.ForwardSetState(sc.state);
+                               sc.SetState(sc.state);
                        }
                        lineCurrent++;
-                       styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
                        if ((sc.state == SCE_P_STRING) || (sc.state == SCE_P_CHARACTER)) {
                                sc.ChangeState(SCE_P_STRINGEOL);
                                sc.ForwardSetState(SCE_P_DEFAULT);
@@ -160,10 +170,12 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                break;
                }
 
+               bool needEOLCheck = false;
+
                // Check for a state end
                if (sc.state == SCE_P_OPERATOR) {
                        kwLast = kwOther;
-                       sc.SetState(SCE_C_DEFAULT);
+                       sc.SetState(SCE_P_DEFAULT);
                } else if (sc.state == SCE_P_NUMBER) {
                        if (!IsAWordChar(sc.ch) &&
                                !(!hexadecimal && ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')))) {
@@ -182,6 +194,8 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                        style = SCE_P_CLASSNAME;
                                } else if (kwLast == kwDef) {
                                        style = SCE_P_DEFNAME;
+                               } else if (keywords2.InList(s)) {
+                                       style = SCE_P_WORD2;
                                }
                                sc.ChangeState(style);
                                sc.SetState(SCE_P_DEFAULT);
@@ -194,9 +208,7 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                                kwLast = kwImport;
                                        else
                                                kwLast = kwOther;
-                               } else if (style == SCE_P_CLASSNAME) {
-                                       kwLast = kwOther;
-                               } else if (style == SCE_P_DEFNAME) {
+                               } else {
                                        kwLast = kwOther;
                                }
                        }
@@ -204,6 +216,10 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                        if (sc.ch == '\r' || sc.ch == '\n') {
                                sc.SetState(SCE_P_DEFAULT);
                        }
+               } else if (sc.state == SCE_P_DECORATOR) {
+                       if (!IsAWordChar(sc.ch)) {
+                               sc.SetState(SCE_P_DEFAULT);
+                       }
                } else if ((sc.state == SCE_P_STRING) || (sc.state == SCE_P_CHARACTER)) {
                        if (sc.ch == '\\') {
                                if ((sc.chNext == '\r') && (sc.GetRelative(2) == '\n')) {
@@ -212,8 +228,10 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                sc.Forward();
                        } else if ((sc.state == SCE_P_STRING) && (sc.ch == '\"')) {
                                sc.ForwardSetState(SCE_P_DEFAULT);
+                               needEOLCheck = true;
                        } else if ((sc.state == SCE_P_CHARACTER) && (sc.ch == '\'')) {
                                sc.ForwardSetState(SCE_P_DEFAULT);
+                               needEOLCheck = true;
                        }
                } else if (sc.state == SCE_P_TRIPLE) {
                        if (sc.ch == '\\') {
@@ -222,6 +240,7 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                sc.Forward();
                                sc.Forward();
                                sc.ForwardSetState(SCE_P_DEFAULT);
+                               needEOLCheck = true;
                        }
                } else if (sc.state == SCE_P_TRIPLEDOUBLE) {
                        if (sc.ch == '\\') {
@@ -230,9 +249,24 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                sc.Forward();
                                sc.Forward();
                                sc.ForwardSetState(SCE_P_DEFAULT);
+                               needEOLCheck = true;
                        }
                }
 
+               if (!indentGood && !IsASpaceOrTab(sc.ch)) {
+                       styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 1);
+                       startIndicator = sc.currentPos;
+                       indentGood = true;
+               }
+
+               // State exit code may have moved on to end of line
+               if (needEOLCheck && sc.atLineEnd) {
+                       lineCurrent++;
+                       styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
+                       if (!sc.More())
+                               break;
+               }
+
                // Check for a new state starting character
                if (sc.state == SCE_P_DEFAULT) {
                        if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
@@ -246,10 +280,12 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                                sc.SetState(SCE_P_OPERATOR);
                        } else if (sc.ch == '#') {
                                sc.SetState(sc.chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE);
+                       } else if (sc.ch == '@') {
+                               sc.SetState(SCE_P_DECORATOR);
                        } else if (IsPyStringStart(sc.ch, sc.chNext, sc.GetRelative(2))) {
-                               int nextIndex = 0;
+                               unsigned int nextIndex = 0;
                                sc.SetState(GetPyStringState(styler, sc.currentPos, &nextIndex));
-                               while (nextIndex > (sc.currentPos + 1)) {
+                               while (nextIndex > (sc.currentPos + 1) && sc.More()) {
                                        sc.Forward();
                                }
                        } else if (IsAWordStart(sc.ch)) {
@@ -257,6 +293,7 @@ static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
                        }
                }
        }
+       styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 0);
        sc.Complete();
 }
 
@@ -357,34 +394,40 @@ static void FoldPyDoc(unsigned int startPos, int length, int /*initStyle - unuse
                        lev = lev + 1;
                }
 
-               // Skip past any blank lines for next indent level info; we skip also comments
-               // starting in column 0 which effectively folds them into surrounding code rather
+               // Skip past any blank lines for next indent level info; we skip also
+               // comments (all comments, not just those starting in column 0)
+               // which effectively folds them into surrounding code rather
                // than screwing up folding.
-               const int saveIndentNext = indentNext;
+
                while (!quote &&
                        (lineNext < docLines) &&
                        ((indentNext & SC_FOLDLEVELWHITEFLAG) ||
-                        (lineNext <= docLines && styler[styler.LineStart(lineNext)] == '#'))) {
+                        (lineNext <= docLines && IsCommentLine(lineNext, styler)))) {
 
                        lineNext++;
                        indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
                }
 
-               // Next compute max indent level of current line and next non-blank line.
-               // This is the level to which we set all the intervening blank or comment lines.
-               const int skip_level = Platform::Maximum(indentCurrentLevel,
-                                      indentNext & SC_FOLDLEVELNUMBERMASK);
+               const int levelAfterComments = indentNext & SC_FOLDLEVELNUMBERMASK;
+               const int levelBeforeComments = Platform::Maximum(indentCurrentLevel,levelAfterComments);
 
                // Now set all the indent levels on the lines we skipped
-               int skipLine = lineCurrent + 1;
-               int skipIndentNext = saveIndentNext;
-               while (skipLine < lineNext) {
-                       int skipLineLevel = skip_level;
-                       if (skipIndentNext & SC_FOLDLEVELWHITEFLAG)
-                               skipLineLevel = SC_FOLDLEVELWHITEFLAG | skipLineLevel;
-                       styler.SetLevel(skipLine, skipLineLevel);
-                       skipLine++;
-                       skipIndentNext = styler.IndentAmount(skipLine, &spaceFlags, NULL);
+               // Do this from end to start.  Once we encounter one line
+               // which is indented more than the line after the end of
+               // the comment-block, use the level of the block before
+
+               int skipLine = lineNext;
+               int skipLevel = levelAfterComments;
+
+               while (--skipLine > lineCurrent) {
+                       int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, NULL);
+
+                       if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
+                               skipLevel = levelBeforeComments;
+
+                       int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
+
+                       styler.SetLevel(skipLine, skipLevel | whiteFlag);
                }
 
                // Set fold header on non-quote/non-comment line
@@ -408,4 +451,11 @@ static void FoldPyDoc(unsigned int startPos, int length, int /*initStyle - unuse
        //styler.SetLevel(lineCurrent, indentCurrent);
 }
 
-LexerModule lmPython(SCLEX_PYTHON, ColourisePyDoc, "python", FoldPyDoc);
+static const char * const pythonWordListDesc[] = {
+       "Keywords",
+       "Highlighted identifiers",
+       0
+};
+
+LexerModule lmPython(SCLEX_PYTHON, ColourisePyDoc, "python", FoldPyDoc,
+                                        pythonWordListDesc);