diff --git a/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs b/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs index 65e786917d..96cf9dabf5 100644 --- a/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs +++ b/src/AddIns/Analysis/MachineSpecifications/MachineSpecifications.Tests/Src/MSpecTestFrameworkTests.cs @@ -31,12 +31,12 @@ namespace ICSharpCode.MachineSpecifications.Tests testProject = fake.an(); var mspecReference = MockRepository.GenerateStub(testProject); mspecReference.setup(x => x.ShortName).Return(MSpecAssemblyName); - testProject.setup(x => x.Items).Return(new ImmutableModelCollection(new[] { mspecReference })); + testProject.setup(x => x.Items).Return(new SimpleModelCollection(new[] { mspecReference })); nonTestProject = fake.an(); var otherReference = MockRepository.GenerateStub(nonTestProject); mspecReference.setup(x => x.ShortName).Return("System.Configuration"); - nonTestProject.setup(x => x.Items).Return(new ImmutableModelCollection(new[] { otherReference })); + nonTestProject.setup(x => x.Items).Return(new SimpleModelCollection(new[] { otherReference })); }; Because of = () => { diff --git a/src/AddIns/Debugger/Debugger.AddIn/Debugger.AddIn.csproj b/src/AddIns/Debugger/Debugger.AddIn/Debugger.AddIn.csproj index 638ef25ee4..ab07982060 100644 --- a/src/AddIns/Debugger/Debugger.AddIn/Debugger.AddIn.csproj +++ b/src/AddIns/Debugger/Debugger.AddIn/Debugger.AddIn.csproj @@ -218,7 +218,6 @@ - diff --git a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphDiff.cs b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphDiff.cs index 3e1747a66a..606109ed55 100644 --- a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphDiff.cs +++ b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphDiff.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using ICSharpCode.SharpDevelop; using Debugger.AddIn.Visualizers.Utils; namespace Debugger.AddIn.Visualizers.Graph.Layout @@ -45,7 +46,7 @@ namespace Debugger.AddIn.Visualizers.Graph.Layout public PositionedNode GetMatchingNewNode(PositionedNode oldNode) { - return matching.GetValue(oldNode); + return matching.GetOrDefault(oldNode); } internal void SetAdded(PositionedNode addedNode) diff --git a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphMatcher.cs b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphMatcher.cs index 3a4c5e0ff3..1ba0da14b2 100644 --- a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphMatcher.cs +++ b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/Layout/GraphMatcher.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using ICSharpCode.SharpDevelop; using Debugger.AddIn.Visualizers.Utils; namespace Debugger.AddIn.Visualizers.Graph.Layout @@ -69,7 +70,7 @@ namespace Debugger.AddIn.Visualizers.Graph.Layout PositionedNode MatchNode(PositionedNode oldNode, Dictionary newNodeMap) { - PositionedNode newNodeFound = newNodeMap.GetValue(oldNode.ObjectNode.HashCode); + PositionedNode newNodeFound = newNodeMap.GetOrDefault(oldNode.ObjectNode.HashCode); if ((newNodeFound != null) && IsSameAddress(oldNode, newNodeFound)) { return newNodeFound; } else { diff --git a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/ObjectGraph/ObjectGraphBuilder.cs b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/ObjectGraph/ObjectGraphBuilder.cs index a43512c486..262d660ea0 100644 --- a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/ObjectGraph/ObjectGraphBuilder.cs +++ b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/GraphVisualizer/ObjectGraph/ObjectGraphBuilder.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using ICSharpCode.NRefactory.Utils; using Debugger.AddIn.TreeModel; using Debugger.AddIn.Visualizers.Graph.Layout; using Debugger.AddIn.Visualizers.Utils; @@ -55,7 +56,7 @@ namespace Debugger.AddIn.Visualizers.Graph /// /// Given hash code, lookup already existing node(s) with this hash code. /// - private Multimap objectNodesForHashCode = new Multimap(); + private MultiDictionary objectNodesForHashCode = new MultiDictionary(); /// /// Creates ObjectGraphBuilder. @@ -302,7 +303,7 @@ namespace Debugger.AddIn.Visualizers.Graph { int objectHashCode = value.InvokeDefaultGetHashCode(); // are there any nodes with the same hash code? - IList nodesWithSameHashCode = objectNodesForHashCode[objectHashCode]; + var nodesWithSameHashCode = objectNodesForHashCode[objectHashCode]; if (nodesWithSameHashCode == null) { return null; } else { diff --git a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/DebuggerHelpers.cs b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/DebuggerHelpers.cs index c447e566a9..4bdf58d767 100644 --- a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/DebuggerHelpers.cs +++ b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/DebuggerHelpers.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Runtime.InteropServices; +using ICSharpCode.NRefactory.CSharp; using Debugger.AddIn.TreeModel; using Debugger.AddIn.Visualizers.Graph; using Debugger.Interop.CorDebug; @@ -74,7 +75,7 @@ namespace Debugger.AddIn.Visualizers.Utils /// public static bool IsAtomic(this IType type) { - return TypeSystemExtensions.IsPrimitiveType(type) || type.FullName == "System.String" || type.Kind == TypeKind.Enum; + return TypeSystemExtensions.IsPrimitiveType(type) || type.IsKnownType(KnownTypeCode.String) || type.Kind == TypeKind.Enum; } /// @@ -121,7 +122,7 @@ namespace Debugger.AddIn.Visualizers.Utils /// public static string FormatNameCSharp(this IType type) { - return type.ToString(); // TODO use an existing C# IType formatter? + return new CSharpAmbience().ConvertType(type); } public static IEnumerable GetFieldsAndNonIndexedProperties(this IType type, GetMemberOptions options = GetMemberOptions.None) { diff --git a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/Multimap.cs b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/Multimap.cs deleted file mode 100644 index c11a81a169..0000000000 --- a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/Multimap.cs +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) -// This code is distributed under the BSD license (for details please see \src\AddIns\Debugger\Debugger.AddIn\license.txt) - -using System; -using System.Collections.Generic; - -namespace Debugger.AddIn.Visualizers.Utils -{ - /// Dictionary that can store multiple values for each key. - public class Multimap - { - /// Wrapped dictionary - private Dictionary> dictionary; - - public Multimap() - { - dictionary = new Dictionary>(); - } - - public IList this[TKey key] - { - get { - IList values = null; - if (dictionary.TryGetValue(key, out values)) { - return values; - } - return null; - } - } - - public void Add(TKey key, TValue value) - { - IList values = null; - if (!dictionary.TryGetValue(key, out values)) { - values = new List(); - dictionary.Add(key, values); - } - values.Add(value); - } - } -} diff --git a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/Util.cs b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/Util.cs index 2dbfb43aad..9efeaf27fe 100644 --- a/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/Util.cs +++ b/src/AddIns/Debugger/Debugger.AddIn/Visualizers/Utils/Util.cs @@ -18,41 +18,6 @@ namespace Debugger.AddIn.Visualizers.Utils return source.Max(selector); } - public static string Repeat(char c, int count) - { - StringBuilder sb = new StringBuilder(); - sb.Append(c, count); - return sb.ToString(); - } - - /// - /// Gets value from Dictionary. Returns null if not found. - /// - public static TValue GetValue(this Dictionary dictionary, TKey key) where TValue : class - { - TValue outValue; - if (dictionary.TryGetValue(key, out outValue)) { - return outValue; - } - return null; - } - - /// - /// Builds a Dictionary for quickly searching a collection. Item keys must be unique. - /// - /// Collection for which to build Dictionary. - /// Function returning key by which to index. - public static Dictionary MakeDictionary(this IEnumerable collection, Func keySelector) - { - Dictionary dictionary = new Dictionary(); - foreach (V item in collection) { - K key = keySelector(item); - if (dictionary.ContainsKey(key)) throw new InvalidOperationException("MakeDictionary: key " + key + " seen twice"); - dictionary[key] = item; - } - return dictionary; - } - public static List Sorted(this List list, IComparer comparer) { list.Sort(comparer); diff --git a/src/Main/Base/Project/Dom/ImmutableModelCollection.cs b/src/Main/Base/Project/Dom/ImmutableModelCollection.cs index 33ac9daa8f..4767dccb2d 100644 --- a/src/Main/Base/Project/Dom/ImmutableModelCollection.cs +++ b/src/Main/Base/Project/Dom/ImmutableModelCollection.cs @@ -5,14 +5,24 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using ICSharpCode.NRefactory.Utils; namespace ICSharpCode.SharpDevelop.Dom { /// /// An immutable model collection. /// - public class ImmutableModelCollection : ReadOnlyCollection, IModelCollection, IMutableModelCollection + public class ImmutableModelCollection : ReadOnlyCollection, IModelCollection { + static ImmutableModelCollection empty = new ImmutableModelCollection(Enumerable.Empty()); + + /// + /// Gets the empty model collection. + /// + public static ImmutableModelCollection Empty { + get { return empty; } + } + public ImmutableModelCollection(IEnumerable items) : base(items.ToList()) { @@ -24,6 +34,13 @@ namespace ICSharpCode.SharpDevelop.Dom { return this; } + } + + class ImmutableModelCollectionImplementsMutableInterface : ImmutableModelCollection, IMutableModelCollection + { + public ImmutableModelCollectionImplementsMutableInterface(IEnumerable items) : base(items) + { + } void IMutableModelCollection.AddRange(IEnumerable items) { diff --git a/src/Main/Base/Project/Dom/ModelCollectionLinq.cs b/src/Main/Base/Project/Dom/ModelCollectionLinq.cs index fd5f159b1b..13766c567d 100644 --- a/src/Main/Base/Project/Dom/ModelCollectionLinq.cs +++ b/src/Main/Base/Project/Dom/ModelCollectionLinq.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices.WindowsRuntime; namespace ICSharpCode.SharpDevelop.Dom { /// - /// Provides LINQ operators for . + /// Provides LINQ operators for . /// public static class ModelCollectionLinq { @@ -18,23 +18,34 @@ namespace ICSharpCode.SharpDevelop.Dom // to implement that without leaking memory. // The problem is that IModelCollection is unordered; but ObservableCollection requires us to maintain a stable order. + #region OfType / Cast + public static IModelCollection OfType(this IModelCollection source) + { + return SelectMany(source, item => item is TResult ? new ImmutableModelCollection(new[] { (TResult)item }) : ImmutableModelCollection.Empty); + } + + public static IModelCollection Cast(this IModelCollection source) + { + return SelectMany(source, item => new ImmutableModelCollection(new[] { (TResult)item })); + } + #endregion + #region Where - /*public static IModelCollection Where(this IModelCollection source, Func predicate) + public static IModelCollection Where(this IModelCollection source, Func predicate) { - - }*/ + return SelectMany(source, item => predicate(item) ? new ImmutableModelCollection(new[] { item }) : ImmutableModelCollection.Empty); + } #endregion #region Select public static IModelCollection Select(this IModelCollection source, Func selector) { - // HACK: emulating Select with SelectMany is much less efficient than a direct implementation could be - return SelectMany(source, item => new ImmutableModelCollection(new[] { item }), (a, b) => selector(b)); + return SelectMany(source, item => new ImmutableModelCollection(new[] { selector(item) })); } #endregion #region SelectMany - public static IModelCollection SelectMany(this IModelCollection input, Func> selector) + public static IModelCollection SelectMany(this IModelCollection input, Func> selector) { return SelectMany(input, selector, (a, b) => b); } diff --git a/src/Main/Base/Project/Src/Project/AbstractProject.cs b/src/Main/Base/Project/Src/Project/AbstractProject.cs index 72a1d4e9b3..8045cf6f0c 100644 --- a/src/Main/Base/Project/Src/Project/AbstractProject.cs +++ b/src/Main/Base/Project/Src/Project/AbstractProject.cs @@ -311,7 +311,7 @@ namespace ICSharpCode.SharpDevelop.Project [Browsable(false)] public virtual IMutableModelCollection Items { get { - return new ImmutableModelCollection(Enumerable.Empty()); + return new ImmutableModelCollectionImplementsMutableInterface(Enumerable.Empty()); } } diff --git a/src/Main/Base/Test/Project/BeforeBuildCustomToolProjectItemsTests.cs b/src/Main/Base/Test/Project/BeforeBuildCustomToolProjectItemsTests.cs index b67a98ba10..5932228b0c 100644 --- a/src/Main/Base/Test/Project/BeforeBuildCustomToolProjectItemsTests.cs +++ b/src/Main/Base/Test/Project/BeforeBuildCustomToolProjectItemsTests.cs @@ -80,7 +80,7 @@ namespace ICSharpCode.SharpDevelop.Project FileProjectItem AddFileToProject(string include) { var projectItem = new FileProjectItem(projectHelper.Project, ItemType.Compile, include); - projectHelper.AddProjectItem(projectItem); + projectHelper.Project.Items.Add(projectItem); return projectItem; } diff --git a/src/Main/Base/Test/Utils/ProjectHelper.cs b/src/Main/Base/Test/Utils/ProjectHelper.cs index 23bfd7025c..d8cc09851b 100644 --- a/src/Main/Base/Test/Utils/ProjectHelper.cs +++ b/src/Main/Base/Test/Utils/ProjectHelper.cs @@ -14,25 +14,16 @@ namespace ICSharpCode.SharpDevelop.Tests.Utils public class ProjectHelper { public IProject Project = MockRepository.GenerateMock(); - public List ProjectItems = new List(); public ProjectHelper(string fileName) { Project.Stub(p => p.FileName).Return(FileName.Create(fileName)); - Project - .Stub(p => p.Items) - .Return(null) - .WhenCalled(mi => mi.ReturnValue = new ImmutableModelCollection(ProjectItems)); + Project.Stub(p => p.Items).Return(new SimpleModelCollection()); Project.Stub(p => p.Preferences).Return(new Properties()); Project.Stub(p => p.SyncRoot).Return(new Object()); } - - public void AddProjectItem(ProjectItem projectItem) - { - ProjectItems.Add(projectItem); - } } }