Fix UDL deletion issue

While there are the UDL in the userDefineLangs folder (new system) and a (or some) UDL in "userDefineLang.xml" (old system). The complete UDL deletion in "userDefineLang.xml" not remove "userDefineLang.xml" file.
This PR fixes the problem.

Fix #8101, close #13513
This commit is contained in:
Don Ho 2023-04-12 15:46:03 +02:00
parent 6d917af8ba
commit 4934b46bd7
2 changed files with 12 additions and 10 deletions

View File

@ -1383,7 +1383,7 @@ bool NppParameters::load()
{
auto r = addUserDefineLangsFromXmlTree(_pXmlUserLangDoc);
if (r.second - r.first > 0)
_pXmlUserLangsDoc.push_back(UdlXmlFileState(_pXmlUserLangDoc, false, r));
_pXmlUserLangsDoc.push_back(UdlXmlFileState(_pXmlUserLangDoc, false, true, r));
}
for (const auto& i : udlFiles)
@ -1398,7 +1398,7 @@ bool NppParameters::load()
{
auto r = addUserDefineLangsFromXmlTree(udlDoc);
if (r.second - r.first > 0)
_pXmlUserLangsDoc.push_back(UdlXmlFileState(udlDoc, false, r));
_pXmlUserLangsDoc.push_back(UdlXmlFileState(udlDoc, false, false, r));
}
}
@ -3097,7 +3097,7 @@ bool NppParameters::importUDLFromFile(const generic_string& sourceFile)
loadOkay = (r.second - r.first) != 0;
if (loadOkay)
{
_pXmlUserLangsDoc.push_back(UdlXmlFileState(nullptr, true, r));
_pXmlUserLangsDoc.push_back(UdlXmlFileState(nullptr, true, true, r));
// imported UDL from xml file will be added into default udl, so we should make default udl dirty
setUdlXmlDirtyFromXmlDoc(_pXmlUserLangDoc);
@ -3298,7 +3298,7 @@ Default UDL + Created + Imported
void NppParameters::writeDefaultUDL()
{
bool firstCleanDone = false;
std::vector<bool> deleteState;
std::vector<std::pair<bool, bool>> deleteState; //vector< pair<toDel, isInDefaultSharedContainer> >
for (const auto& udl : _pXmlUserLangsDoc)
{
if (!_pXmlUserLangDoc)
@ -3310,7 +3310,7 @@ void NppParameters::writeDefaultUDL()
}
bool toDelete = (udl._indexRange.second - udl._indexRange.first) == 0;
deleteState.push_back(toDelete);
deleteState.push_back(std::pair(toDelete, udl._isInDefaultSharedContainer));
if ((!udl._udlXmlDoc || udl._udlXmlDoc == _pXmlUserLangDoc) && udl._isDirty && !toDelete) // new created or/and imported UDL plus _pXmlUserLangDoc (if exist)
{
TiXmlNode *root = _pXmlUserLangDoc->FirstChild(TEXT("NotepadPlus"));
@ -3331,11 +3331,11 @@ void NppParameters::writeDefaultUDL()
}
bool deleteAll = true;
for (bool del : deleteState)
for (std::pair<bool, bool> udlState : deleteState)
{
if (!del)
if (!udlState.first && udlState.second) // if not marked to be delete udl is (&&) in default shared container (ie. "userDefineLang.xml" file)
{
deleteAll = false;
deleteAll = false; // let's keep "userDefineLang.xml" file
break;
}
}
@ -3683,7 +3683,7 @@ int NppParameters::addUserLangToEnd(const UserLangContainer & userLang, const TC
++_nbUserLang;
unsigned char iEnd = _nbUserLang;
_pXmlUserLangsDoc.push_back(UdlXmlFileState(nullptr, true, make_pair(iBegin, iEnd)));
_pXmlUserLangsDoc.push_back(UdlXmlFileState(nullptr, true, true, make_pair(iBegin, iEnd)));
// imported UDL from xml file will be added into default udl, so we should make default udl dirty
setUdlXmlDirtyFromXmlDoc(_pXmlUserLangDoc);

View File

@ -1349,9 +1349,11 @@ private:
struct UdlXmlFileState final {
TiXmlDocument* _udlXmlDoc = nullptr;
bool _isDirty = false;
bool _isInDefaultSharedContainer = false; // contained in "userDefineLang.xml" file
std::pair<unsigned char, unsigned char> _indexRange;
UdlXmlFileState(TiXmlDocument* doc, bool isDirty, std::pair<unsigned char, unsigned char> range) : _udlXmlDoc(doc), _isDirty(isDirty), _indexRange(range) {};
UdlXmlFileState(TiXmlDocument* doc, bool isDirty, bool isInDefaultSharedContainer, std::pair<unsigned char, unsigned char> range)
: _udlXmlDoc(doc), _isDirty(isDirty), _isInDefaultSharedContainer(isInDefaultSharedContainer), _indexRange(range) {};
};
const int NB_LANG = 100;