sw/inc/redline.hxx                    |    2 -
 sw/source/core/doc/docredln.cxx       |   41 ++++++++++++++++------------------
 sw/source/core/text/redlnitr.cxx      |    6 ++--
 sw/source/core/unocore/unoredline.cxx |    4 +--
 sw/source/uibase/misc/redlndlg.cxx    |    4 +--
 5 files changed, 28 insertions(+), 29 deletions(-)

New commits:
commit cebbd2044b4db7977ec33eb0071191a1cac26f82
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Thu Jun 12 13:03:26 2025 +0200
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Thu Jun 12 14:53:21 2025 +0200

    SwRangeRedline::GetDescr can be const
    
    Avoids some const_casts.
    And moving some code working with PaM to a separate function allowed
    to restructure it, and avoid initializing the description variable
    with non-trivial DenoteSpecialCharacters(rPaM.GetText().replace(...)),
    in cases when it had to be immediately replaced with something else.
    
    Change-Id: I3b213ca3daa31ae491f067a823443c70f6b1d22c
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/186415
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sw/inc/redline.hxx b/sw/inc/redline.hxx
index 71ebcebbe37c..b119cf5fd2d4 100644
--- a/sw/inc/redline.hxx
+++ b/sw/inc/redline.hxx
@@ -278,7 +278,7 @@ public:
 
        bSimplified = simplified shortened text to show deletions on margin
      */
-    SW_DLLPUBLIC OUString GetDescr(bool bSimplified = false);
+    SW_DLLPUBLIC OUString GetDescr(bool bSimplified = false) const;
 
     bool operator<( const SwRangeRedline& ) const;
     void dumpAsXml(xmlTextWriterPtr pWriter) const;
diff --git a/sw/source/core/doc/docredln.cxx b/sw/source/core/doc/docredln.cxx
index 295728c8f1d6..a5ca99139dca 100644
--- a/sw/source/core/doc/docredln.cxx
+++ b/sw/source/core/doc/docredln.cxx
@@ -2390,34 +2390,36 @@ const SwRedlineData & 
SwRangeRedline::GetRedlineData(const sal_uInt16 nPos) cons
     return *pCur;
 }
 
