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