Skip to content

Commit 689e671

Browse files
committed
Add code to make copied names unique
1 parent ed0160f commit 689e671

File tree

1 file changed

+97
-2
lines changed

1 file changed

+97
-2
lines changed

src/SIL.LCModel/DomainImpl/OverridesLing_MoClasses.cs

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,6 +2731,26 @@ public void SetCloneProperties(ICmObject clone)
27312731
{
27322732
template.StratumRA = StratumRA;
27332733
}
2734+
MakeTemplateNameUnique(template);
2735+
}
2736+
2737+
internal void MakeTemplateNameUnique(IMoInflAffixTemplate newTemplate)
2738+
{
2739+
// Get existing names.
2740+
ISet<string> names = new HashSet<string>();
2741+
IEnumerable<ICmObject> templates = Services.ObjectRepository.AllInstances(newTemplate.ClassID);
2742+
foreach (IMoInflAffixTemplate template in templates)
2743+
{
2744+
if (template == newTemplate)
2745+
continue;
2746+
for (int i = 0; i < template.Name.StringCount; i++)
2747+
{
2748+
ITsString tss = template.Name.GetStringFromIndex(i, out _);
2749+
names.Add(tss.Text);
2750+
}
2751+
}
2752+
// Make newSlot name unique.
2753+
MoInflAffixSlot.MakeNameUnique(newTemplate.Name, names);
27342754
}
27352755
#endregion
27362756
}
@@ -2897,9 +2917,84 @@ public void SetCloneProperties(ICmObject clone)
28972917
slot.Name.CopyAlternatives(Name);
28982918
slot.Description.CopyAlternatives(Description);
28992919
slot.Optional = Optional;
2900-
foreach (IMoInflAffMsa affix in Affixes)
2920+
// Make copies of Affixes.
2921+
foreach (IMoInflAffMsa msa in Affixes)
2922+
{
2923+
if (msa.Owner is not ILexEntry entry)
2924+
continue;
2925+
foreach (ILexSense sense in entry.SensesOS.ToList()) {
2926+
CopySensesWithMSA(entry, sense, msa, slot);
2927+
}
2928+
}
2929+
MakeSlotNameUnique(slot);
2930+
}
2931+
2932+
/// <summary>
2933+
/// Copy sense with new slot if it's MorphoSyntaxAnalysisRA equals msa.
2934+
/// Recurse on the subsenses.
2935+
/// </summary>
2936+
internal void CopySensesWithMSA(ILexEntry entry, ILexSense sense, IMoInflAffMsa msa, IMoInflAffixSlot newSlot)
2937+
{
2938+
// Recurse on subsense first.
2939+
foreach (ILexSense subsense in sense.SensesOS.ToList())
2940+
{
2941+
CopySensesWithMSA(entry, subsense, msa, newSlot);
2942+
}
2943+
if (sense.MorphoSyntaxAnalysisRA == msa)
29012944
{
2902-
affix.SlotsRC.Add(slot);
2945+
// Create a new sense.
2946+
SandboxGenericMSA sandboxMsa = SandboxGenericMSA.Create(msa);
2947+
sandboxMsa.Slot = newSlot;
2948+
ILexSense newSense = Services.GetInstance<ILexSenseFactory>().Create(entry, sandboxMsa, sense.Gloss.BestAnalysisAlternative);
2949+
newSense.Gloss.MergeAlternatives(sense.Gloss);
2950+
}
2951+
}
2952+
2953+
internal void MakeSlotNameUnique(IMoInflAffixSlot newSlot)
2954+
{
2955+
// Get existing names.
2956+
ISet<string> names = new HashSet<string>();
2957+
IEnumerable<ICmObject> slots = Services.ObjectRepository.AllInstances(newSlot.ClassID);
2958+
foreach (IMoInflAffixSlot slot in slots)
2959+
{
2960+
if (slot == newSlot)
2961+
continue;
2962+
for (int i = 0; i < slot.Name.StringCount; i++)
2963+
{
2964+
ITsString tss = slot.Name.GetStringFromIndex(i, out _);
2965+
names.Add(tss.Text);
2966+
}
2967+
}
2968+
// Make newSlot name unique.
2969+
MakeNameUnique(newSlot.Name, names);
2970+
}
2971+
2972+
static internal void MakeNameUnique(IMultiUnicode name, ISet<string> names)
2973+
{
2974+
for (int i = 0; i < name.StringCount; i++)
2975+
{
2976+
int ws;
2977+
ITsString tss = name.GetStringFromIndex(i, out ws);
2978+
string text = tss.Text;
2979+
// Get existing id.
2980+
int id = 1;
2981+
int idStart = text.Length;
2982+
while (idStart > 0 && Char.IsNumber(text[idStart - 1]))
2983+
idStart = idStart - 1;
2984+
if (idStart < text.Length)
2985+
id = Int32.Parse(text.Substring(idStart));
2986+
// Look for a unique name.
2987+
while (true)
2988+
{
2989+
id += 1;
2990+
string newName = text.Substring(0, idStart) + id.ToString();
2991+
if (!names.Contains(newName))
2992+
{
2993+
// Replace name[i] with unique name.
2994+
name.set_String(ws, newName);
2995+
break;
2996+
}
2997+
}
29032998
}
29042999
}
29053000
#endregion

0 commit comments

Comments
 (0)