Skip to content

Commit

Permalink
Code refactoring, avoid highlighting return name() as function defi…
Browse files Browse the repository at this point in the history
…nition.
  • Loading branch information
zufuliu committed Sep 3, 2021
1 parent f481777 commit 4217bc5
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 63 deletions.
37 changes: 26 additions & 11 deletions scintilla/lexers/LexDart.cxx
Expand Up @@ -47,6 +47,15 @@ enum {
DartLineStateMaskImport = (1 << 1), // import
};

enum class KeywordType {
None = SCE_DART_DEFAULT,
Class = SCE_DART_CLASS,
Enum = SCE_DART_ENUM,
Label = SCE_DART_LABEL,
Return = 0x40,
While,
};

static_assert(DefaultNestedStateBaseStyle + 1 == SCE_DART_STRING_SQ);
static_assert(DefaultNestedStateBaseStyle + 2 == SCE_DART_STRING_DQ);
static_assert(DefaultNestedStateBaseStyle + 3 == SCE_DART_TRIPLE_STRING_SQ);
Expand All @@ -66,7 +75,7 @@ void ColouriseDartDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
int lineStateLineType = 0;
int commentLevel = 0; // nested block comment level

int kwType = SCE_DART_DEFAULT;
KeywordType kwType = KeywordType::None;
int chBeforeIdentifier = 0;

std::vector<int> nestedState; // string interpolation "${}"
Expand Down Expand Up @@ -123,16 +132,18 @@ void ColouriseDartDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
lineStateLineType = DartLineStateMaskImport;
}
} else if (StrEqualsAny(s, "class", "extends", "implements", "new", "throw", "as", "is")) {
kwType = SCE_DART_CLASS;
kwType = KeywordType::Class;
} else if (StrEqual(s, "enum")) {
kwType = SCE_DART_ENUM;
kwType = KeywordType::Enum;
} else if (StrEqualsAny(s, "break", "continue")) {
kwType = SCE_DART_LABEL;
kwType = KeywordType::Label;
} else if (StrEqualsAny(s, "return", "await", "yield")) {
kwType = KeywordType::Return;
}
if (kwType != SCE_DART_DEFAULT) {
if (kwType > KeywordType::None && kwType < KeywordType::Return) {
const int chNext = sc.GetLineNextChar();
if (!IsIdentifierStartEx(chNext)) {
kwType = SCE_DART_DEFAULT;
kwType = KeywordType::None;
}
}
} else if (keywordLists[1]->InList(s)) {
Expand All @@ -149,15 +160,19 @@ void ColouriseDartDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
}
}
} else if (sc.ch != '.') {
if (kwType != SCE_DART_DEFAULT) {
sc.ChangeState(kwType);
if (kwType > KeywordType::None && kwType < KeywordType::Return) {
sc.ChangeState(static_cast<int>(kwType));
} else {
const int chNext = sc.GetDocNextChar(sc.ch == '?');
if (chNext == '(') {
// type method()
// type[] method()
// type<type> method()
sc.ChangeState((IsIdentifierCharEx(chBefore) || chBefore == ']') ? SCE_DART_FUNCTION_DEFINITION : SCE_DART_FUNCTION);
if (kwType != KeywordType::Return && (IsIdentifierCharEx(chBefore) || chBefore == ']')) {
sc.ChangeState(SCE_DART_FUNCTION_DEFINITION );
} else {
sc.ChangeState(SCE_DART_FUNCTION);
}
} else if ((chBeforeIdentifier == '<' && (chNext == '>' || chNext == '<'))
|| IsIdentifierStartEx(chNext)) {
// type<type>
Expand All @@ -170,7 +185,7 @@ void ColouriseDartDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
}
}
if (sc.state != SCE_DART_WORD && sc.ch != '.') {
kwType = SCE_DART_DEFAULT;
kwType = KeywordType::None;
}
sc.SetState(SCE_DART_DEFAULT);
}
Expand Down Expand Up @@ -377,7 +392,7 @@ void ColouriseDartDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
lineStateLineType = 0;
visibleChars = 0;
visibleCharsBefore = 0;
kwType = SCE_DART_DEFAULT;
kwType = KeywordType::None;
}
sc.Forward();
}
Expand Down
51 changes: 32 additions & 19 deletions scintilla/lexers/LexGroovy.cxx
Expand Up @@ -61,6 +61,17 @@ enum class DocTagState {
TagClose, // </tag>
};

