From f1ce3e926fcb25947b86872bc354a175b975bc5e Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 16 Feb 2011 18:49:06 +0100 Subject: [PATCH] Fix order of modifiers. --- .../CSharp/Ast/CSharpModifierToken.cs | 52 +++++++++++-------- .../CSharp/Ast/TypeMembers/AttributedNode.cs | 4 +- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/ICSharpCode.NRefactory/CSharp/Ast/CSharpModifierToken.cs b/ICSharpCode.NRefactory/CSharp/Ast/CSharpModifierToken.cs index 3b48cdf598..6b2b12959a 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/CSharpModifierToken.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/CSharpModifierToken.cs @@ -25,6 +25,7 @@ // THE SOFTWARE. using System; using System.Collections.Generic; +using System.Linq; namespace ICSharpCode.NRefactory.CSharp { @@ -36,33 +37,40 @@ namespace ICSharpCode.NRefactory.CSharp public Modifiers Modifier { get { return modifier; } set { - modifier = value; - if (!lengthTable.TryGetValue (modifier, out tokenLength)) - throw new InvalidOperationException ("Modifier " + modifier + " is invalid."); + for (int i = 0; i < lengthTable.Count; i++) { + if (lengthTable[i].Key == value) { + this.modifier = value; + this.tokenLength = lengthTable[i].Value; + return; + } + } + throw new ArgumentException ("Modifier " + value + " is invalid."); } } - static Dictionary lengthTable = new Dictionary () { - { Modifiers.Public, "public".Length }, - { Modifiers.Protected, "protected".Length }, - { Modifiers.Private, "private".Length }, - { Modifiers.Internal, "internal".Length }, - { Modifiers.New, "new".Length }, - { Modifiers.Unsafe, "unsafe".Length }, - { Modifiers.Abstract, "abstract".Length }, - { Modifiers.Virtual, "virtual".Length }, - { Modifiers.Sealed, "sealed".Length }, - { Modifiers.Static, "static".Length }, - { Modifiers.Override, "override".Length }, - { Modifiers.Readonly, "readonly".Length }, - { Modifiers.Volatile, "volatile".Length }, - { Modifiers.Extern, "extern".Length }, - { Modifiers.Partial, "partial".Length }, - { Modifiers.Const, "const".Length }, + // Not worth using a dictionary for such few elements. + // This table is sorted in the order that modifiers should be output when generating code. + static readonly List> lengthTable = new List> () { + new KeyValuePair(Modifiers.Public, "public".Length), + new KeyValuePair(Modifiers.Protected, "protected".Length), + new KeyValuePair(Modifiers.Private, "private".Length), + new KeyValuePair(Modifiers.Internal, "internal".Length), + new KeyValuePair(Modifiers.New, "new".Length), + new KeyValuePair(Modifiers.Unsafe, "unsafe".Length), + new KeyValuePair(Modifiers.Abstract, "abstract".Length), + new KeyValuePair(Modifiers.Virtual, "virtual".Length), + new KeyValuePair(Modifiers.Sealed, "sealed".Length), + new KeyValuePair(Modifiers.Static, "static".Length), + new KeyValuePair(Modifiers.Override, "override".Length), + new KeyValuePair(Modifiers.Readonly, "readonly".Length), + new KeyValuePair(Modifiers.Volatile, "volatile".Length), + new KeyValuePair(Modifiers.Extern, "extern".Length), + new KeyValuePair(Modifiers.Partial, "partial".Length), + new KeyValuePair(Modifiers.Const, "const".Length) }; - public static ICollection AllModifiers { - get { return lengthTable.Keys; } + public static IEnumerable AllModifiers { + get { return lengthTable.Select(p => p.Key); } } public CSharpModifierToken (AstLocation location, Modifiers modifier) : base (location, 0) diff --git a/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/AttributedNode.cs b/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/AttributedNode.cs index 99d8af8ecc..a2734ae4fc 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/AttributedNode.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/TypeMembers/AttributedNode.cs @@ -42,7 +42,9 @@ namespace ICSharpCode.NRefactory.CSharp if ((m & newValue) != 0) { if ((m & oldValue) == 0) { // Modifier was added - node.InsertChildAfter(insertionPos, new CSharpModifierToken(AstLocation.Empty, m), ModifierRole); + var newToken = new CSharpModifierToken(AstLocation.Empty, m); + node.InsertChildAfter(insertionPos, newToken, ModifierRole); + insertionPos = newToken; } else { // Modifier already exists insertionPos = node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m);