tdf#106380 sw AutoCorrect: track deletion at capitalization

Deletion of the original lowercase character wasn't
tracked during sentence capitalization, resulting broken
change tracking data.

Note: for similar potential problems, see FIXME at the
end of DocumentContentOperationsManager::Overwrite().

Change-Id: If6ddbb77675d87eb126f7d3bf554b529892b88d7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108670
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
(cherry picked from commit aecb7364713871ce068effdb59a5da68ab4b2e53)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108857
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/sw/qa/extras/uiwriter/uiwriter.cxx b/sw/qa/extras/uiwriter/uiwriter.cxx
index 21b3e2f..5ea7da5 100644
--- a/sw/qa/extras/uiwriter/uiwriter.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter.cxx
@@ -7598,12 +7598,15 @@ void SwUiWriterTest::testRedlineAutoCorrect()

    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();

    // show tracked deletion
    // show tracked deletion with enabled change tracking
    RedlineFlags const nMode(pWrtShell->GetRedlineFlags() | RedlineFlags::On);
    CPPUNIT_ASSERT(nMode & (RedlineFlags::ShowDelete | RedlineFlags::ShowInsert));
    pWrtShell->SetRedlineFlags(nMode);
    CPPUNIT_ASSERT(nMode & RedlineFlags::ShowDelete);

    CPPUNIT_ASSERT_MESSAGE("redlining should be on",
                           pDoc->getIDocumentRedlineAccess().IsRedlineOn());

    SwAutoCorrect corr(*SvxAutoCorrCfg::Get().GetAutoCorrect());
    pWrtShell->AutoCorrect(corr, ' ');
    sal_uLong nIndex = pWrtShell->GetCursor()->GetNode().GetIndex();
@@ -7630,7 +7633,8 @@ void SwUiWriterTest::testRedlineAutoCorrect()
    nIndex = pWrtShell->GetCursor()->GetNode().GetIndex();

    // This still keep the tracked deletion, capitalize only the visible text "s"
    sReplaced = "tS ";
    // with tracked deletion of the original character
    sReplaced = "tsS ";
    CPPUNIT_ASSERT_EQUAL(sReplaced, static_cast<SwTextNode*>(pDoc->GetNodes()[nIndex])->GetText());

    // repeat it with visible redlining and word auto replacement of "tset"
diff --git a/sw/source/core/edit/acorrect.cxx b/sw/source/core/edit/acorrect.cxx
index a1c06e0..04efdc2 100644
--- a/sw/source/core/edit/acorrect.cxx
+++ b/sw/source/core/edit/acorrect.cxx
@@ -263,17 +263,12 @@ bool SwAutoCorrDoc::ReplaceRange( sal_Int32 nPos, sal_Int32 nSourceLength, const
        }
        else
        {
            if( nSourceLength != rText.getLength() )
            {
                pPam->SetMark();
                pPam->GetPoint()->nContent = std::min<sal_Int32>(
                    pos.first->GetText().getLength(), pos.second + nSourceLength);
                pDoc->getIDocumentContentOperations().ReplaceRange( *pPam, rText, false );
                pPam->Exchange();
                pPam->DeleteMark();
            }
            else
                pDoc->getIDocumentContentOperations().Overwrite( *pPam, rText );
            pPam->SetMark();
            pPam->GetPoint()->nContent = std::min<sal_Int32>(
                pos.first->GetText().getLength(), pos.second + nSourceLength);
            pDoc->getIDocumentContentOperations().ReplaceRange( *pPam, rText, false );
            pPam->Exchange();
            pPam->DeleteMark();
        }

        if( m_bUndoIdInitialized )