Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var tree = CSharpSyntaxTree.ParseText(
                @"using System;
using System.Collection.Generic;
using System.Text;
namespace HelloWorld {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(""Hello, world!"");
        }
    }
}"
                );

            var root = (CompilationUnitSyntax)tree.GetRoot();

            var location  = typeof(object).Assembly.Location;
            var reference = MetadataReference.CreateFromFile(location);

            var compilation = CSharpCompilation.Create("HelloWorld")
                              .AddReferences(reference)
                              .AddSyntaxTrees(tree);

            var model        = compilation.GetSemanticModel(tree);
            var nameInfo     = model.GetSymbolInfo(root.Usings[0].Name);
            var systemSymbol = (INamespaceSymbol)nameInfo.Symbol;

            foreach (var ns in systemSymbol.GetNamespaceMembers())
            {
                Console.WriteLine(ns.Name);
            }


            var helloWorldString = root.DescendantNodes()
                                   .OfType <LiteralExpressionSyntax>()
                                   .First();

            var literalInfo = model.GetTypeInfo(helloWorldString);

            var stringTypeSymbol = (INamedTypeSymbol)literalInfo.Type;
            var names            =
                (from method in stringTypeSymbol.GetMembers().OfType <IMethodSymbol>()
                 where method.ReturnType.Equals(stringTypeSymbol) &&
                 method.DeclaredAccessibility == Accessibility.Public
                 select method.Name).Distinct();

            foreach (var name in names)
            {
                Console.WriteLine(name);
            }

            var rq = RQName.From(stringTypeSymbol);

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private Task <bool> RenameAsync(string oldFilePath, string newFilePath, bool isCaseSensitive, string oldName, string newName, ISymbol?symbol, string renameOperationName, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            return(_unconfiguredProjectTasksService.LoadedProjectAsync(async() =>
            {
                // Perform the rename operation
                Solution?renamedSolution = await GetRenamedSolutionAsync(oldName, oldFilePath, newFilePath, isCaseSensitive, GetCurrentProject, token);
                if (renamedSolution is null)
                {
                    return false;
                }

                string rqName = RQName.From(symbol);
                Solution?solution = GetCurrentProject()?.Solution;
                if (solution is null)
                {
                    return false;
                }

                IEnumerable <ProjectChanges> changes = renamedSolution.GetChanges(solution).GetProjectChanges();

                DTE?dte = _dte.Value;

                Assumes.NotNull(dte);

                using var undo = UndoScope.Create(dte, renameOperationName);

                // Notify other VS features that symbol is about to be renamed
                NotifyBeforeRename(newName, rqName, changes);

                // Try and apply the changes to the current solution
                token.ThrowIfCancellationRequested();

                if (!_roslynServices.ApplyChangesToSolution(renamedSolution.Workspace, renamedSolution))
                {
                    return false;
                }

                // Notify other VS features that symbol has been renamed
                NotifyAfterRename(newName, rqName, changes);
                return true;
            }));
        }
Ejemplo n.º 3
0
        internal async Task HandleRenameAsync(string oldFilePath, string newFilePath)
        {
            // Do not offer to rename types if the user changes the file extensions
            if (!oldFilePath.EndsWith(Path.GetExtension(newFilePath), StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

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

            // see if the current project contains a compilation
            (bool success, bool isCaseSensitive) = await TryDetermineIfCompilationIsCaseSensitiveAsync(GetCurrentProject());

            if (!success)
            {
                return;
            }

            // Check that the new name is a valid identifier in the current programming language
            string oldName = Path.GetFileNameWithoutExtension(oldFilePath);
            string newName = Path.GetFileNameWithoutExtension(newFilePath);

            if (!CanHandleRename(oldName, newName, isCaseSensitive))
            {
                return;
            }

            // Check if there are any symbols that need to be renamed
            ISymbol?symbol = await TryGetSymbolToRename(oldName, oldFilePath, newFilePath, isCaseSensitive, GetCurrentProject);

            if (symbol is null)
            {
                return;
            }

            // Ask if the user wants to rename the symbol
            bool userConfirmed = await CheckUserConfirmation(oldName);

            if (!userConfirmed)
            {
                return;
            }

            // Try and apply the changes to the current solution
            await _projectVsServices.ThreadingService.SwitchToUIThread();

            string renameOperationName = string.Format(CultureInfo.CurrentCulture, VSResources.Renaming_Type_from_0_to_1, oldName, newName);

            (WaitIndicatorResult result, bool renamedSolutionApplied) = _waitService.WaitForAsyncFunctionWithResult(
                title: VSResources.Renaming_Type,
                message: renameOperationName,
                allowCancel: true,
                async token =>
            {
                token.ThrowIfCancellationRequested();
                return(await _unconfiguredProjectTasksService.LoadedProjectAsync(async() =>
                {
                    // Perform the rename operation
                    Solution?renamedSolution = await GetRenamedSolutionAsync(oldName, oldFilePath, newFilePath, isCaseSensitive, GetCurrentProject, token);
                    if (renamedSolution is null)
                    {
                        return false;
                    }

                    string rqName = RQName.From(symbol);
                    Solution?solution = GetCurrentProject()?.Solution;
                    if (solution is null)
                    {
                        return false;
                    }

                    IEnumerable <ProjectChanges> changes = renamedSolution.GetChanges(solution).GetProjectChanges();

                    using UndoScope undo = await UndoScope.CreateAsync(_dte, _projectVsServices.ThreadingService, renameOperationName, token);

                    // Notify other VS features that symbol is about to be renamed
                    await NotifyBeforeRename(newName, rqName, changes);

                    // Try and apply the changes to the current solution
                    token.ThrowIfCancellationRequested();
                    bool applyChangesSucceeded = _roslynServices.ApplyChangesToSolution(renamedSolution.Workspace, renamedSolution);

                    if (applyChangesSucceeded)
                    {
                        // Notify other VS features that symbol has been renamed
                        await NotifyAfterRename(newName, rqName, changes);
                    }
                    return applyChangesSucceeded;
                }));
            });

            // Do not warn the user if the rename was cancelled by the user
            if (result.WasCanceled())
            {
                return;
            }

            // Notify the user if the rename could not be performed
            if (!renamedSolutionApplied)
            {
                string failureMessage = string.Format(CultureInfo.CurrentCulture, VSResources.RenameSymbolFailed, oldName);
                _userNotificationServices.ShowWarning(failureMessage);
            }

            Project?GetCurrentProject()
            {
                foreach (Project proj in _workspace.CurrentSolution.Projects)
                {
                    if (StringComparers.Paths.Equals(proj.FilePath, _projectVsServices.Project.FullPath))
                    {
                        return(proj);
                    }
                }

                return(null);
            }
        }