Пример #1
0
        private void SortMembers()
        {
            MemberSorter sorter = new MemberSorter(this.dte, true);

            if (sorter.HasSelectedMembers)
            {
                Options options = this.package.Options;
                sorter.SortMembers(options);
            }
        }
Пример #2
0
        public void Remove()
        {
            Tuple <TextPoint, TextPoint> range = null;

            try
            {
                // To use RemoveMember, we'd have to cast to CodeClass, CodeStruct, CodeInterface, or CodeEnum.
                // However, RemoveMember won't remove a leading comment other than a DocComment,
                // and in VB RemoveMember doesn't remove the whitespace line after the member.
                // So to make our Remove consistent with GetCode, we'll use GetCodeRange and Delete.
                range = this.GetCodeRange();
            }
            catch (ArgumentException ex)
            {
                // In VS 2015 Update 2, an ArgumentException can occur while removing members with explicitly
                // implemented interface member names if the same named non-explicitly implemented member
                // was just removed.  The Roslyn code model seems to get out of sync on the explicit members,
                // and we have to look them back up to remove them.  This happens, for example, on the second
                // GetEnumerator when sorting these lines (from DictionarySet.cs):
                //      public IEnumerator<T> GetEnumerator() { throw new NotImplementedException(); }
                //      bool ISet< T >.Add(T item) { throw new NotImplementedException(); }
                //      IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
                bool rethrow = this.element.InfoLocation != vsCMInfoLocation.vsCMInfoLocationProject;
                if (!rethrow)
                {
                    ProjectItem    projectItem = this.element.ProjectItem;
                    FileCodeModel2 codeModel   = (FileCodeModel2)projectItem.FileCodeModel;
                    codeModel.Synchronize();
                    this.element = MemberSorter.FindMember(codeModel.CodeElements, this.Name);
                    if (this.element == null)
                    {
                        rethrow = true;
                    }
                    else
                    {
                        range = this.GetCodeRange();
                    }
                }

                if (rethrow)
                {
                    throw new ArgumentException($"Unable to remove or reorder element {this.Name} due to VS FileCodeModel2 limitations.", ex);
                }
            }

            if (range != null)
            {
                EditPoint startEdit = range.Item1.CreateEditPoint();
                startEdit.StartOfLine();

                EditPoint endEdit = range.Item2.CreateEditPoint();
                endEdit.LineDown();
                if (endEdit.GetLines(endEdit.Line, endEdit.Line + 1).Trim().Length == 0)
                {
                    endEdit.LineDown();
                }

                endEdit.StartOfLine();
                startEdit.Delete(endEdit);
            }
        }
Пример #3
0
        public bool CanExecute(Command command)
        {
            try
            {
                bool result = false;

                if (this.dte != null)
                {
                    switch (command)
                    {
                    // These require a text selection.
                    case Command.SortLines:
                    case Command.Trim:
                    case Command.Statistics:
                    case Command.StreamText:
                    case Command.ExecuteText:
                    case Command.CheckSpelling:
                        result = new TextDocumentHandler(this.dte).HasNonEmptySelection;
                        break;

                    // These require a text selection for specific languages.
                    case Command.CommentSelection:
                    case Command.UncommentSelection:
                        result = CommentHandler.CanCommentSelection(this.dte, command == Command.CommentSelection);
                        break;

                    // These require a document using a supported language.
                    case Command.AddRegion:
                    case Command.CollapseAllRegions:
                    case Command.ExpandAllRegions:
                        result = RegionHandler.IsSupportedLanguage(this.ActiveLanguage);
                        break;

                    // These require an open document with a backing file on disk.
                    case Command.ExecuteFile:
                    case Command.ToggleReadOnly:
                        string fileName = this.GetDocumentFileName();
                        result = File.Exists(fileName);
                        break;

                    case Command.GenerateGuid:
                        result = new TextDocumentHandler(this.dte).CanSetSelectedText;
                        break;

                    case Command.ToggleFiles:
                        result = ToggleFilesHandler.IsSupportedLanguage(this.ActiveLanguage);
                        break;

                    case Command.ListAllProjectProperties:
                        result = ProjectHandler.GetSelectedProjects(this.dte, null);
                        break;

                    case Command.ViewBaseConverter:
                    case Command.ViewTasks:
                        result = true;
                        break;

                    case Command.SortMembers:
                        result = new MemberSorter(this.dte, false).CanFindMembers;
                        break;

                    case Command.AddToDoComment:
                        result = CommentHandler.CanAddToDoComment(this.dte);
                        break;
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                MainPackage.LogException(ex);
                throw;
            }
        }