Browse Source

NRefactoryASTConvertVisitor: Apply IndexerNameAttributes to the names of the converted indexers.

This fixes the ReflectorAddIn being unable to find such indexers in Reflector.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4817 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Christian Hornung 16 years ago
parent
commit
679866ec5a
  1. 22
      src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/NRefactoryASTConvertVisitor.cs
  2. 50
      src/Main/ICSharpCode.SharpDevelop.Dom/Tests/ICSharpCode.SharpDevelop.Dom.Tests/NRefactoryAstConverterTests.cs

22
src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/NRefactoryASTConvertVisitor.cs

@ -10,6 +10,7 @@ using System; @@ -10,6 +10,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using ICSharpCode.NRefactory.Visitors;
using AST = ICSharpCode.NRefactory.Ast;
@ -719,11 +720,32 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -719,11 +720,32 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
i.Parameters.Add(CreateParameter(par));
}
}
// If an IndexerNameAttribute is specified, use the specified name
// for the indexer instead of the default name.
IAttribute indexerNameAttribute = i.Attributes.LastOrDefault(this.IsIndexerNameAttribute);
if (indexerNameAttribute != null && indexerNameAttribute.PositionalArguments.Count > 0) {
string name = indexerNameAttribute.PositionalArguments[0] as string;
if (!String.IsNullOrEmpty(name)) {
i.FullyQualifiedName = String.Concat(i.DeclaringType.FullyQualifiedName, ".", name);
}
}
DefaultClass c = GetCurrentClass();
c.Properties.Add(i);
return null;
}
bool IsIndexerNameAttribute(IAttribute att)
{
if (att == null || att.AttributeType == null)
return false;
string indexerNameAttributeFullName = typeof(System.Runtime.CompilerServices.IndexerNameAttribute).FullName;
IClass indexerNameAttributeClass = this.Cu.ProjectContent.GetClass(indexerNameAttributeFullName, 0, LanguageProperties.CSharp, GetClassOptions.Default | GetClassOptions.ExactMatch);
if (indexerNameAttributeClass == null) {
return String.Equals(att.AttributeType.FullyQualifiedName, indexerNameAttributeFullName, StringComparison.Ordinal);
}
return att.AttributeType.Equals(indexerNameAttributeClass.DefaultReturnType);
}
public override object VisitEventDeclaration(AST.EventDeclaration eventDeclaration, object data)
{
DomRegion region = GetRegion(eventDeclaration.StartLocation, eventDeclaration.EndLocation);

50
src/Main/ICSharpCode.SharpDevelop.Dom/Tests/ICSharpCode.SharpDevelop.Dom.Tests/NRefactoryAstConverterTests.cs

@ -16,9 +16,15 @@ namespace ICSharpCode.SharpDevelop.Dom.Tests @@ -16,9 +16,15 @@ namespace ICSharpCode.SharpDevelop.Dom.Tests
[TestFixture]
public class NRefactoryAstConverterTests
{
ICompilationUnit Parse(string code, SupportedLanguage language)
readonly ProjectContentRegistry projectContentRegistry = new ProjectContentRegistry();
ICompilationUnit Parse(string code, SupportedLanguage language, bool referenceMscorlib)
{
NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(new DefaultProjectContent());
DefaultProjectContent pc = new DefaultProjectContent();
if (referenceMscorlib) {
pc.AddReferencedContent(projectContentRegistry.Mscorlib);
}
NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(pc);
using (IParser p = ParserFactory.CreateParser(language, new StringReader(code))) {
p.ParseMethodBodies = false;
p.Parse();
@ -29,6 +35,11 @@ namespace ICSharpCode.SharpDevelop.Dom.Tests @@ -29,6 +35,11 @@ namespace ICSharpCode.SharpDevelop.Dom.Tests
return visitor.Cu;
}
ICompilationUnit Parse(string code, SupportedLanguage language)
{
return Parse(code, language, false);
}
ICompilationUnit Parse(string code)
{
return Parse(code, SupportedLanguage.CSharp);
@ -165,5 +176,40 @@ static class X {} @@ -165,5 +176,40 @@ static class X {}
Assert.IsTrue(c.IsSealed, "class should be sealed");
Assert.IsTrue(c.IsStatic, "class should be static");
}
[Test]
public void IndexerDefaultNameTest()
{
ICompilationUnit cu = Parse(@"
class X {
public int this[int index] {
get { return 0; }
}
}
");
IProperty p = cu.Classes[0].Properties[0];
Assert.IsTrue(p.IsIndexer, "IsIndexer must be true");
Assert.AreEqual("Item", p.Name);
Assert.AreEqual(1, p.Parameters.Count);
}
[Test]
public void IndexerNonDefaultNameTest()
{
ICompilationUnit cu = Parse(@"
using System.Runtime.CompilerServices;
class X {
[IndexerName(""Foo"")]
public int this[int index] {
get { return 0; }
}
}
", SupportedLanguage.CSharp, true);
IProperty p = cu.Classes[0].Properties[0];
Assert.IsTrue(p.IsIndexer, "IsIndexer must be true");
Assert.AreEqual("Foo", p.Name);
Assert.AreEqual(1, p.Parameters.Count);
}
}
}

Loading…
Cancel
Save