static void FoldFortranDoc()

in ext/scintilla/lexers/LexFortran.cxx [462:691]


static void FoldFortranDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
        Accessor &styler, bool isFixFormat) {

	bool foldComment = styler.GetPropertyInt("fold.comment", 1) != 0;
	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
	Sci_PositionU endPos = startPos + length;
	int visibleChars = 0;
	Sci_Position lineCurrent = styler.GetLine(startPos);
	bool isPrevLine;
	if (lineCurrent > 0) {
		lineCurrent--;
		startPos = styler.LineStart(lineCurrent);
		isPrevLine = true;
	} else {
		isPrevLine = false;
	}
	char chNext = styler[startPos];
	int styleNext = styler.StyleAt(startPos);
	int style = initStyle;
	int levelDeltaNext = 0;

	const unsigned int nComL = 3; // defines how many comment lines should be before they are folded
	Sci_Position nComColB[nComL] = {};
	Sci_Position nComColF[nComL] = {};
	Sci_Position nComCur = 0;
	bool comLineB[nComL] = {};
	bool comLineF[nComL] = {};
	bool comLineCur;
	Sci_Position nLineTotal = styler.GetLine(styler.Length()-1) + 1;
	if (foldComment) {
		for (unsigned int i=0; i<nComL; i++) {
			Sci_Position chL = lineCurrent-(i+1);
			if (chL < 0) {
				comLineB[i] = false;
				break;
			}
			GetIfLineComment(styler, isFixFormat, chL, comLineB[i], nComColB[i]);
			if (!comLineB[i]) {
				for (unsigned int j=i+1; j<nComL; j++) {
					comLineB[j] = false;
				}
				break;
			}
		}
		for (unsigned int i=0; i<nComL; i++) {
			Sci_Position chL = lineCurrent+i+1;
			if (chL >= nLineTotal) {
				comLineF[i] = false;
				break;
			}
			GetIfLineComment(styler, isFixFormat, chL, comLineF[i], nComColF[i]);
		}
		GetIfLineComment(styler, isFixFormat, lineCurrent, comLineCur, nComCur);
		CheckBackComLines(styler, isFixFormat, lineCurrent, nComL, nComColB, nComColF, nComCur, 
				comLineB, comLineF, comLineCur);
	}
	int levelCurrent = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;

	/***************************************/
	Sci_Position lastStart = 0;
	char prevWord[32] = "";
	/***************************************/
	for (Sci_PositionU i = startPos; i < endPos; i++) {
		char ch = chNext;
		chNext = styler.SafeGetCharAt(i + 1);
		char chNextNonBlank = chNext;
		bool nextEOL = false;
		if (IsALineEnd(chNextNonBlank)) {
			nextEOL = true;
		}
		Sci_PositionU j=i+1;
		while(IsABlank(chNextNonBlank) && j<endPos) {
			j ++ ;
			chNextNonBlank = styler.SafeGetCharAt(j);
			if (IsALineEnd(chNextNonBlank)) {
				nextEOL = true;
			}
		}
		if (!nextEOL && j == endPos) {
			nextEOL = true;
		}
		int stylePrev = style;
		style = styleNext;
		styleNext = styler.StyleAt(i + 1);
		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
		//
		if (((isFixFormat && stylePrev == SCE_F_CONTINUATION) || stylePrev == SCE_F_DEFAULT
			|| stylePrev == SCE_F_OPERATOR) && (style == SCE_F_WORD || style == SCE_F_LABEL)) {
			// Store last word and label start point.
			lastStart = i;
		}
		/***************************************/
		if (style == SCE_F_WORD) {
			if(iswordchar(ch) && !iswordchar(chNext)) {
				char s[32];
				Sci_PositionU k;
				for(k=0; (k<31 ) && (k<i-lastStart+1 ); k++) {
					s[k] = static_cast<char>(tolower(styler[lastStart+k]));
				}
				s[k] = '\0';
				// Handle the forall and where statement and structure.
				if (strcmp(s, "forall") == 0 || (strcmp(s, "where") == 0 && strcmp(prevWord, "else") != 0)) {
					if (strcmp(prevWord, "end") != 0) {
						j = i + 1;
						char chBrace = '(', chSeek = ')', ch1 = styler.SafeGetCharAt(j);
						// Find the position of the first (
						while (ch1 != chBrace && j<endPos) {
							j++;
							ch1 = styler.SafeGetCharAt(j);
						}
						char styBrace = styler.StyleAt(j);
						int depth = 1;
						char chAtPos;
						char styAtPos;
						while (j<endPos) {
							j++;
							chAtPos = styler.SafeGetCharAt(j);
							styAtPos = styler.StyleAt(j);
							if (styAtPos == styBrace) {
								if (chAtPos == chBrace) depth++;
								if (chAtPos == chSeek) depth--;
								if (depth == 0) break;
							}
						}
						Sci_Position tmpLineCurrent = lineCurrent;
						while (j<endPos) {
							j++;
							chAtPos = styler.SafeGetCharAt(j);
							styAtPos = styler.StyleAt(j);
							if (!IsALineEnd(chAtPos) && (styAtPos == SCE_F_COMMENT || IsABlank(chAtPos))) continue;
							if (isFixFormat) {
								if (!IsALineEnd(chAtPos)) {
									break;
								} else {
									if (tmpLineCurrent < styler.GetLine(styler.Length()-1)) {
										tmpLineCurrent++;
										j = styler.LineStart(tmpLineCurrent);
										if (styler.StyleAt(j+5) == SCE_F_CONTINUATION
											&& !IsABlank(styler.SafeGetCharAt(j+5)) && styler.SafeGetCharAt(j+5) != '0') {
											j += 5;
											continue;
										} else {
											levelDeltaNext++;
											break;
										}
									}
								}
							} else {
								if (chAtPos == '&' && styler.StyleAt(j) == SCE_F_CONTINUATION) {
									j = GetContinuedPos(j+1, styler);
									continue;
								} else if (IsALineEnd(chAtPos)) {
									levelDeltaNext++;
									break;
								} else {
									break;
								}
							}
						}
					}
				} else {
					int wordLevelDelta = classifyFoldPointFortran(s, prevWord, chNextNonBlank);
					levelDeltaNext += wordLevelDelta;
					if (((strcmp(s, "else") == 0) && (nextEOL || chNextNonBlank == '!')) ||
						(strcmp(prevWord, "else") == 0 && strcmp(s, "where") == 0) || strcmp(s, "elsewhere") == 0) {
						if (!isPrevLine) {
							levelCurrent--;
						}
						levelDeltaNext++;
					} else if ((strcmp(prevWord, "else") == 0 && strcmp(s, "if") == 0) || strcmp(s, "elseif") == 0) {
						if (!isPrevLine) {
							levelCurrent--;
						}
					} else if ((strcmp(prevWord, "select") == 0 && strcmp(s, "case") == 0) || strcmp(s, "selectcase") == 0 ||
							   (strcmp(prevWord, "select") == 0 && strcmp(s, "type") == 0) || strcmp(s, "selecttype") == 0) {
						levelDeltaNext += 2;
					} else if ((strcmp(s, "case") == 0 && chNextNonBlank == '(') || (strcmp(prevWord, "case") == 0 && strcmp(s, "default") == 0) ||
							   (strcmp(prevWord, "type") == 0 && strcmp(s, "is") == 0) ||
							   (strcmp(prevWord, "class") == 0 && strcmp(s, "is") == 0) ||
							   (strcmp(prevWord, "class") == 0 && strcmp(s, "default") == 0) ) {
						if (!isPrevLine) {
							levelCurrent--;
						}
						levelDeltaNext++;
					} else if ((strcmp(prevWord, "end") == 0 && strcmp(s, "select") == 0) || strcmp(s, "endselect") == 0) {
						levelDeltaNext -= 2;
					}

					// There are multiple forms of "do" loop. The older form with a label "do 100 i=1,10" would require matching
					// labels to ensure the folding level does not decrease too far when labels are used for other purposes.
					// Since this is difficult, do-label constructs are not folded.
					if (strcmp(s, "do") == 0 && IsADigit(chNextNonBlank)) {
						// Remove delta for do-label
						levelDeltaNext -= wordLevelDelta;
					}
				}
				strcpy(prevWord, s);
			}
		}
		if (atEOL) {
			if (foldComment) {
				int ldNext;
				CheckLevelCommentLine(nComL, nComColB, nComColF, nComCur, comLineB, comLineF, comLineCur, ldNext);
				levelDeltaNext += ldNext;
			}
			int lev = levelCurrent;
			if (visibleChars == 0 && foldCompact)
				lev |= SC_FOLDLEVELWHITEFLAG;
			if ((levelDeltaNext > 0) && (visibleChars > 0))
				lev |= SC_FOLDLEVELHEADERFLAG;
			if (lev != styler.LevelAt(lineCurrent))
				styler.SetLevel(lineCurrent, lev);

			lineCurrent++;
			levelCurrent += levelDeltaNext;
			levelDeltaNext = 0;
			visibleChars = 0;
			strcpy(prevWord, "");
			isPrevLine = false;

			if (foldComment) {
				StepCommentLine(styler, isFixFormat, lineCurrent, nComL, nComColB, nComColF, nComCur,
						comLineB, comLineF, comLineCur);
			}
		}
		/***************************************/
		if (!isspacechar(ch)) visibleChars++;
	}
	/***************************************/
}