LexPowerShell fixes and improvements
[geany-mirror.git] / scintilla / lexers / LexPowerShell.cxx
blob1b568c0872175fc306517f76c241bed72ec4a518
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(unsigned int startPos, int 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);
81 } else if (sc.state == SCE_POWERSHELL_CHARACTER) {
82 // This is a single quote string
83 if (sc.ch == '\'') {
84 sc.ForwardSetState(SCE_POWERSHELL_DEFAULT);
86 } else if (sc.state == SCE_POWERSHELL_HERE_STRING) {
87 // This is a doubles quotes here-string
88 if (sc.atLineStart && sc.ch == '\"' && sc.chNext == '@') {
89 sc.Forward(2);
90 sc.SetState(SCE_POWERSHELL_DEFAULT);
92 } else if (sc.state == SCE_POWERSHELL_HERE_CHARACTER) {
93 // This is a single quote here-string
94 if (sc.atLineStart && sc.ch == '\'' && sc.chNext == '@') {
95 sc.Forward(2);
96 sc.SetState(SCE_POWERSHELL_DEFAULT);
98 } else if (sc.state == SCE_POWERSHELL_NUMBER) {
99 if (!IsADigit(sc.ch)) {
100 sc.SetState(SCE_POWERSHELL_DEFAULT);
102 } else if (sc.state == SCE_POWERSHELL_VARIABLE) {
103 if (!IsAWordChar(sc.ch)) {
104 sc.SetState(SCE_POWERSHELL_DEFAULT);
106 } else if (sc.state == SCE_POWERSHELL_OPERATOR) {
107 if (!isoperator(static_cast<char>(sc.ch))) {
108 sc.SetState(SCE_POWERSHELL_DEFAULT);
110 } else if (sc.state == SCE_POWERSHELL_IDENTIFIER) {
111 if (!IsAWordChar(sc.ch)) {
112 char s[100];
113 sc.GetCurrentLowered(s, sizeof(s));
115 if (keywords.InList(s)) {
116 sc.ChangeState(SCE_POWERSHELL_KEYWORD);
117 } else if (keywords2.InList(s)) {
118 sc.ChangeState(SCE_POWERSHELL_CMDLET);
119 } else if (keywords3.InList(s)) {
120 sc.ChangeState(SCE_POWERSHELL_ALIAS);
121 } else if (keywords4.InList(s)) {
122 sc.ChangeState(SCE_POWERSHELL_FUNCTION);
123 } else if (keywords5.InList(s)) {
124 sc.ChangeState(SCE_POWERSHELL_USER1);
126 sc.SetState(SCE_POWERSHELL_DEFAULT);
130 // Determine if a new state should be entered.
131 if (sc.state == SCE_POWERSHELL_DEFAULT) {
132 if (sc.ch == '#') {
133 sc.SetState(SCE_POWERSHELL_COMMENT);
134 } else if (sc.ch == '<' && sc.chNext == '#') {
135 sc.SetState(SCE_POWERSHELL_COMMENTSTREAM);
136 } else if (sc.ch == '\"') {
137 sc.SetState(SCE_POWERSHELL_STRING);
138 } else if (sc.ch == '\'') {
139 sc.SetState(SCE_POWERSHELL_CHARACTER);
140 } else if (sc.ch == '@' && sc.chNext == '\"') {
141 sc.SetState(SCE_POWERSHELL_HERE_STRING);
142 } else if (sc.ch == '@' && sc.chNext == '\'') {
143 sc.SetState(SCE_POWERSHELL_HERE_CHARACTER);
144 } else if (sc.ch == '$') {
145 sc.SetState(SCE_POWERSHELL_VARIABLE);
146 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
147 sc.SetState(SCE_POWERSHELL_NUMBER);
148 } else if (isoperator(static_cast<char>(sc.ch))) {
149 sc.SetState(SCE_POWERSHELL_OPERATOR);
150 } else if (IsAWordChar(sc.ch)) {
151 sc.SetState(SCE_POWERSHELL_IDENTIFIER);
155 sc.Complete();
158 // Store both the current line's fold level and the next lines in the
159 // level store to make it easy to pick up with each increment
160 // and to make it possible to fiddle the current level for "} else {".
161 static void FoldPowerShellDoc(unsigned int startPos, int length, int initStyle,
162 WordList *[], Accessor &styler) {
163 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
164 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
165 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
166 unsigned int endPos = startPos + length;
167 int visibleChars = 0;
168 int lineCurrent = styler.GetLine(startPos);
169 int levelCurrent = SC_FOLDLEVELBASE;
170 if (lineCurrent > 0)
171 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
172 int levelMinCurrent = levelCurrent;
173 int levelNext = levelCurrent;
174 char chNext = styler[startPos];
175 int styleNext = styler.StyleAt(startPos);
176 int style = initStyle;
177 for (unsigned int i = startPos; i < endPos; i++) {
178 char ch = chNext;
179 chNext = styler.SafeGetCharAt(i + 1);
180 int stylePrev = style;
181 style = styleNext;
182 styleNext = styler.StyleAt(i + 1);
183 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
184 if (style == SCE_POWERSHELL_OPERATOR) {
185 if (ch == '{') {
186 // Measure the minimum before a '{' to allow
187 // folding on "} else {"
188 if (levelMinCurrent > levelNext) {
189 levelMinCurrent = levelNext;
191 levelNext++;
192 } else if (ch == '}') {
193 levelNext--;
195 } else if (foldComment && style == SCE_POWERSHELL_COMMENTSTREAM) {
196 if (stylePrev != SCE_POWERSHELL_COMMENTSTREAM && stylePrev != SCE_POWERSHELL_COMMENTDOCKEYWORD) {
197 levelNext++;
198 } else if (styleNext != SCE_POWERSHELL_COMMENTSTREAM && styleNext != SCE_POWERSHELL_COMMENTDOCKEYWORD) {
199 levelNext--;
201 } else if (foldComment && style == SCE_POWERSHELL_COMMENT) {
202 if (ch == '#') {
203 unsigned int j = i + 1;
204 while ((j < endPos) && IsASpaceOrTab(styler.SafeGetCharAt(j))) {
205 j++;
207 if (styler.Match(j, "region")) {
208 levelNext++;
209 } else if (styler.Match(j, "endregion")) {
210 levelNext--;
214 if (!IsASpace(ch))
215 visibleChars++;
216 if (atEOL || (i == endPos-1)) {
217 int levelUse = levelCurrent;
218 if (foldAtElse) {
219 levelUse = levelMinCurrent;
221 int lev = levelUse | levelNext << 16;
222 if (visibleChars == 0 && foldCompact)
223 lev |= SC_FOLDLEVELWHITEFLAG;
224 if (levelUse < levelNext)
225 lev |= SC_FOLDLEVELHEADERFLAG;
226 if (lev != styler.LevelAt(lineCurrent)) {
227 styler.SetLevel(lineCurrent, lev);
229 lineCurrent++;
230 levelCurrent = levelNext;
231 levelMinCurrent = levelCurrent;
232 visibleChars = 0;
237 static const char * const powershellWordLists[] = {
238 "Commands",
239 "Cmdlets",
240 "Aliases",
241 "Functions",
242 "User1",
243 "DocComment",
247 LexerModule lmPowerShell(SCLEX_POWERSHELL, ColourisePowerShellDoc, "powershell", FoldPowerShellDoc, powershellWordLists);