private async Task <Solution> GetRenamedSolutionAsync(Project myNewProject, string oldFileName, string newFileName)
        {
            var      project         = myNewProject;
            Solution renamedSolution = null;
            string   oldNameBase     = Path.GetFileNameWithoutExtension(oldFileName);

            while (project != null)
            {
                var newDocument = GetDocument(project, newFileName);
                if (newDocument == null)
                {
                    return(renamedSolution);
                }

                var root = await GetRootNode(newDocument).ConfigureAwait(false);

                if (root == null)
                {
                    return(renamedSolution);
                }

                var declarations = root.DescendantNodes().Where(n => HasMatchingSyntaxNode(newDocument, n, oldNameBase));
                var declaration  = declarations.FirstOrDefault();
                if (declaration == null)
                {
                    return(renamedSolution);
                }

                var semanticModel = await newDocument.GetSemanticModelAsync().ConfigureAwait(false);

                if (semanticModel == null)
                {
                    return(renamedSolution);
                }

                var symbol = semanticModel.GetDeclaredSymbol(declaration);
                if (symbol == null)
                {
                    return(renamedSolution);
                }

                bool userConfirmed = await CheckUserConfirmation(oldFileName).ConfigureAwait(false);

                if (!userConfirmed)
                {
                    return(renamedSolution);
                }

                string newName = Path.GetFileNameWithoutExtension(newDocument.FilePath);

                // Note that RenameSymbolAsync will return a new snapshot of solution.
                renamedSolution = await _roslynServices.RenameSymbolAsync(newDocument.Project.Solution, symbol, newName).ConfigureAwait(false);

                project = renamedSolution.Projects.Where(p => StringComparers.Paths.Equals(p.FilePath, myNewProject.FilePath)).FirstOrDefault();
            }
            return(null);
        }
Beispiel #2
0
        private async Task <Solution?> GetRenamedSolutionAsync(string oldName, string oldFileName, string newFileName, bool isCaseSensitive, Project?project, CancellationToken token = default)
        {
            if (project is null)
            {
                return(null);
            }

            ISymbol?symbolToRename = await TryGetSymbolToRename(oldName, oldFileName, newFileName, isCaseSensitive, project, token);

            if (symbolToRename is null)
            {
                return(null);
            }

            string newName = Path.GetFileNameWithoutExtension(newFileName);

            Solution?renamedSolution = await _roslynServices.RenameSymbolAsync(project.Solution, symbolToRename, newName, token);

            return(renamedSolution);
        }
Beispiel #3
0
        private async Task <Solution?> GetRenamedSolutionAsync(string oldName, string oldFileName, string newFileName, bool isCaseSensitive, Func <Project?> getProject, CancellationToken token)
        {
            ISymbol?symbolToRename = await TryGetSymbolToRename(oldName, oldFileName, newFileName, isCaseSensitive, getProject, token);

            if (symbolToRename is null)
            {
                return(null);
            }

            string newName = Path.GetFileNameWithoutExtension(newFileName);

            Solution?solution = getProject()?.Solution;

            if (solution is null)
            {
                return(null);
            }

            Solution?renamedSolution = await _roslynServices.RenameSymbolAsync(solution, symbolToRename, newName, token);

            return(renamedSolution);
        }
Beispiel #4
0
        public async Task RenameAsync(Project myNewProject)
        {
            Document newDocument = (from d in myNewProject.Documents where StringComparers.Paths.Equals(d.FilePath, _newFilePath) select d).FirstOrDefault();

            if (newDocument == null)
            {
                return;
            }

            var root = await newDocument.GetSyntaxRootAsync().ConfigureAwait(false);

            if (root == null)
            {
                return;
            }

            string oldName = Path.GetFileNameWithoutExtension(_oldFilePath);
            string newName = Path.GetFileNameWithoutExtension(newDocument.FilePath);

            var declaration = root.DescendantNodes().Where(n => HasMatchingSyntaxNode(newDocument, n, oldName)).FirstOrDefault();

            if (declaration == null)
            {
                return;
            }

            var semanticModel = await newDocument.GetSemanticModelAsync().ConfigureAwait(false);

            if (semanticModel == null)
            {
                return;
            }

            var symbol = semanticModel.GetDeclaredSymbol(declaration);

            if (symbol == null)
            {
                return;
            }

            var userConfirmed = true;
            await _threadingService.SwitchToUIThread();

            var userNeedPrompt = _optionsSettings.GetPropertiesValue("Environment", "ProjectsAndSolution", "PromptForRenameSymbol", false);

            if (userNeedPrompt)
            {
                string renamePromptMessage = string.Format(Resources.RenameSymbolPrompt, oldName);

                await _threadingService.SwitchToUIThread();

                userConfirmed = _userNotificationServices.Confirm(renamePromptMessage);
            }

            if (userConfirmed)
            {
                var renamedSolution = await _roslynServices.RenameSymbolAsync(newDocument.Project.Solution, symbol, newName).ConfigureAwait(false);

                await _threadingService.SwitchToUIThread();

                var renamedSolutionApplied = _roslynServices.ApplyChangesToSolution(newDocument.Project.Solution.Workspace, renamedSolution);

                if (!renamedSolutionApplied)
                {
                    string failureMessage = string.Format(Resources.RenameSymbolFailed, oldName);
                    await _threadingService.SwitchToUIThread();

                    _userNotificationServices.NotifyFailure(failureMessage);
                }
            }
        }