editeng/source/editeng/impedit2.cxx | 38 ++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-)
New commits: commit 548f0b1883e18ff2870a40bccf047864ff35c18b Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Tue Jan 2 15:29:52 2024 +0900 Commit: Tomaž Vajngerl <qui...@gmail.com> CommitDate: Tue Jan 2 09:56:55 2024 +0100 editeng: preserve ContentNode in a unique_ptr when moving paras When we move the paragraphs, we can just take the unique_ptr of ContetNodes out of the EditDoc and move it to new position inside EditDoc, like it is done for ParaPortions in ParaPortionList. No need to muck with the raw pointers and releasing the ContentNode from a unique_ptr and later moving it back again. Also just use a std::vector for ParaPortions instead of a new instance of ParaPortionList. Change-Id: I8634ccc83121c1ee683be4c2cfb0cedbd469c05d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161531 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> diff --git a/editeng/source/editeng/impedit2.cxx b/editeng/source/editeng/impedit2.cxx index 78a56dc926bc..6e9351a7c80e 100644 --- a/editeng/source/editeng/impedit2.cxx +++ b/editeng/source/editeng/impedit2.cxx @@ -2199,36 +2199,42 @@ EditSelection ImpEditEngine::ImpMoveParagraphs( Range aOldPositions, sal_Int32 n // do not lose sight of the Position ! ParaPortion* pDestPortion = GetParaPortions().SafeGetObject( nNewPos ); - ParaPortionList aTmpPortionList; + // Temporary containers used for moving the paragraph portions and content nodes to a new location + std::vector<std::unique_ptr<ParaPortion>> aParagraphPortionVector; + std::vector<std::unique_ptr<ContentNode>> aContentNodeVector; + + // Take the paragraph portions and content nodes out of its containers for (tools::Long i = aOldPositions.Min(); i <= aOldPositions.Max(); i++ ) { - // always aOldPositions.Min(), since Remove(). - std::unique_ptr<ParaPortion> pTmpPortion = GetParaPortions().Release(aOldPositions.Min()); - auto pContentNode = maEditDoc.Release(aOldPositions.Min()); - pContentNode.release(); - aTmpPortionList.Append(std::move(pTmpPortion)); + // always aOldPositions.Min() as the index, since we remove and the elements from the containers and the + // other elements shift to the left. + std::unique_ptr<ParaPortion> pPortion = GetParaPortions().Release(aOldPositions.Min()); + aParagraphPortionVector.push_back(std::move(pPortion)); + + std::unique_ptr<ContentNode> pContentNode = maEditDoc.Release(aOldPositions.Min()); + aContentNodeVector.push_back(std::move(pContentNode)); } + // Determine the new location for paragraphs sal_Int32 nRealNewPos = pDestPortion ? GetParaPortions().GetPos( pDestPortion ) : GetParaPortions().Count(); assert( nRealNewPos != EE_PARA_NOT_FOUND && "ImpMoveParagraphs: Invalid Position!" ); + // Add the paragraph portions and content nodes to a new position sal_Int32 i = 0; - while( aTmpPortionList.Count() > 0 ) + for (auto& pPortion : aParagraphPortionVector) { - std::unique_ptr<ParaPortion> pTmpPortion = aTmpPortionList.Release(0); - if ( i == 0 ) - aSelection.Min().SetNode( pTmpPortion->GetNode() ); - - aSelection.Max().SetNode( pTmpPortion->GetNode() ); - aSelection.Max().SetIndex( pTmpPortion->GetNode()->Len() ); + if (i == 0) + aSelection.Min().SetNode(pPortion->GetNode()); + aSelection.Max().SetNode(pPortion->GetNode()); + aSelection.Max().SetIndex(pPortion->GetNode()->Len()); - ContentNode* pNode = pTmpPortion->GetNode(); - maEditDoc.Insert(nRealNewPos+i, std::unique_ptr<ContentNode>(pNode)); + maEditDoc.Insert(nRealNewPos + i, std::move(aContentNodeVector[i])); + GetParaPortions().Insert(nRealNewPos + i, std::move(pPortion)); - GetParaPortions().Insert(nRealNewPos+i, std::move(pTmpPortion)); ++i; } + // Signal end of paragraph moving maEndMovingParagraphsHdl.Call( aMoveParagraphsInfo ); if ( GetNotifyHdl().IsSet() )