protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var projectFile = myLiteral.GetSourceFile().ToProjectFile();

            if (projectFile == null)
            {
                return(null);
            }

            var newName = myLiteral.GetUnquotedText() + ".asmdef";

            return(_ =>
            {
                if (projectFile.Location.Directory.Combine(newName).ExistsFile)
                {
                    MessageBox.ShowError($"File '{newName}' already exists",
                                         $"Cannot rename '{projectFile.Location.Name}'");
                }
                else
                {
                    using (var transactionCookie = solution.CreateTransactionCookie(DefaultAction.Commit, Text,
                                                                                    NullProgressIndicator.Instance))
                    {
                        transactionCookie.Rename(projectFile, newName);
                    }
                }
            });
        }
Exemple #2
0
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var projectFile = (IProjectFile)_highlight.OffendingProjectItem;

            using (var cookie = solution.CreateTransactionCookie(DefaultAction.Rollback, this.GetType().Name, progress))
            {
                IProjectFolder newFolder = (IProjectFolder)_highlight.TargetProject.FindProjectItemByLocation(_highlight.TargetFolder)
                                           ?? _highlight.TargetProject.GetOrCreateProjectFolder(_highlight.TargetFolder, cookie);

                var            workflow     = new MoveToFolderWorkflow(solution, "ManualMoveToFolderQuickFix");
                IProjectFolder targetFolder = newFolder ?? _highlight.TargetProject;

                var dataProvider = new MoveToFolderDataProvider(true, false, targetFolder, new List <string>(), new List <string>());
                workflow.SetDataProvider(dataProvider);

                Lifetimes.Using(
                    (lifetime => WorkflowExecuter.ExecuteWithCustomHost(
                         Shell.Instance.GetComponent <IActionManager>()
                         .DataContexts.CreateWithoutDataRules(lifetime
                                                              , DataRules.AddRule(DataRules.AddRule("ManualMoveToFolderQuickFix"
                                                                                                    , JetBrains.ProjectModel.DataContext.ProjectModelDataConstants.PROJECT_MODEL_ELEMENTS, new IProjectModelElement[] { projectFile })
                                                                                  , "ManualMoveToFolderQuickFix"
                                                                                  , JetBrains.ProjectModel.DataContext.ProjectModelDataConstants.SOLUTION, solution))
                         , workflow, new SimpleWorkflowHost())));

                cookie.Commit(progress);
            }

            return(null);
        }
        private static bool TryGenerateFile(
            ISolution solution,
            IProjectFile sourceFile,
            string filePath,
            string generatedText,
            out IProjectFile generatedFile)
        {
            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator);
                using (transaction)
                {
                    if (!TryCreateFile(filePath, solution, sourceFile, transaction, out generatedFile))
                    {
                        return(false);
                    }

                    IDocument generatedDocument = generatedFile.GetDocument();
                    generatedDocument.ReplaceText(generatedDocument.DocumentRange, generatedText);

                    ExecutionHelpers.OrginizeUsingsAndFormatFile(solution, generatedDocument);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
        private static bool TryGenerateFiles(
            ISolution solution,
            List <FileGeneratorOutput> fileGeneratorOutputs,
            List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs)
        {
            if (!TryProcessFilePaths(solution, fileGeneratorOutputs, out List <FileGeneratorOutput> notFoundProjectFolderOutputs))
            {
                return(false);
            }

            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating files", progressIndicator);
                using (transaction)
                {
                    if (!TryCreateProjectFolders(notFoundProjectFolderOutputs, transaction))
                    {
                        return(false);
                    }

                    if (!TryCreateFiles(fileGeneratorOutputs, transaction))
                    {
                        return(false);
                    }

                    MakeDocumentChanges(solution, fileGeneratorOutputs, inPlaceGeneratorOutputs);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            using (var cookie = solution.CreateTransactionCookie(DefaultAction.Rollback, this.GetType().Name, progress))
            {
                foreach (var file in _files)
                {
                    _currentProject.AddExistenFile(FileSystemPath.Parse(file.FullName), cookie);
                }
                cookie.Commit(progress);
            }

            return(null);
        }
Exemple #6
0
        // To fix offset issue had to implement ExecuteAfterPsiTransaction.
        // http://stackoverflow.com/questions/22545558/resharper-quickfix-highlighter-offset-issue
        protected override Action <ITextControl> ExecuteAfterPsiTransaction(ISolution solution,
                                                                            IProjectModelTransactionCookie cookie, IProgressIndicator progress)
        {
            DocumentRange docRange = _highlighter.Declaration.GetDocumentRange();
            IDocument     textDoc  = _highlighter.Declaration.GetSourceFile().Document;

            if (textDoc != null)
            {
                for (int i = 0; i < textDoc.GetTextLength(); i++)
                {
                    int    docRangeEndOffSet = docRange.TextRange.EndOffset + i;
                    int    docRangeStart     = docRange.TextRange.StartOffset;
                    string docRangeString    = docRange.Document.GetText(new TextRange(docRange.TextRange.StartOffset, docRangeEndOffSet));

                    int location;
                    if (docRangeString.EndsWith("\r\n"))
                    {
                        var lineEnd = docRangeString.LastIndexOf(";\r\n");
                        if (lineEnd >= 0)
                        {
                            location = docRangeStart + lineEnd + 1;
                            using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                            {
                                textDoc.InsertText(location, " // Not L10N");
                            }
                            break;
                        }
                        location = docRangeEndOffSet - 2;
                        using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                        {
                            textDoc.InsertText(location, " // Not L10N");
                        }
                        break;
                    }
                }
            }
            return(null);
        }
        protected override bool TryExecute(IInPlaceGenerator generator, GeneratorExecutionHost executionHost)
        {
            IDocument document = generator.DataProvider.Document;

            bool executed = ExecutionHelpers.TryExecuteGenerator(
                generator,
                out string generatedText,
                out TextRange rangeToDelete,
                out int positionToInsert,
                out IReadOnlyCollection <IUsingDirective> missingUsingDirectives
                );

            if (!executed)
            {
                return(false);
            }

            ISolution solution = generator.DataProvider.Solution;

            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator);
                using (transaction)
                {
                    if (!rangeToDelete.IsEmpty)
                    {
                        document.DeleteText(rangeToDelete);
                    }

                    document.InsertText(positionToInsert, generatedText);

                    var treeTextRange = TreeTextRange.FromLength(new TreeOffset(positionToInsert), generatedText.Length);
                    ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, missingUsingDirectives);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
        public bool ShouldSuppress(IDocument document, bool forceSaveOpenDocuments)
        {
            if (!forceSaveOpenDocuments)
            {
                return(false);
            }

            var projectFile = myDocumentToProjectFileMappingStorage.TryGetProjectFile(document);
            var isUnitySharedProjectFile = projectFile != null &&
                                           myUnitySolutionTracker.IsUnityGeneratedProject.Value &&
                                           projectFile.IsShared();

            if (isUnitySharedProjectFile)
            {
                if (!IsFileAssociatedWithOpenedEditor(document))
                {
                    return(true);
                }

                mySolution.Locks.ExecuteOrQueueWithWriteLockWhenAvailableEx(Lifetime.Eternal, "Sync Unity shared files", () =>
                {
                    using (mySolution.CreateTransactionCookie(DefaultAction.Commit, "Sync Unity shared files"))
                    {
                        var text = projectFile.GetDocument().GetText();
                        foreach (var sharedProjectFile in projectFile.GetSharedProjectFiles())
                        {
                            if (sharedProjectFile == projectFile)
                            {
                                continue;
                            }
                            sharedProjectFile.GetDocument().SetText(text);
                        }
                    }
                });

                myLogger.Verbose("File is shared and contained in Unity project. Skip saving.");
                return(true);
            }

            return(false);
        }
        // To fix offset issue had to implement ExecuteAfterPsiTransaction.
        // http://stackoverflow.com/questions/22545558/resharper-quickfix-highlighter-offset-issue
        protected override Action<ITextControl> ExecuteAfterPsiTransaction(ISolution solution,
            IProjectModelTransactionCookie cookie, IProgressIndicator progress)
        {
            DocumentRange docRange = _highlighter.Declaration.GetDocumentRange();
            IDocument textDoc = _highlighter.Declaration.GetSourceFile().Document;

            if (textDoc != null)
            {
                for (int i = 0; i < textDoc.GetTextLength(); i++)
                {
                    int docRangeEndOffSet = docRange.TextRange.EndOffset + i;
                    int docRangeStart = docRange.TextRange.StartOffset;
                    string docRangeString = docRange.Document.GetText(new TextRange(docRange.TextRange.StartOffset, docRangeEndOffSet));

                    int location;
                    if (docRangeString.EndsWith("\r\n"))
                    {
                        var lineEnd = docRangeString.LastIndexOf(";\r\n");
                        if (lineEnd >= 0)
                        {
                            location = docRangeStart + lineEnd + 1;
                            using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                            {
                                textDoc.InsertText(location, " // Not L10N");
                            }
                            break;
                        }
                        location = docRangeEndOffSet - 2;
                        using (solution.CreateTransactionCookie(DefaultAction.Commit, "L10N insterted"))
                        {
                            textDoc.InsertText(location, " // Not L10N");
                        }
                        break;
                    }
                }
            }
            return null;
        }
Exemple #10
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);
            }
        }
Exemple #11
0
 protected override void RenameFile(ISolution solution, IProjectFile projectFile, string newFileName)
 {
     using (var transactionCookie = solution.CreateTransactionCookie(DefaultAction.Commit, commandName: Text))
         transactionCookie.Rename(projectFile, newFileName);
 }