Skip to content

[HTML][JavaScript][VBScript] Code simplify and cleanup #338

@zufuliu

Description

@zufuliu

Opened to discuss and backport zufuliu/notepad4@1c2360f

  1. else if (state == SCE_HBA_DEFAULT) block is not reachable after int state = stateForPrintState(StateToPrint);, and can be merged with preceding if (state == SCE_HB_DEFAULT) block.

    lexilla/lexers/LexHTML.cxx

    Lines 2706 to 2716 in c14de3d

    } else if (state == SCE_HBA_DEFAULT) { // One of the above succeeded
    if ((ch == '\"') && (nonEmptySegment)) {
    state = SCE_HBA_STRING;
    } else if (ch == '\'') {
    state = SCE_HBA_COMMENTLINE;
    } else if (IsAWordStart(ch)) {
    state = SCE_HBA_WORD;
    } else if (IsOperator(ch)) {
    styler.ColourTo(i, SCE_HBA_DEFAULT);
    }
    } else if (state == SCE_HJ_DEFAULT) { // One of the above succeeded
  2. if (state == SCE_HB_DEFAULT) block inside case SCE_HB_WORD: can be merged with if (state == SCE_HB_DEFAULT) block after the big switch.

    lexilla/lexers/LexHTML.cxx

    Lines 2379 to 2390 in c14de3d

    if (state == SCE_HB_DEFAULT) {
    if (ch == '\"') {
    state = SCE_HB_STRING;
    } else if (ch == '\'') {
    state = SCE_HB_COMMENTLINE;
    } else if (IsOperator(ch)) {
    styler.ColourTo(i, statePrintForState(SCE_HB_DEFAULT, inScriptType));
    state = SCE_HB_DEFAULT;
    }
    }
    }
    break;

    lexilla/lexers/LexHTML.cxx

    Lines 2696 to 2706 in c14de3d

    if (state == SCE_HB_DEFAULT) { // One of the above succeeded
    if ((ch == '\"') && (nonEmptySegment)) {
    state = SCE_HB_STRING;
    } else if (ch == '\'') {
    state = SCE_HB_COMMENTLINE;
    } else if (IsAWordStart(ch)) {
    state = SCE_HB_WORD;
    } else if (IsOperator(ch)) {
    styler.ColourTo(i, SCE_HB_DEFAULT);
    }
    } else if (state == SCE_HBA_DEFAULT) { // One of the above succeeded
  3. state change block inside case SCE_HJ_WORD: can be merged with else if (state == SCE_HJ_DEFAULT) block after the big switch.

    lexilla/lexers/LexHTML.cxx

    Lines 2240 to 2264 in c14de3d

    state = SCE_HJ_DEFAULT;
    if (ch == '/' && chNext == '*') {
    i++;
    if (chNext2 == '*')
    state = SCE_HJ_COMMENTDOC;
    else
    state = SCE_HJ_COMMENT;
    } else if (ch == '/' && chNext == '/') {
    state = SCE_HJ_COMMENTLINE;
    } else if (ch == '\"') {
    state = SCE_HJ_DOUBLESTRING;
    } else if (ch == '\'') {
    state = SCE_HJ_SINGLESTRING;
    } else if (ch == '`') {
    state = SCE_HJ_TEMPLATELITERAL;
    } else if ((ch == '-') && (chNext == '-') && (chNext2 == '>')) {
    styler.ColourTo(i - 1, StateToPrint);
    state = SCE_HJ_COMMENTLINE;
    i += 2;
    } else if (IsOperator(ch)) {
    styler.ColourTo(i, statePrintForState(SCE_HJ_SYMBOLS, inScriptType));
    state = SCE_HJ_DEFAULT;
    }
    }
    break;

    lexilla/lexers/LexHTML.cxx

    Lines 2716 to 2736 in c14de3d

    } else if (state == SCE_HJ_DEFAULT) { // One of the above succeeded
    if (ch == '/' && chNext == '*') {
    i++;
    if (styler.SafeGetCharAt(i + 1) == '*')
    state = SCE_HJ_COMMENTDOC;
    else
    state = SCE_HJ_COMMENT;
    } else if (ch == '/' && chNext == '/') {
    state = SCE_HJ_COMMENTLINE;
    } else if ((ch == '\"') && (nonEmptySegment)) {
    state = SCE_HJ_DOUBLESTRING;
    } else if ((ch == '\'') && (nonEmptySegment)) {
    state = SCE_HJ_SINGLESTRING;
    } else if ((ch == '`') && (nonEmptySegment)) {
    state = SCE_HJ_TEMPLATELITERAL;
    } else if (IsAWordStart(ch)) {
    state = SCE_HJ_WORD;
    } else if (IsOperator(ch)) {
    styler.ColourTo(i, statePrintForState(SCE_HJ_SYMBOLS, inScriptType));
    }
    }
  4. nonEmptySegment check could be eliminated by using continue.
    const bool nonEmptySegment = i >= static_cast<Sci_Position>(styler.GetStartSegment());

Other changes not yet made:

  1. bool tagOpened is never used / tested, so can be removed.
    bool tagOpened = (lineState >> 2) & 0x01; // 1 bit to know if we are in an opened tag
  2. Due to backtracking to start of line not inside tag states, bool tagClosing and bool isLanguageType can be omitted from lineState.

    lexilla/lexers/LexHTML.cxx

    Lines 1206 to 1209 in c14de3d

    // If inside a tag, it may be a script tag, so reread from the start of line starting tag to ensure any language tags are seen
    // PHP string can be heredoc, must find a delimiter first. Reread from beginning of line containing the string, to get the correct lineState
    if (StyleNeedsBacktrack(state)) {
    while ((startPos > 0) && (StyleNeedsBacktrack(styler.StyleIndexAt(startPos - 1)))) {

    lexilla/lexers/LexHTML.cxx

    Lines 671 to 673 in c14de3d

    constexpr bool StyleNeedsBacktrack(int state) noexcept {
    return InTagState(state) || isPHPStringState(state);
    }
  3. (Maybe complex changes) defer and merge state change for JavaScript and VBScript to blocks after the big switch.

    lexilla/lexers/LexHTML.cxx

    Lines 2188 to 2191 in c14de3d

    case SCE_HJ_DEFAULT:
    case SCE_HJ_START:
    case SCE_HJ_SYMBOLS:
    if (IsAWordStart(ch)) {

    lexilla/lexers/LexHTML.cxx

    Lines 2350 to 2352 in c14de3d

    case SCE_HB_DEFAULT:
    case SCE_HB_START:
    if (IsAWordStart(ch)) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    committedIssue fixed in repository but not in releasehtmlCaused by the hypertext lexer

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions