Browse Source

Fixed SD2-765: Create OnEvent method does not work for VB methods without delegate

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.0@1317 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
7ab5aec81b
  1. 2
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/VBNetAmbience.cs
  2. 50
      src/Main/Base/Project/Src/Dom/NRefactoryResolver/NRefactoryASTConvertVisitor.cs
  3. 42
      src/Main/Base/Project/Src/Services/RefactoringService/CodeGenerator.cs

2
src/AddIns/BackendBindings/VBNetBinding/Project/Src/VBNetAmbience.cs

@ -184,7 +184,7 @@ namespace VBNetBinding @@ -184,7 +184,7 @@ namespace VBNetBinding
builder.Append(')');
}
if (c.ClassType == ClassType.Delegate) {
if (ShowReturnType && c.ClassType == ClassType.Delegate) {
builder.Append("(");
if (IncludeHTMLMarkup) builder.Append("<br>");

50
src/Main/Base/Project/Src/Dom/NRefactoryResolver/NRefactoryASTConvertVisitor.cs

@ -326,7 +326,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -326,7 +326,7 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
return ret;
}
void ConvertTemplates(List<AST.TemplateDefinition> templateList, DefaultClass c)
void ConvertTemplates(IList<AST.TemplateDefinition> templateList, DefaultClass c)
{
int index = 0;
if (templateList.Count == 0) {
@ -367,42 +367,48 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -367,42 +367,48 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
DefaultClass c = new DefaultClass(cu, ClassType.Delegate, ConvertModifier(delegateDeclaration.Modifier, ModifierEnum.Internal), region, GetCurrentClass());
c.Documentation = GetDocumentation(region.BeginLine);
ConvertAttributes(delegateDeclaration, c);
CreateDelegate(c, delegateDeclaration.Name, delegateDeclaration.ReturnType,
delegateDeclaration.Templates, delegateDeclaration.Parameters);
return c;
}
void CreateDelegate(DefaultClass c, string name, AST.TypeReference returnType, IList<AST.TemplateDefinition> templates, IList<AST.ParameterDeclarationExpression> parameters)
{
c.BaseTypes.Add(ReflectionReturnType.CreatePrimitive(typeof(Delegate)));
if (currentClass.Count > 0) {
DefaultClass cur = GetCurrentClass();
cur.InnerClasses.Add(c);
c.FullyQualifiedName = cur.FullyQualifiedName + '.' + delegateDeclaration.Name;
c.FullyQualifiedName = cur.FullyQualifiedName + '.' + name;
} else {
if (currentNamespace.Count == 0) {
c.FullyQualifiedName = delegateDeclaration.Name;
c.FullyQualifiedName = name;
} else {
c.FullyQualifiedName = (string)currentNamespace.Peek() + '.' + delegateDeclaration.Name;
c.FullyQualifiedName = (string)currentNamespace.Peek() + '.' + name;
}
cu.Classes.Add(c);
}
currentClass.Push(c); // necessary for CreateReturnType
ConvertTemplates(delegateDeclaration.Templates, c);
DefaultMethod invokeMethod = new DefaultMethod("Invoke", CreateReturnType(delegateDeclaration.ReturnType), ModifierEnum.Public, DomRegion.Empty, DomRegion.Empty, c);
if (delegateDeclaration.Parameters != null) {
foreach (AST.ParameterDeclarationExpression par in delegateDeclaration.Parameters) {
ConvertTemplates(templates, c);
DefaultMethod invokeMethod = new DefaultMethod("Invoke", CreateReturnType(returnType), ModifierEnum.Public, c.Region, DomRegion.Empty, c);
if (parameters != null) {
foreach (AST.ParameterDeclarationExpression par in parameters) {
invokeMethod.Parameters.Add(CreateParameter(par));
}
}
c.Methods.Add(invokeMethod);
invokeMethod = new DefaultMethod("BeginInvoke", CreateReturnType(typeof(IAsyncResult)), ModifierEnum.Public, DomRegion.Empty, DomRegion.Empty, c);
if (delegateDeclaration.Parameters != null) {
foreach (AST.ParameterDeclarationExpression par in delegateDeclaration.Parameters) {
invokeMethod = new DefaultMethod("BeginInvoke", CreateReturnType(typeof(IAsyncResult)), ModifierEnum.Public, c.Region, DomRegion.Empty, c);
if (parameters != null) {
foreach (AST.ParameterDeclarationExpression par in parameters) {
invokeMethod.Parameters.Add(CreateParameter(par));
}
}
invokeMethod.Parameters.Add(new DefaultParameter("callback", CreateReturnType(typeof(AsyncCallback)), DomRegion.Empty));
invokeMethod.Parameters.Add(new DefaultParameter("object", ReflectionReturnType.Object, DomRegion.Empty));
c.Methods.Add(invokeMethod);
invokeMethod = new DefaultMethod("EndInvoke", CreateReturnType(delegateDeclaration.ReturnType), ModifierEnum.Public, DomRegion.Empty, DomRegion.Empty, c);
invokeMethod = new DefaultMethod("EndInvoke", CreateReturnType(returnType), ModifierEnum.Public, c.Region, DomRegion.Empty, c);
invokeMethod.Parameters.Add(new DefaultParameter("result", CreateReturnType(typeof(IAsyncResult)), DomRegion.Empty));
c.Methods.Add(invokeMethod);
currentClass.Pop();
return c;
}
IParameter CreateParameter(AST.ParameterDeclarationExpression par)
@ -544,9 +550,23 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver @@ -544,9 +550,23 @@ namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
{
DomRegion region = GetRegion(eventDeclaration.StartLocation, eventDeclaration.EndLocation);
DomRegion bodyRegion = GetRegion(eventDeclaration.BodyStart, eventDeclaration.BodyEnd);
IReturnType type = CreateReturnType(eventDeclaration.TypeReference);
DefaultClass c = GetCurrentClass();
DefaultEvent e = new DefaultEvent(eventDeclaration.Name, type, ConvertModifier(eventDeclaration.Modifier), region, bodyRegion, GetCurrentClass());
IReturnType type;
if (eventDeclaration.TypeReference.IsNull) {
DefaultClass del = new DefaultClass(cu, ClassType.Delegate,
ConvertModifier(eventDeclaration.Modifier),
region, c);
del.Modifiers |= ModifierEnum.Synthetic;
CreateDelegate(del, eventDeclaration.Name + "EventHandler",
new AST.TypeReference("System.Void"),
new AST.TemplateDefinition[0],
eventDeclaration.Parameters);
type = del.DefaultReturnType;
} else {
type = CreateReturnType(eventDeclaration.TypeReference);
}
DefaultEvent e = new DefaultEvent(eventDeclaration.Name, type, ConvertModifier(eventDeclaration.Modifier), region, bodyRegion, c);
ConvertAttributes(eventDeclaration, e);
c.Events.Add(e);
if (e != null) {

42
src/Main/Base/Project/Src/Services/RefactoringService/CodeGenerator.cs

@ -347,18 +347,22 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -347,18 +347,22 @@ namespace ICSharpCode.SharpDevelop.Refactoring
#region Generate OnEventMethod
public virtual MethodDeclaration CreateOnEventMethod(IEvent e)
{
TypeReference type;
if (e.ReturnType == null) {
type = new TypeReference("?");
} else if (e.ReturnType.TypeArguments != null && e.ReturnType.Name == "EventHandler") {
type = ConvertType(e.ReturnType.TypeArguments[0], new ClassFinder(e));
} else {
type = ConvertType(e.ReturnType, new ClassFinder(e));
if (type.Type.EndsWith("Handler"))
type.Type = type.Type.Substring(0, type.Type.Length - 7) + "Args";
ClassFinder context = new ClassFinder(e);
List<ParameterDeclarationExpression> parameters = new List<ParameterDeclarationExpression>();
bool sender = false;
if (e.ReturnType != null) {
IMethod invoke = e.ReturnType.GetMethods().Find(delegate(IMethod m) { return m.Name=="Invoke"; });
if (invoke != null) {
foreach (IParameter param in invoke.Parameters) {
parameters.Add(new ParameterDeclarationExpression(ConvertType(param.ReturnType, context), param.Name));
}
if (parameters.Count > 0 && string.Equals(parameters[0].ParameterName, "sender", StringComparison.InvariantCultureIgnoreCase)) {
sender = true;
parameters.RemoveAt(0);
}
}
}
List<ParameterDeclarationExpression> parameters = new List<ParameterDeclarationExpression>(1);
parameters.Add(new ParameterDeclarationExpression(type, "e"));
ModifierEnum modifier;
if (e.IsStatic)
modifier = ModifierEnum.Private | ModifierEnum.Static;
@ -371,12 +375,16 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -371,12 +375,16 @@ namespace ICSharpCode.SharpDevelop.Refactoring
new TypeReference("System.Void"),
parameters, null);
List<Expression> arguments = new List<Expression>(2);
if (e.IsStatic)
arguments.Add(new PrimitiveExpression(null, "null"));
else
arguments.Add(new ThisReferenceExpression());
arguments.Add(new IdentifierExpression("e"));
List<Expression> arguments = new List<Expression>();
if (sender) {
if (e.IsStatic)
arguments.Add(new PrimitiveExpression(null, "null"));
else
arguments.Add(new ThisReferenceExpression());
}
foreach (ParameterDeclarationExpression param in parameters) {
arguments.Add(new IdentifierExpression(param.ParameterName));
}
method.Body = new BlockStatement();
method.Body.AddChild(new RaiseEventStatement(e.Name, arguments));

Loading…
Cancel
Save