Browse Source

Merge pull request #91 from mono-soc-2012/simonl-attributesection-fix

Fix end location of AttributeSections
newNRvisualizers
Mike Krüger 14 years ago
parent
commit
5241e34d02
  1. 8
      ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs
  2. 38
      ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs

8
ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs

@ -335,12 +335,16 @@ namespace ICSharpCode.NRefactory.CSharp @@ -335,12 +335,16 @@ namespace ICSharpCode.NRefactory.CSharp
if (loc != null && pos < loc.Count)
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Colon);
}
int attributeCount = 0;
foreach (var attr in GetAttributes (optAttributes)) {
result.AddChild (attr, Roles.Attribute);
attributeCount++;
}
// Left and right bracket + commas between the attributes
int locCount = 2 + attributeCount - 1;
// optional comma
if (loc != null && pos < loc.Count - 1 && !loc [pos].Equals (loc [pos + 1]))
if (loc != null && pos < loc.Count - 1 && loc.Count == locCount + 1)
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
if (loc != null && pos < loc.Count)
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.RBracket);

38
ICSharpCode.NRefactory.Tests/CSharp/Parser/GeneralScope/AttributeSectionTests.cs

@ -108,6 +108,44 @@ public class Form1 { @@ -108,6 +108,44 @@ public class Form1 {
}
}}});
}
[Test]
public void TwoAttributesInSameSectionLocations()
{
string program = @"[A, B] class Test {}";
TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
var attributeSection = type.Attributes.Single();
var firstAttribute = attributeSection.Attributes.First();
Assert.AreEqual(2, firstAttribute.StartLocation.Column);
Assert.AreEqual(3, firstAttribute.EndLocation.Column);
var lastAttribute = attributeSection.Attributes.Last();
Assert.AreEqual(5, lastAttribute.StartLocation.Column);
Assert.AreEqual(6, lastAttribute.EndLocation.Column);
Assert.AreEqual(1, attributeSection.StartLocation.Column);
Assert.AreEqual(7, attributeSection.EndLocation.Column);
}
[Test]
public void TwoAttributesWithOptionalCommaInSameSectionLocations()
{
string program = @"[A, B,] class Test {}";
TypeDeclaration type = ParseUtilCSharp.ParseGlobal<TypeDeclaration>(program);
var attributeSection = type.Attributes.Single();
var firstAttribute = attributeSection.Attributes.First();
Assert.AreEqual(2, firstAttribute.StartLocation.Column);
Assert.AreEqual(3, firstAttribute.EndLocation.Column);
var lastAttribute = attributeSection.Attributes.Last();
Assert.AreEqual(5, lastAttribute.StartLocation.Column);
Assert.AreEqual(6, lastAttribute.EndLocation.Column);
Assert.AreEqual(1, attributeSection.StartLocation.Column);
Assert.AreEqual(8, attributeSection.EndLocation.Column);
}
[Test]
public void AttributesOnTypeParameter()

Loading…
Cancel
Save