Exemplo n.º 1
0
        public void Remove(object index)
        {
            string?importToRemove = index switch
            {
                int indexInt when _importsList.IsPresent(indexInt) => _importsList.Item(indexInt),
                string removeImport when _importsList.IsPresent(removeImport) => removeImport,
                _ => null
            };

            if (importToRemove != null)
            {
                _threadingService.ExecuteSynchronously(async() =>
                {
                    await _projectAccessor.OpenProjectForWriteAsync(ConfiguredProject, project =>
                    {
                        Microsoft.Build.Evaluation.ProjectItem importProjectItem = project.GetItems(ImportItemTypeName)
                                                                                   .First(i => string.Equals(importToRemove, i.EvaluatedInclude, StringComparisons.ItemNames));

                        if (importProjectItem.IsImported)
                        {
                            throw new ArgumentException(string.Format(VSResources.ImportsFromTargetCannotBeDeleted, importToRemove), nameof(index));
                        }

                        project.RemoveItem(importProjectItem);
                    });

                    await _importsList.ReceiveLatestSnapshotAsync();
                });
            }
            else
            {
                throw new ArgumentException(string.Format("{0} - index is neither an Int nor a String, or the Namepsace was not found", index), nameof(index));
            }
        }
Exemplo n.º 2
0
        public static Task Move(ConfiguredProject configuredProject, IProjectAccessor accessor, ImmutableHashSet <string> previousIncludes, IProjectTree target, OrderingMoveAction action)
        {
            Requires.NotNull(configuredProject, nameof(configuredProject));
            Requires.NotNull(accessor, nameof(accessor));
            Requires.NotNull(previousIncludes, nameof(previousIncludes));
            Requires.NotNull(target, nameof(target));

            return(accessor.OpenProjectForWriteAsync(configuredProject, project =>
            {
                // We do a sanity re-evaluation to absolutely ensure changes were met.
                project.ReevaluateIfNecessary();
                ImmutableArray <ProjectItemElement> addedElements = GetAddedItemElements(previousIncludes, project);

                switch (action)
                {
                case OrderingMoveAction.MoveToTop:
                    TryMoveElementsToTop(project, addedElements, target);
                    break;

                case OrderingMoveAction.MoveAbove:
                    TryMoveElementsAbove(project, addedElements, target);
                    break;

                case OrderingMoveAction.MoveBelow:
                    TryMoveElementsBelow(project, addedElements, target);
                    break;

                default:
                    break;
                }
            }));
        }
Exemplo n.º 3
0
        public void Remove(object index)
        {
            bool intIndexPresent    = index is int indexInt && _importsList.IsPresent(indexInt);
            bool stringIndexPresent = index is string removeImport && _importsList.IsPresent(removeImport);

            if (intIndexPresent || stringIndexPresent)
            {
                string?importRemoved = null;
                _threadingService.ExecuteSynchronously(() =>
                {
                    return(_projectAccessor.OpenProjectForWriteAsync(ConfiguredProject, project =>
                    {
                        Microsoft.Build.Evaluation.ProjectItem importProjectItem;
                        if (index is string removeImport1)
                        {
                            importProjectItem = project.GetItems(ImportItemTypeName)
                                                .First(i => string.Equals(removeImport1, i.EvaluatedInclude, StringComparison.OrdinalIgnoreCase));
                        }
                        else if (index is int indexInt1)
                        {
                            importProjectItem = project.GetItems(ImportItemTypeName)
                                                .OrderBy(i => i.EvaluatedInclude)
                                                .ElementAt(indexInt1 - 1);
                        }
                        else
                        {
                            // Cannot reach this point, since index has to be Int or String
                            throw Assumes.NotReachable();
                        }

                        if (importProjectItem.IsImported)
                        {
                            throw new ArgumentException(string.Format(VSResources.ImportsFromTargetCannotBeDeleted, index.ToString()), nameof(index));
                        }

                        importRemoved = importProjectItem.EvaluatedInclude;
                        project.RemoveItem(importProjectItem);
                    }));
                });

                Assumes.NotNull(importRemoved);

                OnImportRemoved(importRemoved);
            }
            else if (index is string)
            {
                throw new ArgumentException(string.Format("{0} - Namespace is not present ", index), nameof(index));
            }
            else
            {
                throw new ArgumentException(string.Format("{0} - index is neither an Int nor a String", index), nameof(index));
            }
        }
        protected override async Task <bool> TryHandleCommandAsync(IProjectTree node, bool focused, long commandExecuteOptions, IntPtr variantArgIn, IntPtr variantArgOut)
        {
            bool didMove = false;

            await _accessor.OpenProjectForWriteAsync(_configuredProject, project => didMove = TryMove(project, node));

            if (didMove)
            {
                // Wait for updating to finish before re-selecting the node that moved.
                // We need to re-select the node after it is moved in order to continuously move the node using hotkeys.
                await _projectTree.TreeService.PublishLatestTreeAsync(waitForFileSystemUpdates : true);

                await NodeHelper.SelectAsync(_configuredProject, _serviceProvider, node);
            }

            return(didMove);
        }
        /// <summary>
        /// Performs a move on any items that were added based on the previous includes.
        /// </summary>
        public static Task MoveAsync(ConfiguredProject configuredProject, IProjectAccessor accessor, ImmutableHashSet <string> previousIncludes, IProjectTree target, OrderingMoveAction action)
        {
            Requires.NotNull(configuredProject, nameof(configuredProject));
            Requires.NotNull(accessor, nameof(accessor));
            Requires.NotNull(previousIncludes, nameof(previousIncludes));
            Requires.NotNull(target, nameof(target));

            return(accessor.OpenProjectForWriteAsync(configuredProject, project =>
            {
                // We do a sanity re-evaluation to absolutely ensure changes were met.
                project.ReevaluateIfNecessary();
                ImmutableArray <ProjectItemElement> addedElements = GetAddedItemElements(previousIncludes, project);

                // TODO: Should the result (success or failure) be ignored?
                _ = action switch
                {
                    OrderingMoveAction.MoveToTop => TryMoveElementsToTop(project, addedElements, target),
                    OrderingMoveAction.MoveAbove => TryMoveElementsAbove(project, addedElements, target),
                    OrderingMoveAction.MoveBelow => TryMoveElementsBelow(project, addedElements, target),
                    _ => false
                };
            }));