Esempio n. 1
0
 private static Tuple <ITypeElement, ITypeElement> GetLinkedTypeWithDerivedName(ITypeElement sourceType)
 {
     return(LinkedTypesUtil.GetLinkedTypes(sourceType)
            .Where(x => DerivedNameUtility.IsDerivedNameAny(sourceType.ShortName, x.ShortName))
            .Select(x => Tuple.Create(sourceType, x))
            .FirstOrDefault());
 }
Esempio n. 2
0
        public static void TryCreateTestOrProductionClass(ITypeElement sourceType, ITextControl textControl)
        {
            var solution = sourceType.GetSolution();

            var typesNearCaretOrFile = solution.GetComponent <ITypesFromTextControlService>().GetTypesNearCaretOrFile(textControl, solution);
            var templateTypes        = typesNearCaretOrFile.Select(GetLinkedTypeWithDerivedName).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 linkedTypeSourceFile = templateLinkedType.GetSingleOrDefaultSourceFile().NotNull("linkedTypeSourceFile != null");
            var linkedTypeProject    = linkedTypeSourceFile.GetProject().NotNull("linkedTypeProject != null");
            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 threading = solution.GetComponent <IThreading>();

            threading.ExecuteOrQueueEx(
                nameof(TryCreateTestOrProductionClass),
                () =>
            {
                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);
            });
        }