Browse Source

Allow grouping search results by file.

pull/1/head
Daniel Grunwald 15 years ago
parent
commit
169c640613
  1. 63
      src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs
  2. 11
      src/AddIns/Misc/SearchAndReplace/Project/Gui/ResultsTreeView.cs
  3. 35
      src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchFileNode.cs
  4. 6
      src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchResultNode.cs
  5. 27
      src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchRootNode.cs
  6. 6
      src/AddIns/Misc/SearchAndReplace/Project/SearchAndReplace.csproj

63
src/AddIns/Misc/SearchAndReplace/Project/Gui/DefaultSearchResult.cs

@ -5,8 +5,11 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; 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.Editor.Search;
using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop.Gui;
@ -43,13 +46,69 @@ namespace SearchAndReplace
WorkbenchSingleton.AssertMainThread(); WorkbenchSingleton.AssertMainThread();
if (resultsTreeViewInstance == null) if (resultsTreeViewInstance == null)
resultsTreeViewInstance = new ResultsTreeView(); resultsTreeViewInstance = new ResultsTreeView();
rootNode.GroupResultsByFile(ResultsTreeView.GroupResultsByFile);
resultsTreeViewInstance.ItemsSource = new object[] { rootNode }; resultsTreeViewInstance.ItemsSource = new object[] { rootNode };
return resultsTreeViewInstance; return resultsTreeViewInstance;
} }
static IList toolbarItems;
static MenuItem flatItem, perFileItem;
public IList GetToolbarItems() 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);
}
}
} }
} }

11
src/AddIns/Misc/SearchAndReplace/Project/Gui/ResultsTreeView.cs

@ -5,6 +5,8 @@ using System;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using ICSharpCode.Core;
namespace SearchAndReplace namespace SearchAndReplace
{ {
/// <summary> /// <summary>
@ -35,5 +37,14 @@ namespace SearchAndReplace
node.ActivateItem(); node.ActivateItem();
e.Handled = true; e.Handled = true;
} }
public static bool GroupResultsByFile {
get {
return PropertyService.Get("SearchAndReplace.GroupResultsByFile", false);
}
set {
PropertyService.Set("SearchAndReplace.GroupResultsByFile", value);
}
}
} }
} }

35
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) + ")")
}
};
}
}
}

6
src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchResultNode.cs

@ -35,7 +35,7 @@ namespace SearchAndReplace
var startPosition = result.GetStartPosition(document); var startPosition = result.GetStartPosition(document);
int lineNumber = startPosition.Line; int lineNumber = startPosition.Line;
int column = startPosition.Column; 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; anchor.SurviveDeletion = true;
if (lineNumber >= 1 && lineNumber <= document.TotalNumberOfLines) { if (lineNumber >= 1 && lineNumber <= document.TotalNumberOfLines) {
@ -82,6 +82,10 @@ namespace SearchAndReplace
} }
} }
public FileName FileName {
get { return anchor.FileName; }
}
protected override object CreateText() protected override object CreateText()
{ {
var location = anchor.Location; var location = anchor.Location;

27
src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchRootNode.cs

@ -18,23 +18,34 @@ namespace SearchAndReplace
{ {
sealed class SearchRootNode : SearchNode sealed class SearchRootNode : SearchNode
{ {
IList<SearchResultNode> results; IList<SearchResultNode> resultNodes;
int fileCount; IList<SearchFileNode> fileNodes;
public string Title { get; private set; } public string Title { get; private set; }
public SearchRootNode(string title, IList<SearchResultMatch> results) public SearchRootNode(string title, IList<SearchResultMatch> results)
{ {
this.Title = title; 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.resultNodes;
this.Children = this.results;
this.IsExpanded = true; 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 { public int Occurrences {
get { return results.Count; } get { return resultNodes.Count; }
} }
protected override object CreateText() protected override object CreateText()
@ -42,9 +53,9 @@ namespace SearchAndReplace
return new TextBlock { return new TextBlock {
Inlines = { Inlines = {
new Bold(new Run(this.Title)), new Bold(new Run(this.Title)),
new Run(" (" + GetOccurrencesString(results.Count) new Run(" (" + GetOccurrencesString(resultNodes.Count)
+ StringParser.Parse(" ${res:MainWindow.Windows.SearchResultPanel.In} ") + StringParser.Parse(" ${res:MainWindow.Windows.SearchResultPanel.In} ")
+ GetFileCountString(fileCount) + ")") + GetFileCountString(fileNodes.Count) + ")")
} }
}; };
} }

6
src/AddIns/Misc/SearchAndReplace/Project/SearchAndReplace.csproj

@ -98,6 +98,7 @@
</Compile> </Compile>
<Compile Include="Gui\SearchAndReplaceDialog.cs" /> <Compile Include="Gui\SearchAndReplaceDialog.cs" />
<Compile Include="Gui\SearchAndReplacePanel.cs" /> <Compile Include="Gui\SearchAndReplacePanel.cs" />
<Compile Include="Gui\SearchFileNode.cs" />
<Compile Include="Gui\SearchNode.cs" /> <Compile Include="Gui\SearchNode.cs" />
<Compile Include="Gui\SearchResultNode.cs" /> <Compile Include="Gui\SearchResultNode.cs" />
<Compile Include="Gui\SearchRootNode.cs" /> <Compile Include="Gui\SearchRootNode.cs" />
@ -131,6 +132,11 @@
<Name>ICSharpCode.Core</Name> <Name>ICSharpCode.Core</Name>
<Private>False</Private> <Private>False</Private>
</ProjectReference> </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"> <ProjectReference Include="..\..\..\..\Main\ICSharpCode.Core.WinForms\ICSharpCode.Core.WinForms.csproj">
<Project>{857CA1A3-FC88-4BE0-AB6A-D1EE772AB288}</Project> <Project>{857CA1A3-FC88-4BE0-AB6A-D1EE772AB288}</Project>
<Name>ICSharpCode.Core.WinForms</Name> <Name>ICSharpCode.Core.WinForms</Name>

Loading…
Cancel
Save