1212using System . Linq ;
1313using System . Text ;
1414using System . Xml ;
15+ using SIL . Extensions ;
1516using SIL . LCModel . Core . Cellar ;
1617using SIL . LCModel . Core . KernelInterfaces ;
1718using SIL . LCModel . Core . Text ;
@@ -2920,28 +2921,32 @@ public void SetCloneProperties(ICmObject clone)
29202921 slot . Description . CopyAlternatives ( Description ) ;
29212922 slot . Optional = Optional ;
29222923 // Make copies of Affixes.
2924+ Dictionary < IMoInflAffMsa , IList < IMoMorphSynAnalysis > > oldToNew = new Dictionary < IMoInflAffMsa , IList < IMoMorphSynAnalysis > > ( ) ;
29232925 foreach ( IMoInflAffMsa msa in Affixes )
29242926 {
29252927 if ( msa . Owner is not ILexEntry entry )
29262928 // Shouldn't happen, but just in case...
29272929 continue ;
2930+ IList < IMoMorphSynAnalysis > newMsas = new List < IMoMorphSynAnalysis > ( ) ;
29282931 foreach ( ILexSense sense in entry . SensesOS . ToList ( ) ) {
2929- CopySensesWithMSA ( entry , sense , msa , slot ) ;
2932+ CopySensesWithMSA ( entry , sense , msa , slot , newMsas ) ;
29302933 }
2934+ oldToNew [ msa ] = newMsas ;
29312935 }
2936+ SortNewAffixes ( slot , oldToNew ) ;
29322937 MakeSlotNameUnique ( slot ) ;
29332938 }
29342939
29352940 /// <summary>
29362941 /// Copy sense with new slot if it's MorphoSyntaxAnalysisRA equals msa.
29372942 /// Recurse on the subsenses.
29382943 /// </summary>
2939- internal void CopySensesWithMSA ( ILexEntry entry , ILexSense sense , IMoInflAffMsa msa , IMoInflAffixSlot newSlot )
2944+ internal void CopySensesWithMSA ( ILexEntry entry , ILexSense sense , IMoInflAffMsa msa , IMoInflAffixSlot newSlot , IList < IMoMorphSynAnalysis > newMsas )
29402945 {
29412946 // Recurse on subsense first.
29422947 foreach ( ILexSense subsense in sense . SensesOS . ToList ( ) )
29432948 {
2944- CopySensesWithMSA ( entry , subsense , msa , newSlot ) ;
2949+ CopySensesWithMSA ( entry , subsense , msa , newSlot , newMsas ) ;
29452950 }
29462951 if ( sense . MorphoSyntaxAnalysisRA == msa )
29472952 {
@@ -2950,9 +2955,37 @@ internal void CopySensesWithMSA(ILexEntry entry, ILexSense sense, IMoInflAffMsa
29502955 sandboxMsa . Slot = newSlot ;
29512956 ILexSense newSense = Services . GetInstance < ILexSenseFactory > ( ) . Create ( entry , sandboxMsa , sense . Gloss . BestAnalysisAlternative ) ;
29522957 newSense . Gloss . MergeAlternatives ( sense . Gloss ) ;
2958+ newMsas . Add ( newSense . MorphoSyntaxAnalysisRA ) ;
29532959 }
29542960 }
29552961
2962+ internal void SortNewAffixes ( IMoInflAffixSlot slot , Dictionary < IMoInflAffMsa , IList < IMoMorphSynAnalysis > > oldToNew )
2963+ {
2964+ // Get the old affixes and the new affixes.
2965+ int flid = Cache . DomainDataByFlid . MetaDataCache . GetFieldId2 ( ClassID , "Affixes" , true ) ;
2966+ IEnumerable < ICmObject > oldAffixes = VirtualOrderingServices . GetOrderedValue ( this , flid , Affixes ) ;
2967+ List < ICmObject > newAffixes = slot . Affixes . ToList < ICmObject > ( ) ;
2968+
2969+ // Sort the new affixes using the order of the old affixes.
2970+ Dictionary < ICmObject , int > newToOld = new Dictionary < ICmObject , int > ( ) ;
2971+ foreach ( var oldMsa in oldToNew . Keys )
2972+ {
2973+ foreach ( var newMsa in oldToNew [ oldMsa ] )
2974+ {
2975+ newToOld [ newMsa ] = oldAffixes . IndexOf ( oldMsa ) ;
2976+ }
2977+ }
2978+ newAffixes . Sort ( delegate ( ICmObject x , ICmObject y )
2979+ {
2980+ if ( newToOld [ x ] > newToOld [ y ] ) return 1 ;
2981+ if ( newToOld [ x ] < newToOld [ y ] ) return - 1 ;
2982+ return 0 ;
2983+ } ) ;
2984+
2985+ // Save the new order.
2986+ VirtualOrderingServices . SetVO ( slot , flid , newAffixes ) ;
2987+ }
2988+
29562989 internal void MakeSlotNameUnique ( IMoInflAffixSlot newSlot )
29572990 {
29582991 // Get existing names.
0 commit comments