Enhance Javascript syntax highlighting

Separate the normal javascript (*.js) syntax highlighting from the
embbeded Javascript (in html) syntax highlighting.
This commit is contained in:
Don Ho 2015-10-11 21:04:22 +02:00
parent e5493a7210
commit 963d7fc885
8 changed files with 154 additions and 46 deletions

View File

@ -39,7 +39,7 @@ enum LangType {L_TEXT, L_PHP , L_C, L_CPP, L_CS, L_OBJC, L_JAVA, L_RC,\
L_ASM, L_DIFF, L_PROPS, L_PS, L_RUBY, L_SMALLTALK, L_VHDL, L_KIX, L_AU3,\
L_CAML, L_ADA, L_VERILOG, L_MATLAB, L_HASKELL, L_INNO, L_SEARCHRESULT,\
L_CMAKE, L_YAML, L_COBOL, L_GUI4CLI, L_D, L_POWERSHELL, L_R, L_JSP,\
L_COFFEESCRIPT, L_JSON,\
L_COFFEESCRIPT, L_JSON, L_JAVASCRIPT,\
// The end of enumated language type, so it should be always at the end
L_EXTERNAL};
enum winVer{WV_UNKNOWN, WV_WIN32S, WV_95, WV_98, WV_ME, WV_NT, WV_W2K, WV_XP, WV_S2003, WV_XPX64, WV_VISTA, WV_WIN7, WV_WIN8, WV_WIN81};

View File

@ -2572,7 +2572,7 @@ enum LangType Notepad_plus::menuID2LangType(int cmdID)
case IDM_LANG_XML :
return L_XML;
case IDM_LANG_JS :
return L_JS;
return L_JAVASCRIPT;
case IDM_LANG_JSON:
return L_JSON;
case IDM_LANG_PHP :

View File

@ -5889,6 +5889,7 @@ int NppParameters::langTypeToCommandID(LangType lt) const
case L_XML :
id = IDM_LANG_XML; break;
case L_JS :
case L_JAVASCRIPT:
id = IDM_LANG_JS; break;
case L_JSON:
id = IDM_LANG_JSON; break;

View File

@ -909,9 +909,7 @@ struct Lang final
for (int i = 0 ; i < NB_LIST ; _langKeyWordList[i] = NULL, ++i);
}
Lang(LangType langID, const TCHAR *name)
: _langID(langID)
, _langName(name ? name : TEXT(""))
Lang(LangType langID, const TCHAR *name) : _langID(langID), _langName(name ? name : TEXT(""))
{
for (int i = 0 ; i < NB_LIST ; _langKeyWordList[i] = NULL, ++i);
}

View File

