From 6682aa6f9253e350eb0d6f3e0ffb8c1462c6edf2 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 10 Sep 2012 13:39:34 +0200 Subject: [PATCH] NRefactory changes from SharpDevelop: add IDocument.FileName and IAssembly.FullAssemblyName, --- .../CSharpProjectContent.cs | 14 ++++++- .../TypeSystem/CSharpAssembly.cs | 4 ++ ICSharpCode.NRefactory/Editor/IDocument.cs | 11 ++++++ .../Editor/ReadOnlyDocument.cs | 17 ++++++-- .../Editor/StringBuilderDocument.cs | 12 +++++- .../TypeSystem/CecilLoader.cs | 6 +-- .../TypeSystem/IAssembly.cs | 12 +++++- .../TypeSystem/ISupportsInterning.cs | 2 +- .../DefaultUnresolvedAssembly.cs | 39 ++++++++++++++++++- ICSharpCode.NRefactory/Utils/KeyComparer.cs | 21 ++++++++++ .../Utils/MultiDictionary.cs | 20 ++++++++++ 11 files changed, 145 insertions(+), 13 deletions(-) diff --git a/ICSharpCode.NRefactory.CSharp/CSharpProjectContent.cs b/ICSharpCode.NRefactory.CSharp/CSharpProjectContent.cs index fccba4622f..5d2112875e 100644 --- a/ICSharpCode.NRefactory.CSharp/CSharpProjectContent.cs +++ b/ICSharpCode.NRefactory.CSharp/CSharpProjectContent.cs @@ -31,6 +31,7 @@ namespace ICSharpCode.NRefactory.CSharp public class CSharpProjectContent : IProjectContent { string assemblyName; + string fullAssemblyName; string projectFileName; string location; Dictionary unresolvedFiles; @@ -48,6 +49,7 @@ namespace ICSharpCode.NRefactory.CSharp protected CSharpProjectContent(CSharpProjectContent pc) { this.assemblyName = pc.assemblyName; + this.fullAssemblyName = pc.fullAssemblyName; this.projectFileName = pc.projectFileName; this.location = pc.location; this.unresolvedFiles = new Dictionary(pc.unresolvedFiles, Platform.FileNameComparer); @@ -71,6 +73,10 @@ namespace ICSharpCode.NRefactory.CSharp get { return assemblyName; } } + public string FullAssemblyName { + get { return fullAssemblyName; } + } + public string Location { get { return location; } } @@ -128,10 +134,16 @@ namespace ICSharpCode.NRefactory.CSharp return new CSharpProjectContent(this); } + /// + /// Sets both the short and the full assembly names. + /// + /// New full assembly name. public IProjectContent SetAssemblyName(string newAssemblyName) { CSharpProjectContent pc = Clone(); - pc.assemblyName = newAssemblyName; + pc.fullAssemblyName = newAssemblyName; + int pos = newAssemblyName != null ? newAssemblyName.IndexOf(',') : -1; + pc.assemblyName = pos < 0 ? newAssemblyName : newAssemblyName.Substring(0, pos); return pc; } diff --git a/ICSharpCode.NRefactory.CSharp/TypeSystem/CSharpAssembly.cs b/ICSharpCode.NRefactory.CSharp/TypeSystem/CSharpAssembly.cs index ec9b9cfeb4..9b0f15a531 100644 --- a/ICSharpCode.NRefactory.CSharp/TypeSystem/CSharpAssembly.cs +++ b/ICSharpCode.NRefactory.CSharp/TypeSystem/CSharpAssembly.cs @@ -54,6 +54,10 @@ namespace ICSharpCode.NRefactory.CSharp.TypeSystem get { return projectContent.AssemblyName; } } + public string FullAssemblyName { + get { return projectContent.FullAssemblyName; } + } + public IList AssemblyAttributes { get { return GetAttributes(ref assemblyAttributes, true); diff --git a/ICSharpCode.NRefactory/Editor/IDocument.cs b/ICSharpCode.NRefactory/Editor/IDocument.cs index f9a793b911..fdd28113db 100644 --- a/ICSharpCode.NRefactory/Editor/IDocument.cs +++ b/ICSharpCode.NRefactory/Editor/IDocument.cs @@ -190,5 +190,16 @@ namespace ICSharpCode.NRefactory.Editor /// /// ITextAnchor CreateAnchor(int offset); + + /// + /// Gets the name of the file the document is stored in. + /// Could also be a non-existent dummy file name or null if no name has been set. + /// + string FileName { get; } + + /// + /// Fired when the file name of the document changes. + /// + event EventHandler FileNameChanged; } } diff --git a/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs b/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs index 0cc4b68dc5..7e0e9ac188 100644 --- a/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs +++ b/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs @@ -28,6 +28,7 @@ namespace ICSharpCode.NRefactory.Editor public sealed class ReadOnlyDocument : IDocument { readonly ITextSource textSource; + readonly string fileName; int[] lines; static readonly char[] newline = { '\r', '\n' }; @@ -35,12 +36,13 @@ namespace ICSharpCode.NRefactory.Editor /// /// Creates a new ReadOnlyDocument from the given text source. /// - public ReadOnlyDocument(ITextSource textSource) + public ReadOnlyDocument(ITextSource textSource, string fileName = null) { if (textSource == null) throw new ArgumentNullException("textSource"); // ensure that underlying buffer is immutable this.textSource = textSource.CreateSnapshot(); + this.fileName = fileName; List lines = new List(); lines.Add(0); int offset = 0; @@ -58,8 +60,8 @@ namespace ICSharpCode.NRefactory.Editor /// /// Creates a new ReadOnlyDocument from the given string. /// - public ReadOnlyDocument(string text) - : this(new StringTextSource(text)) + public ReadOnlyDocument(string text, string fileName = null) + : this(new StringTextSource(text), fileName) { } @@ -423,5 +425,14 @@ namespace ICSharpCode.NRefactory.Editor { return null; } + + /// + /// Will never be raised on . + public event EventHandler FileNameChanged { add {} remove {} } + + /// + public string FileName { + get { return fileName; } + } } } diff --git a/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs b/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs index d310f34c4a..bcd14a939a 100644 --- a/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs +++ b/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs @@ -312,13 +312,13 @@ namespace ICSharpCode.NRefactory.Editor /// public string Text { - get { + get { if (cachedText == null) cachedText = b.ToString(); return cachedText; } set { - Replace(0, b.Length, value); + Replace(0, b.Length, value); } } @@ -481,5 +481,13 @@ namespace ICSharpCode.NRefactory.Editor { return null; } + + /// + public virtual event EventHandler FileNameChanged { add {} remove {} } + + /// + public virtual string FileName { + get { return string.Empty; } + } } } diff --git a/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs b/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs index 82ad487cea..f272fe4750 100644 --- a/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs +++ b/ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs @@ -175,7 +175,7 @@ namespace ICSharpCode.NRefactory.TypeSystem assemblyAttributes = interningProvider.InternList(assemblyAttributes); moduleAttributes = interningProvider.InternList(moduleAttributes); - this.currentAssembly = new CecilUnresolvedAssembly(assemblyDefinition.Name.Name, this.DocumentationProvider); + this.currentAssembly = new CecilUnresolvedAssembly(assemblyDefinition.Name, this.DocumentationProvider); currentAssembly.Location = assemblyDefinition.MainModule.FullyQualifiedName; currentAssembly.AssemblyAttributes.AddRange(assemblyAttributes); currentAssembly.ModuleAttributes.AddRange(assemblyAttributes); @@ -267,8 +267,8 @@ namespace ICSharpCode.NRefactory.TypeSystem { readonly IDocumentationProvider documentationProvider; - public CecilUnresolvedAssembly(string assemblyName, IDocumentationProvider documentationProvider) - : base(assemblyName) + public CecilUnresolvedAssembly(AssemblyNameDefinition assemblyName, IDocumentationProvider documentationProvider) + : base(assemblyName.FullName) { Debug.Assert(assemblyName != null); this.documentationProvider = documentationProvider; diff --git a/ICSharpCode.NRefactory/TypeSystem/IAssembly.cs b/ICSharpCode.NRefactory/TypeSystem/IAssembly.cs index 1c98a0498e..c93484300a 100644 --- a/ICSharpCode.NRefactory/TypeSystem/IAssembly.cs +++ b/ICSharpCode.NRefactory/TypeSystem/IAssembly.cs @@ -30,7 +30,12 @@ namespace ICSharpCode.NRefactory.TypeSystem /// Gets the assembly name (short name). /// string AssemblyName { get; } - + + /// + /// Gets the full assembly name (including public key token etc.) + /// + string FullAssemblyName { get; } + /// /// Gets the path to the assembly location. /// For projects it is the same as the output path. @@ -81,6 +86,11 @@ namespace ICSharpCode.NRefactory.TypeSystem /// string AssemblyName { get; } + /// + /// Gets the full assembly name (including public key token etc.) + /// + string FullAssemblyName { get; } + /// /// Gets the list of all assembly attributes in the project. /// diff --git a/ICSharpCode.NRefactory/TypeSystem/ISupportsInterning.cs b/ICSharpCode.NRefactory/TypeSystem/ISupportsInterning.cs index 21c2e1b36c..369eac6487 100644 --- a/ICSharpCode.NRefactory/TypeSystem/ISupportsInterning.cs +++ b/ICSharpCode.NRefactory/TypeSystem/ISupportsInterning.cs @@ -22,7 +22,7 @@ namespace ICSharpCode.NRefactory.TypeSystem { /// /// Interface for TypeSystem objects that support interning. - /// See for more information. + /// See for more information. /// public interface ISupportsInterning { diff --git a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedAssembly.cs b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedAssembly.cs index d74c1cae84..1111ef638f 100644 --- a/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedAssembly.cs +++ b/ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultUnresolvedAssembly.cs @@ -36,6 +36,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation public class DefaultUnresolvedAssembly : AbstractFreezable, IUnresolvedAssembly { string assemblyName; + string fullAssemblyName; IList assemblyAttributes; IList moduleAttributes; Dictionary typeDefinitions = new Dictionary(FullNameAndTypeParameterCountComparer.Ordinal); @@ -51,15 +52,28 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation } } + /// + /// Creates a new unresolved assembly. + /// + /// Full assembly name public DefaultUnresolvedAssembly(string assemblyName) { if (assemblyName == null) throw new ArgumentNullException("assemblyName"); - this.assemblyName = assemblyName; + this.fullAssemblyName = assemblyName; + int pos = assemblyName != null ? assemblyName.IndexOf(',') : -1; + this.assemblyName = pos < 0 ? assemblyName : assemblyName.Substring(0, pos); this.assemblyAttributes = new List(); this.moduleAttributes = new List(); } + /// + /// Gets/Sets the short assembly name. + /// + /// + /// This class handles the short and the full name independently; + /// if you change the short name, you should also change the full name. + /// public string AssemblyName { get { return assemblyName; } set { @@ -69,7 +83,24 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation assemblyName = value; } } - + + /// + /// Gets/Sets the full assembly name. + /// + /// + /// This class handles the short and the full name independently; + /// if you change the full name, you should also change the short name. + /// + public string FullAssemblyName { + get { return fullAssemblyName; } + set { + if (value == null) + throw new ArgumentNullException("value"); + FreezableHelper.ThrowIfFrozen(this); + fullAssemblyName = value; + } + } + string location; public string Location { get { @@ -287,6 +318,10 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation get { return unresolvedAssembly.AssemblyName; } } + public string FullAssemblyName { + get { return unresolvedAssembly.FullAssemblyName; } + } + public IList AssemblyAttributes { get; private set; } public IList ModuleAttributes { get; private set; } diff --git a/ICSharpCode.NRefactory/Utils/KeyComparer.cs b/ICSharpCode.NRefactory/Utils/KeyComparer.cs index 2bb657e469..27d2fa342f 100644 --- a/ICSharpCode.NRefactory/Utils/KeyComparer.cs +++ b/ICSharpCode.NRefactory/Utils/KeyComparer.cs @@ -27,6 +27,21 @@ namespace ICSharpCode.NRefactory.Utils { return new KeyComparer(keySelector, Comparer.Default, EqualityComparer.Default); } + + public static KeyComparer Create(Func keySelector, IComparer comparer, IEqualityComparer equalityComparer) + { + return new KeyComparer(keySelector, comparer, equalityComparer); + } + + public static IComparer Create(Func keySelector, IComparer comparer) + { + return new KeyComparer(keySelector, comparer, EqualityComparer.Default); + } + + public static IEqualityComparer Create(Func keySelector, IEqualityComparer equalityComparer) + { + return new KeyComparer(keySelector, Comparer.Default, equalityComparer); + } } public class KeyComparer : IComparer, IEqualityComparer @@ -37,6 +52,12 @@ namespace ICSharpCode.NRefactory.Utils public KeyComparer(Func keySelector, IComparer keyComparer, IEqualityComparer keyEqualityComparer) { + if (keySelector == null) + throw new ArgumentNullException("keySelector"); + if (keyComparer == null) + throw new ArgumentNullException("keyComparer"); + if (keyEqualityComparer == null) + throw new ArgumentNullException("keyEqualityComparer"); this.keySelector = keySelector; this.keyComparer = keyComparer; this.keyEqualityComparer = keyEqualityComparer; diff --git a/ICSharpCode.NRefactory/Utils/MultiDictionary.cs b/ICSharpCode.NRefactory/Utils/MultiDictionary.cs index b081ef06f3..96adc8eb16 100644 --- a/ICSharpCode.NRefactory/Utils/MultiDictionary.cs +++ b/ICSharpCode.NRefactory/Utils/MultiDictionary.cs @@ -62,6 +62,15 @@ namespace ICSharpCode.NRefactory.Utils return false; } + /// + /// Removes all entries with the specified key. + /// + /// Returns true if at least one entry was removed. + public bool RemoveAll(TKey key) + { + return dict.Remove(key); + } + public void Clear() { dict.Clear(); @@ -81,10 +90,21 @@ namespace ICSharpCode.NRefactory.Utils } } + /// + /// Returns the number of different keys. + /// public int Count { get { return dict.Count; } } + public ICollection Keys { + get { return dict.Keys; } + } + + public IEnumerable Values { + get { return dict.Values.SelectMany(list => list); } + } + IEnumerable ILookup.this[TKey key] { get { return this[key]; } }