Browse Source

Add workaround for the code dom not returning the correct start column for methods without a modifier and with fully qualified return values.

pull/28/head
Matt Ward 13 years ago
parent
commit
3bf32ecde4
  1. 21
      src/AddIns/Misc/PackageManagement/Project/Src/ClassCodeGenerator.cs
  2. 25
      src/AddIns/Misc/PackageManagement/Test/Src/ClassCodeGeneratorTests.cs

21
src/AddIns/Misc/PackageManagement/Project/Src/ClassCodeGenerator.cs

@ -92,6 +92,16 @@ namespace ICSharpCode.PackageManagement @@ -92,6 +92,16 @@ namespace ICSharpCode.PackageManagement
return null;
}
/// <summary>
/// The fully qualified type will not be added as the method's return value since
/// NRefactory returns the incorrect start column for the method's region in this case.
/// Instead we add the last part of the type so for "System.Object" we add "Object" so the
/// method's region is correct. This only seems to affect methods where there is no modifier
/// (e.g. public, private) explicitly defined in code.
///
/// For MvcScaffolding we only need the start and end points to be correctly defined since the
/// function will be immediately replaced with different code.
/// </summary>
public CodeFunction AddPublicMethod(string name, string type)
{
CodeGenerator codeGenerator = GetCodeGenerator();
@ -104,10 +114,19 @@ namespace ICSharpCode.PackageManagement @@ -104,10 +114,19 @@ namespace ICSharpCode.PackageManagement
{
return new MethodDeclaration {
Name = name,
TypeReference = new TypeReference(type)
TypeReference = new TypeReference(GetShortTypeName(type))
};
}
string GetShortTypeName(string type)
{
int index = type.LastIndexOf('.');
if (index > 0) {
return type.Substring(index + 1);
}
return type;
}
CodeFunction GetMethodInserted(IRefactoringDocumentView view)
{
IMethod method = FindMethod(view);

25
src/AddIns/Misc/PackageManagement/Test/Src/ClassCodeGeneratorTests.cs

@ -205,7 +205,7 @@ namespace PackageManagement.Tests @@ -205,7 +205,7 @@ namespace PackageManagement.Tests
}
[Test]
public void AddPublicMethod_MethodReturnTypeIsCustomType_CodeForMethodAddedAtEndOfClass()
public void AddPublicMethod_MethodReturnTypeIsSystemCustomType_CodeForMethodAddedAtEndOfClass()
{
CreateClass("MyClass");
CreateCodeGenerator();
@ -228,6 +228,29 @@ namespace PackageManagement.Tests @@ -228,6 +228,29 @@ namespace PackageManagement.Tests
Assert.IsTrue(method.Body.IsNull);
}
/// <summary>
/// Workaround NRefactory giving incorrect begin column for interface methods that use fully
/// qualified return types (e.g. System.Object). Begin column points to the "Object" part of the
/// return type not the start of the return type.
/// </summary>
[Test]
public void AddPublicMethod_MethodReturnTypeIsSystemObject_SystemNotAddedToReturnType()
{
CreateClass("MyClass");
CreateCodeGenerator();
var classRegion = new DomRegion(1, 2, 3, 4);
helper.SetClassRegion(classRegion);
string fileName = @"d:\projects\myproject\MyClass.cs";
SetClassFileName(fileName);
SetDocumentFileName(fileName);
AddMethodToClassForReparse("MyClass.MyMethod");
AddPublicMethod("MyMethod", "System.Object");
MethodDeclaration method = fakeCodeGenerator.NodePassedToInsertCodeAtEnd as MethodDeclaration;
Assert.AreEqual("Object", method.TypeReference.Type);
}
[Test]
public void AddPublicMethod_MethodReturnTypeIsString_ReturnsCodeMethod()
{

Loading…
Cancel
Save