1
// Scintilla source code edit control
2
/** @file LexPython.cxx
5
// Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
6
// The License.txt file describes the conditions under which this software may be distributed.
18
#include "StyleContext.h"
20
#include "Scintilla.h"
23
enum kwType { kwOther, kwClass, kwDef, kwImport };
25
static bool IsPyComment(Accessor &styler, int pos, int len) {
26
return len > 0 && styler[pos] == '#';
29
static bool IsPyStringStart(int ch, int chNext, int chNext2) {
30
if (ch == '\'' || ch == '"')
32
if (ch == 'u' || ch == 'U') {
33
if (chNext == '"' || chNext == '\'')
35
if ((chNext == 'r' || chNext == 'R') && (chNext2 == '"' || chNext2 == '\''))
38
if ((ch == 'r' || ch == 'R') && (chNext == '"' || chNext == '\''))
44
/* Return the state to use for the string starting at i; *nextIndex will be set to the first index following the quote(s) */
45
static int GetPyStringState(Accessor &styler, int i, unsigned int *nextIndex) {
46
char ch = styler.SafeGetCharAt(i);
47
char chNext = styler.SafeGetCharAt(i + 1);
49
// Advance beyond r, u, or ur prefix, but bail if there are any unexpected chars
50
if (ch == 'r' || ch == 'R') {
52
ch = styler.SafeGetCharAt(i);
53
chNext = styler.SafeGetCharAt(i + 1);
54
} else if (ch == 'u' || ch == 'U') {
55
if (chNext == 'r' || chNext == 'R')
59
ch = styler.SafeGetCharAt(i);
60
chNext = styler.SafeGetCharAt(i + 1);
63
if (ch != '"' && ch != '\'') {
68
if (ch == chNext && ch == styler.SafeGetCharAt(i + 2)) {
72
return SCE_P_TRIPLEDOUBLE;
81
return SCE_P_CHARACTER;
85
static inline bool IsAWordChar(int ch) {
86
return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
89
static inline bool IsAWordStart(int ch) {
90
return (ch < 0x80) && (isalnum(ch) || ch == '_');
93
static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
94
WordList *keywordlists[], Accessor &styler) {
96
int endPos = startPos + length;
98
// Backtrack to previous line in case need to fix its tab whinging
99
int lineCurrent = styler.GetLine(startPos);
101
if (lineCurrent > 0) {
103
startPos = styler.LineStart(lineCurrent);
105
initStyle = SCE_P_DEFAULT;
107
initStyle = styler.StyleAt(startPos - 1);
111
WordList &keywords = *keywordlists[0];
112
WordList &keywords2 = *keywordlists[1];
114
const int whingeLevel = styler.GetPropertyInt("tab.timmy.whinge.level");
116
initStyle = initStyle & 31;
117
if (initStyle == SCE_P_STRINGEOL) {
118
initStyle = SCE_P_DEFAULT;
121
kwType kwLast = kwOther;
123
styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
124
bool hexadecimal = false;
126
// Python uses a different mask because bad indentation is marked by oring with 32
127
StyleContext sc(startPos, endPos - startPos, initStyle, styler, 0x7f);
129
for (; sc.More(); sc.Forward()) {
131
if (sc.atLineStart) {
132
const char chBad = static_cast<char>(64);
133
const char chGood = static_cast<char>(0);
134
char chFlags = chGood;
135
if (whingeLevel == 1) {
136
chFlags = (spaceFlags & wsInconsistent) ? chBad : chGood;
137
} else if (whingeLevel == 2) {
138
chFlags = (spaceFlags & wsSpaceTab) ? chBad : chGood;
139
} else if (whingeLevel == 3) {
140
chFlags = (spaceFlags & wsSpace) ? chBad : chGood;
141
} else if (whingeLevel == 4) {
142
chFlags = (spaceFlags & wsTab) ? chBad : chGood;
144
sc.SetState(sc.state);
145
styler.SetFlags(chFlags, static_cast<char>(sc.state));
149
if ((sc.state == SCE_P_DEFAULT) ||
150
(sc.state == SCE_P_TRIPLE) ||
151
(sc.state == SCE_P_TRIPLEDOUBLE)) {
152
// Perform colourisation of white space and triple quoted strings at end of each line to allow
153
// tab marking to work inside white space and triple quoted strings
154
sc.SetState(sc.state);
157
styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
158
if ((sc.state == SCE_P_STRING) || (sc.state == SCE_P_CHARACTER)) {
159
sc.ChangeState(SCE_P_STRINGEOL);
160
sc.ForwardSetState(SCE_P_DEFAULT);
166
bool needEOLCheck = false;
168
// Check for a state end
169
if (sc.state == SCE_P_OPERATOR) {
171
sc.SetState(SCE_P_DEFAULT);
172
} else if (sc.state == SCE_P_NUMBER) {
173
if (!IsAWordChar(sc.ch) &&
174
!(!hexadecimal && ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')))) {
175
sc.SetState(SCE_P_DEFAULT);
177
} else if (sc.state == SCE_P_IDENTIFIER) {
178
if ((sc.ch == '.') || (!IsAWordChar(sc.ch))) {
180
sc.GetCurrent(s, sizeof(s));
181
int style = SCE_P_IDENTIFIER;
182
if ((kwLast == kwImport) && (strcmp(s, "as") == 0)) {
184
} else if (keywords.InList(s)) {
186
} else if (kwLast == kwClass) {
187
style = SCE_P_CLASSNAME;
188
} else if (kwLast == kwDef) {
189
style = SCE_P_DEFNAME;
190
} else if (keywords2.InList(s)) {
193
sc.ChangeState(style);
194
sc.SetState(SCE_P_DEFAULT);
195
if (style == SCE_P_WORD) {
196
if (0 == strcmp(s, "class"))
198
else if (0 == strcmp(s, "def"))
200
else if (0 == strcmp(s, "import"))
208
} else if ((sc.state == SCE_P_COMMENTLINE) || (sc.state == SCE_P_COMMENTBLOCK)) {
209
if (sc.ch == '\r' || sc.ch == '\n') {
210
sc.SetState(SCE_P_DEFAULT);
212
} else if (sc.state == SCE_P_DECORATOR) {
213
if (sc.ch == '\r' || sc.ch == '\n') {
214
sc.SetState(SCE_P_DEFAULT);
216
else if (sc.ch == '#') {
217
sc.SetState(sc.chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE);
219
} else if ((sc.state == SCE_P_STRING) || (sc.state == SCE_P_CHARACTER)) {
221
if ((sc.chNext == '\r') && (sc.GetRelative(2) == '\n')) {
225
} else if ((sc.state == SCE_P_STRING) && (sc.ch == '\"')) {
226
sc.ForwardSetState(SCE_P_DEFAULT);
228
} else if ((sc.state == SCE_P_CHARACTER) && (sc.ch == '\'')) {
229
sc.ForwardSetState(SCE_P_DEFAULT);
232
} else if (sc.state == SCE_P_TRIPLE) {
235
} else if (sc.Match("\'\'\'")) {
238
sc.ForwardSetState(SCE_P_DEFAULT);
241
} else if (sc.state == SCE_P_TRIPLEDOUBLE) {
244
} else if (sc.Match("\"\"\"")) {
247
sc.ForwardSetState(SCE_P_DEFAULT);
252
// State exit code may have moved on to end of line
253
if (needEOLCheck && sc.atLineEnd) {
255
styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
260
// Check for a new state starting character
261
if (sc.state == SCE_P_DEFAULT) {
262
if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
263
if (sc.ch == '0' && (sc.chNext == 'x' || sc.chNext == 'X')) {
268
sc.SetState(SCE_P_NUMBER);
269
} else if (isascii(sc.ch) && isoperator(static_cast<char>(sc.ch)) || sc.ch == '`') {
270
sc.SetState(SCE_P_OPERATOR);
271
} else if (sc.ch == '#') {
272
sc.SetState(sc.chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE);
273
} else if (sc.ch == '@') {
274
sc.SetState(SCE_P_DECORATOR);
275
} else if (IsPyStringStart(sc.ch, sc.chNext, sc.GetRelative(2))) {
276
unsigned int nextIndex = 0;
277
sc.SetState(GetPyStringState(styler, sc.currentPos, &nextIndex));
278
while (nextIndex > (sc.currentPos + 1) && sc.More()) {
281
} else if (IsAWordStart(sc.ch)) {
282
sc.SetState(SCE_P_IDENTIFIER);
289
static bool IsCommentLine(int line, Accessor &styler) {
290
int pos = styler.LineStart(line);
291
int eol_pos = styler.LineStart(line + 1) - 1;
292
for (int i = pos; i < eol_pos; i++) {
296
else if (ch != ' ' && ch != '\t')
302
static bool IsQuoteLine(int line, Accessor &styler) {
303
int style = styler.StyleAt(styler.LineStart(line)) & 31;
304
return ((style == SCE_P_TRIPLE) || (style == SCE_P_TRIPLEDOUBLE));
308
static void FoldPyDoc(unsigned int startPos, int length, int /*initStyle - unused*/,
309
WordList *[], Accessor &styler) {
310
const int maxPos = startPos + length;
311
const int maxLines = styler.GetLine(maxPos - 1); // Requested last line
312
const int docLines = styler.GetLine(styler.Length() - 1); // Available last line
313
const bool foldComment = styler.GetPropertyInt("fold.comment.python") != 0;
314
const bool foldQuotes = styler.GetPropertyInt("fold.quotes.python") != 0;
316
// Backtrack to previous non-blank line so we can determine indent level
317
// for any white space lines (needed esp. within triple quoted strings)
318
// and so we can fix any preceding fold level (which is why we go back
319
// at least one line in all cases)
321
int lineCurrent = styler.GetLine(startPos);
322
int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
323
while (lineCurrent > 0) {
325
indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
326
if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG) &&
327
(!IsCommentLine(lineCurrent, styler)) &&
328
(!IsQuoteLine(lineCurrent, styler)))
331
int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
333
// Set up initial loop state
334
startPos = styler.LineStart(lineCurrent);
335
int prev_state = SCE_P_DEFAULT & 31;
336
if (lineCurrent >= 1)
337
prev_state = styler.StyleAt(startPos - 1) & 31;
338
int prevQuote = foldQuotes && ((prev_state == SCE_P_TRIPLE) || (prev_state == SCE_P_TRIPLEDOUBLE));
340
if (lineCurrent >= 1)
341
prevComment = foldComment && IsCommentLine(lineCurrent - 1, styler);
343
// Process all characters to end of requested range or end of any triple quote
344
// or comment that hangs over the end of the range. Cap processing in all cases
345
// to end of document (in case of unclosed quote or comment at end).
346
while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) || prevQuote || prevComment)) {
349
int lev = indentCurrent;
350
int lineNext = lineCurrent + 1;
351
int indentNext = indentCurrent;
353
if (lineNext <= docLines) {
354
// Information about next line is only available if not at end of document
355
indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
356
int style = styler.StyleAt(styler.LineStart(lineNext)) & 31;
357
quote = foldQuotes && ((style == SCE_P_TRIPLE) || (style == SCE_P_TRIPLEDOUBLE));
359
const int quote_start = (quote && !prevQuote);
360
const int quote_continue = (quote && prevQuote);
361
const int comment = foldComment && IsCommentLine(lineCurrent, styler);
362
const int comment_start = (comment && !prevComment && (lineNext <= docLines) &&
363
IsCommentLine(lineNext, styler) && (lev > SC_FOLDLEVELBASE));
364
const int comment_continue = (comment && prevComment);
365
if ((!quote || !prevQuote) && !comment)
366
indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
368
indentNext = indentCurrentLevel;
369
if (indentNext & SC_FOLDLEVELWHITEFLAG)
370
indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
373
// Place fold point at start of triple quoted string
374
lev |= SC_FOLDLEVELHEADERFLAG;
375
} else if (quote_continue || prevQuote) {
376
// Add level to rest of lines in the string
378
} else if (comment_start) {
379
// Place fold point at start of a block of comments
380
lev |= SC_FOLDLEVELHEADERFLAG;
381
} else if (comment_continue) {
382
// Add level to rest of lines in the block
386
// Skip past any blank lines for next indent level info; we skip also
387
// comments (all comments, not just those starting in column 0)
388
// which effectively folds them into surrounding code rather
389
// than screwing up folding.
392
(lineNext < docLines) &&
393
((indentNext & SC_FOLDLEVELWHITEFLAG) ||
394
(lineNext <= docLines && IsCommentLine(lineNext, styler)))) {
397
indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
400
const int levelAfterComments = indentNext & SC_FOLDLEVELNUMBERMASK;
401
const int levelBeforeComments = Platform::Maximum(indentCurrentLevel,levelAfterComments);
403
// Now set all the indent levels on the lines we skipped
404
// Do this from end to start. Once we encounter one line
405
// which is indented more than the line after the end of
406
// the comment-block, use the level of the block before
408
int skipLine = lineNext;
409
int skipLevel = levelAfterComments;
411
while (--skipLine > lineCurrent) {
412
int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, NULL);
414
if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
415
skipLevel = levelBeforeComments;
417
int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
419
styler.SetLevel(skipLine, skipLevel | whiteFlag);
422
// Set fold header on non-quote/non-comment line
423
if (!quote && !comment && !(indentCurrent & SC_FOLDLEVELWHITEFLAG) ) {
424
if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
425
lev |= SC_FOLDLEVELHEADERFLAG;
428
// Keep track of triple quote and block comment state of previous line
430
prevComment = comment_start || comment_continue;
432
// Set fold level for this line and move to next line
433
styler.SetLevel(lineCurrent, lev);
434
indentCurrent = indentNext;
435
lineCurrent = lineNext;
438
// NOTE: Cannot set level of last line here because indentCurrent doesn't have
439
// header flag set; the loop above is crafted to take care of this case!
440
//styler.SetLevel(lineCurrent, indentCurrent);
443
static const char * const pythonWordListDesc[] = {
445
"Highlighted identifiers",
449
LexerModule lmPython(SCLEX_PYTHON, ColourisePyDoc, "python", FoldPyDoc,