-OUString SwRangeRedline::GetDescr(bool bSimplified)
+static OUString getRedlineDescrFromPaM(const SwPaM& rPaM, bool bSimplified)
+{
+    if (const SwTextNode* pTextNode = rPaM.GetPointNode().GetTextNode())
+    {
+        if (const SwTextAttr* pTextAttr = pTextNode->GetFieldTextAttrAt(
+                rPaM.GetPoint()->GetContentIndex() - 1, 
::sw::GetTextAttrMode::Default))
+        {
+            OUString result = 
pTextAttr->GetFormatField().GetField()->GetFieldName();
+            return bSimplified ? result
+                               : SwResId(STR_START_QUOTE) + result + 
SwResId(STR_END_QUOTE);
+        }
+    }
+    return DenoteSpecialCharacters(rPaM.GetText().replace('
', ' '), /*bQuoted=*/!bSimplified);
+}
+
+OUString SwRangeRedline::GetDescr(bool bSimplified) const
 {
     // get description of redline data (e.g.: "insert $1")
     OUString aResult = GetRedlineData().GetDescr();
 
-    SwPaM * pPaM = nullptr;
-    bool bDeletePaM = false;
-
+    OUString sDescr;
     // if this redline is visible the content is in this PaM
     if (!m_oContentSect.has_value())
     {
-        pPaM = this;
+        sDescr = getRedlineDescrFromPaM(*this, bSimplified);
     }
     else // otherwise it is saved in pContentSect
     {
-        pPaM = new SwPaM( m_oContentSect->GetNode(), 
*m_oContentSect->GetNode().EndOfSectionNode() );
-        bDeletePaM = true;
-    }
-
-    OUString sDescr = DenoteSpecialCharacters(pPaM->GetText().replace('
', ' '), /*bQuoted=*/!bSimplified);
-    if (const SwTextNode *pTextNode = pPaM->GetPointNode().GetTextNode())
-    {
-        if (const SwTextAttr* pTextAttr = 
pTextNode->GetFieldTextAttrAt(pPaM->GetPoint()->GetContentIndex() - 1, 
::sw::GetTextAttrMode::Default))
-        {
-            sDescr = ( bSimplified ? u""_ustr : SwResId(STR_START_QUOTE) )
-                + pTextAttr->GetFormatField().GetField()->GetFieldName()
-                + ( bSimplified ? u""_ustr : SwResId(STR_END_QUOTE) );
-        }
+        const SwNode& rNode = m_oContentSect->GetNode();
+        sDescr = getRedlineDescrFromPaM(SwPaM(rNode, 
*rNode.EndOfSectionNode()), bSimplified);
     }
 
     // replace $1 in description by description of the redlines text
@@ -2439,9 +2441,6 @@ OUString SwRangeRedline::GetDescr(bool bSimplified)
             aResult = aTmpStr.copy(0, nPos + SwResId(STR_LDOTS).getLength());
     }
 
-    if (bDeletePaM)
-        delete pPaM;
-
     return aResult;
 }
 
diff --git a/sw/source/core/text/redlnitr.cxx b/sw/source/core/text/redlnitr.cxx
index 2c2a30a8a396..87564e2a3fbb 100644
--- a/sw/source/core/text/redlnitr.cxx
+++ b/sw/source/core/text/redlnitr.cxx
@@ -1144,7 +1144,7 @@ bool SwRedlineItr::CheckLine(
                 // start to collect text of invisible redlines for 
ChangesInMargin layout
                 if (rRedlineText.isEmpty() && !pRedline->IsVisible())
                 {
-                    rRedlineText = 
const_cast<SwRangeRedline*>(pRedline)->GetDescr(/*bSimplified=*/true);
+                    rRedlineText = pRedline->GetDescr(/*bSimplified=*/true);
                     pPrevRedline = pRedline;
                     isExtendText = true;
                 }
@@ -1153,7 +1153,7 @@ bool SwRedlineItr::CheckLine(
                 else if (pPrevRedline && !pRedline->IsVisible() &&
                     *pRedline->Start() == *pPrevRedline->Start() && 
*pRedline->End() == *pPrevRedline->End() )
                 {
-                    OUString 
sExtendText(const_cast<SwRangeRedline*>(pRedline)->GetDescr(/*bSimplified=*/true));
+                    OUString 
sExtendText(pRedline->GetDescr(/*bSimplified=*/true));
                     if (!sExtendText.isEmpty())
                     {
                         if (rRedlineText.getLength() < 12)
@@ -1161,7 +1161,7 @@ bool SwRedlineItr::CheckLine(
                             // TODO: remove extra space from GetDescr(true),
                             // but show deletion of paragraph or line break
                             rRedlineText = rRedlineText +
-                                    
const_cast<SwRangeRedline*>(pRedline)->GetDescr(/*bSimplified=*/true).subView(1);
+                                    
pRedline->GetDescr(/*bSimplified=*/true).subView(1);
                         }
                         else
                             rRedlineText = 
OUString::Concat(rRedlineText.subView(0, rRedlineText.getLength() - 3)) + "...";
diff --git a/sw/source/core/unocore/unoredline.cxx 
b/sw/source/core/unocore/unoredline.cxx
index f9783e4cd318..b4070ae2f03c 100644
--- a/sw/source/core/unocore/unoredline.cxx
+++ b/sw/source/core/unocore/unoredline.cxx
@@ -274,7 +274,7 @@ uno::Any  SwXRedlinePortion::GetPropertyValue( 
std::u16string_view rPropertyName
     else if (rPropertyName == UNO_NAME_REDLINE_COMMENT)
         aRet <<= rRedline.GetComment();
     else if(rPropertyName == UNO_NAME_REDLINE_DESCRIPTION)
-        aRet <<= const_cast<SwRangeRedline&>(rRedline).GetDescr();
+        aRet <<= rRedline.GetDescr();
     else if(rPropertyName == UNO_NAME_REDLINE_TYPE)
     {
         aRet <<= SwRedlineTypeToOUString(rRedline.GetType());
@@ -315,7 +315,7 @@ uno::Sequence< beans::PropertyValue > 
SwXRedlinePortion::CreateRedlineProperties
     pRet[nPropIdx].Name = UNO_NAME_REDLINE_COMMENT;
     pRet[nPropIdx++].Value <<= rRedline.GetComment();
     pRet[nPropIdx].Name = UNO_NAME_REDLINE_DESCRIPTION;
-    pRet[nPropIdx++].Value <<= 
const_cast<SwRangeRedline&>(rRedline).GetDescr();
+    pRet[nPropIdx++].Value <<= rRedline.GetDescr();
     pRet[nPropIdx].Name = UNO_NAME_REDLINE_TYPE;
     pRet[nPropIdx++].Value <<= SwRedlineTypeToOUString(rRedline.GetType());
     pRet[nPropIdx].Name = UNO_NAME_REDLINE_IDENTIFIER;
diff --git a/sw/source/uibase/misc/redlndlg.cxx 
b/sw/source/uibase/misc/redlndlg.cxx
index 2ad441d1c5e7..776b881475de 100644
--- a/sw/source/uibase/misc/redlndlg.cxx
+++ b/sw/source/uibase/misc/redlndlg.cxx
@@ -562,7 +562,7 @@ void SwRedlineAcceptDlg::Activate()
             bool bShowDeletedTextAsComment = bIsShowChangesInMargin &&
                 RedlineType::Delete == rRedln.GetType() && 
rRedln.GetComment().isEmpty();
             const OUString sComment = bShowDeletedTextAsComment
-                    ? const_cast<SwRangeRedline&>(rRedln).GetDescr()
+                    ? rRedln.GetDescr()
                     : rRedln.GetComment();
             if (pParent->xTLBParent)
             {
@@ -1011,7 +1011,7 @@ void 
SwRedlineAcceptDlg::InsertParents(SwRedlineTable::size_type nStart, SwRedli
         bool bShowDeletedTextAsComment = bIsShowChangesInMargin &&
                 RedlineType::Delete == rRedln.GetType() && 
rRedln.GetComment().isEmpty();
         const OUString sComment = bShowDeletedTextAsComment
-                    ? const_cast<SwRangeRedline&>(rRedln).GetDescr()
+                    ? rRedln.GetDescr()
                     : rRedln.GetComment();
         pRedlineParent->sComment = sComment.replace('
', ' ');
         m_RedlineParents.insert(m_RedlineParents.begin() + i,

Reply via email to