// 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"
-enum kwType { kwOther, kwClass, kwDef, kwImport };
+#ifdef SCI_NAMESPACE
+using namespace Scintilla;
+#endif
+
+/* kwCDef, kwCTypeName only used for Cython */
+enum kwType { kwOther, kwClass, kwDef, kwImport, kwCDef, kwCTypeName };
+
+static const int indicatorWhitespace = 1;
static bool IsPyComment(Accessor &styler, int pos, int len) {
return len > 0 && styler[pos] == '#';
}
-static bool IsPyStringStart(int ch, int chNext, int chNext2) {
+enum literalsAllowed { litNone=0, litU=1, litB=2};
+
+static bool IsPyStringTypeChar(int ch, literalsAllowed allowed) {
+ return
+ ((allowed & litB) && (ch == 'b' || ch == 'B')) ||
+ ((allowed & litU) && (ch == 'u' || ch == 'U'));
+}
+
+static bool IsPyStringStart(int ch, int chNext, int chNext2, literalsAllowed allowed) {
if (ch == '\'' || ch == '"')
return true;
- if (ch == 'u' || ch == 'U') {
+ if (IsPyStringTypeChar(ch, allowed)) {
if (chNext == '"' || chNext == '\'')
return true;
if ((chNext == 'r' || chNext == 'R') && (chNext2 == '"' || 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, literalsAllowed allowed) {
char ch = styler.SafeGetCharAt(i);
char chNext = styler.SafeGetCharAt(i + 1);
- // Advance beyond r, u, or ur prefix, but bail if there are any unexpected chars
+ // Advance beyond r, u, or ur prefix (or r, b, or br in Python 3.0), but bail if there are any unexpected chars
if (ch == 'r' || ch == 'R') {
i++;
ch = styler.SafeGetCharAt(i);
chNext = styler.SafeGetCharAt(i + 1);
- } else if (ch == 'u' || ch == 'U') {
+ } else if (IsPyStringTypeChar(ch, allowed)) {
if (chNext == 'r' || chNext == 'R')
i += 2;
else
}
static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
- WordList *keywordlists[], Accessor &styler) {
+ WordList *keywordlists[], Accessor &styler) {
int endPos = startPos + length;
int lineCurrent = styler.GetLine(startPos);
if (startPos > 0) {
if (lineCurrent > 0) {
- startPos = styler.LineStart(lineCurrent - 1);
- if (startPos == 0)
- initStyle = SCE_P_DEFAULT;
- else
- initStyle = styler.StyleAt(startPos - 1);
+ lineCurrent--;
+ // Look for backslash-continued lines
+ while (lineCurrent > 0) {
+ int eolPos = styler.LineStart(lineCurrent) - 1;
+ int eolStyle = styler.StyleAt(eolPos);
+ if (eolStyle == SCE_P_STRING
+ || eolStyle == SCE_P_CHARACTER
+ || eolStyle == SCE_P_STRINGEOL) {
+ lineCurrent -= 1;
+ } else {
+ break;
+ }
+ }
+ startPos = styler.LineStart(lineCurrent);
}
+ initStyle = startPos == 0 ? SCE_P_DEFAULT : styler.StyleAt(startPos - 1);
}
WordList &keywords = *keywordlists[0];
-
+ WordList &keywords2 = *keywordlists[1];
+
+ // property tab.timmy.whinge.level
+ // For Python code, checks whether indenting is consistent.
+ // The default, 0 turns off indentation checking,
+ // 1 checks whether each line is potentially inconsistent with the previous line,
+ // 2 checks whether any space characters occur before a tab character in the indentation,
+ // 3 checks whether any spaces are in the indentation, and
+ // 4 checks for any tab characters in the indentation.
+ // 1 is a good level to use.
const int whingeLevel = styler.GetPropertyInt("tab.timmy.whinge.level");
+ // property lexer.python.literals.binary
+ // Set to 0 to not recognise Python 3 binary and octal literals: 0b1011 0o712.
+ bool base2or8Literals = styler.GetPropertyInt("lexer.python.literals.binary", 1) != 0;
+
+ // property lexer.python.strings.u
+ // Set to 0 to not recognise Python Unicode literals u"x" as used before Python 3.
+ literalsAllowed allowedLiterals = (styler.GetPropertyInt("lexer.python.strings.u", 1)) ? litU : litNone;
+
+ // property lexer.python.strings.b
+ // Set to 0 to not recognise Python 3 bytes literals b"x".
+ if (styler.GetPropertyInt("lexer.python.strings.b", 1))
+ allowedLiterals = static_cast<literalsAllowed>(allowedLiterals | litB);
+
+ // property lexer.python.strings.over.newline
+ // Set to 1 to allow strings to span newline characters.
+ bool stringsOverNewline = styler.GetPropertyInt("lexer.python.strings.over.newline") != 0;
+
initStyle = initStyle & 31;
if (initStyle == SCE_P_STRINGEOL) {
initStyle = SCE_P_DEFAULT;
kwType kwLast = kwOther;
int spaceFlags = 0;
styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
- bool hexadecimal = false;
+ bool base_n_number = 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;
+ bool inContinuedString = false;
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) {
(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);
+ if (inContinuedString || stringsOverNewline) {
+ inContinuedString = false;
+ } else {
+ sc.ChangeState(SCE_P_STRINGEOL);
+ sc.ForwardSetState(SCE_P_DEFAULT);
+ }
}
if (!sc.More())
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')))) {
+ !(!base_n_number && ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')))) {
sc.SetState(SCE_P_DEFAULT);
}
} else if (sc.state == SCE_P_IDENTIFIER) {
style = SCE_P_CLASSNAME;
} else if (kwLast == kwDef) {
style = SCE_P_DEFNAME;
+ } else if (kwLast == kwCDef) {
+ int pos = sc.currentPos;
+ unsigned char ch = styler.SafeGetCharAt(pos, '\0');
+ while (ch != '\0') {
+ if (ch == '(') {
+ style = SCE_P_DEFNAME;
+ break;
+ } else if (ch == ':') {
+ style = SCE_P_CLASSNAME;
+ break;
+ } else if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') {
+ pos++;
+ ch = styler.SafeGetCharAt(pos, '\0');
+ } else {
+ break;
+ }
+ }
+ } else if (keywords2.InList(s)) {
+ style = SCE_P_WORD2;
}
sc.ChangeState(style);
sc.SetState(SCE_P_DEFAULT);
kwLast = kwDef;
else if (0 == strcmp(s, "import"))
kwLast = kwImport;
- else
+ else if (0 == strcmp(s, "cdef"))
+ kwLast = kwCDef;
+ else if (0 == strcmp(s, "cimport"))
+ kwLast = kwImport;
+ else if (kwLast != kwCDef)
kwLast = kwOther;
- } else if (style == SCE_P_CLASSNAME) {
- kwLast = kwOther;
- } else if (style == SCE_P_DEFNAME) {
+ } else if (kwLast != kwCDef) {
kwLast = kwOther;
}
}
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')) {
sc.Forward();
}
- sc.Forward();
+ if (sc.chNext == '\n' || sc.chNext == '\r') {
+ inContinuedString = true;
+ } else {
+ // Don't roll over the newline.
+ 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 == '\\') {
sc.Forward();
sc.Forward();
sc.ForwardSetState(SCE_P_DEFAULT);
+ needEOLCheck = true;
}
} else if (sc.state == SCE_P_TRIPLEDOUBLE) {
if (sc.ch == '\\') {
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;
+ }
+
+ // One cdef line, clear kwLast only at end of line
+ if (kwLast == kwCDef && sc.atLineEnd) {
+ kwLast = kwOther;
+ }
+
+ // 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))) {
if (sc.ch == '0' && (sc.chNext == 'x' || sc.chNext == 'X')) {
- hexadecimal = true;
+ base_n_number = true;
+ sc.SetState(SCE_P_NUMBER);
+ } else if (sc.ch == '0' &&
+ (sc.chNext == 'o' || sc.chNext == 'O' || sc.chNext == 'b' || sc.chNext == 'B')) {
+ if (base2or8Literals) {
+ base_n_number = true;
+ sc.SetState(SCE_P_NUMBER);
+ } else {
+ sc.SetState(SCE_P_NUMBER);
+ sc.ForwardSetState(SCE_P_IDENTIFIER);
+ }
} else {
- hexadecimal = false;
+ base_n_number = false;
+ sc.SetState(SCE_P_NUMBER);
}
- sc.SetState(SCE_P_NUMBER);
- } else if (isascii(sc.ch) && isoperator(static_cast<char>(sc.ch)) || sc.ch == '`') {
+ } else if ((isascii(sc.ch) && isoperator(static_cast<char>(sc.ch))) || sc.ch == '`') {
sc.SetState(SCE_P_OPERATOR);
} else if (sc.ch == '#') {
sc.SetState(sc.chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE);
- } else if (IsPyStringStart(sc.ch, sc.chNext, sc.GetRelative(2))) {
- int nextIndex = 0;
- sc.SetState(GetPyStringState(styler, sc.currentPos, &nextIndex));
- while (nextIndex > (sc.currentPos + 1)) {
+ } else if (sc.ch == '@') {
+ sc.SetState(SCE_P_DECORATOR);
+ } else if (IsPyStringStart(sc.ch, sc.chNext, sc.GetRelative(2), allowedLiterals)) {
+ unsigned int nextIndex = 0;
+ sc.SetState(GetPyStringState(styler, sc.currentPos, &nextIndex, allowedLiterals));
+ while (nextIndex > (sc.currentPos + 1) && sc.More()) {
sc.Forward();
}
} else if (IsAWordStart(sc.ch)) {
}
}
}
+ styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 0);
sc.Complete();
}
const int maxPos = startPos + length;
const int maxLines = styler.GetLine(maxPos - 1); // Requested last line
const int docLines = styler.GetLine(styler.Length() - 1); // Available last line
+
+ // property fold.comment.python
+ // This option enables folding multi-line comments when using the Python lexer.
const bool foldComment = styler.GetPropertyInt("fold.comment.python") != 0;
+
+ // property fold.quotes.python
+ // This option enables folding multi-line quoted strings when using the Python lexer.
const bool foldQuotes = styler.GetPropertyInt("fold.quotes.python") != 0;
+ const bool foldCompact = styler.GetPropertyInt("fold.compact") != 0;
+
// Backtrack to previous non-blank line so we can determine indent level
// for any white space lines (needed esp. within triple quoted strings)
// and so we can fix any preceding fold level (which is why we go back
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 (foldCompact) {
+ if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
+ skipLevel = levelBeforeComments;
+
+ int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
+
+ styler.SetLevel(skipLine, skipLevel | whiteFlag);
+ } else {
+ if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments &&
+ !(skipLineIndent & SC_FOLDLEVELWHITEFLAG) &&
+ !IsCommentLine(skipLine, styler))
+ skipLevel = levelBeforeComments;
+
+ styler.SetLevel(skipLine, skipLevel);
+ }
}
// Set fold header on non-quote/non-comment line
}
static const char * const pythonWordListDesc[] = {
- "Python keywords",
+ "Keywords",
+ "Highlighted identifiers",
0
};
-LexerModule lmPython(SCLEX_PYTHON, ColourisePyDoc, "python", FoldPyDoc,
+LexerModule lmPython(SCLEX_PYTHON, ColourisePyDoc, "python", FoldPyDoc,
pythonWordListDesc);
+