Skip to content

Commit

Permalink
Improve JSON5 string line continuation handing.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed May 8, 2024
1 parent bc26818 commit ddc177c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
13 changes: 3 additions & 10 deletions scintilla/lexers/LexCPP.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,9 @@ void ColouriseCppDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,

if (AnyOf(initStyle, SCE_C_COMMENTLINE, SCE_C_COMMENTLINEDOC, SCE_C_PREPROCESSOR, SCE_C_STRING, SCE_C_CHARACTER)) {
// Set continuationLine if last character of previous line is '\'
if (lineCurrent > 0) {
const char chBack = styler.SafeGetCharAt(startPos - 1);
const char chBack2 = styler.SafeGetCharAt(startPos - 2);
char lineEndChar = '!';
if (chBack2 == '\r' && chBack == '\n') {
lineEndChar = styler.SafeGetCharAt(startPos - 3);
} else if (chBack == '\n' || chBack == '\r') {
lineEndChar = chBack2;
}
continuationLine = lineEndChar == '\\';
const Sci_Position pos = styler.LineEnd(lineCurrent - 1) - 1;
if (pos > 0 && styler[pos] == '\\') {
continuationLine = true;
}
}

Expand Down
15 changes: 10 additions & 5 deletions scintilla/lexers/LexJSON.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ bool IsJsonProperty(LexAccessor &styler, Sci_PositionU startPos, uint8_t chNext)
void ColouriseJSONDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, LexerWordList keywordLists, Accessor &styler) {
const bool fold = styler.GetPropertyBool("fold");

// JSON5 line continuation
bool lineContinuation = false;
bool atLineStart = true;
int state = initStyle;
uint8_t chNext = styler[startPos];
styler.StartAt(startPos);
Expand All @@ -68,16 +71,19 @@ void ColouriseJSONDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
int levelCurrent = SC_FOLDLEVELBASE;
if (lineCurrent > 0) {
levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
if (state == SCE_JSON_STRING_DQ || state == SCE_JSON_STRING_SQ) {
const Sci_Position pos = styler.LineEnd(lineCurrent - 1) - 1;
if (pos > 0 && styler[pos] == '\\') {
lineContinuation = true;
}
}
}
int levelNext = levelCurrent;

constexpr int MaxLexWordLength = 9; // Infinity
char buf[MaxLexWordLength + 1];
int wordLen = 0;

// JSON5 line continuation
bool lineContinuation = false;
bool atLineStart = true;
assert(startPos == static_cast<Sci_PositionU>(styler.LineStart(lineCurrent)));
Sci_PositionU lineStartNext = styler.LineStart(lineCurrent + 1);
const Sci_PositionU endPos = startPos + lengthDoc;
Expand Down Expand Up @@ -148,12 +154,11 @@ void ColouriseJSONDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
state = SCE_JSON_DEFAULT;
}
} else if (ch == '\\') {
styler.ColorTo(currentPos, state);
if (IsEOLChar(chNext)) {
lineContinuation = true;
styler.ColorTo(startPos, SCE_JSON_ESCAPECHAR);
} else {
// highlight any character as escape sequence
styler.ColorTo(currentPos, state);
++startPos;
if (chNext == 'u' || chNext == 'x') {
int count = (chNext == 'x') ? 2 : 4;
Expand Down

0 comments on commit ddc177c

Please sign in to comment.