sw/source/uibase/shells/basesh.cxx |   21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
New commits:
commit e65b27e79f177d27131479c46d089cad33df557b
Author:     Pranam Lashkari <lpra...@collabora.com>
AuthorDate: Sat Jan 7 02:39:38 2023 +0530
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Mon Jan 9 10:18:52 2023 +0000

    sw, UpdateFields: update in sorted order
    
    This was using the order used by GetItemSurrogates() which can be hard to 
predict
    
    So it used to update field in uncertain ways
    
    Now it updates field in order of the fields in file
    
    Change-Id: I7fc39b476cae6ed6a7fb7186803d4205832d7a73
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145167
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Andras Timar <andras.ti...@collabora.com>
    (cherry picked from commit 83f5340538d8aa8c469de3fee6012e83f8b56317)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145129
    Tested-by: Jenkins
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sw/source/uibase/shells/basesh.cxx 
b/sw/source/uibase/shells/basesh.cxx
index 9d2b17182daf..d6d4ac2ff8bb 100644
--- a/sw/source/uibase/shells/basesh.cxx
+++ b/sw/source/uibase/shells/basesh.cxx
@@ -801,10 +801,27 @@ bool UpdateFieldContents(SfxRequest& rReq, SwWrtShell& 
rWrtSh)
     SwDoc* pDoc = rWrtSh.GetDoc();
     pDoc->GetIDocumentUndoRedo().StartUndo(SwUndoId::INSBOOKMARK, nullptr);
     rWrtSh.StartAction();
+
+    std::vector<const SwFormatRefMark*> aRefMarks;
+
+    for (sal_uInt16 i = 0; i < pDoc->GetRefMarks(); ++i)
+    {
+        aRefMarks.push_back(pDoc->GetRefMark(i));
+    }
+
+    std::sort(aRefMarks.begin(), aRefMarks.end(),
+              [](const SwFormatRefMark* pMark1, const SwFormatRefMark* pMark2) 
-> bool {
+                  const SwTextRefMark* pTextRefMark1 = 
pMark1->GetTextRefMark();
+                  const SwTextRefMark* pTextRefMark2 = 
pMark2->GetTextRefMark();
+                  SwPosition aPos1(pTextRefMark1->GetTextNode(), 
pTextRefMark1->GetStart());
+                  SwPosition aPos2(pTextRefMark2->GetTextNode(), 
pTextRefMark2->GetStart());
+                  return aPos1 < aPos2;
+              });
+
     sal_uInt16 nFieldIndex = 0;
-    for (sal_uInt16 nRefMark = 0; nRefMark < pDoc->GetRefMarks(); ++nRefMark)
+    for (auto& pIntermediateRefMark : aRefMarks)
     {
-        auto pRefMark = 
const_cast<SwFormatRefMark*>(pDoc->GetRefMark(nRefMark));
+        auto pRefMark = const_cast<SwFormatRefMark*>(pIntermediateRefMark);
         if (!pRefMark->GetRefName().startsWith(rNamePrefix))
         {
             continue;

Reply via email to