diff options
Diffstat (limited to 'src/LexCPP.cxx')
| -rw-r--r-- | src/LexCPP.cxx | 105 | 
1 files changed, 63 insertions, 42 deletions
| diff --git a/src/LexCPP.cxx b/src/LexCPP.cxx index b6138b1ab..966601ed3 100644 --- a/src/LexCPP.cxx +++ b/src/LexCPP.cxx @@ -44,6 +44,66 @@ static bool isOKBeforeRE(char ch) {  	return (ch == '(') || (ch == '=') || (ch == ',');  } +static void FoldCppDoc(unsigned int startPos, int length, int initStyle, WordList *[], +                            Accessor &styler) { +	// Finished the styling, time for folding +	bool fold = styler.GetPropertyInt("fold"); +	bool foldComment = styler.GetPropertyInt("fold.comment"); +	unsigned int lengthDoc = startPos + length; +	int visibleChars = 0; +	int lineCurrent = styler.GetLine(startPos); +	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK; +	int levelCurrent = levelPrev; +	char chNext = styler[startPos]; +	int styleNext = styler.StyleAt(startPos); +	int style = initStyle; +	if (fold) { +		for (unsigned int j = startPos; j < lengthDoc; j++) { +			char ch = chNext; +			chNext = styler.SafeGetCharAt(j + 1); +			int stylePrev = style; +			style = styleNext; +			styleNext = styler.StyleAt(j + 1); +			bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); +			if (foldComment && +				(style == SCE_C_COMMENT || style == SCE_C_COMMENTDOC)) { +				if (style != stylePrev) { +					levelCurrent++; +				} else if ((style != styleNext) && !atEOL) { +					// Comments don't end at end of line and the next character may be unstyled. +					levelCurrent--; +				} +			} +			if (style == SCE_C_OPERATOR) { +				if (ch == '{') { +					levelCurrent++; +				} else if (ch == '}') { +					levelCurrent--; +				} +			} +			if (atEOL) { +				int lev = levelPrev; +				if (visibleChars == 0) +					lev |= SC_FOLDLEVELWHITEFLAG; +				if ((levelCurrent > levelPrev) && (visibleChars > 0)) +					lev |= SC_FOLDLEVELHEADERFLAG; +				if (lev != styler.LevelAt(lineCurrent)) { +					styler.SetLevel(lineCurrent, lev); +				} +				lineCurrent++; +				levelPrev = levelCurrent; +				visibleChars = 0; +			} +			if (!isspacechar(ch)) +				visibleChars++; +		} +		// Fill in the real level of the next line, keeping the current flags as they will be filled in later +		int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK; +		//styler.SetLevel(lineCurrent, levelCurrent | flagsNext); +		styler.SetLevel(lineCurrent, levelPrev | flagsNext); +	} +} +  static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],                              Accessor &styler) { @@ -51,12 +111,8 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  	styler.StartAt(startPos); -	bool fold = styler.GetPropertyInt("fold"); -	bool foldComment = styler.GetPropertyInt("fold.comment");  	bool stylingWithinPreprocessor = styler.GetPropertyInt("styling.within.preprocessor"); -	int lineCurrent = styler.GetLine(startPos); -	int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK; -	int levelCurrent = levelPrev; +	//int lineCurrent = styler.GetLine(startPos);  	int state = initStyle;  	int styleBeforeLineStart = initStyle; @@ -108,8 +164,6 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  				}  			} else if (ch == '/' && chNext == '*') {  				styler.ColourTo(i - 1, state); -				if (foldComment) -					levelCurrent++;  				if (styler.SafeGetCharAt(i + 2) == '*' ||  				        styler.SafeGetCharAt(i + 2) == '!')	// Support of Qt/Doxygen doc. style  					state = SCE_C_COMMENTDOC; @@ -144,17 +198,12 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  			} else if (isoperator(ch)) {  				styler.ColourTo(i-1, state);  				styler.ColourTo(i, SCE_C_OPERATOR); -				if ((ch == '{') || (ch == '}')) { -					levelCurrent += (ch == '{') ? 1 : -1; -				}  			}  		} else if (state == SCE_C_IDENTIFIER) {  			if (!iswordchar(ch)) {  				lastWordWasUUID = classifyWordCpp(styler.GetStartSegment(), i - 1, keywords, styler);  				state = SCE_C_DEFAULT;  				if (ch == '/' && chNext == '*') { -					if (foldComment) -						levelCurrent++;  					if (styler.SafeGetCharAt(i + 2) == '*')  						state = SCE_C_COMMENTDOC;  					else @@ -167,9 +216,6 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  					state = SCE_C_CHARACTER;  				} else if (isoperator(ch)) {  					styler.ColourTo(i, SCE_C_OPERATOR); -					if ((ch == '{') || (ch == '}')) { -						levelCurrent += (ch == '{') ? 1 : -1; -					}  				}  			}  		} else { @@ -192,8 +238,6 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  					            (i > styler.GetStartSegment())))) {  						styler.ColourTo(i, state);  						state = SCE_C_DEFAULT; -						if (foldComment) -							levelCurrent--;  					}  				}  			} else if (state == SCE_C_COMMENTDOC) { @@ -203,8 +247,6 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  					            (i > styler.GetStartSegment())))) {  						styler.ColourTo(i, state);  						state = SCE_C_DEFAULT; -						if (foldComment) -							levelCurrent--;  					}  				}  			} else if (state == SCE_C_COMMENTLINE || state == SCE_C_COMMENTLINEDOC) { @@ -274,21 +316,6 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  		}  		if (atEOL) { -			if (fold) { -				int lev = levelPrev; -				if (visibleChars == 0) -					lev |= SC_FOLDLEVELWHITEFLAG; -				if ((levelCurrent > levelPrev) && (visibleChars > 0)) -					lev |= SC_FOLDLEVELHEADERFLAG; -				if (lev != styler.LevelAt(lineCurrent)) { -					styler.ColourTo(i, state); -					styler.Flush(); -					styler.SetLevel(lineCurrent, lev); -					styler.StartAt(i + 1); -				} -				lineCurrent++; -				levelPrev = levelCurrent; -			}  			styleBeforeLineStart = state;  			visibleChars = 0;  		} @@ -300,14 +327,8 @@ static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, Wo  			chPrevNonWhite = ch;  	}  	styler.ColourTo(lengthDoc - 1, state); - -	// Fill in the real level of the next line, keeping the current flags as they will be filled in later -	if (fold) { -		int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK; -		//styler.SetLevel(lineCurrent, levelCurrent | flagsNext); -		styler.SetLevel(lineCurrent, levelPrev | flagsNext); - -	} +	styler.Flush(); +	FoldCppDoc(startPos, length, initStyle, keywordlists, styler);  }  LexerModule lmCPP(SCLEX_CPP, ColouriseCppDoc); | 
