Skip to content

Commit 9133498

Browse files
committed
Update Themes - JavaScript.js edge case
per notepad-plus-plus#17347 (comment), need to specially handle the colors for old themes that were missing JavaScript.js, otherwise users will think that N++ broke their themes (which used to work due to the magic handling of missing-JavaScript.js-uses-embedded-JavaScript-lexer)
1 parent 2cb9672 commit 9133498

File tree

1 file changed

+101
-8
lines changed

1 file changed

+101
-8
lines changed

PowerEditor/src/Parameters.cpp

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,64 @@ void NppParameters::updateStylesXml(TiXmlElement* rootUser, TiXmlElement* rootMo
24522452
if (modelLexerName.empty())
24532453
continue;
24542454

2455+
bool flag = false;
2456+
if (modelLexerName == L"javascript" || modelLexerName == L"javascript.js")
2457+
flag = true;
2458+
2459+
// map styleID numbers: index will be the target dot-js ID, intermediate index is fgColor/bgColor, stored value will be the source embedded-javascript color string
2460+
std::map <std::wstring, std::map<std::wstring, std::wstring>> mapColorsEmbeddedToDotJs;
2461+
if ((modelLexerName == L"javascript.js") && mapUserLexers.contains(L"javascript"))
2462+
{
2463+
TiXmlElement* srcEmbeddedLexer = mapUserLexers[L"javascript"];
2464+
2465+
// iterate through each embedded WordsStyle element
2466+
for (TiXmlElement* embeddedWordsStyle = srcEmbeddedLexer->FirstChildElement(L"WordsStyle");
2467+
embeddedWordsStyle;
2468+
embeddedWordsStyle = embeddedWordsStyle->NextSiblingElement(L"WordsStyle"))
2469+
{
2470+
const wchar_t* embeddedID = embeddedWordsStyle->Attribute(L"styleID");
2471+
const wchar_t* embeddedFG = embeddedWordsStyle->Attribute(L"fgColor");
2472+
const wchar_t* embeddedBG = embeddedWordsStyle->Attribute(L"bgColor");
2473+
if (embeddedID)
2474+
{
2475+
auto do_embedded_to_dot_js_map = [](std::map <std::wstring, std::map<std::wstring, std::wstring>>&colorid_map, std::wstring dotjs_id, std::wstring emb_id_desired, const wchar_t* embID, const wchar_t* embFG, const wchar_t* embBG) {
2476+
if (emb_id_desired == embID)
2477+
{
2478+
if (embFG)
2479+
colorid_map[dotjs_id][L"fgColor"] = embFG;
2480+
if (embBG)
2481+
colorid_map[dotjs_id][L"bgColor"] = embBG;
2482+
}
2483+
};
2484+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"11", L"41", embeddedID, embeddedFG, embeddedBG); // get DOTJS::DEFAULT from EMBEDDED::DEFAULT
2485+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"4", L"45", embeddedID, embeddedFG, embeddedBG); // get DOTJS::NUMBER from EMBEDDED::NUMBER
2486+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"16", L"46", embeddedID, embeddedFG, embeddedBG); // get DOTJS::TYPE_WORD<type1> from EMBEDDED::WORD
2487+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"5", L"47", embeddedID, embeddedFG, embeddedBG); // get DOTJS::INSTRUCTION_WORD <instre1> from EMBEDDED::KEYWORD <instre1>
2488+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"19", L"47", embeddedID, embeddedFG, embeddedBG); // get DOTJS::WINDOW_INSTRUCTION <instre2> also from EMBEDDED::KEYWORD <instre1> (there isn't 1:1 mapping, so multiple .js styles inherit from from same embedded style)
2489+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"6", L"48", embeddedID, embeddedFG, embeddedBG); // get DOTJS::STRING from EMBEDDED::DOUBLE STRING
2490+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"20", L"48", embeddedID, embeddedFG, embeddedBG); // get DOTJS::STRING_RAW also from EMBEDDED::DOUBLE STRING
2491+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"7", L"49", embeddedID, embeddedFG, embeddedBG); // get DOTJS::CHARACTER from EMBEDDED::SINGLE STRING
2492+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"10", L"50", embeddedID, embeddedFG, embeddedBG); // get DOTJS::OPERATOR from EMBEDDED::SYMBOLS
2493+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"14", L"52", embeddedID, embeddedFG, embeddedBG); // get DOTJS::REGEX from EMBEDDED::REGEX
2494+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"1", L"42", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT from EMBEDDED::COMMENT
2495+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"2", L"43", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT LINE from EMBEDDED::COMMENT LINE
2496+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"3", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT DOC from EMBEDDED::COMMENT DOC
2497+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"15", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT LINE DOC also from EMBEDDED::COMMENT DOC
2498+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"17", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT LINE DOC also from EMBEDDED::COMMENT DOC
2499+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"18", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT DOC KEYWORD also from EMBEDDED::COMMENT DOC
2500+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"19", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT DOC KEYWORD ERROR also from EMBEDDED::COMMENT DOC
2501+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"128", L"200", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2502+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"129", L"201", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2503+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"130", L"202", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2504+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"131", L"203", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2505+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"132", L"204", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2506+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"133", L"205", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2507+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"134", L"206", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2508+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"135", L"207", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2509+
}
2510+
}
2511+
}
2512+
24552513
// see if lexer already exists in UserStyles
24562514
if (mapUserLexers.contains(modelLexerName))
24572515
{
@@ -2499,30 +2557,53 @@ void NppParameters::updateStylesXml(TiXmlElement* rootUser, TiXmlElement* rootMo
24992557

25002558
if (useDefaultColors)
25012559
{
2560+
std::wstring newFg = defaultFgColor;
2561+
std::wstring newBg = defaultBgColor;
2562+
std::wstring dest_id = elementFromUser->Attribute(L"styleID");
2563+
if (!dest_id.empty() && mapColorsEmbeddedToDotJs.contains(dest_id))
2564+
{
2565+
//std::wstring src_id = mapColorsEmbeddedToDotJs[dest_id];
2566+
if (attrName == L"fgColor" && mapColorsEmbeddedToDotJs[dest_id].contains(attrName))
2567+
newFg = mapColorsEmbeddedToDotJs[dest_id][attrName];
2568+
if (attrName == L"bgColor" && mapColorsEmbeddedToDotJs[dest_id].contains(attrName))
2569+
newBg = mapColorsEmbeddedToDotJs[dest_id][attrName];
2570+
}
2571+
25022572
// override the value from the model file with the default value, for fgColor and bgColor only
25032573
if (attrName == L"fgColor")
2504-
elementFromUser->SetAttribute(attrName, defaultFgColor);
2574+
elementFromUser->SetAttribute(attrName, newFg);
25052575
else if (attrName == L"bgColor")
2506-
elementFromUser->SetAttribute(attrName, defaultBgColor);
2576+
elementFromUser->SetAttribute(attrName, newBg);
25072577
}
25082578
}
25092579

25102580
}
25112581
}
25122582
else
25132583
{
2514-
// if doesn't exist, need to clone it from model to the right parent lexer in the user list
2584+
// if WordsStyle doesn't exist, need to clone it from model to the right parent lexer in the user list
25152585
TiXmlNode* p_clone = wordsStyleFromModel->Clone();
25162586

2517-
25182587
// if using the default colors, need to override fgColor and bgColor
25192588
if (useDefaultColors)
25202589
{
25212590
TiXmlElement* p_cloneElement = p_clone->ToElement();
2591+
2592+
std::wstring newFg = defaultFgColor;
2593+
std::wstring newBg = defaultBgColor;
2594+
std::wstring dest_id = p_cloneElement->Attribute(L"styleID");
2595+
if (!dest_id.empty() && mapColorsEmbeddedToDotJs.contains(dest_id))
2596+
{
2597+
if (p_cloneElement->Attribute(L"fgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"fgColor"))
2598+
newFg = mapColorsEmbeddedToDotJs[dest_id][L"fgColor"];
2599+
if (p_cloneElement->Attribute(L"bgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"bgColor"))
2600+
newBg = mapColorsEmbeddedToDotJs[dest_id][L"bgColor"];
2601+
}
2602+
25222603
if (p_cloneElement->Attribute(L"fgColor"))
2523-
p_cloneElement->SetAttribute(L"fgColor", defaultFgColor);
2604+
p_cloneElement->SetAttribute(L"fgColor", newFg);
25242605
if (p_cloneElement->Attribute(L"bgColor"))
2525-
p_cloneElement->SetAttribute(L"bgColor", defaultBgColor);
2606+
p_cloneElement->SetAttribute(L"bgColor", newBg);
25262607
}
25272608

25282609
// now that XML element is cloned properly, add it to the current lexer
@@ -2537,15 +2618,27 @@ void NppParameters::updateStylesXml(TiXmlElement* rootUser, TiXmlElement* rootMo
25372618

25382619
if (useDefaultColors)
25392620
{
2621+
std::wstring newFg = defaultFgColor;
2622+
std::wstring newBg = defaultBgColor;
2623+
25402624
// iterate through all WordsStyle in the clone, and override fg and bg colors as needed
25412625
for (TiXmlElement* wordsStyleFromClone = p_clone->FirstChildElement(L"WordsStyle");
25422626
wordsStyleFromClone;
25432627
wordsStyleFromClone = wordsStyleFromClone->NextSiblingElement(L"WordsStyle"))
25442628
{
2629+
std::wstring dest_id = wordsStyleFromClone->Attribute(L"styleID");
2630+
if (!dest_id.empty() && mapColorsEmbeddedToDotJs.contains(dest_id))
2631+
{
2632+
if (wordsStyleFromClone->Attribute(L"fgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"fgColor"))
2633+
newFg = mapColorsEmbeddedToDotJs[dest_id][L"fgColor"];
2634+
if (wordsStyleFromClone->Attribute(L"bgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"bgColor"))
2635+
newBg = mapColorsEmbeddedToDotJs[dest_id][L"bgColor"];
2636+
}
2637+
25452638
if (wordsStyleFromClone->Attribute(L"fgColor"))
2546-
wordsStyleFromClone->SetAttribute(L"fgColor", defaultFgColor);
2639+
wordsStyleFromClone->SetAttribute(L"fgColor", newFg);
25472640
if (wordsStyleFromClone->Attribute(L"bgColor"))
2548-
wordsStyleFromClone->SetAttribute(L"bgColor", defaultBgColor);
2641+
wordsStyleFromClone->SetAttribute(L"bgColor", newBg);
25492642
}
25502643
}
25512644

0 commit comments

Comments
 (0)