Exemple #1
0
        public bool ReplaceAndSelect(TextPointer start, bool round, out TextPointer endPosition, string findText,
                                     string replaceText)
        {
            endPosition = null;

            if (String.IsNullOrEmpty(findText) || start == null)
            {
                return(true);
            }

            if (_manager == null)
            {
                _manager = new FindAndReplaceManager(FlowDocument);
            }

            _manager.CurrentPosition = start;

            TextRange textRange = _manager.Replace(findText, replaceText, GetFindOptions());

            if (textRange != null)
            {
                endPosition = textRange.End;
                return(SearchString(endPosition, true, true));
            }
            if (_manager.CurrentPosition.CompareTo(FlowDocument.ContentEnd) == 0 && round)
            {
                return(ReplaceAndSelect(FlowDocument.ContentStart, false, out endPosition, findText, replaceText));
            }
            RaiseMatchingChangedEvent(true, null, true);
            return(false);
        }
Exemple #2
0
        public bool SearchString(TextPointer start, bool round, bool fromReplace)
        {
            String findText = SearchText;

            if (String.IsNullOrEmpty(findText))
            {
                RaiseMatchingChangedEvent(false, null, fromReplace);
                return(true);
            }

            if (_manager == null)
            {
                _manager = new FindAndReplaceManager(FlowDocument);
            }

            _manager.CurrentPosition = start;

            TextRange textRange = _manager.FindNext(findText, GetFindOptions());

            if (textRange != null)
            {
                RaiseMatchingChangedEvent(true, textRange, fromReplace);
                return(true);
            }
            if (_manager.CurrentPosition.CompareTo(FlowDocument.ContentEnd) == 0 && round)
            {
                return(SearchString(FlowDocument.ContentStart, false, fromReplace));
            }
            RaiseMatchingChangedEvent(true, null, false);
            return(false);
        }
        public void CommandLine_FindCaseSensitive_Tests()
        {
            string[] args = new string[]
            {
                @"-f=MICROSOFT",
                @"-r=Denis",
                @"-p=*.json",
                $"-b={BASE_FOLDER}",                    // From base folder
                @"-d"                                   // In demo mode (no write file)
            };

            var arguments = new Arguments(args);
            var manager   = new FindAndReplaceManager(arguments);

            manager.Start();

            Assert.AreEqual(1, manager.FilesMatched.Count);
        }
        public void CommandLine_AppSettingsJsonFile_Tests()
        {
            string[] args = new string[]
            {
                $"-f=\"ApiGlobalPrefix\"",
                $"-r=Denis",
                $"-p=*.json",
                $"-b={BASE_FOLDER}",                    // From base folder
                $"-d"                                   // In demo mode (no write file)
            };

            var arguments = new Arguments(args);
            var manager   = new FindAndReplaceManager(arguments);

            manager.Logger = (file, content) =>
            {
                Assert.IsTrue(content.Contains("Denis"));
            };
            manager.Start();

            Assert.AreEqual(1, manager.FilesMatched.Count);
        }
        public void CommandLine_RegExVersion_AllJsonFiles_Tests()
        {
            string[] args = new string[]
            {
                @"-f=""""version"": ""\d.\d.\d\""""",   // 'version': 'x.x.x'
                @"-r=""""version"": ""9.1.2""""",       // 'version': '9.1.2'
                @"-p=**/*.json",                        // All json in current and subfolders
                $"-b={BASE_FOLDER}",                    // From base folder
                //@"-d"                                 // In demo mode (no write file)
            };

            var arguments = new Arguments(args);
            var manager   = new FindAndReplaceManager(arguments);

            manager.Logger = (file, content) =>
            {
                Assert.IsTrue(content.Contains("9.1.2"));
            };
            manager.Start();

            Assert.AreEqual(2, manager.FilesMatched.Count);
        }
        public void CommandLine_Simple_AllJsonFiles_Tests()
        {
            string[] args = new string[]
            {
                @"-f=Microsoft",
                @"-r=Denis",
                @"-p=**/*.json",                        // All json in current and subfolders
                $"-b={BASE_FOLDER}",                    // From base folder
                @"-d"                                   // In demo mode (no write file)
            };

            var arguments = new Arguments(args);
            var manager   = new FindAndReplaceManager(arguments);

            manager.Logger = (file, content) =>
            {
                Assert.IsTrue(content.Contains("Denis"));
            };
            manager.Start();

            Assert.AreEqual(3, manager.FilesMatched.Count);
        }
        public void CommandLine_ModeJson_Tests()
        {
            string[] args = new string[]
            {
                @"-f=Version",
                @"-r=9.1.3",
                @"-p=**/*.json",
                $"-b={BASE_FOLDER}",                    // From base folder
                $"-m=JSON",                             // JSON Mode => Key / Value
                @"-d"                                   // In demo mode (no write file)
            };

            var arguments = new Arguments(args);
            var manager   = new FindAndReplaceManager(arguments);

            manager.Logger = (file, content) =>
            {
                Assert.IsTrue(content.Contains("\"Version\": \"9.1.3\""));
            };
            manager.Start();

            Assert.AreEqual(3, manager.FilesMatched.Count);
        }
