editeng/source/editeng/editview.cxx |   31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

New commits:
commit a09b59cdbde2ab92bbbb6737337b74a7a62b17f1
Author:     Justin Luth <[email protected]>
AuthorDate: Thu Nov 2 12:29:47 2023 -0400
Commit:     Justin Luth <[email protected]>
CommitDate: Fri Nov 3 03:19:06 2023 +0100

    NFC related tdf#158031 editeng: flatten and simplify the code
    
    No Functional Change intended.
    Just some slight optimizing that leads to some nice flattening.
    
    Change-Id: I46d20a56821f218973c728f51575b104e6020500
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158854
    Tested-by: Jenkins
    Reviewed-by: Justin Luth <[email protected]>

diff --git a/editeng/source/editeng/editview.cxx 
b/editeng/source/editeng/editview.cxx
index 72411fab6588..a50dc9ebc6fe 100644
--- a/editeng/source/editeng/editview.cxx
+++ b/editeng/source/editeng/editview.cxx
@@ -1346,26 +1346,29 @@ const SvxFieldItem* 
EditView::GetFieldUnderMousePointer( sal_Int32& nPara, sal_I
 
 const SvxFieldItem* EditView::GetFieldAtSelection() const
 {
+    // a field is a dummy character - so it cannot span nodes or be a 
selection larger than 1
     EditSelection aSel( pImpEditView->GetEditSelection() );
+    if (aSel.Min().GetNode() != aSel.Max().GetNode())
+        return nullptr;
+
+    // normalize: min < max
     aSel.Adjust( pImpEditView->pEditEngine->GetEditDoc() );
+
+    const sal_Int32 nMinIndex =  aSel.Min().GetIndex();
+    const sal_Int32 nMaxIndex =  aSel.Max().GetIndex();
+    if (nMaxIndex > nMinIndex + 1)
+        return nullptr;
+
     // Only when cursor is in font of field, no selection,
     // or only selecting field
-    if ( ( aSel.Min().GetNode() == aSel.Max().GetNode() ) &&
-         ( ( aSel.Max().GetIndex() == aSel.Min().GetIndex() ) ||
-           ( aSel.Max().GetIndex() == aSel.Min().GetIndex()+1 ) ) )
+    const CharAttribList::AttribsType& rAttrs = 
aSel.Min().GetNode()->GetCharAttribs().GetAttribs();
+    for (const auto& rAttr: rAttrs)
     {
-        EditPaM aPaM = aSel.Min();
-        const CharAttribList::AttribsType& rAttrs = 
aPaM.GetNode()->GetCharAttribs().GetAttribs();
-        const sal_Int32 nXPos = aPaM.GetIndex();
-        for (size_t nAttr = rAttrs.size(); nAttr; )
+        if (rAttr->Which() == EE_FEATURE_FIELD)
         {
-            const EditCharAttrib& rAttr = *rAttrs[--nAttr];
-            if (rAttr.GetStart() == nXPos)
-                if (rAttr.Which() == EE_FEATURE_FIELD)
-                {
-                    DBG_ASSERT(dynamic_cast<const SvxFieldItem* 
>(rAttr.GetItem() ) != nullptr, "No FieldItem...");
-                    return static_cast<const SvxFieldItem*>(rAttr.GetItem());
-                }
+            DBG_ASSERT(dynamic_cast<const SvxFieldItem*>(rAttr->GetItem()), 
"No FieldItem...");
+            if (rAttr->GetStart() == nMinIndex)
+                return static_cast<const SvxFieldItem*>(rAttr->GetItem());
         }
     }
     return nullptr;

Reply via email to