Ejemplo n.º 1
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);
            });
        }
Ejemplo n.º 2
0
        public async void Execute(ISolution solution, ITextControl textControl)
        {
            if (this.ExistingProjectFile != null)
            {
                await ShowProjectFile(solution, this.ExistingProjectFile, null);

                return;
            }

            using (ReadLockCookie.Create())
            {
                string         testClassName;
                string         testNamespace;
                string         testFileName;
                IProjectFolder testFolder;
                Template       testFileTemplate;

                using (var cookie = solution.CreateTransactionCookie(DefaultAction.Rollback, this.Text, NullProgressIndicator.Create()))
                {
                    var declaration = this._provider.GetSelectedElement <ICSharpTypeDeclaration>();

                    var declaredType = declaration?.DeclaredElement;
                    if (declaredType == null)
                    {
                        return;
                    }

                    var settingsStore  = declaration.GetSettingsStore();
                    var helperSettings = ReSharperHelperSettings.GetSettings(settingsStore);

                    var testProject = this.CachedTestProject ?? this.ResolveTargetTestProject(declaration, solution, helperSettings);
                    if (testProject == null)
                    {
                        return;
                    }

                    var classNamespaceParts = TrimDefaultProjectNamespace(declaration.GetProject().NotNull(), declaredType.GetContainingNamespace().QualifiedName);
                    var testFolderLocation  = classNamespaceParts.Aggregate(testProject.Location, (current, part) => current.Combine(part));

                    testNamespace = StringUtil.MakeFQName(testProject.GetDefaultNamespace(), StringUtil.MakeFQName(classNamespaceParts));

                    testFolder = testProject.GetOrCreateProjectFolder(testFolderLocation, cookie);
                    if (testFolder == null)
                    {
                        return;
                    }

                    testClassName = declaredType.ShortName + helperSettings.TestClassNameSuffix;
                    testFileName  = testClassName + ".cs";

                    testFileTemplate = StoredTemplatesProvider.Instance.EnumerateTemplates(settingsStore, TemplateApplicability.File).FirstOrDefault(t => t.Description == TemplateDescription);

                    cookie.Commit(NullProgressIndicator.Create());
                }

                if (testFileTemplate != null)
                {
                    await FileTemplatesManager.Instance.CreateFileFromTemplateAsync(testFileName, new ProjectFolderWithLocation(testFolder), testFileTemplate);

                    return;
                }

                var newFile = AddNewItemHelper.AddFile(testFolder, testFileName);
                if (newFile == null)
                {
                    return;
                }

                int?caretPosition = -1;
                solution.GetPsiServices()
                .Transactions.Execute(this.Text, () =>
                {
                    var psiSourceFile = newFile.ToSourceFile();

                    var csharpFile = psiSourceFile?.GetDominantPsiFile <CSharpLanguage>() as ICSharpFile;
                    if (csharpFile == null)
                    {
                        return;
                    }

                    var elementFactory = CSharpElementFactory.GetInstance(csharpFile);

                    var namespaceDeclaration = elementFactory.CreateNamespaceDeclaration(testNamespace);
                    var addedNs = csharpFile.AddNamespaceDeclarationAfter(namespaceDeclaration, null);

                    var classLikeDeclaration = (IClassLikeDeclaration)elementFactory.CreateTypeMemberDeclaration("public class $0 {}", testClassName);
                    var addedTypeDeclaration = addedNs.AddTypeDeclarationAfter(classLikeDeclaration, null) as IClassDeclaration;

                    caretPosition = addedTypeDeclaration?.Body?.GetDocumentRange().TextRange.StartOffset + 1;
                });

                await ShowProjectFile(solution, newFile, caretPosition);
            }
        }