public static void MarkAll(IProgressMonitor monitor) { SetSearchOptions(monitor); ClearSelection(); find.Reset(); if (!find.SearchStrategy.CompilePattern(monitor)) { return; } List <ITextEditor> textAreas = new List <ITextEditor>(); int count; for (count = 0;; count++) { SearchResultMatch result = SearchReplaceManager.find.FindNext(monitor); if (result == null) { break; } else { MarkResult(textAreas, result); } } find.Reset(); ShowMarkDoneMessage(count, monitor); }
static void MarkResult(List <ITextEditor> textAreas, SearchResultMatch result) { ITextEditor textArea = OpenTextArea(result.FileName); if (textArea != null) { if (!textAreas.Contains(textArea)) { textAreas.Add(textArea); } textArea.Caret.Offset = result.Offset; IDocumentLine segment = textArea.Document.GetLineForOffset(result.Offset); int lineNr = segment.LineNumber; foreach (var bookmark in BookmarkManager.GetBookmarks(result.FileName)) { if (bookmark.CanToggle && bookmark.LineNumber == lineNr) { // bookmark or breakpoint already exists at that line return; } } BookmarkManager.AddMark(new Bookmark(result.FileName, textArea.Document.OffsetToPosition(result.Offset))); } }
public static void MarkAll(int offset, int length, IProgressMonitor monitor) { SetSearchOptions(monitor); find.Reset(); if (!find.SearchStrategy.CompilePattern(monitor)) { return; } List <TextEditorControl> textAreas = new List <TextEditorControl>(); int count; for (count = 0;; count++) { SearchResultMatch result = find.FindNext(offset, length); if (result == null) { break; } else { MarkResult(textAreas, result); } } find.Reset(); foreach (TextEditorControl ctl in textAreas) { ctl.Refresh(); } ShowMarkDoneMessage(count, monitor); }
public static void ReplaceAll(int offset, int length, IProgressMonitor monitor) { SetSearchOptions(monitor); find.Reset(); if (!find.SearchStrategy.CompilePattern(monitor)) { return; } for (int count = 0;; count++) { SearchResultMatch result = find.FindNext(offset, length); if (result == null) { ShowReplaceDoneMessage(count, monitor); return; } string replacement = result.TransformReplacePattern(SearchOptions.ReplacePattern); find.Replace(result.Offset, result.Length, replacement); length -= result.Length - replacement.Length; // HACK - Move the cursor to the correct offset - the caret gets // moved before the replace range if we replace a string with a // single character. The ProvidedDocInfo.Replace method assumes that // the current offset is at the end of the found text which it is not. find.CurrentDocumentInformation.CurrentOffset = result.Offset + replacement.Length - 1; } }
public SearchResultNode(IDocument document, SearchResultMatch result) { drawDefault = false; this.result = result; startPosition = result.GetStartPosition(document); TextLocation endPosition = result.GetEndPosition(document); positionText = "(" + (startPosition.Y + 1) + ", " + (startPosition.X + 1) + ") "; LineSegment line = document.GetLineSegment(startPosition.Y); drawableLine = new DrawableLine(document, line, RegularMonospacedFont, BoldMonospacedFont); drawableLine.SetBold(0, drawableLine.LineLength, false); if (startPosition.Y == endPosition.Y) { drawableLine.SetBold(startPosition.X, endPosition.X, true); } specialText = result.DisplayText; if (specialText != null) { displayText = positionText + specialText; } else { displayText = positionText + document.GetText(line).Replace("\t", " "); } Text = displayText; }
public void SetUp() { // Set up SearchOptions required by the BruteForceSearchStrategy. SearchOptions.CurrentFindPattern = "foo"; SearchOptions.MatchCase = false; SearchOptions.MatchWholeWord = false; // Create search. search = new Search(); search.TextIteratorBuilder = new ForwardTextIteratorBuilder(); search.SearchStrategy = new BruteForceSearchStrategy(); MockCurrentDocumentIterator currentDocIterator = new MockCurrentDocumentIterator(); search.DocumentIterator = currentDocIterator; // Create the document to be searched. MockDocument doc = new MockDocument(); StringTextBufferStrategy textBufferStrategy = new StringTextBufferStrategy(); textBufferStrategy.SetContent("foo"); doc.TextBufferStrategy = textBufferStrategy; // Create a doc info with an initial end offset right // at the end of the text. ProvidedDocumentInformation docInfo = new ProvidedDocumentInformation(doc, @"C:\Temp\test.txt", doc.TextLength); currentDocIterator.Current = docInfo; currentDocIterator.CurrentFileName = docInfo.FileName; // Search the document. search.SearchStrategy.CompilePattern(null); result = search.FindNext(null); }
SearchResultMatch CreateNamedSearchResult(SearchResultMatch pos) { if (info == null || pos == null) { return null; } pos.ProvidedDocumentInformation = info; return pos; }
SearchResultMatch CreateNamedSearchResult(SearchResultMatch pos) { if (info == null || pos == null) { return(null); } pos.ProvidedDocumentInformation = info; return(pos); }
public SearchResultMatch FindNext(IProgressMonitor monitor) { // insanity check Debug.Assert(searchStrategy != null); Debug.Assert(documentIterator != null); Debug.Assert(textIteratorBuilder != null); if (monitor != null && monitor.CancellationToken.IsCancellationRequested) { return(null); } if (info != null && textIterator != null && documentIterator.CurrentFileName != null) { ProvidedDocumentInformation currentInfo = documentIterator.Current; if (currentInfo != null) { if (!info.Equals(currentInfo)) // create new iterator, if document changed { info = currentInfo; textIterator = textIteratorBuilder.BuildTextIterator(info); } else // old document -> initialize iterator position to caret pos { textIterator.Position = info.CurrentOffset; } SearchResultMatch result = CreateNamedSearchResult(searchStrategy.FindNext(textIterator)); if (result != null) { info.CurrentOffset = textIterator.Position; return(result); } } } // not found or first start -> move forward to the next document if (documentIterator.MoveForward()) { info = documentIterator.Current; // document is valid for searching -> set iterator & fileName if (info != null && info.EndOffset >= 0 && info.EndOffset <= info.Document.TextLength) { textIterator = textIteratorBuilder.BuildTextIterator(info); } else { textIterator = null; } return(FindNext(monitor)); } return(null); }
public static void MarkAll(IProgressMonitor monitor) { SetSearchOptions(monitor); ClearSelection(); if (PAT.Common.Ultility.Ultility.IsUnixOS) { ITextEditorControlProvider provider = FormMain.CurrentActiveTab; if (provider != null) { provider.TextEditorControl.Document.BookmarkManager.Clear(); } } else { if (WorkbenchSingleton.ActiveControl != null) { ITextEditorControlProvider provider = WorkbenchSingleton.ActiveControl as ITextEditorControlProvider; if (provider != null) { provider.TextEditorControl.Document.BookmarkManager.Clear(); } } } find.Reset(); if (!find.SearchStrategy.CompilePattern(monitor)) { return; } List <TextEditorControl> textAreas = new List <TextEditorControl>(); int count; for (count = 0;; count++) { SearchResultMatch result = SearchReplaceManager.find.FindNext(monitor); if (result == null) { break; } else { MarkResult(textAreas, result); } } find.Reset(); foreach (TextEditorControl ctl in textAreas) { ctl.Refresh(); } ShowMarkDoneMessage(count, monitor); }
void FindNextButtonClicked(object sender, EventArgs e) { try { WritebackOptions(); var location = new SearchLocation(SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories, SearchOptions.SearchTarget == SearchTarget.CurrentSelection ? SearchManager.GetActiveSelection(true) : null); var strategy = SearchStrategyFactory.Create(SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchMode); lastMatch = SearchManager.FindNext(strategy, location); SearchManager.SelectResult(lastMatch); Focus(); } catch (SearchPatternException ex) { MessageService.ShowError(ex.Message); } }
public static void Replace(SearchResultMatch lastMatch, string replacement) { if (lastMatch == null) { return; } ITextEditor textArea = OpenTextArea(lastMatch.FileName); if (textArea != null) { textArea.Document.Replace(lastMatch.StartOffset, lastMatch.Length, lastMatch.TransformReplacePattern(replacement)); } }
public static void FindNext(IProgressMonitor monitor) { SetSearchOptions(monitor); if (find == null || SearchOptions.FindPattern == null || SearchOptions.FindPattern.Length == 0) { return; } if (!find.SearchStrategy.CompilePattern(monitor)) { find.Reset(); lastResult = null; return; } ITextEditor textArea = null; while (textArea == null) { SearchResultMatch result = find.FindNext(monitor); if (result == null) { ShowNotFoundMessage(monitor); find.Reset(); lastResult = null; return; } else { textArea = OpenTextArea(result.FileName); if (textArea != null) { if (lastResult != null && lastResult.FileName == result.FileName && textArea.Caret.Offset != lastResult.Offset + lastResult.Length) { find.Reset(); } int startPos = Math.Min(textArea.Document.TextLength, Math.Max(0, result.Offset)); int endPos = Math.Min(textArea.Document.TextLength, startPos + result.Length); textArea.Select(startPos, endPos - startPos); lastResult = result; } } } }
public static bool IsResultSelected(SearchResultMatch result) { if (result == null) { return(false); } var editor = GetActiveTextEditor(); if (editor == null) { return(false); } return(result.FileName.Equals(editor.FileName) && result.StartOffset == editor.SelectionStart && result.Length == editor.SelectionLength); }
public SearchResultNode(SearchResultMatch result) { this.result = result; IDocument document = result.CreateDocument(); var startPosition = result.GetStartPosition(document); int lineNumber = startPosition.Line; int column = startPosition.Column; this.anchor = new PermanentAnchor(result.FileName, lineNumber, column); anchor.SurviveDeletion = true; if (lineNumber >= 1 && lineNumber <= document.TotalNumberOfLines) { IDocumentLine matchedLine = document.GetLine(lineNumber); inlineBuilder = new HighlightedInlineBuilder(matchedLine.Text); inlineBuilder.SetFontFamily(0, inlineBuilder.Text.Length, resultLineFamily); IHighlighter highlighter = document.GetService(typeof(IHighlighter)) as IHighlighter; if (highlighter != null) { HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber); int startOffset = highlightedLine.DocumentLine.Offset; // copy only the foreground color foreach (HighlightedSection section in highlightedLine.Sections) { if (section.Color.Foreground != null) { inlineBuilder.SetForeground(section.Offset - startOffset, section.Length, section.Color.Foreground.GetBrush(null)); } } } // now highlight the match in bold if (column >= 1) { var endPosition = result.GetEndPosition(document); if (endPosition.Line == startPosition.Line && endPosition.Column > startPosition.Column) { // subtract one from the column to get the offset inside the line's text int startOffset = column - 1; int endOffset = Math.Min(inlineBuilder.Text.Length, endPosition.Column - 1); inlineBuilder.SetFontWeight(startOffset, endOffset - startOffset, FontWeights.Bold); } } } }
public static void SelectResult(SearchResultMatch result) { if (result == null) { ShowNotFoundMessage(); return; } var editor = OpenTextArea(result.FileName, true); if (editor != null) { var start = editor.Document.PositionToOffset(result.StartLocation.Line, result.StartLocation.Column); var end = editor.Document.PositionToOffset(result.EndLocation.Line, result.EndLocation.Column); editor.Caret.Offset = start; editor.Select(start, end - start); } }
public SearchResultMatch FindNext(int offset, int length) { if (info != null && textIterator != null && documentIterator.CurrentFileName != null) { ProvidedDocumentInformation currentInfo = documentIterator.Current; if (currentInfo != null) { if (!info.Equals(currentInfo)) // create new iterator, if document changed { info = currentInfo; textIterator = textIteratorBuilder.BuildTextIterator(info); } else // old document -> initialize iterator position to caret pos { textIterator.Position = info.CurrentOffset; } SearchResultMatch result = CreateNamedSearchResult(searchStrategy.FindNext(textIterator, offset, length)); if (result != null) { info.CurrentOffset = textIterator.Position; return(result); } } } // not found or first start -> move forward to the next document if (documentIterator.MoveForward()) { info = documentIterator.Current; // document is valid for searching -> set iterator & fileName if (info != null && info.EndOffset >= 0 && info.EndOffset <= info.Document.TextLength) { textIterator = textIteratorBuilder.BuildTextIterator(info); } else { textIterator = null; } return(FindNext(offset, length)); } return(null); }
static void MarkResult(List <TextEditorControl> textAreas, SearchResultMatch result) { TextEditorControl textArea = OpenTextArea(result.FileName); if (textArea != null) { if (!textAreas.Contains(textArea)) { textAreas.Add(textArea); } textArea.ActiveTextAreaControl.Caret.Position = textArea.Document.OffsetToPosition(result.Offset); LineSegment segment = textArea.Document.GetLineSegmentForOffset(result.Offset); int lineNr = segment.LineNumber; if (!textArea.Document.BookmarkManager.IsMarked(lineNr)) { textArea.Document.BookmarkManager.ToggleMarkAt(new TextLocation(result.Offset - segment.Offset, lineNr)); } } }
public static void FindAll(int offset, int length, IProgressMonitor monitor) { if (!InitializeSearchInFiles(monitor)) { return; } List <SearchResultMatch> results = new List <SearchResultMatch>(); while (true) { SearchResultMatch result = find.FindNext(offset, length); if (result == null) { break; } results.Add(result); } FinishSearchInFiles(results); }
public static void FindFirstInSelection(int offset, int length, IProgressMonitor monitor) { foundAtLeastOneItem = false; textSelection = null; SetSearchOptions(monitor); if (find == null || SearchOptions.FindPattern == null || SearchOptions.FindPattern.Length == 0) { return; } if (!find.SearchStrategy.CompilePattern(monitor)) { find.Reset(); lastResult = null; return; } textSelection = new TextSelection(offset, length); FindNextInSelection(monitor); }
static void MarkResult(SearchResultMatch result, bool switchToOpenedView = true) { ITextEditor textArea = OpenTextArea(result.FileName, switchToOpenedView); if (textArea != null) { if (switchToOpenedView) { textArea.Caret.Position = result.StartLocation; } foreach (var bookmark in BookmarkManager.GetBookmarks(result.FileName)) { if (bookmark.CanToggle && bookmark.LineNumber == result.StartLocation.Line) { // bookmark or breakpoint already exists at that line return; } } BookmarkManager.AddMark(new Bookmark(result.FileName, result.StartLocation)); } }
public static void FindNext(IProgressMonitor monitor) { SetSearchOptions(monitor); if (find == null || SearchOptions.FindPattern == null || SearchOptions.FindPattern.Length == 0) { return; } if (!find.SearchStrategy.CompilePattern(monitor)) { find.Reset(); lastResult = null; return; } TextEditorControl textArea = null; while (textArea == null) { SearchResultMatch result = find.FindNext(monitor); if (result == null) { ShowNotFoundMessage(monitor); find.Reset(); lastResult = null; return; } else { textArea = OpenTextArea(result.FileName); if (textArea != null) { if (lastResult != null && lastResult.FileName == result.FileName && textArea.ActiveTextAreaControl.Caret.Offset != lastResult.Offset + lastResult.Length) { find.Reset(); } int startPos = Math.Min(textArea.Document.TextLength, Math.Max(0, result.Offset)); int endPos = Math.Min(textArea.Document.TextLength, startPos + result.Length); SearchReplaceUtilities.SelectText(textArea, startPos, endPos); lastResult = result; } } } }
public static bool FindNextInSelection(IProgressMonitor monitor) { ITextEditor textArea = null; while (textArea == null) { SearchResultMatch result = find.FindNext(textSelection.Offset, textSelection.Length); if (result == null) { if (!foundAtLeastOneItem) { ShowNotFoundMessage(monitor); } find.Reset(); lastResult = null; foundAtLeastOneItem = false; return(false); } else { textArea = OpenTextArea(result.FileName); if (textArea != null) { foundAtLeastOneItem = true; if (lastResult != null && lastResult.FileName == result.FileName && textArea.Caret.Offset != lastResult.Offset + lastResult.Length) { } int startPos = Math.Min(textArea.Document.TextLength, Math.Max(0, result.Offset)); int endPos = Math.Min(textArea.Document.TextLength, startPos + result.Length); textArea.Select(startPos, endPos - startPos); lastResult = result; } } } return(true); }
public static void ReplaceAll(IProgressMonitor monitor) { SetSearchOptions(monitor); ClearSelection(); find.Reset(); if (!find.SearchStrategy.CompilePattern(monitor)) { return; } List <TextEditorControl> textAreas = new List <TextEditorControl>(); TextEditorControl textArea = null; for (int count = 0;; count++) { SearchResultMatch result = SearchReplaceManager.find.FindNext(monitor); if (result == null) { if (count != 0) { foreach (TextEditorControl ta in textAreas) { ta.EndUpdate(); ta.Refresh(); } } ShowReplaceDoneMessage(count, monitor); find.Reset(); return; } else { if (textArea == null || textArea.FileName != result.FileName) { // we need to open another text area textArea = OpenTextArea(result.FileName); if (textArea != null) { if (!textAreas.Contains(textArea)) { textArea.BeginUpdate(); textArea.ActiveTextAreaControl.TextArea.SelectionManager.SelectionCollection.Clear(); textAreas.Add(textArea); } } } if (textArea != null) { string transformedPattern = result.TransformReplacePattern(SearchOptions.ReplacePattern); find.Replace(result.Offset, result.Length, transformedPattern); if (find.CurrentDocumentInformation.Document == null) { textArea.Document.Replace(result.Offset, result.Length, transformedPattern); } } else { count--; } } } }
static void MarkResult(List<TextEditorControl> textAreas, SearchResultMatch result) { TextEditorControl textArea = OpenTextArea(result.FileName); if (textArea != null) { if (!textAreas.Contains(textArea)) { textAreas.Add(textArea); } textArea.ActiveTextAreaControl.Caret.Position = textArea.Document.OffsetToPosition(result.Offset); LineSegment segment = textArea.Document.GetLineSegmentForOffset(result.Offset); int lineNr = segment.LineNumber; if (!textArea.Document.BookmarkManager.IsMarked(lineNr)) { textArea.Document.BookmarkManager.ToggleMarkAt(new TextLocation(result.Offset - segment.Offset, lineNr)); } } }
public static bool FindNextInSelection(IProgressMonitor monitor) { TextEditorControl textArea = null; while (textArea == null) { SearchResultMatch result = find.FindNext(textSelection.Offset, textSelection.Length); if (result == null) { if (!foundAtLeastOneItem) { ShowNotFoundMessage(monitor); } find.Reset(); lastResult = null; foundAtLeastOneItem = false; return false; } else { textArea = OpenTextArea(result.FileName); if (textArea != null) { foundAtLeastOneItem = true; if (lastResult != null && lastResult.FileName == result.FileName && textArea.ActiveTextAreaControl.Caret.Offset != lastResult.Offset + lastResult.Length) { } int startPos = Math.Min(textArea.Document.TextLength, Math.Max(0, result.Offset)); int endPos = Math.Min(textArea.Document.TextLength, startPos + result.Length); SearchReplaceUtilities.SelectText(textArea, startPos, endPos); lastResult = result; } } } return true; }
public static void ShowAsSearchResults(string pattern, List<Reference> list) { if (list == null) return; List<SearchResultMatch> results = new List<SearchResultMatch>(list.Count); foreach (Reference r in list) { SearchResultMatch res = new SearchResultMatch(r.Offset, r.Length); res.ProvidedDocumentInformation = GetDocumentInformation(r.FileName); results.Add(res); } SearchResultPanel.Instance.ShowSearchResults(new SearchResult(pattern, results)); }
public SearchResultMatch FindNext() { // Setup search inside current or first file. SearchableFileContentFinder finder = new SearchableFileContentFinder(); int index = GetCurrentFileIndex(); int i = 0; int searchOffset = 0; SearchResultMatch result = null; if (index > -1) { ITextEditor editor = GetActiveTextEditor(); if (editor.SelectionLength > 0) { searchOffset = editor.SelectionStart + editor.SelectionLength; } else { searchOffset = editor.Caret.Offset + 1; } var document = editor.Document; int length; // if (target == SearchTarget.CurrentSelection) selection will not be null // hence use the selection as search region. if (location.Selection != null) { searchOffset = Math.Max(location.Selection.Offset, searchOffset); length = location.Selection.EndOffset - searchOffset; } else { length = document.TextLength - searchOffset; } // try to find a result if (length > 0 && (searchOffset + length) <= document.TextLength) { result = Find(editor.FileName, document, searchOffset, length); } } // try the other files until we find something, or have processed all of them while (result == null && i < files.Length) { index = (index + 1) % files.Length; FileName file = files[index]; ITextBuffer buffer = finder.Create(file); if (buffer == null) { continue; } int length; if (location.Selection != null) { searchOffset = location.Selection.Offset; length = location.Selection.Length; } else { searchOffset = 0; length = buffer.TextLength; } // try to find a result result = Find(file, DocumentUtilitites.LoadReadOnlyDocumentFromBuffer(buffer), searchOffset, length); i++; } return(result); }
public SearchResultNode(SearchResultMatch result) { this.result = result; this.anchor = new PermanentAnchor(result.FileName, result.StartLocation.Line, result.StartLocation.Column); anchor.SurviveDeletion = true; }
public static void ReplaceAll(IProgressMonitor monitor) { SetSearchOptions(monitor); ClearSelection(); find.Reset(); if (!find.SearchStrategy.CompilePattern(monitor)) { return; } List <ITextEditor> textAreas = new List <ITextEditor>(); ITextEditor textArea = null; for (int count = 0;; count++) { SearchResultMatch result = SearchReplaceManager.find.FindNext(monitor); if (result == null) { if (count != 0) { foreach (ITextEditor ta in textAreas) { ta.Document.EndUndoableAction(); } } ShowReplaceDoneMessage(count, monitor); find.Reset(); return; } else { if (textArea == null || textArea.FileName != result.FileName) { // we need to open another text area textArea = OpenTextArea(result.FileName); if (textArea != null) { if (!textAreas.Contains(textArea)) { textArea.Document.StartUndoableAction(); textArea.Select(textArea.SelectionStart, 0); textAreas.Add(textArea); } } } if (textArea != null) { string transformedPattern = result.TransformReplacePattern(SearchOptions.ReplacePattern); find.Replace(result.Offset, result.Length, transformedPattern); if (find.CurrentDocumentInformation.IsDocumentCreatedFromTextBuffer) { textArea.Document.Replace(result.Offset, result.Length, transformedPattern); } } else { count--; } } } }