/// <summary>Finds all instances of a text in the document object for the provided item.</summary>
        /// <param name="item">Project item to search texts</param>
        /// <param name="text">Text to look for</param>
        /// <returns>A collection of BaseHardCodedString objects that references to text</returns>
        public static ReadOnlyCollection <BaseHardCodedString> FindAllInstancesInDocument(ProjectItem item, string text)
        {
            TextDocument doc = GetDocumentForItem(item);
            Collection <BaseHardCodedString> instances = new Collection <BaseHardCodedString>();
            EditPoint start = doc.StartPoint.CreateEditPoint();

            start.MoveToAbsoluteOffset(1);
            EditPoint end = null;
            // Unused object for FindPattern method.
            TextRanges ranges = null;

            System.Collections.IEnumerator comments = null;
            Match currentMatch = null;

            BaseHardCodedString instance = BaseHardCodedString.GetHardCodedString(item.Document);

            while (start.FindPattern(text, (int)(vsFindOptions.vsFindOptionsMatchCase), ref end, ref ranges))
            {
                if (instance != null)
                {
                    if (comments == null)
                    {
                        comments = instance.FindCommentsInDocument(item).GetEnumerator();
                    }
                    bool inComment = false;
                    while (currentMatch != null || (comments.MoveNext()))
                    {
                        currentMatch = (Match)(comments.Current);
                        // If this match appears earlier then current text skip
                        if (currentMatch.Index + currentMatch.Length <= (start.AbsoluteCharOffset - 1))
                        {
                            currentMatch = null;
                        }
                        // If this comment is later then current text stop processing comments
                        else if (currentMatch.Index >= (end.AbsoluteCharOffset - 1))
                        {
                            break;
                        }
                        // At this point current text must be part of a comment block
                        else
                        {
                            inComment = true;
                            break;
                        }
                    }
                    if (!inComment)
                    {
                        instance = instance.CreateInstance(item, start.AbsoluteCharOffset - 1, end.AbsoluteCharOffset - 1);
                        instances.Add(instance);
                    }
                }
                start = end;
            }
            return(new ReadOnlyCollection <BaseHardCodedString>(instances));
        }
Esempio n. 2
0
        public void Exec(string cmdName, vsCommandExecOption executeOption, ref object variantIn, ref object variantOut, ref bool handled)
        {
            if (cmdName == null)
            {
                throw new ArgumentNullException("CmdName");
            }
            if (cmdName == typeof(Connect).FullName + "." + Strings.RefactorCommandName)
            {
                TextSelection selection = (TextSelection)(applicationObject.ActiveDocument.Selection);
                if (applicationObject.ActiveDocument.ProjectItem.Object != null)
                {
                    Common.BaseHardCodedString stringInstance = BaseHardCodedString.GetHardCodedString(applicationObject.ActiveDocument);

                    if (stringInstance == null)
                    {
                        MessageBox.Show(
                            Strings.UnsupportedFile + " (" + applicationObject.ActiveDocument.Language + ")",
                            Strings.WarningTitle,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        return;
                    }

                    Common.MatchResult scanResult = stringInstance.CheckForHardCodedString(
                        selection.Parent,
                        selection.AnchorPoint.AbsoluteCharOffset - 1,
                        selection.BottomPoint.AbsoluteCharOffset - 1);

                    if (!scanResult.Result && selection.AnchorPoint.AbsoluteCharOffset < selection.BottomPoint.AbsoluteCharOffset)
                    {
                        scanResult.StartIndex = selection.AnchorPoint.AbsoluteCharOffset - 1;
                        scanResult.EndIndex   = selection.BottomPoint.AbsoluteCharOffset - 1;
                        scanResult.Result     = true;
                    }
                    if (scanResult.Result)
                    {
                        stringInstance = stringInstance.CreateInstance(applicationObject.ActiveDocument.ProjectItem, scanResult.StartIndex, scanResult.EndIndex);
                        if (stringInstance != null && stringInstance.Parent != null)
                        {
                            PerformAction(stringInstance);
                        }
                    }
                    else
                    {
                        MessageBox.Show(Strings.NotStringLiteral, Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }