private static Tuple <ITypeElement, ITypeElement> GetLinkedTypeWithDerivedName(LinkedTypesService linkedTypesService, ITypeElement sourceType) { return(linkedTypesService.GetLinkedTypes(sourceType) .Where(x => DerivedNameUtility.IsDerivedNameAny(sourceType.ShortName, x.ShortName)) .Select(x => Tuple.Create(sourceType, x)) .FirstOrDefault()); }
public static void TryCreateTestOrProductionClass(ITypeElement sourceType, ITextControl textControl) { var solution = sourceType.GetSolution(); var linkedTypesService = solution.GetComponent <LinkedTypesService>(); var typesNearCaretOrFile = solution.GetComponent <ITypesFromTextControlService>().GetTypesNearCaretOrFile(textControl, solution); var templateTypes = typesNearCaretOrFile.Select(x => GetLinkedTypeWithDerivedName(linkedTypesService, x)).WhereNotNull().FirstOrDefault(); if (templateTypes == null) { MessageBox.ShowInfo( "Could not find a template to create production/test class from.\r\n" + "There must exist at least one pair of production+test classes for this project."); return; } var templateSourceType = templateTypes.Item1; var templateLinkedType = templateTypes.Item2; var linkedTypeName = DerivedNameUtility.GetDerivedName(sourceType.ShortName, templateSourceType.ShortName, templateLinkedType.ShortName); var linkedTypeNamespace = DerivedNameUtility.GetDerivedNamespace(sourceType, templateLinkedType); var linkedTypeProject = templateLinkedType.GetSingleOrDefaultSourceFile().GetProject().NotNull(); var linkedTypeKind = !solution.GetComponent <IUnitTestElementStuff>() .IsElementOfKind(templateLinkedType, UnitTestElementKind.TestContainer) ? TypeKind.Production : TypeKind.Test; if (!MessageBox.ShowYesNo( $"Class: {linkedTypeName}\r\nProject: {linkedTypeProject.Name}\r\nNamespace: {linkedTypeNamespace}\r\n", $"Create {linkedTypeKind} Class for {sourceType.ShortName}?")) { return; } var linkedTypeProjectFolder = GetLinkedTypeFolder(linkedTypeNamespace, linkedTypeProject); var linkedTypeFile = GetLinkedTypeFile(linkedTypeName, linkedTypeNamespace, templateLinkedType); var linkedTypeProjectFile = AddNewItemHelper.AddFile(linkedTypeProjectFolder, linkedTypeName + ".cs", linkedTypeFile.GetText()); linkedTypeProjectFile.Navigate(Shell.Instance.GetComponent <IMainWindowPopupWindowContext>().Source, transferFocus: true); }