enum class KeywordType {
None = SCE_GROOVY_DEFAULT,
Annotation = SCE_GROOVY_ANNOTATION,
Class = SCE_GROOVY_CLASS,
Interface = SCE_GROOVY_INTERFACE,
Enum = SCE_GROOVY_ENUM,
Trait = SCE_GROOVY_TRAIT,
Return = 0x40,
While,
};

static_assert(DefaultNestedStateBaseStyle + 1 == SCE_GROOVY_STRING_DQ);
static_assert(DefaultNestedStateBaseStyle + 2 == SCE_GROOVY_TRIPLE_STRING_DQ);
static_assert(DefaultNestedStateBaseStyle + 3 == SCE_GROOVY_SLASHY_STRING);
Expand All @@ -84,7 +95,7 @@ constexpr bool IsSlashyStringStart(int chPrevNonWhite, int stylePrevNonWhite) no
void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, LexerWordList keywordLists, Accessor &styler) {
int lineStateLineType = 0;

int kwType = SCE_GROOVY_DEFAULT;
KeywordType kwType = KeywordType::None;
int chBeforeIdentifier = 0;

std::vector<int> nestedState; // string interpolation "${}"
Expand All @@ -94,7 +105,7 @@ void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int init
int chBefore = 0;
int visibleCharsBefore = 0;
int chPrevNonWhite = 0;
int stylePrevNonWhite = SCE_JS_DEFAULT;
int stylePrevNonWhite = SCE_GROOVY_DEFAULT;
DocTagState docTagState = DocTagState::None;
EscapeSequence escSeq;

Expand Down Expand Up @@ -145,7 +156,7 @@ void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int init
if (s[0] == '@') {
if (StrEqual(s, "@interface")) {
sc.ChangeState(SCE_GROOVY_WORD);
kwType = SCE_GROOVY_ANNOTATION;
kwType = KeywordType::Annotation;
} else {
sc.ChangeState(SCE_GROOVY_ANNOTATION);
continue;
Expand All @@ -159,22 +170,24 @@ void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int init
lineStateLineType = GroovyLineStateMaskImport;
}
} else if (StrEqualsAny(s, "class", "new", "extends", "instanceof", "throws", "as")) {
kwType = SCE_GROOVY_CLASS;
kwType = KeywordType::Class;
} else if (StrEqualsAny(s, "interface", "implements")) {
kwType = SCE_GROOVY_INTERFACE;
kwType = KeywordType::Interface;
} else if (StrEqual(s, "trait")) {
kwType = SCE_GROOVY_TRAIT;
kwType = KeywordType::Trait;
} else if (StrEqual(s, "enum")) {
kwType = SCE_GROOVY_ENUM;
kwType = KeywordType::Enum;
} else if (StrEqual(s, "return")) {
kwType = KeywordType::Return;
} else if (StrEqualsAny(s, "if", "while")) {
// to avoid treating following code as type cast:
// if (identifier) expression, while (identifier) expression
kwType = SCE_GROOVY_WORD;
kwType = KeywordType::While;
}
if (kwType != SCE_GROOVY_DEFAULT && kwType != SCE_GROOVY_WORD) {
if (kwType > KeywordType::None && kwType < KeywordType::Return) {
const int chNext = sc.GetDocNextChar();
if (!IsIdentifierStartEx(chNext)) {
kwType = SCE_GROOVY_DEFAULT;
kwType = KeywordType::None;
}
}
}
Expand Down Expand Up @@ -203,20 +216,20 @@ void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int init
sc.ChangeState(SCE_GROOVY_PROPERTY);
}
} else if (sc.ch != '.') {
if (kwType != SCE_GROOVY_DEFAULT && kwType != SCE_GROOVY_WORD) {
sc.ChangeState(kwType);
if (kwType > KeywordType::None && kwType < KeywordType::Return) {
sc.ChangeState(static_cast<int>(kwType));
} else {
const int chNext = sc.GetDocNextChar(sc.ch == ')');
if (sc.ch == ')') {
if (chBeforeIdentifier == '(' && (chNext == '(' || (kwType != SCE_GROOVY_WORD && IsIdentifierCharEx(chNext)))) {
if (chBeforeIdentifier == '(' && (chNext == '(' || (kwType != KeywordType::While && IsIdentifierCharEx(chNext)))) {
// (type)(expression)
// (type)expression, (type)++identifier, (type)--identifier
sc.ChangeState(SCE_GROOVY_CLASS);
}
} else if (chNext == '(' || IsADigit(chNext) || chNext == '\'' || chNext == '"') {
// property value
// method parameter
if (chNext == '(' && (IsIdentifierCharEx(chBefore) || chBefore == ']')) {
if (chNext == '(' && kwType != KeywordType::Return && (IsIdentifierCharEx(chBefore) || chBefore == ']')) {
// type method()
// type[] method()
// type<type> method()
Expand Down Expand Up @@ -245,7 +258,7 @@ void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int init
}
stylePrevNonWhite = sc.state;
if (sc.state != SCE_GROOVY_WORD && sc.ch != '.') {
kwType = SCE_GROOVY_DEFAULT;
kwType = KeywordType::None;
}
sc.SetState(SCE_GROOVY_DEFAULT);
}
Expand Down Expand Up @@ -492,9 +505,9 @@ void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int init
} else if (IsNumberStartEx(sc.chPrev, sc.ch, sc.chNext)) {
sc.SetState(SCE_GROOVY_NUMBER);
} else if (IsIdentifierStartEx(sc.ch)) {
chBefore = chPrevNonWhite;
if (chBefore != '.') {
chBeforeIdentifier = chBefore;
chBefore = (stylePrevNonWhite == SCE_GROOVY_FUNCTION) ? 0 : chPrevNonWhite;
if (chPrevNonWhite != '.') {
chBeforeIdentifier = chPrevNonWhite;
}
sc.SetState(SCE_GROOVY_IDENTIFIER);
} else if ((sc.ch == '@' || sc.ch == '$') && IsIdentifierStartEx(sc.chNext)) {
Expand Down Expand Up @@ -541,7 +554,7 @@ void ColouriseGroovyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int init
visibleChars = 0;
visibleCharsBefore = 0;
docTagState = DocTagState::None;
kwType = SCE_GROOVY_DEFAULT;
kwType = KeywordType::None;
}
sc.Forward();
}
Expand Down
50 changes: 34 additions & 16 deletions scintilla/lexers/LexJava.cxx
Expand Up @@ -64,6 +64,18 @@ enum class DocTagState {
TagClose, // </tag>
};

enum class KeywordType {
None = SCE_JAVA_DEFAULT,
Annotation = SCE_JAVA_ANNOTATION,
Class = SCE_JAVA_CLASS,
Interface = SCE_JAVA_INTERFACE,
Enum = SCE_JAVA_ENUM,
Record = SCE_JAVA_RECORD,
Label = SCE_JAVA_LABEL,
Return = 0x40,
While,
};

constexpr bool IsSpaceEquiv(int state) noexcept {
return state <= SCE_JAVA_TASKMARKER;
}
Expand Down Expand Up @@ -149,7 +161,7 @@ void ColouriseJavaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
int lineStateLineType = 0;
bool insideUrl = false;

int kwType = SCE_JAVA_DEFAULT;
KeywordType kwType = KeywordType::None;
int chBeforeIdentifier = 0;

int visibleChars = 0;
Expand Down Expand Up @@ -186,7 +198,7 @@ void ColouriseJavaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
if (s[0] == '@') {
if (StrEqual(s, "@interface")) {
sc.ChangeState(SCE_JAVA_WORD);
kwType = SCE_JAVA_ANNOTATION;
kwType = KeywordType::Annotation;
} else {
sc.ChangeState(SCE_JAVA_ANNOTATION);
continue;
Expand All @@ -198,24 +210,26 @@ void ColouriseJavaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
lineStateLineType = JavaLineStateMaskImport;
}
} else if (StrEqualsAny(s, "class", "new", "extends", "instanceof", "throws")) {
kwType = SCE_JAVA_CLASS;
kwType = KeywordType::Class;
} else if (StrEqualsAny(s, "interface", "implements")) {
kwType = SCE_JAVA_INTERFACE;
kwType = KeywordType::Interface;
} else if (StrEqual(s, "enum")) {
kwType = SCE_JAVA_ENUM;
kwType = KeywordType::Enum;
} else if (StrEqual(s, "record")) {
kwType = SCE_JAVA_RECORD;
kwType = KeywordType::Record;
} else if (StrEqualsAny(s, "break", "continue")) {
kwType = SCE_JAVA_LABEL;
kwType = KeywordType::Label;
} else if (StrEqualsAny(s, "return", "yield")) {
kwType = KeywordType::Return;
} else if (StrEqualsAny(s, "if", "while")) {
// to avoid treating following code as type cast:
// if (identifier) expression, while (identifier) expression
kwType = SCE_JAVA_WORD;
kwType = KeywordType::While;
}
if (kwType != SCE_JAVA_DEFAULT && kwType != SCE_JAVA_WORD) {
if (kwType > KeywordType::None && kwType < KeywordType::Return) {
const int chNext = sc.GetDocNextChar();
if (!IsIdentifierStartEx(chNext)) {
kwType = SCE_JAVA_DEFAULT;
kwType = KeywordType::None;
}
}
} else if (keywordLists[1]->InList(s)) {
Expand All @@ -238,12 +252,12 @@ void ColouriseJavaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
sc.ChangeState(SCE_JAVA_LABEL);
}
} else if (sc.ch != '.') {
if (kwType != SCE_JAVA_DEFAULT && kwType != SCE_JAVA_WORD) {
sc.ChangeState(kwType);
if (kwType > KeywordType::None && kwType < KeywordType::Return) {
sc.ChangeState(static_cast<int>(kwType));
} else {
const int chNext = sc.GetDocNextChar(sc.ch == ')');
if (sc.ch == ')') {
if (chBeforeIdentifier == '(' && (chNext == '(' || (kwType != SCE_JAVA_WORD && IsIdentifierCharEx(chNext)))) {
if (chBeforeIdentifier == '(' && (chNext == '(' || (kwType != KeywordType::While && IsIdentifierCharEx(chNext)))) {
// (type)(expression)
// (type)expression, (type)++identifier, (type)--identifier
sc.ChangeState(SCE_JAVA_CLASS);
Expand All @@ -252,7 +266,11 @@ void ColouriseJavaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
// type method()
// type[] method()
// type<type> method()
sc.ChangeState((IsIdentifierCharEx(chBefore) || chBefore == ']') ? SCE_JAVA_FUNCTION_DEFINITION : SCE_JAVA_FUNCTION);
if (kwType != KeywordType::Return && (IsIdentifierCharEx(chBefore) || chBefore == ']')) {
sc.ChangeState(SCE_JAVA_FUNCTION_DEFINITION);
} else {
sc.ChangeState(SCE_JAVA_FUNCTION);
}
} else if (sc.Match('[', ']')
|| (sc.ch == '<' && (sc.chNext == '>' || sc.chNext == '?'))
|| (chBeforeIdentifier == '<' && (chNext == '>' || chNext == '<'))
Expand All @@ -268,7 +286,7 @@ void ColouriseJavaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
}
}
if (sc.state != SCE_JAVA_WORD && sc.ch != '.') {
kwType = SCE_JAVA_DEFAULT;
kwType = KeywordType::None;
}
sc.SetState(SCE_JAVA_DEFAULT);
}
Expand Down Expand Up @@ -481,7 +499,7 @@ void ColouriseJavaDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
visibleChars = 0;
visibleCharsBefore = 0;
docTagState = DocTagState::None;
kwType = SCE_JAVA_DEFAULT;
kwType = KeywordType::None;
}
sc.Forward();
}
Expand Down

0 comments on commit 4217bc5

Please sign in to comment.