~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/SearchAndReplace/Project/Commands/SearchMainMenuCommands.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using ICSharpCode.SharpDevelop.Editor;
 
5
using System;
 
6
using ICSharpCode.Core;
 
7
 
 
8
namespace SearchAndReplace
 
9
{
 
10
        public class Find : AbstractMenuCommand
 
11
        {
 
12
                public static void SetSearchPattern()
 
13
                {
 
14
                        // Get Highlighted value and set it to FindDialog.searchPattern
 
15
                        ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor();
 
16
                        if (textArea != null) {
 
17
                                string selectedText = textArea.SelectedText;
 
18
                                if (selectedText != null && selectedText.Length > 0 && !IsMultipleLines(selectedText)) {
 
19
                                        SearchOptions.CurrentFindPattern = selectedText;
 
20
                                }
 
21
                        }
 
22
                }
 
23
                
 
24
                public override void Run()
 
25
                {
 
26
                        SetSearchPattern();
 
27
                        SearchAndReplaceDialog.ShowSingleInstance(SearchAndReplaceMode.Search);
 
28
                }
 
29
                
 
30
                public static bool IsMultipleLines(string text)
 
31
                {
 
32
                        return text.IndexOf('\n') != -1;
 
33
                }
 
34
        }
 
35
        
 
36
        public class FindNext : AbstractMenuCommand
 
37
        {
 
38
                public override void Run()
 
39
                {
 
40
                        if (SearchOptions.CurrentFindPattern.Length > 0) {
 
41
                                SearchReplaceManager.FindNext(null);
 
42
                        } else {
 
43
                                Find find = new Find();
 
44
                                find.Run();
 
45
                        }
 
46
                }
 
47
        }
 
48
        
 
49
        /// <summary>
 
50
        /// Finds the next text match based on the text currently
 
51
        /// selected. It uses the currently active search options and only changes
 
52
        /// the current find pattern. If the currently active search is inside the
 
53
        /// current text selection then the quick find will change the search so it is
 
54
        /// across the active document, otherwise it will not change the current setting.
 
55
        /// </summary>
 
56
        /// <remarks>
 
57
        /// If there is a piece of text selected on a single line then the quick
 
58
        /// find will search for that. If multiple lines of text are selected then
 
59
        /// the word at the start of the selection is determined and searche for.
 
60
        /// If no text is selected then the word next to the caret is used. If
 
61
        /// no text is selected, but the caret is immediately surrounded by whitespace
 
62
        /// then quick find does nothing.
 
63
        /// </remarks>
 
64
        public class FindNextSelected : AbstractMenuCommand
 
65
        {
 
66
                public override void Run()
 
67
                {
 
68
                        ITextEditor textArea = SearchReplaceUtilities.GetActiveTextEditor();
 
69
                        if (textArea == null) {
 
70
                                return;
 
71
                        }
 
72
                        
 
73
                        // Determine what text we should search for.
 
74
                        string textToFind;
 
75
                        
 
76
                        string selectedText = textArea.SelectedText;
 
77
                        if (selectedText.Length > 0) {
 
78
                                if (Find.IsMultipleLines(selectedText)) {
 
79
                                        // Locate the nearest word at the selection start.
 
80
                                        textToFind = textArea.Document.GetWordAt(textArea.SelectionStart);
 
81
                                } else {
 
82
                                        // Search for selected text.
 
83
                                        textToFind = selectedText;
 
84
                                }
 
85
                        } else {
 
86
                                textToFind = textArea.Document.GetWordAt(textArea.Caret.Offset);
 
87
                        }
 
88
                        
 
89
                        if (textToFind != null && textToFind.Length > 0) {
 
90
                                SearchOptions.CurrentFindPattern = textToFind;
 
91
                                if (SearchOptions.DocumentIteratorType == DocumentIteratorType.CurrentSelection) {
 
92
                                        SearchOptions.DocumentIteratorType = DocumentIteratorType.CurrentDocument;
 
93
                                }
 
94
                                SearchReplaceManager.FindNext(null);
 
95
                        }
 
96
                }
 
97
        }
 
98
        
 
99
        public class Replace : AbstractMenuCommand
 
100
        {
 
101
                public override void Run()
 
102
                {
 
103
                        Find.SetSearchPattern();
 
104
                        SearchAndReplaceDialog.ShowSingleInstance(SearchAndReplaceMode.Replace);
 
105
                }
 
106
        }
 
107
}