Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Allow grouping search results by file.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Sep 28, 2010
1 parent b1f91f0 commit 169c640
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 11 deletions.
Expand Up @@ -5,8 +5,11 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

using ICSharpCode.Core;
using ICSharpCode.Core.Presentation;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Editor.Search;
using ICSharpCode.SharpDevelop.Gui;

Expand Down Expand Up @@ -43,13 +46,69 @@ public object GetControl()
WorkbenchSingleton.AssertMainThread();
if (resultsTreeViewInstance == null)
resultsTreeViewInstance = new ResultsTreeView();
rootNode.GroupResultsByFile(ResultsTreeView.GroupResultsByFile);
resultsTreeViewInstance.ItemsSource = new object[] { rootNode };
return resultsTreeViewInstance;
}

static IList toolbarItems;
static MenuItem flatItem, perFileItem;

public IList GetToolbarItems()
{
return null;
WorkbenchSingleton.AssertMainThread();
if (toolbarItems == null) {
toolbarItems = new List<object>();
DropDownButton perFileDropDown = new DropDownButton();
perFileDropDown.Content = new Image { Height = 16, Source = PresentationResourceService.GetBitmapSource("Icons.16x16.FindIcon") };
perFileDropDown.SetValueToExtension(DropDownButton.ToolTipProperty, new LocalizeExtension("MainWindow.Windows.SearchResultPanel.SelectViewMode.ToolTip"));

flatItem = new MenuItem();
flatItem.SetValueToExtension(MenuItem.HeaderProperty, new LocalizeExtension("MainWindow.Windows.SearchResultPanel.Flat"));
flatItem.Click += delegate { SetPerFile(false); };

perFileItem = new MenuItem();
perFileItem.SetValueToExtension(MenuItem.HeaderProperty, new LocalizeExtension("MainWindow.Windows.SearchResultPanel.PerFile"));
perFileItem.Click += delegate { SetPerFile(true); };

perFileDropDown.DropDownMenu = new ContextMenu();
perFileDropDown.DropDownMenu.Items.Add(flatItem);
perFileDropDown.DropDownMenu.Items.Add(perFileItem);
toolbarItems.Add(perFileDropDown);
toolbarItems.Add(new Separator());

Button expandAll = new Button();
expandAll.SetValueToExtension(Button.ToolTipProperty, new LocalizeExtension("MainWindow.Windows.SearchResultPanel.ExpandAll.ToolTip"));
expandAll.Content = new Image { Height = 16, Source = PresentationResourceService.GetBitmapSource("Icons.16x16.OpenAssembly") };
expandAll.Click += delegate { ExpandCollapseAll(true); };
toolbarItems.Add(expandAll);

Button collapseAll = new Button();
collapseAll.SetValueToExtension(Button.ToolTipProperty, new LocalizeExtension("MainWindow.Windows.SearchResultPanel.CollapseAll.ToolTip"));
collapseAll.Content = new Image { Height = 16, Source = PresentationResourceService.GetBitmapSource("Icons.16x16.Assembly") };
collapseAll.Click += delegate { ExpandCollapseAll(false); };
toolbarItems.Add(collapseAll);
}
return toolbarItems;
}

static void ExpandCollapseAll(bool newIsExpanded)
{
if (resultsTreeViewInstance != null) {
foreach (SearchNode node in resultsTreeViewInstance.ItemsSource.OfType<SearchNode>().Flatten(n => n.Children)) {
node.IsExpanded = newIsExpanded;
}
}
}

static void SetPerFile(bool perFile)
{
ResultsTreeView.GroupResultsByFile = perFile;
if (resultsTreeViewInstance != null) {
foreach (SearchRootNode node in resultsTreeViewInstance.ItemsSource.OfType<SearchRootNode>()) {
node.GroupResultsByFile(perFile);
}
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/AddIns/Misc/SearchAndReplace/Project/Gui/ResultsTreeView.cs
Expand Up @@ -5,6 +5,8 @@
using System.Windows.Controls;
using System.Windows.Input;

using ICSharpCode.Core;

namespace SearchAndReplace
{
/// <summary>
Expand Down Expand Up @@ -35,5 +37,14 @@ protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
node.ActivateItem();
e.Handled = true;
}

public static bool GroupResultsByFile {
get {
return PropertyService.Get("SearchAndReplace.GroupResultsByFile", false);
}
set {
PropertyService.Set("SearchAndReplace.GroupResultsByFile", value);
}
}
}
}
35 changes: 35 additions & 0 deletions src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchFileNode.cs
@@ -0,0 +1,35 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using System.IO;
using System.Windows.Controls;
using System.Windows.Documents;

