Skip to content

Commit

Permalink
Support code folding for Batch file.
Browse files Browse the repository at this point in the history
Improve highlighting for echo, title and prompt command.
  • Loading branch information
zufuliu committed Jan 3, 2019
1 parent ce1af5e commit 092017b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build/VS2017/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ IF /I "%ARCH%" == "ARM64" SET NEED_ARM64=1
CALL :SubVSPath
IF NOT EXIST "%VS_PATH%" CALL :SUBMSG "ERROR" "Visual Studio 2017 NOT FOUND, please check VS_PATH environment variable!"

IF "%processor_architecture%"=="AMD64" (
IF /I "%processor_architecture%"=="AMD64" (
SET "HOST_ARCH=amd64"
) else (
) ELSE (
SET "HOST_ARCH=x86"
)

Expand Down
38 changes: 34 additions & 4 deletions scintilla/lexers/LexBatch.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ bool GetBatEscapeLen(int state, int& length, int ch, int chNext, int chNext2) no
return length != 0;
}

constexpr int LevelNumber(int level) noexcept {
return (level & SC_FOLDLEVELNUMBERMASK) - SC_FOLDLEVELBASE;
}

}

/*static const char *const batchWordListDesc[] = {
Expand All @@ -95,6 +99,13 @@ static void ColouriseBatchDoc(Sci_PositionU startPos, Sci_Position length, int i
StyleContext sc(startPos, length, initStyle, styler);
std::vector<int> nestedState;

int levelCurrent = SC_FOLDLEVELBASE;
if (sc.currentLine > 0) {
levelCurrent = styler.LevelAt(sc.currentLine - 1) >> 16;
}
int levelNext = levelCurrent;
int parenCount = 0;

while (sc.More()) {
if (sc.atLineStart) {
quatedVar = false;
Expand Down Expand Up @@ -123,9 +134,12 @@ static void ColouriseBatchDoc(Sci_PositionU startPos, Sci_Position length, int i
} else {
if (!inEcho && keywords.InList(s)) { // not in echo ?
sc.ChangeState(SCE_BAT_WORD);
inEcho = strcmp(s, "echo") == 0;
inEcho = strcmp(s, "echo") == 0 || strcmp(s, "title") == 0 || strcmp(s, "prompt") == 0;
isGoto = strcmp(s, "goto") == 0;
isCall = strcmp(s, "call") == 0;
if (inEcho) {
parenCount = levelNext;
}
} else if (!inEcho && visibleChars == static_cast<int>(strlen(s))) {
if (visibleChars == 1 && sc.ch == ':' && sc.chNext == '\\') {
sc.Forward(2);
Expand Down Expand Up @@ -327,11 +341,18 @@ static void ColouriseBatchDoc(Sci_PositionU startPos, Sci_Position length, int i
sc.SetState(SCE_BAT_COMMENT);
} else if (IsWordStart(sc.ch)) { // all file name
sc.SetState(SCE_BAT_IDENTIFIER);
} else if (sc.ch == '(' || sc.ch == ')') {
if (!inEcho || LevelNumber(parenCount) > 0) {
sc.SetState(SCE_BAT_OPERATOR);
if (sc.ch == '(') {
levelNext++;
} else if (sc.ch == ')') {
levelNext--;
inEcho = false;
}
}
} else if (IsBatOp(sc.ch, inEcho)) {
sc.SetState(SCE_BAT_OPERATOR);
if (inEcho && sc.ch == ')') {
inEcho = false;
}
if (sc.ch == '|' || sc.ch == '&') { // pipe
if (sc.ch == sc.chNext) { // cmd1 || cmd2, cmd1 && cmd2
sc.Forward();
Expand All @@ -354,6 +375,15 @@ static void ColouriseBatchDoc(Sci_PositionU startPos, Sci_Position length, int i

if (sc.atLineEnd) {
visibleChars = 0;
const int levelUse = levelCurrent;
int lev = levelUse | levelNext << 16;
if (levelUse < levelNext) {
lev |= SC_FOLDLEVELHEADERFLAG;
}
if (lev != styler.LevelAt(sc.currentLine)) {
styler.SetLevel(sc.currentLine, lev);
}
levelCurrent = levelNext;
}
if (!isspacechar(sc.ch)) {
visibleChars++;
Expand Down
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from ad99cd to 944d8b

0 comments on commit 092017b

Please sign in to comment.