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)
4
using ICSharpCode.SharpDevelop.Editor;
6
using System.Collections;
7
using System.Collections.Generic;
9
using ICSharpCode.Core;
10
using ICSharpCode.SharpDevelop.Dom.Refactoring;
11
using ICSharpCode.SharpDevelop.Gui;
13
namespace SearchAndReplace
15
public sealed class SearchReplaceUtilities
17
public static bool IsTextAreaSelected {
19
return WorkbenchSingleton.Workbench.ActiveViewContent is ITextEditorProvider;
23
public static ITextEditor GetActiveTextEditor()
25
ITextEditorProvider provider = WorkbenchSingleton.Workbench.ActiveViewContent as ITextEditorProvider;
26
if (provider != null) {
27
return provider.TextEditor;
33
static bool IsWordPart(char c)
35
return char.IsLetterOrDigit(c) || c == '_';
38
public static bool IsWholeWordAt(IDocument document, int offset, int length)
40
return (offset - 1 < 0 || !IsWordPart(document.GetCharAt(offset - 1))) &&
41
(offset + length + 1 >= document.TextLength || !IsWordPart(document.GetCharAt(offset + length)));
44
public static ISearchStrategy CreateSearchStrategy(SearchStrategyType 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();
54
throw new System.NotImplementedException("CreateSearchStrategy for type " + type);
58
public static IDocumentIterator CreateDocumentIterator(DocumentIteratorType type, IProgressMonitor monitor)
61
case DocumentIteratorType.CurrentDocument:
62
case DocumentIteratorType.CurrentSelection:
63
return new CurrentDocumentIterator();
64
case DocumentIteratorType.Directory:
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();
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();
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();
88
throw new System.NotImplementedException("CreateDocumentIterator for type " + type);
92
static List<string> excludedFileExtensions;
94
public static bool IsSearchable(string fileName)
99
if (excludedFileExtensions == null) {
100
excludedFileExtensions = AddInTree.BuildItems<string>("/AddIns/DefaultTextEditor/Search/ExcludedFileExtensions", null, false);
102
string extension = Path.GetExtension(fileName);
103
if (extension != null) {
104
foreach (string excludedExtension in excludedFileExtensions) {
105
if (String.Compare(excludedExtension, extension, true) == 0) {