using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Editor.Search;

namespace SearchAndReplace
{
class SearchFileNode : SearchNode
{
FileName fileName;

public SearchFileNode(FileName fileName, SearchResultNode[] resultNodes)
{
this.fileName = fileName;
this.Children = resultNodes;
this.IsExpanded = true;
}

protected override object CreateText()
{
return new TextBlock {
Inlines = {
new Bold(new Run(Path.GetFileName(fileName))),
new Run(StringParser.Parse(" (${res:MainWindow.Windows.SearchResultPanel.In} ") + Path.GetDirectoryName(fileName) + ")")
}
};
}
}
}
Expand Up @@ -35,7 +35,7 @@ public SearchResultNode(SearchResultMatch result)
var startPosition = result.GetStartPosition(document);
int lineNumber = startPosition.Line;
int column = startPosition.Column;
this.anchor = new PermanentAnchor(FileName.Create(result.FileName), lineNumber, column);
this.anchor = new PermanentAnchor(result.FileName, lineNumber, column);
anchor.SurviveDeletion = true;

if (lineNumber >= 1 && lineNumber <= document.TotalNumberOfLines) {
Expand Down Expand Up @@ -82,6 +82,10 @@ public SearchResultNode(SearchResultMatch result)
}
}

public FileName FileName {
get { return anchor.FileName; }
}

protected override object CreateText()
{
var location = anchor.Location;
Expand Down
27 changes: 19 additions & 8 deletions src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchRootNode.cs
Expand Up @@ -18,33 +18,44 @@ namespace SearchAndReplace
{
sealed class SearchRootNode : SearchNode
{
IList<SearchResultNode> results;
int fileCount;
IList<SearchResultNode> resultNodes;
IList<SearchFileNode> fileNodes;

public string Title { get; private set; }

public SearchRootNode(string title, IList<SearchResultMatch> results)
{
this.Title = title;
this.results = results.Select(r => new SearchResultNode(r)).ToArray();
this.resultNodes = results.Select(r => new SearchResultNode(r)).ToArray();
this.fileNodes = resultNodes.GroupBy(r => r.FileName).Select(g => new SearchFileNode(g.Key, g.ToArray())).ToArray();

fileCount = results.GroupBy(r => r.FileName).Count();
this.Children = this.results;
this.Children = this.resultNodes;
this.IsExpanded = true;
}

public void GroupResultsByFile(bool perFile)
{
if (perFile)
this.Children = fileNodes;
else
this.Children = resultNodes;
foreach (SearchResultNode node in resultNodes) {
node.ShowFileName = !perFile;
}
}

public int Occurrences {
get { return results.Count; }
get { return resultNodes.Count; }
}

protected override object CreateText()
{
return new TextBlock {
Inlines = {
new Bold(new Run(this.Title)),
new Run(" (" + GetOccurrencesString(results.Count)
new Run(" (" + GetOccurrencesString(resultNodes.Count)
+ StringParser.Parse(" ${res:MainWindow.Windows.SearchResultPanel.In} ")
+ GetFileCountString(fileCount) + ")")
+ GetFileCountString(fileNodes.Count) + ")")
}
};
}
Expand Down
Expand Up @@ -98,6 +98,7 @@
</Compile>
<Compile Include="Gui\SearchAndReplaceDialog.cs" />
<Compile Include="Gui\SearchAndReplacePanel.cs" />
<Compile Include="Gui\SearchFileNode.cs" />
<Compile Include="Gui\SearchNode.cs" />
<Compile Include="Gui\SearchResultNode.cs" />
<Compile Include="Gui\SearchRootNode.cs" />
Expand Down Expand Up @@ -131,6 +132,11 @@
<Name>ICSharpCode.Core</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Main\ICSharpCode.Core.Presentation\ICSharpCode.Core.Presentation.csproj">
<Project>{7E4A7172-7FF5-48D0-B719-7CD959DD1AC9}</Project>
<Name>ICSharpCode.Core.Presentation</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Main\ICSharpCode.Core.WinForms\ICSharpCode.Core.WinForms.csproj">
<Project>{857CA1A3-FC88-4BE0-AB6A-D1EE772AB288}</Project>
<Name>ICSharpCode.Core.WinForms</Name>
Expand Down

0 comments on commit 169c640

Please sign in to comment.