@ -136,7 +136,8 @@ LanguageName ScintillaEditView::langNames[L_EXTERNAL+1] = {
{TEXT("r"), TEXT("R"), TEXT("R programming language"), L_R, SCLEX_R},
{TEXT("jsp"), TEXT("JSP"), TEXT("JavaServer Pages script file"), L_JSP, SCLEX_HTML},
{TEXT("coffeescript"), TEXT("CoffeeScript"), TEXT("CoffeeScript file"), L_COFFEESCRIPT, SCLEX_COFFEESCRIPT},
{ TEXT("json"), TEXT("json"), TEXT("JSON file"), L_JSON, SCLEX_CPP },
{TEXT("json"), TEXT("json"), TEXT("JSON file"), L_JSON, SCLEX_CPP },
{TEXT("javascript.js"), TEXT("JavaScript"), TEXT("JavaScript file"), L_JAVASCRIPT, SCLEX_CPP },
{TEXT("ext"), TEXT("External"), TEXT("External"), L_EXTERNAL, SCLEX_NULL}
};
@ -825,11 +826,9 @@ void ScintillaEditView::setCppLexer(LangType langType)
const char *cppTypes;
const TCHAR *doxygenKeyWords = _pParameter->getWordList(L_CPP, LANG_INDEX_TYPE2);
const TCHAR *lexerName = ScintillaEditView::langNames[langType].lexerName;
execute(SCI_SETLEXER, SCLEX_CPP);
if ((langType != L_RC) && (langType != L_JS))
if (langType != L_RC)
{
if (doxygenKeyWords)
{
@ -839,39 +838,6 @@ void ScintillaEditView::setCppLexer(LangType langType)
}
}
if (langType == L_JS)
{
LexerStyler *pStyler = (_pParameter->getLStylerArray()).getLexerStylerByName(lexerName);
if (pStyler)
{
for (int i = 0, nb = pStyler->getNbStyler() ; i < nb ; ++i)
{
Style style = pStyler->getStyler(i); //not by reference, but copy
int cppID = style._styleID;
switch (style._styleID)
{
case SCE_HJ_DEFAULT : cppID = SCE_C_DEFAULT; break;
case SCE_HJ_WORD : cppID = SCE_C_IDENTIFIER; break;
case SCE_HJ_SYMBOLS : cppID = SCE_C_OPERATOR; break;
case SCE_HJ_COMMENT : cppID = SCE_C_COMMENT; break;
case SCE_HJ_COMMENTLINE : cppID = SCE_C_COMMENTLINE; break;
case SCE_HJ_COMMENTDOC : cppID = SCE_C_COMMENTDOC; break;
case SCE_HJ_NUMBER : cppID = SCE_C_NUMBER; break;
case SCE_HJ_KEYWORD : cppID = SCE_C_WORD; break;
case SCE_HJ_DOUBLESTRING : cppID = SCE_C_STRING; break;
case SCE_HJ_SINGLESTRING : cppID = SCE_C_CHARACTER; break;
case SCE_HJ_REGEX : cppID = SCE_C_REGEX; break;
}
style._styleID = cppID;
setStyle(style);
}
}
execute(SCI_STYLESETEOLFILLED, SCE_C_DEFAULT, true);
execute(SCI_STYLESETEOLFILLED, SCE_C_COMMENTLINE, true);
execute(SCI_STYLESETEOLFILLED, SCE_C_COMMENT, true);
execute(SCI_STYLESETEOLFILLED, SCE_C_COMMENTDOC, true);
}
const TCHAR *pKwArray[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
makeStyle(langType, pKwArray);
@ -905,6 +871,120 @@ void ScintillaEditView::setCppLexer(LangType langType)
execute(SCI_SETPROPERTY, reinterpret_cast<WPARAM>("lexer.cpp.track.preprocessor"), reinterpret_cast<LPARAM>("0"));
}
void ScintillaEditView::setJsLexer()
{
const TCHAR *doxygenKeyWords = _pParameter->getWordList(L_CPP, LANG_INDEX_TYPE2);
execute(SCI_SETLEXER, SCLEX_CPP);
const TCHAR *pKwArray[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
if (doxygenKeyWords)
{
WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance();
const char * doxygenKeyWords_char = wmc->wchar2char(doxygenKeyWords, CP_ACP);
execute(SCI_SETKEYWORDS, 2, (LPARAM)doxygenKeyWords_char);
}
const TCHAR *newLexerName = ScintillaEditView::langNames[L_JAVASCRIPT].lexerName;
LexerStyler *pNewStyler = (_pParameter->getLStylerArray()).getLexerStylerByName(newLexerName);
if (pNewStyler) // New js styler is available, so we can use it do more modern styling
{
if (pNewStyler)
{
for (int i = 0, nb = pNewStyler->getNbStyler(); i < nb; ++i)
{
Style & style = pNewStyler->getStyler(i);
setStyle(style);
}
}
basic_string<char> keywordListInstruction("");
basic_string<char> keywordListType("");
basic_string<char> keywordListInstruction2("");
if (pKwArray[LANG_INDEX_INSTR])
{
basic_string<wchar_t> kwlW = pKwArray[LANG_INDEX_INSTR];
keywordListInstruction = wstring2string(kwlW, CP_ACP);
}
const char *jsInstrs = getCompleteKeywordList(keywordListInstruction, L_JAVASCRIPT, LANG_INDEX_INSTR);
if (pKwArray[LANG_INDEX_TYPE])
{
basic_string<wchar_t> kwlW = pKwArray[LANG_INDEX_TYPE];
keywordListType = wstring2string(kwlW, CP_ACP);
}
const char *jsTypes = getCompleteKeywordList(keywordListType, L_JAVASCRIPT, LANG_INDEX_TYPE);
if (pKwArray[LANG_INDEX_INSTR2])
{
basic_string<wchar_t> kwlW = pKwArray[LANG_INDEX_INSTR2];
keywordListInstruction2 = wstring2string(kwlW, CP_ACP);
}
const char *jsInstrs2 = getCompleteKeywordList(keywordListInstruction2, L_JAVASCRIPT, LANG_INDEX_INSTR2);
execute(SCI_SETKEYWORDS, 0, (LPARAM)jsInstrs);
execute(SCI_SETKEYWORDS, 1, (LPARAM)jsTypes);
execute(SCI_SETKEYWORDS, 3, (LPARAM)jsInstrs2);
}
else // New js styler is not available, we use the old styling for the sake of retro-compatibility
{
const TCHAR *lexerName = ScintillaEditView::langNames[L_JS].lexerName;
LexerStyler *pOldStyler = (_pParameter->getLStylerArray()).getLexerStylerByName(lexerName);
if (pOldStyler)
{
for (int i = 0, nb = pOldStyler->getNbStyler(); i < nb; ++i)
{
Style style = pOldStyler->getStyler(i); //not by reference, but copy
int cppID = style._styleID;
switch (style._styleID)
{
case SCE_HJ_DEFAULT: cppID = SCE_C_DEFAULT; break;
case SCE_HJ_WORD: cppID = SCE_C_IDENTIFIER; break;
case SCE_HJ_SYMBOLS: cppID = SCE_C_OPERATOR; break;
case SCE_HJ_COMMENT: cppID = SCE_C_COMMENT; break;
case SCE_HJ_COMMENTLINE: cppID = SCE_C_COMMENTLINE; break;
case SCE_HJ_COMMENTDOC: cppID = SCE_C_COMMENTDOC; break;
case SCE_HJ_NUMBER: cppID = SCE_C_NUMBER; break;
case SCE_HJ_KEYWORD: cppID = SCE_C_WORD; break;
case SCE_HJ_DOUBLESTRING: cppID = SCE_C_STRING; break;
case SCE_HJ_SINGLESTRING: cppID = SCE_C_CHARACTER; break;
case SCE_HJ_REGEX: cppID = SCE_C_REGEX; break;
}
style._styleID = cppID;
setStyle(style);
}
}
execute(SCI_STYLESETEOLFILLED, SCE_C_DEFAULT, true);
execute(SCI_STYLESETEOLFILLED, SCE_C_COMMENTLINE, true);
execute(SCI_STYLESETEOLFILLED, SCE_C_COMMENT, true);
execute(SCI_STYLESETEOLFILLED, SCE_C_COMMENTDOC, true);
makeStyle(L_JS, pKwArray);
basic_string<char> keywordListInstruction("");
if (pKwArray[LANG_INDEX_INSTR])
{
basic_string<wchar_t> kwlW = pKwArray[LANG_INDEX_INSTR];
keywordListInstruction = wstring2string(kwlW, CP_ACP);
}
const char *jsEmbbededInstrs = getCompleteKeywordList(keywordListInstruction, L_JS, LANG_INDEX_INSTR);
execute(SCI_SETKEYWORDS, 0, (LPARAM)jsEmbbededInstrs);
}
execute(SCI_SETPROPERTY, reinterpret_cast<WPARAM>("fold"), reinterpret_cast<LPARAM>("1"));
execute(SCI_SETPROPERTY, reinterpret_cast<WPARAM>("fold.compact"), reinterpret_cast<LPARAM>("0"));
execute(SCI_SETPROPERTY, reinterpret_cast<WPARAM>("fold.comment"), reinterpret_cast<LPARAM>("1"));
execute(SCI_SETPROPERTY, reinterpret_cast<WPARAM>("fold.preprocessor"), reinterpret_cast<LPARAM>("1"));
// Disable track preprocessor to avoid incorrect detection.
// In the most of cases, the symbols are defined outside of file.
execute(SCI_SETPROPERTY, reinterpret_cast<WPARAM>("lexer.cpp.track.preprocessor"), reinterpret_cast<LPARAM>("0"));
}
void ScintillaEditView::setTclLexer()
{
const char *tclInstrs;
@ -1218,13 +1298,16 @@ void ScintillaEditView::defineDocType(LangType typeDoc)
{
case L_C :
case L_CPP :
case L_JS:
case L_JAVA :
case L_RC :
case L_CS :
case L_FLASH :
setCppLexer(typeDoc); break;
case L_JS:
case L_JAVASCRIPT:
setJsLexer(); break;
case L_TCL :
setTclLexer(); break;

View File

@ -692,6 +692,7 @@ protected:
//Complex lexers (same lexer, different language)
void setXmlLexer(LangType type);
void setCppLexer(LangType type);
void setJsLexer();
void setTclLexer();
void setObjCLexer(LangType type);
void setUserLexer(const TCHAR *userLangName = NULL);

View File

@ -110,8 +110,15 @@
<Keywords name="instre1">instanceof assert if else switch case default break goto return for while do continue new throw throws try catch finally this super extends implements import true false null</Keywords>
<Keywords name="type1">package transient strictfp void char short int long double float const static volatile byte boolean class interface native private protected public final abstract synchronized enum</Keywords>
</Language>
<Language name="javascript" ext="js jsm" commentLine="//" commentStart="/*" commentEnd="*/">
<!-- Embbeded javascript -->
<Language name="javascript" ext="" commentLine="//" commentStart="/*" commentEnd="*/">
<Keywords name="instre1">abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends final finally float for from function goto if implements import in instanceof int interface let long native new of package private protected public return short static super switch synchronized this throw throws transient try typeof var void volatile while with true false prototype</Keywords>
</Language>
<Language name="javascript.js" ext="js jsm" commentLine="//" commentStart="/*" commentEnd="*/">
<Keywords name="instre1">abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends final finally float for from function goto if implements import in instanceof int interface let long native new of package private protected public return short static super switch synchronized this throw throws transient try typeof var void volatile while with true false prototype</Keywords>
<Keywords name="type1">Array Date eval hasOwnProperty Infinity isFinite isNaN isPrototypeOf Math NaN Number Object prototype String toString undefined valueOf</Keywords>
<Keywords name="instre2">alert all anchor anchors area assign blur button checkbox clearInterval clearTimeout clientInformation close closed confirm constructor crypto decodeURI decodeURIComponent defaultStatus document element elements embed embeds encodeURI encodeURIComponent escape event fileUpload focus form forms frame innerHeight innerWidth layer layers link location mimeTypes navigate navigator frames frameRate hidden history image images offscreenBuffering open opener option outerHeight outerWidth packages pageXOffset pageYOffset parent parseFloat parseInt password pkcs11 plugin prompt propertyIsEnum radio reset screenX screenY scroll secure select self setInterval setTimeout status submit taint text textarea top unescape untaint window</Keywords>
<Keywords name="type2">onblur onclick onerror onfocus onkeydown onkeypress onkeyup onmouseover onload onmouseup onmousedown onsubmit</Keywords>
</Language>
<Language name="json" ext="json" commentLine="" commentStart="" commentEnd=""/>
<Language name="jsp" ext="jsp" commentLine="//" commentStart="/*" commentEnd="*/"/>

View File

@ -380,7 +380,7 @@
<WordsStyle name="COMMENT DOC KEYWORD" styleID="17" fgColor="008080" bgColor="FFFFFF" fontName="" fontStyle="1" fontSize="" />
<WordsStyle name="COMMENT DOC KEYWORD ERROR" styleID="18" fgColor="008080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
</LexerType>
<LexerType name="javascript" desc="Javascript" ext="">
<LexerType name="javascript" desc="Javascript (embbeded)" ext="">
<WordsStyle name="DEFAULT" styleID="41" fgColor="000000" bgColor="F2F4FF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="NUMBER" styleID="45" fgColor="FF0000" bgColor="F2F4FF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="WORD" styleID="46" fgColor="000000" bgColor="F2F4FF" fontName="" fontStyle="0" fontSize="" />
@ -393,6 +393,24 @@
<WordsStyle name="COMMENTLINE" styleID="43" fgColor="008000" bgColor="F2F4FF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="COMMENTDOC" styleID="44" fgColor="008080" bgColor="F2F4FF" fontName="" fontStyle="0" fontSize="" />
</LexerType>
<LexerType name="javascript.js" desc="Javascript" ext="">
<WordsStyle name="DEFAULT" styleID="11" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="INSTRUCTION WORD" styleID="5" fgColor="0000FF" bgColor="FFFFFF" fontName="" fontStyle="1" fontSize="" keywordClass="instre1" />
<WordsStyle name="TYPE WORD" styleID="16" fgColor="8000FF" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" keywordClass="type1" />
<WordsStyle name="WINDOW INSTRUCTION" styleID="19" fgColor="804000" bgColor="FFFFFF" fontName="" fontStyle="1" fontSize="" keywordClass="instre2" />
<WordsStyle name="NUMBER" styleID="4" fgColor="FF8000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="STRING" styleID="6" fgColor="808080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="CHARACTER" styleID="7" fgColor="808080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="OPERATOR" styleID="10" fgColor="000080" bgColor="FFFFFF" fontName="" fontStyle="1" fontSize="" />
<WordsStyle name="VERBATIM" styleID="13" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="REGEX" styleID="14" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="1" fontSize="" />
<WordsStyle name="COMMENT" styleID="1" fgColor="008000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="COMMENT LINE" styleID="2" fgColor="008000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="COMMENT DOC" styleID="3" fgColor="008080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="COMMENT LINE DOC" styleID="15" fgColor="008080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="COMMENT DOC KEYWORD" styleID="17" fgColor="008080" bgColor="FFFFFF" fontName="" fontStyle="1" fontSize="" />
<WordsStyle name="COMMENT DOC KEYWORD ERROR" styleID="18" fgColor="008080" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
</LexerType>
<LexerType name="json" desc="JSON" ext="">
<WordsStyle name="DEFAULT" styleID="11" fgColor="000000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />
<WordsStyle name="NUMBER" styleID="4" fgColor="FF8000" bgColor="FFFFFF" fontName="" fontStyle="0" fontSize="" />