Skip to content

Commit 3bdb223

Browse files
pryrtdonho
authored andcommitted
Fix JavaScript.js edge case in Update Themes feature
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) Fix notepad-plus-plus#17347 (comment) Close notepad-plus-plus#17409
1 parent dd0cc9b commit 3bdb223

1 file changed

Lines changed: 97 additions & 8 deletions

File tree

PowerEditor/src/Parameters.cpp

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

2455+
// 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
2456+
std::map <std::wstring, std::map<std::wstring, std::wstring>> mapColorsEmbeddedToDotJs;
2457+
if ((modelLexerName == L"javascript.js") && mapUserLexers.contains(L"javascript"))
2458+
{
2459+
TiXmlElement* srcEmbeddedLexer = mapUserLexers[L"javascript"];
2460+
2461+
// iterate through each embedded WordsStyle element
2462+
for (TiXmlElement* embeddedWordsStyle = srcEmbeddedLexer->FirstChildElement(L"WordsStyle");
2463+
embeddedWordsStyle;
2464+
embeddedWordsStyle = embeddedWordsStyle->NextSiblingElement(L"WordsStyle"))
2465+
{
2466+
const wchar_t* embeddedID = embeddedWordsStyle->Attribute(L"styleID");
2467+
const wchar_t* embeddedFG = embeddedWordsStyle->Attribute(L"fgColor");
2468+
const wchar_t* embeddedBG = embeddedWordsStyle->Attribute(L"bgColor");
2469+
if (embeddedID)
2470+
{
2471+
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) {
2472+
if (emb_id_desired == embID)
2473+
{
2474+
if (embFG)
2475+
colorid_map[dotjs_id][L"fgColor"] = embFG;
2476+
if (embBG)
2477+
colorid_map[dotjs_id][L"bgColor"] = embBG;
2478+
}
2479+
};
2480+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"11", L"41", embeddedID, embeddedFG, embeddedBG); // get DOTJS::DEFAULT from EMBEDDED::DEFAULT
2481+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"4", L"45", embeddedID, embeddedFG, embeddedBG); // get DOTJS::NUMBER from EMBEDDED::NUMBER
2482+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"16", L"46", embeddedID, embeddedFG, embeddedBG); // get DOTJS::TYPE_WORD<type1> from EMBEDDED::WORD
2483+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"5", L"47", embeddedID, embeddedFG, embeddedBG); // get DOTJS::INSTRUCTION_WORD <instre1> from EMBEDDED::KEYWORD <instre1>
2484+
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)
2485+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"6", L"48", embeddedID, embeddedFG, embeddedBG); // get DOTJS::STRING from EMBEDDED::DOUBLE STRING
2486+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"20", L"48", embeddedID, embeddedFG, embeddedBG); // get DOTJS::STRING_RAW also from EMBEDDED::DOUBLE STRING
2487+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"7", L"49", embeddedID, embeddedFG, embeddedBG); // get DOTJS::CHARACTER from EMBEDDED::SINGLE STRING
2488+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"10", L"50", embeddedID, embeddedFG, embeddedBG); // get DOTJS::OPERATOR from EMBEDDED::SYMBOLS
2489+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"14", L"52", embeddedID, embeddedFG, embeddedBG); // get DOTJS::REGEX from EMBEDDED::REGEX
2490+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"1", L"42", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT from EMBEDDED::COMMENT
2491+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"2", L"43", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT LINE from EMBEDDED::COMMENT LINE
2492+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"3", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT DOC from EMBEDDED::COMMENT DOC
2493+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"15", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT LINE DOC also from EMBEDDED::COMMENT DOC
2494+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"17", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT LINE DOC also from EMBEDDED::COMMENT DOC
2495+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"18", L"44", embeddedID, embeddedFG, embeddedBG); // get DOTJS::COMMENT DOC KEYWORD also from EMBEDDED::COMMENT DOC
2496+
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
2497+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"128", L"200", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2498+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"129", L"201", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2499+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"130", L"202", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2500+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"131", L"203", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2501+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"132", L"204", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2502+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"133", L"205", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2503+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"134", L"206", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2504+
do_embedded_to_dot_js_map(mapColorsEmbeddedToDotJs, L"135", L"207", embeddedID, embeddedFG, embeddedBG); // get DOTJS::USER* from EMBEDDED::USER*
2505+
}
2506+
}
2507+
}
2508+
24552509
// see if lexer already exists in UserStyles
24562510
if (mapUserLexers.contains(modelLexerName))
24572511
{
@@ -2499,30 +2553,53 @@ void NppParameters::updateStylesXml(TiXmlElement* rootUser, TiXmlElement* rootMo
24992553

25002554
if (useDefaultColors)
25012555
{
2556+
std::wstring newFg = defaultFgColor;
2557+
std::wstring newBg = defaultBgColor;
2558+
std::wstring dest_id = elementFromUser->Attribute(L"styleID");
2559+
if (!dest_id.empty() && mapColorsEmbeddedToDotJs.contains(dest_id))
2560+
{
2561+
//std::wstring src_id = mapColorsEmbeddedToDotJs[dest_id];
2562+
if (attrName == L"fgColor" && mapColorsEmbeddedToDotJs[dest_id].contains(attrName))
2563+
newFg = mapColorsEmbeddedToDotJs[dest_id][attrName];
2564+
if (attrName == L"bgColor" && mapColorsEmbeddedToDotJs[dest_id].contains(attrName))
2565+
newBg = mapColorsEmbeddedToDotJs[dest_id][attrName];
2566+
}
2567+
25022568
// override the value from the model file with the default value, for fgColor and bgColor only
25032569
if (attrName == L"fgColor")
2504-
elementFromUser->SetAttribute(attrName, defaultFgColor);
2570+
elementFromUser->SetAttribute(attrName, newFg);
25052571
else if (attrName == L"bgColor")
2506-
elementFromUser->SetAttribute(attrName, defaultBgColor);
2572+
elementFromUser->SetAttribute(attrName, newBg);
25072573
}
25082574
}
25092575

25102576
}
25112577
}
25122578
else
25132579
{
2514-
// if doesn't exist, need to clone it from model to the right parent lexer in the user list
2580+
// if WordsStyle doesn't exist, need to clone it from model to the right parent lexer in the user list
25152581
TiXmlNode* p_clone = wordsStyleFromModel->Clone();
25162582

2517-
25182583
// if using the default colors, need to override fgColor and bgColor
25192584
if (useDefaultColors)
25202585
{
25212586
TiXmlElement* p_cloneElement = p_clone->ToElement();
2587+
2588+
std::wstring newFg = defaultFgColor;
2589+
std::wstring newBg = defaultBgColor;
2590+
std::wstring dest_id = p_cloneElement->Attribute(L"styleID");
2591+
if (!dest_id.empty() && mapColorsEmbeddedToDotJs.contains(dest_id))
2592+
{
2593+
if (p_cloneElement->Attribute(L"fgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"fgColor"))
2594+
newFg = mapColorsEmbeddedToDotJs[dest_id][L"fgColor"];
2595+
if (p_cloneElement->Attribute(L"bgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"bgColor"))
2596+
newBg = mapColorsEmbeddedToDotJs[dest_id][L"bgColor"];
2597+
}
2598+
25222599
if (p_cloneElement->Attribute(L"fgColor"))
2523-
p_cloneElement->SetAttribute(L"fgColor", defaultFgColor);
2600+
p_cloneElement->SetAttribute(L"fgColor", newFg);
25242601
if (p_cloneElement->Attribute(L"bgColor"))
2525-
p_cloneElement->SetAttribute(L"bgColor", defaultBgColor);
2602+
p_cloneElement->SetAttribute(L"bgColor", newBg);
25262603
}
25272604

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

25382615
if (useDefaultColors)
25392616
{
2617+
std::wstring newFg = defaultFgColor;
2618+
std::wstring newBg = defaultBgColor;
2619+
25402620
// iterate through all WordsStyle in the clone, and override fg and bg colors as needed
25412621
for (TiXmlElement* wordsStyleFromClone = p_clone->FirstChildElement(L"WordsStyle");
25422622
wordsStyleFromClone;
25432623
wordsStyleFromClone = wordsStyleFromClone->NextSiblingElement(L"WordsStyle"))
25442624
{
2625+
std::wstring dest_id = wordsStyleFromClone->Attribute(L"styleID");
2626+
if (!dest_id.empty() && mapColorsEmbeddedToDotJs.contains(dest_id))
2627+
{
2628+
if (wordsStyleFromClone->Attribute(L"fgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"fgColor"))
2629+
newFg = mapColorsEmbeddedToDotJs[dest_id][L"fgColor"];
2630+
if (wordsStyleFromClone->Attribute(L"bgColor") && mapColorsEmbeddedToDotJs[dest_id].contains(L"bgColor"))
2631+
newBg = mapColorsEmbeddedToDotJs[dest_id][L"bgColor"];
2632+
}
2633+
25452634
if (wordsStyleFromClone->Attribute(L"fgColor"))
2546-
wordsStyleFromClone->SetAttribute(L"fgColor", defaultFgColor);
2635+
wordsStyleFromClone->SetAttribute(L"fgColor", newFg);
25472636
if (wordsStyleFromClone->Attribute(L"bgColor"))
2548-
wordsStyleFromClone->SetAttribute(L"bgColor", defaultBgColor);
2637+
wordsStyleFromClone->SetAttribute(L"bgColor", newBg);
25492638
}
25502639
}
25512640

0 commit comments

Comments
 (0)