Exemple #8
0
 public void ResetManager()
 {
     _manager = null;
 }
Exemple #9
0
        static void Main(string[] args)
        {
            FindAndReplaceManager.FindNext("hello");

            Console.ReadLine();
        }
Exemple #10
0
        static void Main(string[] args)
        {
            FindAndReplaceManager.FileNext("Hello");

            Console.ReadKey();
        }
        private void bFindReplace_Click(object sender, RoutedEventArgs e)
        {
            var         content     = frameSidebar.Content as Page;
            var         grid        = content.Content as Grid;
            TextBox     tbFind      = (TextBox)grid.Children[2];
            TextBox     tbReplace   = (TextBox)grid.Children[4];
            var         expander    = (Expander)grid.Children[9];
            CheckBox    cbMatchCase = (CheckBox)((Grid)expander.Content).Children[0];
            CheckBox    cbMatchWord = (CheckBox)((Grid)expander.Content).Children[1];
            TextRange   textRange   = null;
            FindOptions findOptions = FindOptions.None;

            if (cbMatchCase.IsChecked == true)
            {
                findOptions = FindOptions.MatchCase;
            }
            if (cbMatchWord.IsChecked == true)
            {
                findOptions ^= FindOptions.MatchWholeWord;
            }
            if (findReplaceChanges)
            {
                findAndReplace     = new FindAndReplaceManager(rtbMain.Document);
                findReplaceChanges = false;
            }
            switch (((Button)sender).Content.ToString())
            {
            case "Find Next":
                textRange = findAndReplace.FindNext(tbFind.Text, findOptions);
                break;

            case "Replace All":
                int results = findAndReplace.ReplaceAll(tbFind.Text, tbReplace.Text, findOptions, null);
                customMessageBox customMessageBox = new customMessageBox();
                customMessageBox.SetupMsgBox($"Replaced {results} occurences.", "Replace All", this.FindResource("iconInformation"));
                customMessageBox.ShowDialog();
                rtbMain.Focus();
                return;

            case "Replace Next":
                textRange = findAndReplace.Replace(tbFind.Text, tbReplace.Text, findOptions);
                break;

            case "Count":
                RichTextBox rtbTemp = XamlReader.Parse(XamlWriter.Save(rtbMain)) as RichTextBox;
                findAndReplace = new FindAndReplaceManager(rtbTemp.Document);
                int count = findAndReplace.ReplaceAll(tbFind.Text, "", findOptions, null);
                customMessageBox customMessageBox2 = new customMessageBox();
                customMessageBox2.SetupMsgBox($"\"{tbFind.Text}\" appeared {count} times.", "Count", this.FindResource("iconInformation"));
                customMessageBox2.ShowDialog();
                findReplaceChanges = true;
                return;

            default:
                break;
            }

            if (textRange == null)
            {
                customMessageBox customMessageBox = new customMessageBox();
                customMessageBox.SetupMsgBox("No (more) results.\nDo you wish to start at the top again?", "Reset search?", this.FindResource("iconInformation"), "Yes", "No");
                customMessageBox.ShowDialog();
                if (customMessageBox.result.ToString() == "Yes")
                {
                    findAndReplace     = new FindAndReplaceManager(rtbMain.Document);
                    findReplaceChanges = false;
                    textRange          = findAndReplace.FindNext(tbFind.Text, findOptions);
                    if (textRange == null)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            rtbMain.Selection.Select(textRange.Start, textRange.End);
            rtbMain.Focus();
        }