Debian Codegraph @

scintilla/lexilla/lexers/LexGDScript.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexGDScript.cxx
3 ** Lexer for GDScript.
4 **/
5 // Copyright 1998-2002 by Neil Hodgson <[email protected]>
6 // Heavily modified later for GDScript
7 // The License.txt file describes the conditions under which this software may be distributed.
8
9 #include <cstdlib>
10 #include <cassert>
11 #include <cstring>
12
13 #include <string>
14 #include <string_view>
15 #include <vector>
16 #include <map>
17 #include <algorithm>
18 #include <functional>
19
20 #include "ILexer.h"
21 #include "Scintilla.h"
22 #include "SciLexer.h"
23
24 #include "StringCopy.h"
25 #include "WordList.h"
26 #include "LexAccessor.h"
27 #include "Accessor.h"
28 #include "StyleContext.h"
29 #include "CharacterSet.h"
30 #include "CharacterCategory.h"
31 #include "LexerModule.h"
32 #include "OptionSet.h"
33 #include "SubStyles.h"
34 #include "DefaultLexer.h"
35
36 using namespace Scintilla;
37 using namespace Lexilla;
38
39 namespace {
40
41 enum kwType { kwOther, kwClass, kwDef, kwExtends};
42
43 constexpr int indicatorWhitespace = 1;
44
45 bool IsGDStringStart(int ch) {
46 return (ch == '\'' || ch == '"');
47 }
48
49 bool IsGDComment(Accessor &styler, Sci_Position pos, Sci_Position len) {
50 return len > 0 && styler[pos] == '#';
51 }
52
53 constexpr bool IsGDSingleQuoteStringState(int st) noexcept {
54 return ((st == SCE_GD_CHARACTER) || (st == SCE_GD_STRING));
55 }
56
57 constexpr bool IsGDTripleQuoteStringState(int st) noexcept {
58 return ((st == SCE_GD_TRIPLE) || (st == SCE_GD_TRIPLEDOUBLE));
59 }
60
61 char GetGDStringQuoteChar(int st) noexcept {
62 if ((st == SCE_GD_CHARACTER) || (st == SCE_GD_TRIPLE))
63 return '\'';
64 if ((st == SCE_GD_STRING) || (st == SCE_GD_TRIPLEDOUBLE))
65 return '"';
66
67 return '\0';
68 }
69
70 /* Return the state to use for the string starting at i; *nextIndex will be set to the first index following the quote(s) */
71 int GetGDStringState(Accessor &styler, Sci_Position i, Sci_PositionU *nextIndex) {
72 char ch = styler.SafeGetCharAt(i);
73 char chNext = styler.SafeGetCharAt(i + 1);
74
75 if (ch != '"' && ch != '\'') {
76 *nextIndex = i + 1;
77 return SCE_GD_DEFAULT;
78 }
79
80 if (ch == chNext && ch == styler.SafeGetCharAt(i + 2)) {
81 *nextIndex = i + 3;
82
83 if (ch == '"')
84 return SCE_GD_TRIPLEDOUBLE;
85 else
86 return SCE_GD_TRIPLE;
87 } else {
88 *nextIndex = i + 1;
89
90 if (ch == '"')
91 return SCE_GD_STRING;
92 else
93 return SCE_GD_CHARACTER;
94 }
95 }
96
97 int GetGDStringState(int ch) {
98 if (ch != '"' && ch != '\'')
99 return SCE_GD_DEFAULT;
100
101 if (ch == '"')
102 return SCE_GD_STRING;
103 else
104 return SCE_GD_CHARACTER;
105 }
106
107 inline bool IsAWordChar(int ch, bool unicodeIdentifiers) {
108 if (IsASCII(ch))
109 return (IsAlphaNumeric(ch) || ch == '.' || ch == '_');
110
111 if (!unicodeIdentifiers)
112 return false;
113
114 return IsXidContinue(ch);
115 }
116
117 inline bool IsANodePathChar(int ch, bool unicodeIdentifiers) {
118 if (IsASCII(ch))
119 return (IsAlphaNumeric(ch) || ch == '_' || ch == '/' || ch =='%');
120
121 if (!unicodeIdentifiers)
122 return false;
123
124 return IsXidContinue(ch);
125 }
126
127 inline bool IsAWordStart(int ch, bool unicodeIdentifiers) {
128 if (IsASCII(ch))
129 return (IsUpperOrLowerCase(ch) || ch == '_');
130
131 if (!unicodeIdentifiers)
132 return false;
133
134 return IsXidStart(ch);
135 }
136
137 bool IsFirstNonWhitespace(Sci_Position pos, Accessor &styler) {
138 const Sci_Position line = styler.GetLine(pos);
139 const Sci_Position start_pos = styler.LineStart(line);
140 for (Sci_Position i = start_pos; i < pos; i++) {
141 const char ch = styler[i];
142 if (!(ch == ' ' || ch == '\t'))
143 return false;
144 }
145 return true;
146 }
147
148 // Options used for LexerGDScript
149 struct OptionsGDScript {
150 int whingeLevel;
151 bool base2or8Literals;
152 bool stringsOverNewline;
153 bool keywords2NoSubIdentifiers;
154 bool fold;
155 bool foldQuotes;
156 bool foldCompact;
157 bool unicodeIdentifiers;
158
159 OptionsGDScript() noexcept {
160 whingeLevel = 0;
161 base2or8Literals = true;
162 stringsOverNewline = false;
163 keywords2NoSubIdentifiers = false;
164 fold = false;
165 foldQuotes = false;
166 foldCompact = false;
167 unicodeIdentifiers = true;
168 }
169 };
170
171 const char *const gdscriptWordListDesc[] = {
172 "Keywords",
173 "Highlighted identifiers",
174 nullptr
175 };
176
177 struct OptionSetGDScript : public OptionSet<OptionsGDScript> {
178 OptionSetGDScript() {
179 DefineProperty("lexer.gdscript.whinge.level", &OptionsGDScript::whingeLevel,
180 "For GDScript code, checks whether indenting is consistent. "
181 "The default, 0 turns off indentation checking, "
182 "1 checks whether each line is potentially inconsistent with the previous line, "
183 "2 checks whether any space characters occur before a tab character in the indentation, "
184 "3 checks whether any spaces are in the indentation, and "
185 "4 checks for any tab characters in the indentation. "
186 "1 is a good level to use.");
187
188 DefineProperty("lexer.gdscript.literals.binary", &OptionsGDScript::base2or8Literals,
189 "Set to 0 to not recognise binary and octal literals: 0b1011 0o712.");
190
191 DefineProperty("lexer.gdscript.strings.over.newline", &OptionsGDScript::stringsOverNewline,
192 "Set to 1 to allow strings to span newline characters.");
193
194 DefineProperty("lexer.gdscript.keywords2.no.sub.identifiers", &OptionsGDScript::keywords2NoSubIdentifiers,
195 "When enabled, it will not style keywords2 items that are used as a sub-identifier. "
196 "Example: when set, will not highlight \"foo.open\" when \"open\" is a keywords2 item.");
197
198 DefineProperty("fold", &OptionsGDScript::fold);
199
200 DefineProperty("fold.gdscript.quotes", &OptionsGDScript::foldQuotes,
201 "This option enables folding multi-line quoted strings when using the GDScript lexer.");
202
203 DefineProperty("fold.compact", &OptionsGDScript::foldCompact);
204
205 DefineProperty("lexer.gdscript.unicode.identifiers", &OptionsGDScript::unicodeIdentifiers,
206 "Set to 0 to not recognise Unicode identifiers.");
207
208 DefineWordListSets(gdscriptWordListDesc);
209 }
210 };
211
212 const char styleSubable[] = { SCE_GD_IDENTIFIER, 0 };
213
214 LexicalClass lexicalClasses[] = {
215 // Lexer GDScript SCLEX_GDSCRIPT SCE_GD_:
216 0, "SCE_GD_DEFAULT", "default", "White space",
217 1, "SCE_GD_COMMENTLINE", "comment line", "Comment",
218 2, "SCE_GD_NUMBER", "literal numeric", "Number",
219 3, "SCE_GD_STRING", "literal string", "String",
220 4, "SCE_GD_CHARACTER", "literal string", "Single quoted string",
221 5, "SCE_GD_WORD", "keyword", "Keyword",
222 6, "SCE_GD_TRIPLE", "literal string", "Triple quotes",
223 7, "SCE_GD_TRIPLEDOUBLE", "literal string", "Triple double quotes",
224 8, "SCE_GD_CLASSNAME", "identifier", "Class name definition",
225 9, "SCE_GD_FUNCNAME", "identifier", "Function or method name definition",
226 10, "SCE_GD_OPERATOR", "operator", "Operators",
227 11, "SCE_GD_IDENTIFIER", "identifier", "Identifiers",
228 12, "SCE_GD_COMMENTBLOCK", "comment", "Comment-blocks",
229 13, "SCE_GD_STRINGEOL", "error literal string", "End of line where string is not closed",
230 14, "SCE_GD_WORD2", "identifier", "Highlighted identifiers",
231 15, "SCE_GD_ANNOTATION", "annotation", "Annotations",
232 16, "SCE_GD_NODEPATH", "path", "Node path",
233 };
234
235 }
236
237 class LexerGDScript : public DefaultLexer {
238 WordList keywords;
239 WordList keywords2;
240 OptionsGDScript options;
241 OptionSetGDScript osGDScript;
242 enum { ssIdentifier };
243 SubStyles subStyles{styleSubable};
244 public:
245 explicit LexerGDScript() :
246 DefaultLexer("gdscript", SCLEX_GDSCRIPT, lexicalClasses, ELEMENTS(lexicalClasses)) {
247 }
248 ~LexerGDScript() override {
249 }
250 void SCI_METHOD Release() override {
251 delete this;
252 }
253 int SCI_METHOD Version() const override {
254 return lvRelease5;
255 }
256 const char *SCI_METHOD PropertyNames() override {
257 return osGDScript.PropertyNames();
258 }
259 int SCI_METHOD PropertyType(const char *name) override {
260 return osGDScript.PropertyType(name);
261 }
262 const char *SCI_METHOD DescribeProperty(const char *name) override {
263 return osGDScript.DescribeProperty(name);
264 }
265 Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
266 const char * SCI_METHOD PropertyGet(const char *key) override {
267 return osGDScript.PropertyGet(key);
268 }
269 const char *SCI_METHOD DescribeWordListSets() override {
270 return osGDScript.DescribeWordListSets();
271 }
272 Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
273 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
274 void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
275
276 void *SCI_METHOD PrivateCall(int, void *) override {
277 return nullptr;
278 }
279
280 int SCI_METHOD LineEndTypesSupported() override {
281 return SC_LINE_END_TYPE_UNICODE;
282 }
283
284 int SCI_METHOD AllocateSubStyles(int styleBase, int numberStyles) override {
285 return subStyles.Allocate(styleBase, numberStyles);
286 }
287 int SCI_METHOD SubStylesStart(int styleBase) override {
288 return subStyles.Start(styleBase);
289 }
290 int SCI_METHOD SubStylesLength(int styleBase) override {
291 return subStyles.Length(styleBase);
292 }
293 int SCI_METHOD StyleFromSubStyle(int subStyle) override {
294 const int styleBase = subStyles.BaseStyle(subStyle);
295 return styleBase;
296 }
297 int SCI_METHOD PrimaryStyleFromStyle(int style) override {
298 return style;
299 }
300 void SCI_METHOD FreeSubStyles() override {
301 subStyles.Free();
302 }
303 void SCI_METHOD SetIdentifiers(int style, const char *identifiers) override {
304 subStyles.SetIdentifiers(style, identifiers);
305 }
306 int SCI_METHOD DistanceToSecondaryStyles() override {
307 return 0;
308 }
309 const char *SCI_METHOD GetSubStyleBases() override {
310 return styleSubable;
311 }
312
313 static ILexer5 *LexerFactoryGDScript() {
314 return new LexerGDScript();
315 }
316
317 private:
318 void ProcessLineEnd(StyleContext &sc, bool &inContinuedString);
319 };
320
321 Sci_Position SCI_METHOD LexerGDScript::PropertySet(const char *key, const char *val) {
322 if (osGDScript.PropertySet(&options, key, val)) {
323 return 0;
324 }
325 return -1;
326 }
327
328 Sci_Position SCI_METHOD LexerGDScript::WordListSet(int n, const char *wl) {
329 WordList *wordListN = nullptr;
330 switch (n) {
331 case 0:
332 wordListN = &keywords;
333 break;
334 case 1:
335 wordListN = &keywords2;
336 break;
337 default:
338 break;
339 }
340 Sci_Position firstModification = -1;
341 if (wordListN) {
342 WordList wlNew;
343 wlNew.Set(wl);
344 if (*wordListN != wlNew) {
345 wordListN->Set(wl);
346 firstModification = 0;
347 }
348 }
349 return firstModification;
350 }
351
352 void LexerGDScript::ProcessLineEnd(StyleContext &sc, bool &inContinuedString) {
353 if ((sc.state == SCE_GD_DEFAULT)
354 || IsGDTripleQuoteStringState(sc.state)) {
355 // Perform colourisation of white space and triple quoted strings at end of each line to allow
356 // tab marking to work inside white space and triple quoted strings
357 sc.SetState(sc.state);
358 }
359
360 if (IsGDSingleQuoteStringState(sc.state)) {
361 if (inContinuedString || options.stringsOverNewline) {
362 inContinuedString = false;
363 } else {
364 sc.ChangeState(SCE_GD_STRINGEOL);
365 sc.ForwardSetState(SCE_GD_DEFAULT);
366 }
367 }
368 }
369
370 void SCI_METHOD LexerGDScript::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
371 Accessor styler(pAccess, nullptr);
372
373 const Sci_Position endPos = startPos + length;
374
375 // Backtrack to previous line in case need to fix its tab whinging
376 Sci_Position lineCurrent = styler.GetLine(startPos);
377 if (startPos > 0) {
378 if (lineCurrent > 0) {
379 lineCurrent--;
380 // Look for backslash-continued lines
381 while (lineCurrent > 0) {
382 const Sci_Position eolPos = styler.LineStart(lineCurrent) - 1;
383 const int eolStyle = styler.StyleAt(eolPos);
384 if (eolStyle == SCE_GD_STRING || eolStyle == SCE_GD_CHARACTER
385 || eolStyle == SCE_GD_STRINGEOL) {
386 lineCurrent -= 1;
387 } else {
388 break;
389 }
390 }
391 startPos = styler.LineStart(lineCurrent);
392 }
393 initStyle = startPos == 0 ? SCE_GD_DEFAULT : styler.StyleAt(startPos - 1);
394 }
395
396 initStyle = initStyle & 31;
397 if (initStyle == SCE_GD_STRINGEOL) {
398 initStyle = SCE_GD_DEFAULT;
399 }
400
401 kwType kwLast = kwOther;
402 int spaceFlags = 0;
403 styler.IndentAmount(lineCurrent, &spaceFlags, IsGDComment);
404 bool base_n_number = false;
405
406 const WordClassifier &classifierIdentifiers = subStyles.Classifier(SCE_GD_IDENTIFIER);
407
408 StyleContext sc(startPos, endPos - startPos, initStyle, styler);
409
410 bool indentGood = true;
411 Sci_Position startIndicator = sc.currentPos;
412 bool inContinuedString = false;
413 bool percentIsNodePath = false;
414 int nodePathStringState = SCE_GD_DEFAULT;
415
416 for (; sc.More(); sc.Forward()) {
417
418 if (sc.atLineStart) {
419 styler.IndentAmount(lineCurrent, &spaceFlags, IsGDComment);
420 indentGood = true;
421 if (options.whingeLevel == 1) {
422 indentGood = (spaceFlags & wsInconsistent) == 0;
423 } else if (options.whingeLevel == 2) {
424 indentGood = (spaceFlags & wsSpaceTab) == 0;
425 } else if (options.whingeLevel == 3) {
426 indentGood = (spaceFlags & wsSpace) == 0;
427 } else if (options.whingeLevel == 4) {
428 indentGood = (spaceFlags & wsTab) == 0;
429 }
430 if (!indentGood) {
431 styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 0);
432 startIndicator = sc.currentPos;
433 }
434 }
435
436 if (sc.atLineEnd) {
437 percentIsNodePath = false;
438 ProcessLineEnd(sc, inContinuedString);
439 lineCurrent++;
440 if (!sc.More())
441 break;
442 }
443
444 bool needEOLCheck = false;
445
446 if (sc.state == SCE_GD_OPERATOR) {
447 kwLast = kwOther;
448 sc.SetState(SCE_GD_DEFAULT);
449 } else if (sc.state == SCE_GD_NUMBER) {
450 if (!IsAWordChar(sc.ch, false) &&
451 !(!base_n_number && ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')))) {
452 sc.SetState(SCE_GD_DEFAULT);
453 }
454 } else if (sc.state == SCE_GD_IDENTIFIER) {
455 if ((sc.ch == '.') || (!IsAWordChar(sc.ch, options.unicodeIdentifiers))) {
456 char s[100];
457 sc.GetCurrent(s, sizeof(s));
458 int style = SCE_GD_IDENTIFIER;
459 if (keywords.InList(s)) {
460 style = SCE_GD_WORD;
461 } else if (kwLast == kwClass) {
462 style = SCE_GD_CLASSNAME;
463 } else if (kwLast == kwDef) {
464 style = SCE_GD_FUNCNAME;
465 } else if (keywords2.InList(s)) {
466 if (options.keywords2NoSubIdentifiers) {
467 // We don't want to highlight keywords2
468 // that are used as a sub-identifier,
469 // i.e. not open in "foo.open".
470 const Sci_Position pos = styler.GetStartSegment() - 1;
471 if (pos < 0 || (styler.SafeGetCharAt(pos, '\0') != '.'))
472 style = SCE_GD_WORD2;
473 } else {
474 style = SCE_GD_WORD2;
475 }
476 } else {
477 const int subStyle = classifierIdentifiers.ValueFor(s);
478 if (subStyle >= 0) {
479 style = subStyle;
480 }
481 }
482 sc.ChangeState(style);
483 sc.SetState(SCE_GD_DEFAULT);
484 if (style == SCE_GD_WORD) {
485 if (0 == strcmp(s, "class"))
486 kwLast = kwClass;
487 else if (0 == strcmp(s, "func"))
488 kwLast = kwDef;
489 else if (0 == strcmp(s, "extends"))
490 kwLast = kwExtends;
491 else
492 kwLast = kwOther;
493 } else {
494 kwLast = kwOther;
495 }
496 }
497 } else if ((sc.state == SCE_GD_COMMENTLINE) || (sc.state == SCE_GD_COMMENTBLOCK)) {
498 if (sc.ch == '\r' || sc.ch == '\n') {
499 sc.SetState(SCE_GD_DEFAULT);
500 }
501 } else if (sc.state == SCE_GD_ANNOTATION) {
502 if (!IsAWordStart(sc.ch, options.unicodeIdentifiers)) {
503 sc.SetState(SCE_GD_DEFAULT);
504 }
505 } else if (sc.state == SCE_GD_NODEPATH) {
506 if (nodePathStringState != SCE_GD_DEFAULT) {
507 if (sc.ch == GetGDStringQuoteChar(nodePathStringState) ) {
508 nodePathStringState = SCE_GD_DEFAULT;
509 }
510 } else {
511 if (IsGDStringStart(sc.ch)) {
512 nodePathStringState = GetGDStringState(sc.ch);
513 } else if (!IsANodePathChar(sc.ch, options.unicodeIdentifiers)) {
514 sc.SetState(SCE_GD_DEFAULT);
515 }
516 }
517 } else if (IsGDSingleQuoteStringState(sc.state)) {
518 if (sc.ch == '\\') {
519 if ((sc.chNext == '\r') && (sc.GetRelative(2) == '\n')) {
520 sc.Forward();
521 }
522 if (sc.chNext == '\n' || sc.chNext == '\r') {
523 inContinuedString = true;
524 } else {
525 // Don't roll over the newline.
526 sc.Forward();
527 }
528 } else if (sc.ch == GetGDStringQuoteChar(sc.state)) {
529 sc.ForwardSetState(SCE_GD_DEFAULT);
530 needEOLCheck = true;
531 }
532 } else if (sc.state == SCE_GD_TRIPLE) {
533 if (sc.ch == '\\') {
534 sc.Forward();
535 } else if (sc.Match(R"(''')")) {
536 sc.Forward();
537 sc.Forward();
538 sc.ForwardSetState(SCE_GD_DEFAULT);
539 needEOLCheck = true;
540 }
541 } else if (sc.state == SCE_GD_TRIPLEDOUBLE) {
542 if (sc.ch == '\\') {
543 sc.Forward();
544 } else if (sc.Match(R"(""")")) {
545 sc.Forward();
546 sc.Forward();
547 sc.ForwardSetState(SCE_GD_DEFAULT);
548 needEOLCheck = true;
549 }
550 }
551
552 if (!indentGood && !IsASpaceOrTab(sc.ch)) {
553 styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 1);
554 startIndicator = sc.currentPos;
555 indentGood = true;
556 }
557
558 // State exit code may have moved on to end of line
559 if (needEOLCheck && sc.atLineEnd) {
560 ProcessLineEnd(sc, inContinuedString);
561 lineCurrent++;
562 styler.IndentAmount(lineCurrent, &spaceFlags, IsGDComment);
563 if (!sc.More())
564 break;
565 }
566
567 // Check for a new state starting character
568 if (sc.state == SCE_GD_DEFAULT) {
569 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
570 if (sc.ch == '0' && (sc.chNext == 'x' || sc.chNext == 'X')) {
571 base_n_number = true;
572 sc.SetState(SCE_GD_NUMBER);
573 } else if (sc.ch == '0' &&
574 (sc.chNext == 'o' || sc.chNext == 'O' || sc.chNext == 'b' || sc.chNext == 'B')) {
575 if (options.base2or8Literals) {
576 base_n_number = true;
577 sc.SetState(SCE_GD_NUMBER);
578 } else {
579 sc.SetState(SCE_GD_NUMBER);
580 sc.ForwardSetState(SCE_GD_IDENTIFIER);
581 }
582 } else {
583 base_n_number = false;
584 sc.SetState(SCE_GD_NUMBER);
585 }
586 } else if ((sc.ch == '$') || (sc.ch == '%' && (percentIsNodePath || IsFirstNonWhitespace(sc.currentPos, styler)))) {
587 percentIsNodePath = false;
588 sc.SetState(SCE_GD_NODEPATH);
589 } else if (isoperator(sc.ch) || sc.ch == '`') {
590 percentIsNodePath = !((sc.ch == ')') || (sc.ch == ']') || (sc.ch == '}'));
591 sc.SetState(SCE_GD_OPERATOR);
592 } else if (sc.ch == '#') {
593 sc.SetState(sc.chNext == '#' ? SCE_GD_COMMENTBLOCK : SCE_GD_COMMENTLINE);
594 } else if (sc.ch == '@') {
595 if (IsFirstNonWhitespace(sc.currentPos, styler))
596 sc.SetState(SCE_GD_ANNOTATION);
597 else
598 sc.SetState(SCE_GD_OPERATOR);
599 } else if (IsGDStringStart(sc.ch)) {
600 Sci_PositionU nextIndex = 0;
601 sc.SetState(GetGDStringState(styler, sc.currentPos, &nextIndex));
602 while (nextIndex > (sc.currentPos + 1) && sc.More()) {
603 sc.Forward();
604 }
605 } else if (IsAWordStart(sc.ch, options.unicodeIdentifiers)) {
606 sc.SetState(SCE_GD_IDENTIFIER);
607 }
608 }
609 }
610 styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 0);
611 sc.Complete();
612 }
613
614 static bool IsCommentLine(Sci_Position line, Accessor &styler) {
615 const Sci_Position pos = styler.LineStart(line);
616 const Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
617 for (Sci_Position i = pos; i < eol_pos; i++) {
618 const char ch = styler[i];
619 if (ch == '#')
620 return true;
621 else if (ch != ' ' && ch != '\t')
622 return false;
623 }
624 return false;
625 }
626
627 static bool IsQuoteLine(Sci_Position line, const Accessor &styler) {
628 const int style = styler.StyleAt(styler.LineStart(line)) & 31;
629 return IsGDTripleQuoteStringState(style);
630 }
631
632
633 void SCI_METHOD LexerGDScript::Fold(Sci_PositionU startPos, Sci_Position length, int /*initStyle - unused*/, IDocument *pAccess) {
634 if (!options.fold)
635 return;
636
637 Accessor styler(pAccess, nullptr);
638
639 const Sci_Position maxPos = startPos + length;
640 const Sci_Position maxLines = (maxPos == styler.Length()) ? styler.GetLine(maxPos) : styler.GetLine(maxPos - 1); // Requested last line
641 const Sci_Position docLines = styler.GetLine(styler.Length()); // Available last line
642
643 // Backtrack to previous non-blank line so we can determine indent level
644 // for any white space lines (needed esp. within triple quoted strings)
645 // and so we can fix any preceding fold level (which is why we go back
646 // at least one line in all cases)
647 int spaceFlags = 0;
648 Sci_Position lineCurrent = styler.GetLine(startPos);
649 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, nullptr);
650 while (lineCurrent > 0) {
651 lineCurrent--;
652 indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, nullptr);
653 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG) &&
654 (!IsCommentLine(lineCurrent, styler)) &&
655 (!IsQuoteLine(lineCurrent, styler)))
656 break;
657 }
658 int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
659
660 // Set up initial loop state
661 startPos = styler.LineStart(lineCurrent);
662 int prev_state = SCE_GD_DEFAULT & 31;
663 if (lineCurrent >= 1)
664 prev_state = styler.StyleAt(startPos - 1) & 31;
665 int prevQuote = options.foldQuotes && IsGDTripleQuoteStringState(prev_state);
666
667 // Process all characters to end of requested range or end of any triple quote
668 //that hangs over the end of the range. Cap processing in all cases
669 // to end of document (in case of unclosed quote at end).
670 while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) || prevQuote)) {
671
672 // Gather info
673 int lev = indentCurrent;
674 Sci_Position lineNext = lineCurrent + 1;
675 int indentNext = indentCurrent;
676 int quote = false;
677 if (lineNext <= docLines) {
678 // Information about next line is only available if not at end of document
679 indentNext = styler.IndentAmount(lineNext, &spaceFlags, nullptr);
680 const Sci_Position lookAtPos = (styler.LineStart(lineNext) == styler.Length()) ? styler.Length() - 1 : styler.LineStart(lineNext);
681 const int style = styler.StyleAt(lookAtPos) & 31;
682 quote = options.foldQuotes && IsGDTripleQuoteStringState(style);
683 }
684 const bool quote_start = (quote && !prevQuote);
685 const bool quote_continue = (quote && prevQuote);
686 if (!quote || !prevQuote)
687 indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
688 if (quote)
689 indentNext = indentCurrentLevel;
690 if (indentNext & SC_FOLDLEVELWHITEFLAG)
691 indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
692
693 if (quote_start) {
694 // Place fold point at start of triple quoted string
695 lev |= SC_FOLDLEVELHEADERFLAG;
696 } else if (quote_continue || prevQuote) {
697 // Add level to rest of lines in the string
698 lev = lev + 1;
699 }
700
701 // Skip past any blank lines for next indent level info; we skip also
702 // comments (all comments, not just those starting in column 0)
703 // which effectively folds them into surrounding code rather
704 // than screwing up folding. If comments end file, use the min
705 // comment indent as the level after
706
707 int minCommentLevel = indentCurrentLevel;
708 while (!quote &&
709 (lineNext < docLines) &&
710 ((indentNext & SC_FOLDLEVELWHITEFLAG) || (IsCommentLine(lineNext, styler)))) {
711
712 if (IsCommentLine(lineNext, styler) && indentNext < minCommentLevel) {
713 minCommentLevel = indentNext;
714 }
715
716 lineNext++;
717 indentNext = styler.IndentAmount(lineNext, &spaceFlags, nullptr);
718 }
719
720 const int levelAfterComments = ((lineNext < docLines) ? indentNext & SC_FOLDLEVELNUMBERMASK : minCommentLevel);
721 const int levelBeforeComments = std::max(indentCurrentLevel, levelAfterComments);
722
723 // Now set all the indent levels on the lines we skipped
724 // Do this from end to start. Once we encounter one line
725 // which is indented more than the line after the end of
726 // the comment-block, use the level of the block before
727
728 Sci_Position skipLine = lineNext;
729 int skipLevel = levelAfterComments;
730
731 while (--skipLine > lineCurrent) {
732 const int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, nullptr);
733
734 if (options.foldCompact) {
735 if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
736 skipLevel = levelBeforeComments;
737
738 const int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
739
740 styler.SetLevel(skipLine, skipLevel | whiteFlag);
741 } else {
742 if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments &&
743 !(skipLineIndent & SC_FOLDLEVELWHITEFLAG) &&
744 !IsCommentLine(skipLine, styler))
745 skipLevel = levelBeforeComments;
746
747 styler.SetLevel(skipLine, skipLevel);
748 }
749 }
750
751 // Set fold header on non-quote line
752 if (!quote && !(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
753 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
754 lev |= SC_FOLDLEVELHEADERFLAG;
755 }
756
757 // Keep track of triple quote state of previous line
758 prevQuote = quote;
759
760 // Set fold level for this line and move to next line
761 styler.SetLevel(lineCurrent, options.foldCompact ? lev : lev & ~SC_FOLDLEVELWHITEFLAG);
762 indentCurrent = indentNext;
763 lineCurrent = lineNext;
764 }
765
766 // NOTE: Cannot set level of last line here because indentCurrent doesn't have
767 // header flag set; the loop above is crafted to take care of this case!
768 //styler.SetLevel(lineCurrent, indentCurrent);
769 }
770
771 extern const LexerModule lmGDScript(SCLEX_GDSCRIPT, LexerGDScript::LexerFactoryGDScript, "gdscript",
772 gdscriptWordListDesc);