Theme improvements (#1382)
[geany-mirror.git] / scintilla / lexers / LexPowerShell.cxx
blob0b744c4ebe217ce8fa6f788d1f0662bf6a521bd5
1 // Scintilla source code edit control
2 /** @file LexPowerShell.cxx
3 ** Lexer for PowerShell scripts.
4 **/
5 // Copyright 2008 by Tim Gerundt <tim@gerundt.de>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <assert.h>
13 #include <ctype.h>
15 #include "ILexer.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
19 #include "WordList.h"
20 #include "LexAccessor.h"
21 #include "Accessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
26 #ifdef SCI_NAMESPACE
27 using namespace Scintilla;
28 #endif
30 // Extended to accept accented characters
31 static inline bool IsAWordChar(int ch) {
32 return ch >= 0x80 || isalnum(ch) || ch == '-' || ch == '_';
35 static void ColourisePowerShellDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
36 WordList *keywordlists[], Accessor &styler) {
38 WordList &keywords = *keywordlists[0];
39 WordList &keywords2 = *keywordlists[1];
40 WordList &keywords3 = *keywordlists[2];
41 WordList &keywords4 = *keywordlists[3];
42 WordList &keywords5 = *keywordlists[4];
43 WordList &keywords6 = *keywordlists[5];
45 styler.StartAt(startPos);
47 StyleContext sc(startPos, length, initStyle, styler);
49 for (; sc.More(); sc.Forward()) {
51 if (sc.state == SCE_POWERSHELL_COMMENT) {
52 if (sc.atLineEnd) {
53 sc.SetState(SCE_POWERSHELL_DEFAULT);
55 } else if (sc.state == SCE_POWERSHELL_COMMENTSTREAM) {
56 if (sc.atLineStart) {
57 while (IsASpaceOrTab(sc.ch)) {
58 sc.Forward();
60 if (sc.ch == '.' && IsAWordChar(sc.chNext)) {
61 sc.SetState(SCE_POWERSHELL_COMMENTDOCKEYWORD);
64 if (sc.ch == '>' && sc.chPrev == '#') {
65 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
67 } else if (sc.state == SCE_POWERSHELL_COMMENTDOCKEYWORD) {
68 if (!IsAWordChar(sc.ch)) {
69 char s[100];
70 sc.GetCurrentLowered(s, sizeof(s));
71 if (!keywords6.InList(s + 1)) {
72 sc.ChangeState(SCE_POWERSHELL_COMMENTSTREAM);
74 sc.SetState(SCE_POWERSHELL_COMMENTSTREAM);
76 } else if (sc.state == SCE_POWERSHELL_STRING) {
77 // This is a doubles quotes string
78 if (sc.ch == '\"') {
79 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
80 } else if (sc.ch == '`') {
81 sc.Forward(); // skip next escaped character
83 } else if (sc.state == SCE_POWERSHELL_CHARACTER) {
84 // This is a single quote string
85 if (sc.ch == '\'') {
86 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
87 } else if (sc.ch == '`') {
88 sc.Forward(); // skip next escaped character
90 } else if (sc.state == SCE_POWERSHELL_HERE_STRING) {
91 // This is a doubles quotes here-string
92 if (sc.atLineStart && sc.ch == '\"' && sc.chNext == '@') {
93 sc.Forward(2);
94 sc.SetState(SCE_POWERSHELL_DEFAULT);
96 } else if (sc.state == SCE_POWERSHELL_HERE_CHARACTER) {
97 // This is a single quote here-string
98 if (sc.atLineStart && sc.ch == '\'' && sc.chNext == '@') {
99 sc.Forward(2);
100 sc.SetState(SCE_POWERSHELL_DEFAULT);
102 } else if (sc.state == SCE_POWERSHELL_NUMBER) {
103 if (!IsADigit(sc.ch)) {
104 sc.SetState(SCE_POWERSHELL_DEFAULT);
106 } else if (sc.state == SCE_POWERSHELL_VARIABLE) {
107 if (!IsAWordChar(sc.ch)) {
108 sc.SetState(SCE_POWERSHELL_DEFAULT);
110 } else if (sc.state == SCE_POWERSHELL_OPERATOR) {
111 if (!isoperator(static_cast<char>(sc.ch))) {
112 sc.SetState(SCE_POWERSHELL_DEFAULT);
114 } else if (sc.state == SCE_POWERSHELL_IDENTIFIER) {
115 if (!IsAWordChar(sc.ch)) {
116 char s[100];
117 sc.GetCurrentLowered(s, sizeof(s));
119 if (keywords.InList(s)) {
120 sc.ChangeState(SCE_POWERSHELL_KEYWORD);
121 } else if (keywords2.InList(s)) {
122 sc.ChangeState(SCE_POWERSHELL_CMDLET);
123 } else if (keywords3.InList(s)) {
124 sc.ChangeState(SCE_POWERSHELL_ALIAS);
125 } else if (keywords4.InList(s)) {
126 sc.ChangeState(SCE_POWERSHELL_FUNCTION);
127 } else if (keywords5.InList(s)) {
128 sc.ChangeState(SCE_POWERSHELL_USER1);
130 sc.SetState(SCE_POWERSHELL_DEFAULT);
134 // Determine if a new state should be entered.
135 if (sc.state == SCE_POWERSHELL_DEFAULT) {
136 if (sc.ch == '#') {
137 sc.SetState(SCE_POWERSHELL_COMMENT);
138 } else if (sc.ch == '<' && sc.chNext == '#') {
139 sc.SetState(SCE_POWERSHELL_COMMENTSTREAM);
140 } else if (sc.ch == '\"') {
141 sc.SetState(SCE_POWERSHELL_STRING);
142 } else if (sc.ch == '\'') {
143 sc.SetState(SCE_POWERSHELL_CHARACTER);
144 } else if (sc.ch == '@' && sc.chNext == '\"') {
145 sc.SetState(SCE_POWERSHELL_HERE_STRING);
146 } else if (sc.ch == '@' && sc.chNext == '\'') {
147 sc.SetState(SCE_POWERSHELL_HERE_CHARACTER);
148 } else if (sc.ch == '$') {
149 sc.SetState(SCE_POWERSHELL_VARIABLE);
150 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
151 sc.SetState(SCE_POWERSHELL_NUMBER);
152 } else if (isoperator(static_cast<char>(sc.ch))) {
153 sc.SetState(SCE_POWERSHELL_OPERATOR);
154 } else if (IsAWordChar(sc.ch)) {
155 sc.SetState(SCE_POWERSHELL_IDENTIFIER);
156 } else if (sc.ch == '`') {
157 sc.Forward(); // skip next escaped character
161 sc.Complete();
164 // Store both the current line's fold level and the next lines in the
165 // level store to make it easy to pick up with each increment
166 // and to make it possible to fiddle the current level for "} else {".
167 static void FoldPowerShellDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
168 WordList *[], Accessor &styler) {
169 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
170 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
171 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
172 Sci_PositionU endPos = startPos + length;
173 int visibleChars = 0;
174 Sci_Position lineCurrent = styler.GetLine(startPos);
175 int levelCurrent = SC_FOLDLEVELBASE;
176 if (lineCurrent > 0)
177 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
178 int levelMinCurrent = levelCurrent;
179 int levelNext = levelCurrent;
180 char chNext = styler[startPos];
181 int styleNext = styler.StyleAt(startPos);
182 int style = initStyle;
183 for (Sci_PositionU i = startPos; i < endPos; i++) {
184 char ch = chNext;
185 chNext = styler.SafeGetCharAt(i + 1);
186 int stylePrev = style;
187 style = styleNext;
188 styleNext = styler.StyleAt(i + 1);
189 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
190 if (style == SCE_POWERSHELL_OPERATOR) {
191 if (ch == '{') {
192 // Measure the minimum before a '{' to allow
193 // folding on "} else {"
194 if (levelMinCurrent > levelNext) {
195 levelMinCurrent = levelNext;
197 levelNext++;
198 } else if (ch == '}') {
199 levelNext--;
201 } else if (foldComment && style == SCE_POWERSHELL_COMMENTSTREAM) {
202 if (stylePrev != SCE_POWERSHELL_COMMENTSTREAM && stylePrev != SCE_POWERSHELL_COMMENTDOCKEYWORD) {
203 levelNext++;
204 } else if (styleNext != SCE_POWERSHELL_COMMENTSTREAM && styleNext != SCE_POWERSHELL_COMMENTDOCKEYWORD) {
205 levelNext--;
207 } else if (foldComment && style == SCE_POWERSHELL_COMMENT) {
208 if (ch == '#') {
209 Sci_PositionU j = i + 1;
210 while ((j < endPos) && IsASpaceOrTab(styler.SafeGetCharAt(j))) {
211 j++;
213 if (styler.Match(j, "region")) {
214 levelNext++;
215 } else if (styler.Match(j, "endregion")) {
216 levelNext--;
220 if (!IsASpace(ch))
221 visibleChars++;
222 if (atEOL || (i == endPos-1)) {
223 int levelUse = levelCurrent;
224 if (foldAtElse) {
225 levelUse = levelMinCurrent;
227 int lev = levelUse | levelNext << 16;
228 if (visibleChars == 0 && foldCompact)
229 lev |= SC_FOLDLEVELWHITEFLAG;
230 if (levelUse < levelNext)
231 lev |= SC_FOLDLEVELHEADERFLAG;
232 if (lev != styler.LevelAt(lineCurrent)) {
233 styler.SetLevel(lineCurrent, lev);
235 lineCurrent++;
236 levelCurrent = levelNext;
237 levelMinCurrent = levelCurrent;
238 visibleChars = 0;
243 static const char *const powershellWordLists[] = {
244 "Commands",
245 "Cmdlets",
246 "Aliases",
247 "Functions",
248 "User1",
249 "DocComment",
253 LexerModule lmPowerShell(SCLEX_POWERSHELL, ColourisePowerShellDoc, "powershell", FoldPowerShellDoc, powershellWordLists);