public void FindInWebProject()
        {
            // Get Project object
            Project testProject = (Project)(extensibility.Solution.Projects.Item(4));
            ReadOnlyCollection <BaseHardCodedString> collection =
                BaseHardCodedString.FindAllInstancesInProject(testProject, "\"Test String\"");

            Assert.AreEqual(2, collection.Count, Messages.CountInvalid);
        }
        public void FindInDocumentVBCommentTest()
        {
            // Get Project object
            Project     testProject = (Project)(extensibility.Solution.Projects.Item(2));
            ProjectItem codeFile    = testProject.ProjectItems.Item("Comment Test.vb");
            ReadOnlyCollection <BaseHardCodedString> collection =
                BaseHardCodedString.FindAllInstancesInDocument(codeFile, "\"TestString\"");

            Assert.AreEqual(1, collection.Count, Messages.CountInvalid);
        }
        public void FindInDocumentCSharpCommentTest()
        {
            // Get Project object
            Project     testProject = (Project)(extensibility.Solution.Projects.Item(1));
            ProjectItem codeFile    = testProject.ProjectItems.Item("CommentTest.cs");
            ReadOnlyCollection <BaseHardCodedString> collection =
                BaseHardCodedString.FindAllInstancesInDocument(codeFile, "\"Test String\"");

            Assert.AreEqual(2, collection.Count, Messages.CountInvalid);
            Assert.AreEqual(11, collection[0].StartingLine, Messages.MatchResultInvalid);
            Assert.AreEqual(11, collection[1].StartingLine, Messages.MatchResultInvalid);
        }
Example #4
0
        private void PerformAction(BaseHardCodedString stringInstance)
        {
            ExtractToResourceActionSite site = new ExtractToResourceActionSite(stringInstance);

            if (site.ActionObject != null)
            {
                RefactorStringDialog dialog = new RefactorStringDialog();
                dialog.ShowDialog(site);
            }
            else
            {
                MessageBox.Show(Strings.UnsupportedFile, Strings.WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        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);
                    }
                }
            }
        }
Example #6
0
        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void MenuItemCallback(object sender, EventArgs e)
        {
            DTE2 applicationObject = Package.GetGlobalService(typeof(DTE)) as DTE2;

            TextSelection selection = (TextSelection)(applicationObject.ActiveDocument.Selection);

            if (applicationObject.ActiveDocument.ProjectItem.Object != null)
            {
                BaseHardCodedString stringInstance = BaseHardCodedString.GetHardCodedString(applicationObject.ActiveDocument);

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

                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);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Invokes replace method on the provided objects
        /// </summary>
        /// <param name="codeFile">Code file to test</param>
        /// <param name="resFile">Resource file to use</param>
        /// <param name="hcs">Hardcoded string to use</param>
        /// <param name="expected">Expected test string</param>
        internal static void TestReplaceMethod(ProjectItem codeFile, string resourceFileName, BaseHardCodedString hcs, string expected, string resourceName)
        {
            string fileName = codeFile.get_FileNames(1);
            bool   readOnly = false;

            try
            {
                readOnly = CommonMethods.ToggleReadOnly(fileName, false);
                ExtractToResourceActionSite refactorSite = new ExtractToResourceActionSite(hcs);
                ResourceFileCollection      resources    = new ResourceFileCollection(codeFile.ContainingProject,
                                                                                      new FilterMethod(refactorSite.ActionObject.IsValidResourceFile));
                ResourceFile resFile = resources[resourceFileName];
                refactorSite.ExtractStringToResource(resFile, resourceName);
                TextDocument doc  = ((EnvDTE.TextDocument)codeFile.Document.Object(null));
                EditPoint    ep   = doc.StartPoint.CreateEditPoint();
                string       line = ep.GetLines(hcs.StartingLine + 1, hcs.StartingLine + 2);
                Assert.AreEqual(expected, line, "New line does not match the expected output");
            }
            finally
            {
                if (readOnly)
                {
                    CommonMethods.ToggleReadOnly(fileName, true);
                }
            }
        }