| 1 | // Scintilla source code edit control |
| 2 | /** @file LexProps.cxx |
| 3 | ** Lexer for properties files. |
| 4 | **/ |
| 5 | // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org> |
| 6 | // The License.txt file describes the conditions under which this software may be distributed. |
| 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <assert.h> |
| 13 | #include <ctype.h> |
| 14 | |
| 15 | #include <string> |
| 16 | #include <string_view> |
| 17 | |
| 18 | #include "ILexer.h" |
| 19 | #include "Scintilla.h" |
| 20 | #include "SciLexer.h" |
| 21 | |
| 22 | #include "WordList.h" |
| 23 | #include "LexAccessor.h" |
| 24 | #include "Accessor.h" |
| 25 | #include "StyleContext.h" |
| 26 | #include "CharacterSet.h" |
| 27 | #include "LexerModule.h" |
| 28 | |
| 29 | using namespace Lexilla; |
| 30 | |
| 31 | static inline bool AtEOL(Accessor &styler, Sci_PositionU i) { |
| 32 | return (styler[i] == '\n') || |
| 33 | ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n')); |
| 34 | } |
| 35 | |
| 36 | static inline bool isassignchar(unsigned char ch) { |
| 37 | return (ch == '=') || (ch == ':'); |
| 38 | } |
| 39 | |
| 40 | static void ColourisePropsLine( |
| 41 | const char *lineBuffer, |
| 42 | Sci_PositionU lengthLine, |
| 43 | Sci_PositionU startLine, |
| 44 | Sci_PositionU endPos, |
| 45 | Accessor &styler, |
| 46 | bool allowInitialSpaces) { |
| 47 | |
| 48 | Sci_PositionU i = 0; |
| 49 | if (allowInitialSpaces) { |
| 50 | while ((i < lengthLine) && isspacechar(lineBuffer[i])) // Skip initial spaces |
| 51 | i++; |
| 52 | } else { |
| 53 | if (isspacechar(lineBuffer[i])) // don't allow initial spaces |
| 54 | i = lengthLine; |
| 55 | } |
| 56 | |
| 57 | if (i < lengthLine) { |
| 58 | if (lineBuffer[i] == '#' || lineBuffer[i] == '!' || lineBuffer[i] == ';') { |
| 59 | styler.ColourTo(endPos, SCE_PROPS_COMMENT); |
| 60 | } else if (lineBuffer[i] == '[') { |
| 61 | styler.ColourTo(endPos, SCE_PROPS_SECTION); |
| 62 | } else if (lineBuffer[i] == '@') { |
| 63 | styler.ColourTo(startLine + i, SCE_PROPS_DEFVAL); |
| 64 | if (isassignchar(lineBuffer[i++])) |
| 65 | styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT); |
| 66 | styler.ColourTo(endPos, SCE_PROPS_DEFAULT); |
| 67 | } else { |
| 68 | // Search for the '=' character |
| 69 | while ((i < lengthLine) && !isassignchar(lineBuffer[i])) |
| 70 | i++; |
| 71 | if ((i < lengthLine) && isassignchar(lineBuffer[i])) { |
| 72 | styler.ColourTo(startLine + i - 1, SCE_PROPS_KEY); |
| 73 | styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT); |
| 74 | styler.ColourTo(endPos, SCE_PROPS_DEFAULT); |
| 75 | } else { |
| 76 | styler.ColourTo(endPos, SCE_PROPS_DEFAULT); |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | styler.ColourTo(endPos, SCE_PROPS_DEFAULT); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void ColourisePropsDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) { |
| 85 | std::string lineBuffer; |
| 86 | styler.StartAt(startPos); |
| 87 | styler.StartSegment(startPos); |
| 88 | Sci_PositionU startLine = startPos; |
| 89 | |
| 90 | // property lexer.props.allow.initial.spaces |
| 91 | // For properties files, set to 0 to style all lines that start with whitespace in the default style. |
| 92 | // This is not suitable for SciTE .properties files which use indentation for flow control but |
| 93 | // can be used for RFC2822 text where indentation is used for continuation lines. |
| 94 | const bool allowInitialSpaces = styler.GetPropertyInt("lexer.props.allow.initial.spaces" , 1) != 0; |
| 95 | |
| 96 | for (Sci_PositionU i = startPos; i < startPos + length; i++) { |
| 97 | lineBuffer.push_back(styler[i]); |
| 98 | if (AtEOL(styler, i)) { |
| 99 | // End of line (or of line buffer) met, colourise it |
| 100 | ColourisePropsLine(lineBuffer.c_str(), lineBuffer.length(), startLine, i, styler, allowInitialSpaces); |
| 101 | lineBuffer.clear(); |
| 102 | startLine = i + 1; |
| 103 | } |
| 104 | } |
| 105 | if (lineBuffer.length() > 0) { // Last line does not have ending characters |
| 106 | ColourisePropsLine(lineBuffer.c_str(), lineBuffer.length(), startLine, startPos + length - 1, styler, allowInitialSpaces); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // adaption by ksc, using the "} else {" trick of 1.53 |
| 111 | // 030721 |
| 112 | static void FoldPropsDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) { |
| 113 | const bool foldCompact = styler.GetPropertyInt("fold.compact" , 1) != 0; |
| 114 | |
| 115 | const Sci_PositionU endPos = startPos + length; |
| 116 | int visibleChars = 0; |
| 117 | Sci_Position lineCurrent = styler.GetLine(startPos); |
| 118 | |
| 119 | char chNext = styler[startPos]; |
| 120 | int styleNext = styler.StyleAt(startPos); |
| 121 | bool = false; |
| 122 | int lev; |
| 123 | |
| 124 | for (Sci_PositionU i = startPos; i < endPos; i++) { |
| 125 | const char ch = chNext; |
| 126 | chNext = styler[i+1]; |
| 127 | |
| 128 | const int style = styleNext; |
| 129 | styleNext = styler.StyleAt(i + 1); |
| 130 | const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n'); |
| 131 | |
| 132 | if (style == SCE_PROPS_SECTION) { |
| 133 | headerPoint = true; |
| 134 | } |
| 135 | |
| 136 | if (atEOL) { |
| 137 | lev = SC_FOLDLEVELBASE; |
| 138 | |
| 139 | if (lineCurrent > 0) { |
| 140 | const int levelPrevious = styler.LevelAt(lineCurrent - 1); |
| 141 | |
| 142 | if (levelPrevious & SC_FOLDLEVELHEADERFLAG) { |
| 143 | lev = SC_FOLDLEVELBASE + 1; |
| 144 | } else { |
| 145 | lev = levelPrevious & SC_FOLDLEVELNUMBERMASK; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (headerPoint) { |
| 150 | lev = SC_FOLDLEVELBASE; |
| 151 | } |
| 152 | if (visibleChars == 0 && foldCompact) |
| 153 | lev |= SC_FOLDLEVELWHITEFLAG; |
| 154 | |
| 155 | if (headerPoint) { |
| 156 | lev |= SC_FOLDLEVELHEADERFLAG; |
| 157 | } |
| 158 | if (lev != styler.LevelAt(lineCurrent)) { |
| 159 | styler.SetLevel(lineCurrent, lev); |
| 160 | } |
| 161 | |
| 162 | lineCurrent++; |
| 163 | visibleChars = 0; |
| 164 | headerPoint = false; |
| 165 | } |
| 166 | if (!isspacechar(ch)) |
| 167 | visibleChars++; |
| 168 | } |
| 169 | |
| 170 | if (lineCurrent > 0) { |
| 171 | const int levelPrevious = styler.LevelAt(lineCurrent - 1); |
| 172 | if (levelPrevious & SC_FOLDLEVELHEADERFLAG) { |
| 173 | lev = SC_FOLDLEVELBASE + 1; |
| 174 | } else { |
| 175 | lev = levelPrevious & SC_FOLDLEVELNUMBERMASK; |
| 176 | } |
| 177 | } else { |
| 178 | lev = SC_FOLDLEVELBASE; |
| 179 | } |
| 180 | int flagsNext = styler.LevelAt(lineCurrent); |
| 181 | styler.SetLevel(lineCurrent, lev | (flagsNext & ~SC_FOLDLEVELNUMBERMASK)); |
| 182 | } |
| 183 | |
| 184 | static const char *const emptyWordListDesc[] = { |
| 185 | 0 |
| 186 | }; |
| 187 | |
| 188 | LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc, "props" , FoldPropsDoc, emptyWordListDesc); |
| 189 | |