public static int ReplaceAll(int offset, int length) { SetSearchOptions(); find.Reset(); if (!find.SearchStrategy.CompilePattern()) { return(0); } for (var count = 0;; count++) { SearchResultMatch result = find.FindNext(offset, length); if (result == null) { return(count); } var 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 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(); doc.Text = "foo"; // 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); }
SearchedFile SearchForIssues(FileName fileName, ITextSource fileContent, IEnumerable <IssueManager.IssueProvider> providers, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var context = SDRefactoringContext.Create(fileName, fileContent, TextLocation.Empty, cancellationToken); ReadOnlyDocument document = null; IHighlighter highlighter = null; var results = new List <SearchResultMatch>(); foreach (var provider in providers) { cancellationToken.ThrowIfCancellationRequested(); foreach (var issue in provider.GetIssues(context)) { if (document == null) { document = new ReadOnlyDocument(fileContent, fileName); highlighter = SD.EditorControlService.CreateHighlighter(document); highlighter.BeginHighlighting(); } results.Add(SearchResultMatch.Create(document, issue.Start, issue.End, highlighter)); } } if (highlighter != null) { highlighter.Dispose(); } if (results.Count > 0) { return(new SearchedFile(fileName, results)); } else { return(null); } }
public static int MarkAll(int offset, int length) { SetSearchOptions(); find.Reset(); if (!find.SearchStrategy.CompilePattern()) { return(0); } 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(); } return(count); }
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); }
public static void ShowAsSearchResults(string title, List <Reference> list) { if (list == null) { return; } List <SearchResultMatch> results = new List <SearchResultMatch>(list.Count); TextDocument document = null; ITextBuffer buffer = null; FileName fileName = null; ISyntaxHighlighter highlighter = null; foreach (Reference r in list) { var f = new FileName(r.FileName); if (document == null || !f.Equals(fileName)) { buffer = ParserService.GetParseableFileContent(r.FileName); document = new TextDocument(DocumentUtilitites.GetTextSource(buffer)); fileName = new FileName(r.FileName); highlighter = EditorControlService.Instance.CreateHighlighter(new AvalonEditDocumentAdapter(document, null), fileName); } var start = document.GetLocation(r.Offset).ToLocation(); var end = document.GetLocation(r.Offset + r.Length).ToLocation(); var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter); SearchResultMatch res = new SearchResultMatch(fileName, start, end, r.Offset, r.Length, builder, highlighter.DefaultTextColor); results.Add(res); } SearchResultsPad.Instance.ShowSearchResults(title, results); SearchResultsPad.Instance.BringToFront(); }
public static void MarkAll() { SetSearchOptions(); ClearSelection(); find.Reset(); if (!find.SearchStrategy.CompilePattern()) { return; } List <TextEditorControl> textAreas = new List <TextEditorControl>(); int count; for (count = 0;; count++) { SearchResultMatch result = SearchReplaceManager.find.FindNext(); if (result == null) { break; } else { MarkResult(textAreas, result); } } find.Reset(); foreach (TextEditorControl ctl in textAreas) { ctl.Refresh(); } ShowMarkDoneMessage(count); }
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); }
static SearchResultMatch CreateSearchResultMatch(ReferenceEntry entry) { ITextSource textSource = SD.FileService.GetFileContent(entry.fileName); var document = new ReadOnlyDocument(textSource, entry.fileName); TextLocation start = document.GetLocation(entry.minChar); TextLocation end = document.GetLocation(entry.minChar + entry.length); IHighlighter highlighter = SD.EditorControlService.CreateHighlighter(document); return(SearchResultMatch.Create(document, start, end, highlighter)); }
void RenameAllReferences(List <SearchResultMatch> references) { SearchResultMatch firstResult = references.First(); var document = new ReadOnlyDocument(SD.FileService.GetFileContent(firstResult.FileName)); string name = document.GetText(firstResult.StartOffset, firstResult.Length); string newName = GetNewName(name); if (ShouldRenameReference(newName, name)) { ApplyRenames(references, newName); } }
public static bool FindNext() { SetSearchOptions(); if (find == null || SearchOptions.FindPattern == null || SearchOptions.FindPattern.Length == 0) { return(false); } if (!find.SearchStrategy.CompilePattern()) { find.Reset(); lastResult = null; return(false); } TextEditorControl textArea = null; while (textArea == null) { var result = find.FindNext(); if (result == null) { find.Reset(); lastResult = null; return(false); } 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; return(true); } } } return(false); }
public SearchResultMatch FindNext() { // insanity check Debug.Assert(searchStrategy != null); Debug.Assert(documentIterator != null); Debug.Assert(textIteratorBuilder != 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.TextBuffer != null && info.EndOffset >= 0 && info.EndOffset <= info.TextBuffer.Length) { textIterator = textIteratorBuilder.BuildTextIterator(info); } else { textIterator = null; } return(FindNext()); } return(null); }
public static void ShowAsSearchResults(string title, List <Reference> list) { if (list == null) { return; } List <SearchResultMatch> results = new List <SearchResultMatch>(list.Count); foreach (Reference r in list) { SearchResultMatch res = new SearchResultMatch(GetDocumentInformation(r.FileName), r.Offset, r.Length); results.Add(res); } SearchResultsPad.Instance.ShowSearchResults(title, results); SearchResultsPad.Instance.BringToFront(); }
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)); }
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 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(FileName.Create(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 override void FixtureSetUp() { base.FixtureSetUp(); SD.Services.AddService(typeof(IWorkbench), MockRepository.GenerateStub <IWorkbench>()); // Set up SearchOptions required by the BruteForceSearchStrategy. SearchOptions.CurrentFindPattern = "foo"; SearchOptions.MatchCase = false; SearchOptions.MatchWholeWord = false; // Create the document to be searched. var doc = new ReadOnlyDocument("foo"); var location = MockRepository.GenerateStub <SearchLocation>(SearchOptions.SearchTarget, SearchOptions.LookIn, SearchOptions.LookInFiletypes, SearchOptions.IncludeSubdirectories, null); location.Stub(l => l.GenerateFileList()).Return(new[] { new FileName(@"C:\Temp\test.txt") }); // Search the document. var strategy = SearchStrategyFactory.Create(SearchOptions.FindPattern, !SearchOptions.MatchCase, SearchOptions.MatchWholeWord, SearchOptions.SearchMode); result = SearchManager.FindNext(strategy, location); }
public static bool FindFirstInSelection(int offset, int length) { _foundAtLeastOneItem = false; textSelection = null; SetSearchOptions(); if (find == null || SearchOptions.FindPattern == null || SearchOptions.FindPattern.Length == 0) { return(false); } if (!find.SearchStrategy.CompilePattern()) { find.Reset(); lastResult = null; return(false); } textSelection = new TextSelection(offset, length); return(FindNextInSelection()); }
public static bool FindNextInSelection() { TextEditorControl textArea = null; while (textArea == null) { SearchResultMatch result = find.FindNext(textSelection.Offset, textSelection.Length); if (result == null) { if (!_foundAtLeastOneItem) { ShowNotFoundMessage(); } 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 title, List <Reference> list) { if (list == null) { return; } List <SearchResultMatch> results = new List <SearchResultMatch>(list.Count); TextDocument document = null; FileName fileName = null; IHighlighter highlighter = null; foreach (Reference r in list) { var f = new FileName(r.FileName); if (document == null || !f.Equals(fileName)) { document = new TextDocument(DocumentUtilitites.GetTextSource(ParserService.GetParseableFileContent(r.FileName))); fileName = new FileName(r.FileName); var def = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(r.FileName)); if (def != null) { highlighter = new DocumentHighlighter(document, def.MainRuleSet); } else { highlighter = null; } } var start = document.GetLocation(r.Offset).ToLocation(); var end = document.GetLocation(r.Offset + r.Length).ToLocation(); var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter); SearchResultMatch res = new SearchResultMatch(fileName, start, end, r.Offset, r.Length, builder); results.Add(res); } SearchResultsPad.Instance.ShowSearchResults(title, results); SearchResultsPad.Instance.BringToFront(); }
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; } } } }
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))); } }
void ReplaceButtonClicked(object sender, EventArgs e) { try { WritebackOptions(); if (SearchManager.IsResultSelected(lastMatch)) SearchManager.Replace(lastMatch, SearchOptions.ReplacePattern); 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 SearchResultNode(SearchResultMatch result) { this.result = result; this.anchor = new PermanentAnchor(result.FileName, result.StartLocation.Line, result.StartLocation.Column); anchor.SurviveDeletion = true; }
IEnumerable <SearchResultMatch> FindAndFixIssues(FileName fileName, List <IssueManager.IssueProvider> providers, CancellationToken cancellationToken, ref int fixedIssueCount) { cancellationToken.ThrowIfCancellationRequested(); var openedFile = SD.FileService.GetOpenedFile(fileName); bool documentWasLoadedFromDisk = false; IDocument document = null; if (openedFile != null && openedFile.CurrentView != null) { var provider = openedFile.CurrentView.GetService <IFileDocumentProvider>(); if (provider != null) { document = provider.GetDocumentForFile(openedFile); } } if (document == null) { documentWasLoadedFromDisk = true; document = new TextDocument(SD.FileService.GetFileContent(fileName)) { FileName = fileName }; } var context = SDRefactoringContext.Create(fileName, document, TextLocation.Empty, cancellationToken); List <CodeIssue> allIssues = new List <CodeIssue>(); bool documentWasChanged = false; foreach (var provider in providers) { cancellationToken.ThrowIfCancellationRequested(); var issues = provider.GetIssues(context).ToList(); // Fix issues, if possible: if (provider.Attribute.SupportsAutoFix && issues.Any(i => i.Actions.Count > 0)) { using (var script = context.StartScript()) { foreach (var issue in issues) { if (issue.Actions.Count > 0) { fixedIssueCount++; issue.Actions[0].Run(script); } } } documentWasChanged = true; // Update context now that we've modified the document context = SDRefactoringContext.Create(fileName, document, TextLocation.Empty, cancellationToken); // Find remaining issues: allIssues.AddRange(provider.GetIssues(context)); } else { allIssues.AddRange(issues); } } if (documentWasChanged && documentWasLoadedFromDisk) { // Save changes back to disk using (var writer = new StreamWriter(fileName, false, SD.FileService.DefaultFileEncoding)) { document.WriteTextTo(writer); } } if (allIssues.Count > 0) { using (var highlighter = SD.EditorControlService.CreateHighlighter(document)) { highlighter.BeginHighlighting(); return(allIssues.Select(issue => SearchResultMatch.Create(document, issue.Start, issue.End, highlighter)).ToList()); } } else { return(Enumerable.Empty <SearchResultMatch>()); } }
public static int ReplaceAll() { SetSearchOptions(); ClearSelection(); find.Reset(); if (!find.SearchStrategy.CompilePattern()) { return(0); } List <TextEditorControl> textAreas = new List <TextEditorControl>(); TextEditorControl textArea = null; for (int count = 0;; count++) { SearchResultMatch result = SearchReplaceManager.find.FindNext(); if (result == null) { if (count != 0) { foreach (TextEditorControl ta in textAreas) { ta.EndUpdate(); ta.Refresh(); } } find.Reset(); return(count); } 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--; } } } }
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); }
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; }
void FindNextButtonClicked(object sender, EventArgs e) { 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(); }