Skip to content

Commit

Permalink
[VBA] Handle line continuation in comment, issue #716.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Nov 2, 2024
1 parent 0de46ed commit 97f2cd7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
11 changes: 6 additions & 5 deletions scintilla/include/SciLexer.iface
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,9 @@ val SCE_RB_LIKE_CLASS=
val SCE_RB_BUILTIN_CONSTANT=
val SCE_RB_BUILTIN_FUNCTION=
val SCE_RB_UPPER_BOUND=
# Lexical states for SCLEX_VISUALBASIC, SCLEX_VBSCRIPT, SCLEX_POWERBASIC, SCLEX_BLITZBASIC, SCLEX_PUREBASIC, SCLEX_FREEBASIC
# Lexical states for SCLEX_VISUALBASIC, SCLEX_VBSCRIPT
lex VisualBasic=SCLEX_VISUALBASIC SCE_VB_
lex VBScript=SCLEX_VBSCRIPT SCE_VB_
#lex PowerBasic=SCLEX_POWERBASIC SCE_B_
#lex BlitzBasic=SCLEX_BLITZBASIC SCE_B_
#lex PureBasic=SCLEX_PUREBASIC SCE_B_
#lex FreeBasic=SCLEX_FREEBASIC SCE_B_
val SCE_VB_DEFAULT=
val SCE_VB_COMMENTLINE=
val SCE_VB_LINE_CONTINUATION=
Expand All @@ -554,6 +550,11 @@ val SCE_VB_BASIC_FUNCTION=
val SCE_VB_PREPROCESSOR=
val SCE_VB_PREPROCESSOR_WORD=
val SCE_VB_LABEL=
# Lexical states for SCLEX_POWERBASIC, SCLEX_BLITZBASIC, SCLEX_PUREBASIC, SCLEX_FREEBASIC
#lex PowerBasic=SCLEX_POWERBASIC SCE_B_
#lex BlitzBasic=SCLEX_BLITZBASIC SCE_B_
#lex PureBasic=SCLEX_PUREBASIC SCE_B_
#lex FreeBasic=SCLEX_FREEBASIC SCE_B_
# Lexical states for SCLEX_PROPERTIES
lex Properties=SCLEX_PROPERTIES SCE_PROPS_
val SCE_PROPS_DEFAULT=0
Expand Down
24 changes: 22 additions & 2 deletions scintilla/lexers/LexVB.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ enum class Language {
VBScript,
};

enum {
VBLineStateLineContinuation = 1 << 1,
};

#define LexCharAt(pos) styler.SafeGetCharAt(pos)

// https://learn.microsoft.com/en-us/dotnet/visual-basic/reference/language-specification/lexical-grammar#type-characters
Expand Down Expand Up @@ -84,6 +88,7 @@ void ColouriseVBDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
const WordList &keywords5 = keywordLists[4];
const WordList &keywords6 = keywordLists[5];

int lineState = 0;
int fileNbDigits = 0;
int visibleChars = 0;
int chPrevNonWhite = 0;
Expand All @@ -93,12 +98,15 @@ void ColouriseVBDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
const Language language = static_cast<Language>(styler.GetPropertyInt("lexer.lang"));

StyleContext sc(startPos, length, initStyle, styler);
if (sc.currentLine > 0) {
lineState = styler.GetLineState(sc.currentLine - 1);
lineState &= VBLineStateLineContinuation;
}
if (startPos != 0 && IsSpaceEquiv(initStyle)) {
LookbackNonWhite(styler, startPos, SCE_VB_LINE_CONTINUATION, chPrevNonWhite, stylePrevNonWhite);
}

while (sc.More()) {

switch (sc.state) {
case SCE_VB_OPERATOR:
case SCE_VB_LINE_CONTINUATION:
Expand Down Expand Up @@ -156,6 +164,7 @@ void ColouriseVBDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
} else if (keywords6.InList(s)) {
sc.ChangeState(SCE_VB_CONSTANT);
}
stylePrevNonWhite = sc.state;
}
sc.SetState(SCE_VB_DEFAULT);
}
Expand Down Expand Up @@ -186,7 +195,17 @@ void ColouriseVBDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,

case SCE_VB_COMMENTLINE:
if (sc.atLineStart) {
sc.SetState(SCE_VB_DEFAULT);
if (lineState == VBLineStateLineContinuation) {
lineState = 0;
} else {
sc.SetState(SCE_VB_DEFAULT);
}
} else if (language == Language::VBA && sc.ch == '_' && sc.chPrev <= ' ') {
if (sc.GetLineNextChar(true) == '\0') {
lineState |= VBLineStateLineContinuation;
sc.SetState(SCE_VB_LINE_CONTINUATION);
sc.ForwardSetState(SCE_VB_COMMENTLINE);
}
}
break;

Expand Down Expand Up @@ -255,6 +274,7 @@ void ColouriseVBDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
}
}
if (sc.atLineEnd) {
styler.SetLineState(sc.currentLine, lineState);
isIfThenPreprocessor = false;
isEndPreprocessor = false;
visibleChars = 0;
Expand Down

0 comments on commit 97f2cd7

Please sign in to comment.