Exemplo n.º 1
0
        private async Task <bool> FixHierarchyContentAsync(IVsHierarchyCodeCleanupScope hierarchyContent, ICodeCleanUpExecutionContext context, CancellationToken cancellationToken)
        {
            var hierarchy = hierarchyContent.Hierarchy;

            if (hierarchy == null)
            {
                return(await FixSolutionAsync(_workspace.CurrentSolution, context, cancellationToken).ConfigureAwait(true));
            }

            var itemId = hierarchyContent.ItemId;

            if (itemId == (uint)VSConstants.VSITEMID.Root)
            {
                // Map the hierarchy to a ProjectId. For hierarchies mapping to multitargeted projects, we first try to
                // get the project in the most recent active context, but fall back to the first target framework if no
                // active context is available.
                var hierarchyToProjectMap = _workspace.Services.GetRequiredService <IHierarchyItemToProjectIdMap>();

                await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();

                ProjectId projectId = null;
                if (ErrorHandler.Succeeded(hierarchy.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID8.VSHPROPID_ActiveIntellisenseProjectContext, out var contextProjectNameObject)) &&
                    contextProjectNameObject is string contextProjectName)
                {
                    projectId = _workspace.GetProjectWithHierarchyAndName(hierarchy, contextProjectName)?.Id;
                }

                if (projectId is null)
                {
                    var projectHierarchyItem = _vsHierarchyItemManager.GetHierarchyItem(hierarchyContent.Hierarchy, itemId);
                    if (!hierarchyToProjectMap.TryGetProjectId(projectHierarchyItem, targetFrameworkMoniker: null, out projectId))
                    {
                        return(false);
                    }
                }

                await TaskScheduler.Default;

                var project = _workspace.CurrentSolution.GetProject(projectId);
                if (project == null)
                {
                    return(false);
                }

                return(await FixProjectAsync(project, context, cancellationToken).ConfigureAwait(true));
            }
            else if (hierarchy.GetCanonicalName(itemId, out var path) == 0)
            {
                var attr = File.GetAttributes(path);
                if (attr.HasFlag(FileAttributes.Directory))
                {
                    // directory
                    // TODO: this one will be implemented later
                    // https://github.com/dotnet/roslyn/issues/30165
                }
                else
                {
                    // document
                    // TODO: this one will be implemented later
                    // https://github.com/dotnet/roslyn/issues/30165
                }
            }

            return(false);
        }