~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchReplaceUtilities.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 System.Collections;
 
7
using System.Collections.Generic;
 
8
using System.IO;
 
9
using ICSharpCode.Core;
 
10
using ICSharpCode.SharpDevelop.Dom.Refactoring;
 
11
using ICSharpCode.SharpDevelop.Gui;
 
12
 
 
13
namespace SearchAndReplace
 
14
{
 
15
        public sealed class SearchReplaceUtilities
 
16
        {
 
17
                public static bool IsTextAreaSelected {
 
18
                        get {
 
19
                                return WorkbenchSingleton.Workbench.ActiveViewContent is ITextEditorProvider;
 
20
                        }
 
21
                }
 
22
                
 
23
                public static ITextEditor GetActiveTextEditor()
 
24
                {
 
25
                        ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
 
26
                        if (provider != null) {
 
27
                                return provider.TextEditor;
 
28
                        } else {
 
29
                                return null;
 
30
                        }
 
31
                }
 
32
                
 
33
                static bool IsWordPart(char c)
 
34
                {
 
35
                        return char.IsLetterOrDigit(c) || c == '_';
 
36
                }
 
37
                
 
38
                public static bool IsWholeWordAt(IDocument document, int offset, int length)
 
39
                {
 
40
                        return (offset - 1 < 0 || !IsWordPart(document.GetCharAt(offset - 1))) &&
 
41
                               (offset + length + 1 >= document.TextLength || !IsWordPart(document.GetCharAt(offset + length)));
 
42
                }
 
43
                
 
44
                public static ISearchStrategy CreateSearchStrategy(SearchStrategyType type)
 
45
                {
 
46
                        switch (type) {
 
47
                                case SearchStrategyType.Normal:
 
48
                                        return new BruteForceSearchStrategy(); // new KMPSearchStrategy();
 
49
                                case SearchStrategyType.RegEx:
 
50
                                        return new RegExSearchStrategy();
 
51
                                case SearchStrategyType.Wildcard:
 
52
                                        return new WildcardSearchStrategy();
 
53
                                default:
 
54
                                        throw new System.NotImplementedException("CreateSearchStrategy for type " + type);
 
55
                        }
 
56
                }
 
57
                
 
58
                public static IDocumentIterator CreateDocumentIterator(DocumentIteratorType type, IProgressMonitor monitor)
 
59
                {
 
60
                        switch (type) {
 
61
                                case DocumentIteratorType.CurrentDocument:
 
62
                                case DocumentIteratorType.CurrentSelection:
 
63
                                        return new CurrentDocumentIterator();
 
64
                                case DocumentIteratorType.Directory:
 
65
                                        try {
 
66
                                                if (!Directory.Exists(SearchOptions.LookIn)) {
 
67
                                                        if (monitor != null) monitor.ShowingDialog = true;
 
68
                                                        MessageService.ShowMessageFormatted("${res:Dialog.NewProject.SearchReplace.SearchStringNotFound.Title}", "${res:Dialog.NewProject.SearchReplace.LookIn.DirectoryNotFound}", FileUtility.NormalizePath(SearchOptions.LookIn));
 
69
                                                        if (monitor != null) monitor.ShowingDialog = false;
 
70
                                                        return new DummyDocumentIterator();
 
71
                                                }
 
72
                                        } catch (Exception ex) {
 
73
                                                if (monitor != null) monitor.ShowingDialog = true;
 
74
                                                MessageService.ShowMessage(ex.Message);
 
75
                                                if (monitor != null) monitor.ShowingDialog = false;
 
76
                                                return new DummyDocumentIterator();
 
77
                                        }
 
78
                                        return new DirectoryDocumentIterator(SearchOptions.LookIn,
 
79
                                                                             SearchOptions.LookInFiletypes,
 
80
                                                                             SearchOptions.IncludeSubdirectories);
 
81
                                case DocumentIteratorType.AllOpenFiles:
 
82
                                        return new AllOpenDocumentIterator();
 
83
                                case DocumentIteratorType.WholeProject:
 
84
                                        return new WholeProjectDocumentIterator();
 
85
                                case DocumentIteratorType.WholeSolution:
 
86
                                        return new WholeSolutionDocumentIterator();
 
87
                                default:
 
88
                                        throw new System.NotImplementedException("CreateDocumentIterator for type " + type);
 
89
                        }
 
90
                }
 
91
                
 
92
                static List<string> excludedFileExtensions;
 
93
                
 
94
                public static bool IsSearchable(string fileName)
 
95
                {
 
96
                        if (fileName == null)
 
97
                                return false;
 
98
                        
 
99
                        if (excludedFileExtensions == null) {
 
100
                                excludedFileExtensions = AddInTree.BuildItems<string>("/AddIns/DefaultTextEditor/Search/ExcludedFileExtensions", null, false);
 
101
                        }
 
102
                        string extension = Path.GetExtension(fileName);
 
103
                        if (extension != null) {
 
104
                                foreach (string excludedExtension in excludedFileExtensions) {
 
105
                                        if (String.Compare(excludedExtension, extension, true) == 0) {
 
106
                                                return false;
 
107
                                        }
 
108
                                }
 
109
                        }
 
110
                        return true;
 
111
                }
 
112
        }
 
113
}