Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C#

Standard Regular Expression Searcher Addin For VS.NET 2003

Rate me:
Please Sign up or sign in to vote.
4.88/5 (42 votes)
11 Jan 2005CPOL3 min read 107.2K   930   52  
Standard Regular Expression Searcher add-in for VS.NET 2003.
using System;
using System.Collections.Specialized;
using System.Collections;
using EnvDTE;

using SearcherAddIn.Common;
using SearcherAddIn.Helper;

namespace SearcherAddIn
{
	/// <summary>
	/// OpenFilesSearcher ��ժҪ˵����
	/// </summary>
	public class OpenFilesSearcher : Searcher {
		private WindowSearcher _windowSearcher = null;
		private StringCollection _unhandledOpenFileList = new StringCollection();
		private string _activeOpenFullFilename = string.Empty;

		public OpenFilesSearcher(_DTE dte) : base(dte) {
		}

		private void findNextInOpenFiles(){
			if(_unhandledOpenFileList.Count > 0){
				checkWindowSearcher();
				if(_activeOpenFullFilename != _unhandledOpenFileList[0]){
					_dte.ItemOperations.OpenFile(_unhandledOpenFileList[0], Constants.vsViewKindTextView);
					_activeOpenFullFilename = _unhandledOpenFileList[0];
					_windowSearcher.MoveToStartInTextWindow();
				}
					
				if(!_windowSearcher.FindNextInSearchInFiles()){
					_unhandledOpenFileList.RemoveAt(0);
					findNextInOpenFiles();
				}
			}
			else{
				UIHelper.ShowInfo(Info.UI_PROMPT_FIND_TO_END);
			}
		}

		public void FindNext() {
			getSearchScopePath();
			if(_unhandledOpenFileList.Count > 0){
				findNextInOpenFiles();
			}
		}

		private void checkWindowSearcher(){
			if(_windowSearcher == null){
				initWindowSearcher();
				return;
			}

			if(!(_windowSearcher.Pattern == base.Pattern && _windowSearcher.RegexOptions == base.RegexOptions))
				initWindowSearcher();
		}

		private void initWindowSearcher(){
			_windowSearcher = new WindowSearcher(_dte);
			_windowSearcher.Pattern = base.Pattern;
			_windowSearcher.RegexOptions = base.RegexOptions;
		}

		private void replaceAllInOpenFiles(string replacePattern){
			if(_unhandledOpenFileList.Count > 0){
				int replaceCount = 0;
				checkWindowSearcher();
				foreach(string fullFilename in _unhandledOpenFileList){
					_dte.ItemOperations.OpenFile(fullFilename, Constants.vsViewKindTextView);
					replaceCount += _windowSearcher.ReplaceAll(replacePattern, true);
				}
				UIHelper.ShowInfo(replaceCount + " replaces happenned");
			}
		}

		private void replaceNextInOpenFiles(string replacePattern){
			if(_unhandledOpenFileList.Count > 0){
				checkWindowSearcher();

				if(_activeOpenFullFilename != _unhandledOpenFileList[0]){
					_dte.ItemOperations.OpenFile(_unhandledOpenFileList[0], Constants.vsViewKindTextView);
					_activeOpenFullFilename = _unhandledOpenFileList[0];
					_windowSearcher.MoveToStartInTextWindow();
				}
					
				if(!_windowSearcher.ReplaceNextInSearchInFiles(replacePattern)){
					_unhandledOpenFileList.RemoveAt(0);
					replaceNextInOpenFiles(replacePattern);
				}
			}
			else{
				UIHelper.ShowInfo(Info.UI_PROMPT_FIND_TO_END);
			}
		}

		public void ReplaceNext(string replacePattern){
			getSearchScopePath();
			if(_unhandledOpenFileList.Count > 0){
				replaceNextInOpenFiles(replacePattern);
				return;
			}
		}

		protected override void getSearchScopePath(){
			if(_unhandledOpenFileList.Count == 0){
				foreach(Document document in _dte.Documents){
					_unhandledOpenFileList.Add(document.FullName);
				}
			}
		}
		public void ReplaceAll(string replacePattern){
			getSearchScopePath();
			if(_unhandledOpenFileList.Count > 0){
				replaceAllInOpenFiles(replacePattern);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions