Debian Codegraph @

scintilla/lexilla/lexers/LexProps.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexProps.cxx
3 ** Lexer for properties files.
4 **/
5 // Copyright 1998-2001 by Neil Hodgson <[email protected]>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <cstdlib>
9 #include <cassert>
10
11 #include <string>
12 #include <string_view>
13
14 #include "ILexer.h"
15 #include "Scintilla.h"
16 #include "SciLexer.h"
17
18 #include "WordList.h"
19 #include "LexAccessor.h"
20 #include "Accessor.h"
21 #include "StyleContext.h"
22 #include "CharacterSet.h"
23 #include "LexerModule.h"
24
25 using namespace Lexilla;
26
27 namespace {
28
29 bool AtEOL(Accessor &styler, Sci_PositionU i) {
30 return (styler[i] == '\n') ||
31 ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
32 }
33
34 constexpr bool isAssignChar(char ch) noexcept {
35 return (ch == '=') || (ch == ':');
36 }
37
38 void ColourisePropsLine(
39 const char *lineBuffer,
40 Sci_PositionU lengthLine,
41 Sci_PositionU startLine,
42 Sci_PositionU endPos,
43 Accessor &styler,
44 bool allowInitialSpaces) {
45
46 Sci_PositionU i = 0;
47 if (allowInitialSpaces) {
48 while ((i < lengthLine) && isspacechar(lineBuffer[i])) // Skip initial spaces
49 i++;
50 } else {
51 if (isspacechar(lineBuffer[i])) // don't allow initial spaces
52 i = lengthLine;
53 }
54
55 if (i < lengthLine) {
56 if (lineBuffer[i] == '#' || lineBuffer[i] == '!' || lineBuffer[i] == ';') {
57 styler.ColourTo(endPos, SCE_PROPS_COMMENT);
58 } else if (lineBuffer[i] == '[') {
59 styler.ColourTo(endPos, SCE_PROPS_SECTION);
60 } else if (lineBuffer[i] == '@') {
61 styler.ColourTo(startLine + i, SCE_PROPS_DEFVAL);
62 if (isAssignChar(lineBuffer[i++]))
63 styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT);
64 styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
65 } else {
66 // Search for the '=' character
67 while ((i < lengthLine) && !isAssignChar(lineBuffer[i]))
68 i++;
69 if ((i < lengthLine) && isAssignChar(lineBuffer[i])) {
70 styler.ColourTo(startLine + i - 1, SCE_PROPS_KEY);
71 styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT);
72 styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
73 } else {
74 styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
75 }
76 }
77 } else {
78 styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
79 }
80 }
81
82 void ColourisePropsDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
83 std::string lineBuffer;
84 styler.StartAt(startPos);
85 styler.StartSegment(startPos);
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.push_back(styler[i]);
96 if (AtEOL(styler, i)) {
97 // End of line (or of line buffer) met, colourise it
98 ColourisePropsLine(lineBuffer.c_str(), lineBuffer.length(), startLine, i, styler, allowInitialSpaces);
99 lineBuffer.clear();
100 startLine = i + 1;
101 }
102 }
103 if (lineBuffer.length() > 0) { // Last line does not have ending characters
104 ColourisePropsLine(lineBuffer.c_str(), lineBuffer.length(), startLine, startPos + length - 1, styler, allowInitialSpaces);
105 }
106 }
107
108 // adaption by ksc, using the "} else {" trick of 1.53
109 // 030721
110 void FoldPropsDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
111 const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
112
113 const Sci_PositionU endPos = startPos + length;
114 int visibleChars = 0;
115 Sci_Position lineCurrent = styler.GetLine(startPos);
116
117 char chNext = styler[startPos];
118 bool headerPoint = false;
119 int levelPrevious = (lineCurrent > 0) ? styler.LevelAt(lineCurrent - 1) : SC_FOLDLEVELBASE;
120
121 for (Sci_PositionU i = startPos; i < endPos; i++) {
122 const char ch = chNext;
123 chNext = styler[i+1];
124
125 const int style = styler.StyleIndexAt(i);
126 const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
127
128 if (style == SCE_PROPS_SECTION) {
129 headerPoint = true;
130 }
131
132 if (atEOL) {
133 int lev = levelPrevious & SC_FOLDLEVELNUMBERMASK;
134 if (headerPoint) {
135 lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
136 if (levelPrevious & SC_FOLDLEVELHEADERFLAG) {
137 // previous section is empty
138 styler.SetLevel(lineCurrent - 1, SC_FOLDLEVELBASE);
139 }
140 } else if (levelPrevious & SC_FOLDLEVELHEADERFLAG) {
141 lev += 1;
142 }
143
144 if (visibleChars == 0 && foldCompact)
145 lev |= SC_FOLDLEVELWHITEFLAG;
146 if (lev != styler.LevelAt(lineCurrent)) {
147 styler.SetLevel(lineCurrent, lev);
148 }
149
150 lineCurrent++;
151 visibleChars = 0;
152 headerPoint = false;
153 levelPrevious = lev;
154 }
155 if (!isspacechar(ch))
156 visibleChars++;
157 }
158
159 int level = levelPrevious & SC_FOLDLEVELNUMBERMASK;
160 if (levelPrevious & SC_FOLDLEVELHEADERFLAG) {
161 level += 1;
162 }
163 const int flagsNext = styler.LevelAt(lineCurrent);
164 styler.SetLevel(lineCurrent, level | (flagsNext & ~SC_FOLDLEVELNUMBERMASK));
165 }
166
167 const char *const emptyWordListDesc[] = {
168 nullptr
169 };
170
171 }
172
173 extern const LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc, "props", FoldPropsDoc, emptyWordListDesc);